Merge branch 'collectd-4.7' into collectd-4.8
[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  *      <Match>
32  *        Regex "S=([1-9][0-9]*)"
33  *        DSType "CounterAdd"
34  *        Type "ipt_bytes"
35  *        Instance "total"
36  *      </Match>
37  *    </File>
38  *  </Plugin>
39  */
40
41 struct ctail_config_match_s
42 {
43   char *regex;
44   int flags;
45   char *type;
46   char *type_instance;
47 };
48 typedef struct ctail_config_match_s ctail_config_match_t;
49
50 cu_tail_match_t **tail_match_list = NULL;
51 size_t tail_match_list_num = 0;
52
53 static int ctail_config_add_string (const char *name, char **dest, oconfig_item_t *ci)
54 {
55   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
56   {
57     WARNING ("tail plugin: `%s' needs exactly one string argument.", name);
58     return (-1);
59   }
60
61   sfree (*dest);
62   *dest = strdup (ci->values[0].value.string);
63   if (*dest == NULL)
64     return (-1);
65
66   return (0);
67 } /* int ctail_config_add_string */
68
69 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
70     oconfig_item_t *ci)
71 {
72   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
73   {
74     WARNING ("tail plugin: `DSType' needs exactly one string argument.");
75     return (-1);
76   }
77
78   if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
79   {
80     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
81     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
82       cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
83     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
84       cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
85     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
86       cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
87     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
88       cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
89     else
90       cm->flags = 0;
91   }
92   else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
93   {
94     cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
95     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
96       cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
97     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
98       cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
99     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
100       cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
101     else
102       cm->flags = 0;
103   }
104   else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
105   {
106     cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
107     if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
108       cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
109     else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
110       cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
111     else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
112       cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
113     else
114       cm->flags = 0;
115   }
116   else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
117   {
118     cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
119     if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
120       cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
121     else
122       cm->flags = 0;
123   }
124   else
125   {
126     cm->flags = 0;
127   }
128
129   if (cm->flags == 0)
130   {
131     WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
132         ci->values[0].value.string);
133     return (-1);
134   }
135
136   return (0);
137 } /* int ctail_config_add_match_dstype */
138
139 static int ctail_config_add_match (cu_tail_match_t *tm,
140     const char *plugin_instance, oconfig_item_t *ci)
141 {
142   ctail_config_match_t cm;
143   int status;
144   int i;
145
146   memset (&cm, '\0', sizeof (cm));
147
148   if (ci->values_num != 0)
149   {
150     WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
151   }
152
153   status = 0;
154   for (i = 0; i < ci->children_num; i++)
155   {
156     oconfig_item_t *option = ci->children + i;
157
158     if (strcasecmp ("Regex", option->key) == 0)
159       status = ctail_config_add_string ("Regex", &cm.regex, option);
160     else if (strcasecmp ("DSType", option->key) == 0)
161       status = ctail_config_add_match_dstype (&cm, option);
162     else if (strcasecmp ("Type", option->key) == 0)
163       status = ctail_config_add_string ("Type", &cm.type, option);
164     else if (strcasecmp ("Instance", option->key) == 0)
165       status = ctail_config_add_string ("Instance", &cm.type_instance, option);
166     else
167     {
168       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
169       status = -1;
170     }
171
172     if (status != 0)
173       break;
174   } /* for (i = 0; i < ci->children_num; i++) */
175
176   while (status == 0)
177   {
178     if (cm.regex == NULL)
179     {
180       WARNING ("tail plugin: `Regex' missing in `Match' block.");
181       status = -1;
182       break;
183     }
184
185     if (cm.type == NULL)
186     {
187       WARNING ("tail plugin: `Type' missing in `Match' block.");
188       status = -1;
189       break;
190     }
191
192     if (cm.flags == 0)
193     {
194       WARNING ("tail plugin: `DSType' missing in `Match' block.");
195       status = -1;
196       break;
197     }
198
199     break;
200   } /* while (status == 0) */
201
202   if (status == 0)
203   {
204     status = tail_match_add_match_simple (tm, cm.regex, cm.flags,
205         "tail", plugin_instance, cm.type, cm.type_instance);
206
207     if (status != 0)
208     {
209       ERROR ("tail plugin: tail_match_add_match_simple failed.");
210     }
211   }
212
213   sfree (cm.regex);
214   sfree (cm.type);
215   sfree (cm.type_instance);
216
217   return (status);
218 } /* int ctail_config_add_match */
219
220 static int ctail_config_add_file (oconfig_item_t *ci)
221 {
222   cu_tail_match_t *tm;
223   char *plugin_instance = NULL;
224   int num_matches = 0;
225   int status;
226   int i;
227
228   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
229   {
230     WARNING ("tail plugin: `File' needs exactly one string argument.");
231     return (-1);
232   }
233
234   tm = tail_match_create (ci->values[0].value.string);
235   if (tm == NULL)
236   {
237     ERROR ("tail plugin: tail_match_create (%s) failed.",
238         ci->values[0].value.string);
239     return (-1);
240   }
241
242   status = 0;
243   for (i = 0; i < ci->children_num; i++)
244   {
245     oconfig_item_t *option = ci->children + i;
246
247     if (strcasecmp ("Match", option->key) == 0)
248     {
249       status = ctail_config_add_match (tm, plugin_instance, option);
250       if (status == 0)
251         num_matches++;
252       /* Be mild with failed matches.. */
253       status = 0;
254     }
255     else if (strcasecmp ("Instance", option->key) == 0)
256       status = ctail_config_add_string ("Instance", &plugin_instance, option);
257     else
258     {
259       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
260       status = -1;
261     }
262
263     if (status != 0)
264       break;
265   } /* for (i = 0; i < ci->children_num; i++) */
266
267   if (num_matches == 0)
268   {
269     ERROR ("tail plugin: No (valid) matches found for file `%s'.",
270         ci->values[0].value.string);
271     tail_match_destroy (tm);
272     return (-1);
273   }
274   else
275   {
276     cu_tail_match_t **temp;
277
278     temp = (cu_tail_match_t **) realloc (tail_match_list,
279         sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
280     if (temp == NULL)
281     {
282       ERROR ("tail plugin: realloc failed.");
283       tail_match_destroy (tm);
284       return (-1);
285     }
286
287     tail_match_list = temp;
288     tail_match_list[tail_match_list_num] = tm;
289     tail_match_list_num++;
290   }
291
292   return (0);
293 } /* int ctail_config_add_file */
294
295 static int ctail_config (oconfig_item_t *ci)
296 {
297   int i;
298
299   for (i = 0; i < ci->children_num; i++)
300   {
301     oconfig_item_t *option = ci->children + i;
302
303     if (strcasecmp ("File", option->key) == 0)
304       ctail_config_add_file (option);
305     else
306     {
307       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
308     }
309   } /* for (i = 0; i < ci->children_num; i++) */
310
311   return (0);
312 } /* int ctail_config */
313
314 static int ctail_init (void)
315 {
316   if (tail_match_list_num == 0)
317   {
318     WARNING ("tail plugin: File list is empty. Returning an error.");
319     return (-1);
320   }
321
322   return (0);
323 } /* int ctail_init */
324
325 static int ctail_read (void)
326 {
327   int success = 0;
328   size_t i;
329
330   for (i = 0; i < tail_match_list_num; i++)
331   {
332     int status;
333
334     status = tail_match_read (tail_match_list[i]);
335     if (status != 0)
336     {
337       ERROR ("tail plugin: tail_match_read[%zu] failed.", i);
338     }
339     else
340     {
341       success++;
342     }
343   }
344
345   if (success == 0)
346     return (-1);
347   return (0);
348 } /* int ctail_read */
349
350 static int ctail_shutdown (void)
351 {
352   size_t i;
353
354   for (i = 0; i < tail_match_list_num; i++)
355   {
356     tail_match_destroy (tail_match_list[i]);
357     tail_match_list[i] = NULL;
358   }
359   sfree (tail_match_list);
360   tail_match_list_num = 0;
361
362   return (0);
363 } /* int ctail_shutdown */
364
365 void module_register (void)
366 {
367   plugin_register_complex_config ("tail", ctail_config);
368   plugin_register_init ("tail", ctail_init);
369   plugin_register_read ("tail", ctail_read);
370   plugin_register_shutdown ("tail", ctail_shutdown);
371 } /* void module_register */
372
373 /* vim: set sw=2 sts=2 ts=8 : */