fix dstype gauge values and expands the number of possible subtypes
[collectd.git] / src / tail.c
1 /**
2  * collectd - src/tail.c
3  * Copyright (C) 2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_tail_match.h"
26
27 /*
28  *  <Plugin tail>
29  *    <File "/var/log/exim4/mainlog">
30  *      Instance "exim"
31  *      Interval 60
32  *      <Match>
33  *        Regex "S=([1-9][0-9]*)"
34  *        ExcludeRegex "U=root.*S="
35  *        DSType "CounterAdd"
36  *        Type "ipt_bytes"
37  *        Instance "total"
38  *      </Match>
39  *    </File>
40  *  </Plugin>
41  */
42
43 struct ctail_config_match_s
44 {
45   char *regex;
46   char *excluderegex;
47   int flags;
48   char *type;
49   char *type_instance;
50   cdtime_t interval;
51 };
52 typedef struct ctail_config_match_s ctail_config_match_t;
53
54 cu_tail_match_t **tail_match_list = NULL;
55 size_t tail_match_list_num = 0;
56 cdtime_t tail_match_list_intervals[255];
57
58 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
59     oconfig_item_t *ci)
60 {
61   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
62   {
63     WARNING ("tail plugin: `DSType' needs exactly one string argument.");
64     return (-1);
65   }
66
67   if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
68   {
69     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
70     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
71       cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
72     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
73       cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
74     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
75       cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
76     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
77       cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
78     else if (strcasecmp ("GaugeInc", ci->values[0].value.string) == 0)
79       cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
80     else if (strcasecmp ("GaugeAdd", ci->values[0].value.string) == 0)
81       cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
82     else
83       cm->flags = 0;
84   }
85   else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
86   {
87     cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
88     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
89       cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
90     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
91       cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
92     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
93       cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
94     else
95       cm->flags = 0;
96   }
97   else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
98   {
99     cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
100     if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
101       cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
102     else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
103       cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
104     else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
105       cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
106     else
107       cm->flags = 0;
108   }
109   else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
110   {
111     cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
112     if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
113       cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
114     else
115       cm->flags = 0;
116   }
117   else
118   {
119     cm->flags = 0;
120   }
121
122   if (cm->flags == 0)
123   {
124     WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
125         ci->values[0].value.string);
126     return (-1);
127   }
128
129   return (0);
130 } /* int ctail_config_add_match_dstype */
131
132 static int ctail_config_add_match (cu_tail_match_t *tm,
133     const char *plugin_instance, oconfig_item_t *ci, cdtime_t interval)
134 {
135   ctail_config_match_t cm;
136   int status;
137   int i;
138
139   memset (&cm, '\0', sizeof (cm));
140
141   if (ci->values_num != 0)
142   {
143     WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
144   }
145
146   status = 0;
147   for (i = 0; i < ci->children_num; i++)
148   {
149     oconfig_item_t *option = ci->children + i;
150
151     if (strcasecmp ("Regex", option->key) == 0)
152       status = cf_util_get_string (option, &cm.regex);
153     else if (strcasecmp ("ExcludeRegex", option->key) == 0)
154       status = cf_util_get_string (option, &cm.excluderegex);
155     else if (strcasecmp ("DSType", option->key) == 0)
156       status = ctail_config_add_match_dstype (&cm, option);
157     else if (strcasecmp ("Type", option->key) == 0)
158       status = cf_util_get_string (option, &cm.type);
159     else if (strcasecmp ("Instance", option->key) == 0)
160       status = cf_util_get_string (option, &cm.type_instance);
161     else
162     {
163       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
164       status = -1;
165     }
166
167     if (status != 0)
168       break;
169   } /* for (i = 0; i < ci->children_num; i++) */
170
171   while (status == 0)
172   {
173     if (cm.regex == NULL)
174     {
175       WARNING ("tail plugin: `Regex' missing in `Match' block.");
176       status = -1;
177       break;
178     }
179
180     if (cm.type == NULL)
181     {
182       WARNING ("tail plugin: `Type' missing in `Match' block.");
183       status = -1;
184       break;
185     }
186
187     if (cm.flags == 0)
188     {
189       WARNING ("tail plugin: `DSType' missing in `Match' block.");
190       status = -1;
191       break;
192     }
193
194     break;
195   } /* while (status == 0) */
196
197   if (status == 0)
198   {
199     status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
200         cm.flags, "tail", plugin_instance, cm.type, cm.type_instance, interval);
201
202     if (status != 0)
203     {
204       ERROR ("tail plugin: tail_match_add_match_simple failed.");
205     }
206   }
207
208   sfree (cm.regex);
209   sfree (cm.excluderegex);
210   sfree (cm.type);
211   sfree (cm.type_instance);
212
213   return (status);
214 } /* int ctail_config_add_match */
215
216 static int ctail_config_add_file (oconfig_item_t *ci)
217 {
218   cu_tail_match_t *tm;
219   cdtime_t interval = 0;
220   char *plugin_instance = NULL;
221   int num_matches = 0;
222   int status;
223   int i;
224
225   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
226   {
227     WARNING ("tail plugin: `File' needs exactly one string argument.");
228     return (-1);
229   }
230
231   tm = tail_match_create (ci->values[0].value.string);
232   if (tm == NULL)
233   {
234     ERROR ("tail plugin: tail_match_create (%s) failed.",
235         ci->values[0].value.string);
236     return (-1);
237   }
238
239   status = 0;
240   for (i = 0; i < ci->children_num; i++)
241   {
242     oconfig_item_t *option = ci->children + i;
243
244     if (strcasecmp ("Instance", option->key) == 0)
245       status = cf_util_get_string (option, &plugin_instance);
246     else if (strcasecmp ("Interval", option->key) == 0)
247       cf_util_get_cdtime (option, &interval);
248     else if (strcasecmp ("Match", option->key) == 0)
249     {
250       status = ctail_config_add_match (tm, plugin_instance, option, interval);
251       if (status == 0)
252         num_matches++;
253       /* Be mild with failed matches.. */
254       status = 0;
255     }
256     else
257     {
258       status = -1;
259     }
260
261     if (status != 0)
262       break;
263   } /* for (i = 0; i < ci->children_num; i++) */
264
265   if (num_matches == 0)
266   {
267     ERROR ("tail plugin: No (valid) matches found for file `%s'.",
268         ci->values[0].value.string);
269     tail_match_destroy (tm);
270     return (-1);
271   }
272   else
273   {
274     cu_tail_match_t **temp;
275
276     temp = (cu_tail_match_t **) realloc (tail_match_list,
277         sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
278     if (temp == NULL)
279     {
280       ERROR ("tail plugin: realloc failed.");
281       tail_match_destroy (tm);
282       return (-1);
283     }
284
285     tail_match_list = temp;
286     tail_match_list[tail_match_list_num] = tm;
287     tail_match_list_intervals[tail_match_list_num] = interval;
288     tail_match_list_num++;
289   }
290
291   return (0);
292 } /* int ctail_config_add_file */
293
294 static int ctail_config (oconfig_item_t *ci)
295 {
296   int i;
297
298   for (i = 0; i < ci->children_num; i++)
299   {
300     oconfig_item_t *option = ci->children + i;
301
302     if (strcasecmp ("File", option->key) == 0)
303       ctail_config_add_file (option);
304     else
305     {
306       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
307     }
308   } /* for (i = 0; i < ci->children_num; i++) */
309
310   return (0);
311 } /* int ctail_config */
312
313 static int ctail_read (user_data_t *ud)
314 {
315   int status;
316
317   status = tail_match_read ((cu_tail_match_t *)ud->data);
318   if (status != 0)
319   {
320     ERROR ("tail plugin: tail_match_read failed.");
321     return (-1);
322   }
323
324   return (0);
325 } /* int ctail_read */
326
327 static int ctail_init (void)
328 {
329   struct timespec cb_interval;
330   char str[255];
331   user_data_t ud;
332   size_t i;
333
334   if (tail_match_list_num == 0)
335   {
336     WARNING ("tail plugin: File list is empty. Returning an error.");
337     return (-1);
338   }
339
340   for (i = 0; i < tail_match_list_num; i++)
341   {
342     ud.data = (void *)tail_match_list[i];
343     ssnprintf(str, sizeof(str), "tail-%zu", i);
344     CDTIME_T_TO_TIMESPEC (tail_match_list_intervals[i], &cb_interval);
345     plugin_register_complex_read (NULL, str, ctail_read, &cb_interval, &ud);
346   }
347
348   return (0);
349 } /* int ctail_init */
350
351 static int ctail_shutdown (void)
352 {
353   size_t i;
354
355   for (i = 0; i < tail_match_list_num; i++)
356   {
357     tail_match_destroy (tail_match_list[i]);
358     tail_match_list[i] = NULL;
359   }
360   sfree (tail_match_list);
361   tail_match_list_num = 0;
362
363   return (0);
364 } /* int ctail_shutdown */
365
366 void module_register (void)
367 {
368   plugin_register_complex_config ("tail", ctail_config);
369   plugin_register_init ("tail", ctail_init);
370   plugin_register_shutdown ("tail", ctail_shutdown);
371 } /* void module_register */
372
373 /* vim: set sw=2 sts=2 ts=8 : */