Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / tail.c
1 /**
2  * collectd - src/tail.c
3  * Copyright (C) 2008       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_tail_match.h"
31
32 /*
33  *  <Plugin tail>
34  *    <File "/var/log/exim4/mainlog">
35  *      Instance "exim"
36  *      Interval 60
37  *      <Match>
38  *        Regex "S=([1-9][0-9]*)"
39  *        ExcludeRegex "U=root.*S="
40  *        DSType "CounterAdd"
41  *        Type "ipt_bytes"
42  *        Instance "total"
43  *      </Match>
44  *    </File>
45  *  </Plugin>
46  */
47
48 struct ctail_config_match_s
49 {
50   char *regex;
51   char *excluderegex;
52   int flags;
53   char *type;
54   char *type_instance;
55   cdtime_t interval;
56 };
57 typedef struct ctail_config_match_s ctail_config_match_t;
58
59 cu_tail_match_t **tail_match_list = NULL;
60 size_t tail_match_list_num = 0;
61 cdtime_t tail_match_list_intervals[255];
62
63 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
64     oconfig_item_t *ci)
65 {
66   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
67   {
68     WARNING ("tail plugin: `DSType' needs exactly one string argument.");
69     return (-1);
70   }
71
72   if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
73   {
74     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
75     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
76       cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
77     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
78       cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
79     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
80       cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
81     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
82       cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
83     else if (strcasecmp ("GaugeInc", ci->values[0].value.string) == 0)
84       cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
85     else if (strcasecmp ("GaugeAdd", ci->values[0].value.string) == 0)
86       cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
87     else
88       cm->flags = 0;
89   }
90   else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
91   {
92     cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
93     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
94       cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
95     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
96       cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
97     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
98       cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
99     else
100       cm->flags = 0;
101   }
102   else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
103   {
104     cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
105     if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
106       cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
107     else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
108       cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
109     else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
110       cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
111     else
112       cm->flags = 0;
113   }
114   else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
115   {
116     cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
117     if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
118       cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
119     else
120       cm->flags = 0;
121   }
122   else
123   {
124     cm->flags = 0;
125   }
126
127   if (cm->flags == 0)
128   {
129     WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
130         ci->values[0].value.string);
131     return (-1);
132   }
133
134   return (0);
135 } /* int ctail_config_add_match_dstype */
136
137 static int ctail_config_add_match (cu_tail_match_t *tm,
138     const char *plugin_instance, oconfig_item_t *ci, cdtime_t interval)
139 {
140   ctail_config_match_t cm;
141   int status;
142   int i;
143
144   memset (&cm, '\0', sizeof (cm));
145
146   if (ci->values_num != 0)
147   {
148     WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
149   }
150
151   status = 0;
152   for (i = 0; i < ci->children_num; i++)
153   {
154     oconfig_item_t *option = ci->children + i;
155
156     if (strcasecmp ("Regex", option->key) == 0)
157       status = cf_util_get_string (option, &cm.regex);
158     else if (strcasecmp ("ExcludeRegex", option->key) == 0)
159       status = cf_util_get_string (option, &cm.excluderegex);
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 = cf_util_get_string (option, &cm.type);
164     else if (strcasecmp ("Instance", option->key) == 0)
165       status = cf_util_get_string (option, &cm.type_instance);
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.excluderegex,
205         cm.flags, "tail", plugin_instance, cm.type, cm.type_instance, interval);
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.excluderegex);
215   sfree (cm.type);
216   sfree (cm.type_instance);
217
218   return (status);
219 } /* int ctail_config_add_match */
220
221 static int ctail_config_add_file (oconfig_item_t *ci)
222 {
223   cu_tail_match_t *tm;
224   cdtime_t interval = 0;
225   char *plugin_instance = NULL;
226   int num_matches = 0;
227   int i;
228
229   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
230   {
231     WARNING ("tail plugin: `File' needs exactly one string argument.");
232     return (-1);
233   }
234
235   tm = tail_match_create (ci->values[0].value.string);
236   if (tm == NULL)
237   {
238     ERROR ("tail plugin: tail_match_create (%s) failed.",
239         ci->values[0].value.string);
240     return (-1);
241   }
242
243   for (i = 0; i < ci->children_num; i++)
244   {
245     oconfig_item_t *option = ci->children + i;
246     int status = 0;
247
248     if (strcasecmp ("Instance", option->key) == 0)
249       status = cf_util_get_string (option, &plugin_instance);
250     else if (strcasecmp ("Interval", option->key) == 0)
251       cf_util_get_cdtime (option, &interval);
252     else if (strcasecmp ("Match", option->key) == 0)
253     {
254       status = ctail_config_add_match (tm, plugin_instance, option, interval);
255       if (status == 0)
256         num_matches++;
257       /* Be mild with failed matches.. */
258       status = 0;
259     }
260     else
261     {
262       status = -1;
263     }
264
265     if (status != 0)
266       break;
267   } /* for (i = 0; i < ci->children_num; i++) */
268
269   sfree (plugin_instance);
270
271   if (num_matches == 0)
272   {
273     ERROR ("tail plugin: No (valid) matches found for file `%s'.",
274         ci->values[0].value.string);
275     tail_match_destroy (tm);
276     return (-1);
277   }
278   else
279   {
280     cu_tail_match_t **temp;
281
282     temp = (cu_tail_match_t **) realloc (tail_match_list,
283         sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
284     if (temp == NULL)
285     {
286       ERROR ("tail plugin: realloc failed.");
287       tail_match_destroy (tm);
288       return (-1);
289     }
290
291     tail_match_list = temp;
292     tail_match_list[tail_match_list_num] = tm;
293     tail_match_list_intervals[tail_match_list_num] = interval;
294     tail_match_list_num++;
295   }
296
297   return (0);
298 } /* int ctail_config_add_file */
299
300 static int ctail_config (oconfig_item_t *ci)
301 {
302   int i;
303
304   for (i = 0; i < ci->children_num; i++)
305   {
306     oconfig_item_t *option = ci->children + i;
307
308     if (strcasecmp ("File", option->key) == 0)
309       ctail_config_add_file (option);
310     else
311     {
312       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
313     }
314   } /* for (i = 0; i < ci->children_num; i++) */
315
316   return (0);
317 } /* int ctail_config */
318
319 static int ctail_read (user_data_t *ud)
320 {
321   int status;
322
323   status = tail_match_read ((cu_tail_match_t *)ud->data);
324   if (status != 0)
325   {
326     ERROR ("tail plugin: tail_match_read failed.");
327     return (-1);
328   }
329
330   return (0);
331 } /* int ctail_read */
332
333 static int ctail_init (void)
334 {
335   struct timespec cb_interval;
336   char str[255];
337   user_data_t ud;
338   size_t i;
339
340   if (tail_match_list_num == 0)
341   {
342     WARNING ("tail plugin: File list is empty. Returning an error.");
343     return (-1);
344   }
345
346   memset(&ud, '\0', sizeof(ud));
347
348   for (i = 0; i < tail_match_list_num; i++)
349   {
350     ud.data = (void *)tail_match_list[i];
351     ssnprintf(str, sizeof(str), "tail-%zu", i);
352     CDTIME_T_TO_TIMESPEC (tail_match_list_intervals[i], &cb_interval);
353     plugin_register_complex_read (NULL, str, ctail_read, &cb_interval, &ud);
354   }
355
356   return (0);
357 } /* int ctail_init */
358
359 static int ctail_shutdown (void)
360 {
361   size_t i;
362
363   for (i = 0; i < tail_match_list_num; i++)
364   {
365     tail_match_destroy (tail_match_list[i]);
366     tail_match_list[i] = NULL;
367   }
368   sfree (tail_match_list);
369   tail_match_list_num = 0;
370
371   return (0);
372 } /* int ctail_shutdown */
373
374 void module_register (void)
375 {
376   plugin_register_complex_config ("tail", ctail_config);
377   plugin_register_init ("tail", ctail_init);
378   plugin_register_shutdown ("tail", ctail_shutdown);
379 } /* void module_register */
380
381 /* vim: set sw=2 sts=2 ts=8 : */