{GPL, other}: Relicense to MIT license.
[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
84       cm->flags = 0;
85   }
86   else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
87   {
88     cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
89     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
90       cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
91     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
92       cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
93     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
94       cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
95     else
96       cm->flags = 0;
97   }
98   else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
99   {
100     cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
101     if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
102       cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
103     else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
104       cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
105     else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
106       cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
107     else
108       cm->flags = 0;
109   }
110   else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
111   {
112     cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
113     if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
114       cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
115     else
116       cm->flags = 0;
117   }
118   else
119   {
120     cm->flags = 0;
121   }
122
123   if (cm->flags == 0)
124   {
125     WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
126         ci->values[0].value.string);
127     return (-1);
128   }
129
130   return (0);
131 } /* int ctail_config_add_match_dstype */
132
133 static int ctail_config_add_match (cu_tail_match_t *tm,
134     const char *plugin_instance, oconfig_item_t *ci, cdtime_t interval)
135 {
136   ctail_config_match_t cm;
137   int status;
138   int i;
139
140   memset (&cm, '\0', sizeof (cm));
141
142   if (ci->values_num != 0)
143   {
144     WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
145   }
146
147   status = 0;
148   for (i = 0; i < ci->children_num; i++)
149   {
150     oconfig_item_t *option = ci->children + i;
151
152     if (strcasecmp ("Regex", option->key) == 0)
153       status = cf_util_get_string (option, &cm.regex);
154     else if (strcasecmp ("ExcludeRegex", option->key) == 0)
155       status = cf_util_get_string (option, &cm.excluderegex);
156     else if (strcasecmp ("DSType", option->key) == 0)
157       status = ctail_config_add_match_dstype (&cm, option);
158     else if (strcasecmp ("Type", option->key) == 0)
159       status = cf_util_get_string (option, &cm.type);
160     else if (strcasecmp ("Instance", option->key) == 0)
161       status = cf_util_get_string (option, &cm.type_instance);
162     else
163     {
164       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
165       status = -1;
166     }
167
168     if (status != 0)
169       break;
170   } /* for (i = 0; i < ci->children_num; i++) */
171
172   while (status == 0)
173   {
174     if (cm.regex == NULL)
175     {
176       WARNING ("tail plugin: `Regex' missing in `Match' block.");
177       status = -1;
178       break;
179     }
180
181     if (cm.type == NULL)
182     {
183       WARNING ("tail plugin: `Type' missing in `Match' block.");
184       status = -1;
185       break;
186     }
187
188     if (cm.flags == 0)
189     {
190       WARNING ("tail plugin: `DSType' missing in `Match' block.");
191       status = -1;
192       break;
193     }
194
195     break;
196   } /* while (status == 0) */
197
198   if (status == 0)
199   {
200     status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
201         cm.flags, "tail", plugin_instance, cm.type, cm.type_instance, interval);
202
203     if (status != 0)
204     {
205       ERROR ("tail plugin: tail_match_add_match_simple failed.");
206     }
207   }
208
209   sfree (cm.regex);
210   sfree (cm.excluderegex);
211   sfree (cm.type);
212   sfree (cm.type_instance);
213
214   return (status);
215 } /* int ctail_config_add_match */
216
217 static int ctail_config_add_file (oconfig_item_t *ci)
218 {
219   cu_tail_match_t *tm;
220   cdtime_t interval = 0;
221   char *plugin_instance = NULL;
222   int num_matches = 0;
223   int status;
224   int i;
225
226   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
227   {
228     WARNING ("tail plugin: `File' needs exactly one string argument.");
229     return (-1);
230   }
231
232   tm = tail_match_create (ci->values[0].value.string);
233   if (tm == NULL)
234   {
235     ERROR ("tail plugin: tail_match_create (%s) failed.",
236         ci->values[0].value.string);
237     return (-1);
238   }
239
240   status = 0;
241   for (i = 0; i < ci->children_num; i++)
242   {
243     oconfig_item_t *option = ci->children + i;
244
245     if (strcasecmp ("Instance", option->key) == 0)
246       status = cf_util_get_string (option, &plugin_instance);
247     else if (strcasecmp ("Interval", option->key) == 0)
248       cf_util_get_cdtime (option, &interval);
249     else if (strcasecmp ("Match", option->key) == 0)
250     {
251       status = ctail_config_add_match (tm, plugin_instance, option, interval);
252       if (status == 0)
253         num_matches++;
254       /* Be mild with failed matches.. */
255       status = 0;
256     }
257     else
258     {
259       status = -1;
260     }
261
262     if (status != 0)
263       break;
264   } /* for (i = 0; i < ci->children_num; i++) */
265
266   if (num_matches == 0)
267   {
268     ERROR ("tail plugin: No (valid) matches found for file `%s'.",
269         ci->values[0].value.string);
270     tail_match_destroy (tm);
271     return (-1);
272   }
273   else
274   {
275     cu_tail_match_t **temp;
276
277     temp = (cu_tail_match_t **) realloc (tail_match_list,
278         sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
279     if (temp == NULL)
280     {
281       ERROR ("tail plugin: realloc failed.");
282       tail_match_destroy (tm);
283       return (-1);
284     }
285
286     tail_match_list = temp;
287     tail_match_list[tail_match_list_num] = tm;
288     tail_match_list_intervals[tail_match_list_num] = interval;
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_read (user_data_t *ud)
315 {
316   int status;
317
318   status = tail_match_read ((cu_tail_match_t *)ud->data);
319   if (status != 0)
320   {
321     ERROR ("tail plugin: tail_match_read failed.");
322     return (-1);
323   }
324
325   return (0);
326 } /* int ctail_read */
327
328 static int ctail_init (void)
329 {
330   struct timespec cb_interval;
331   char str[255];
332   user_data_t ud;
333   size_t i;
334
335   if (tail_match_list_num == 0)
336   {
337     WARNING ("tail plugin: File list is empty. Returning an error.");
338     return (-1);
339   }
340
341   for (i = 0; i < tail_match_list_num; i++)
342   {
343     ud.data = (void *)tail_match_list[i];
344     ssnprintf(str, sizeof(str), "tail-%zu", i);
345     CDTIME_T_TO_TIMESPEC (tail_match_list_intervals[i], &cb_interval);
346     plugin_register_complex_read (NULL, str, ctail_read, &cb_interval, &ud);
347   }
348
349   return (0);
350 } /* int ctail_init */
351
352 static int ctail_shutdown (void)
353 {
354   size_t i;
355
356   for (i = 0; i < tail_match_list_num; i++)
357   {
358     tail_match_destroy (tail_match_list[i]);
359     tail_match_list[i] = NULL;
360   }
361   sfree (tail_match_list);
362   tail_match_list_num = 0;
363
364   return (0);
365 } /* int ctail_shutdown */
366
367 void module_register (void)
368 {
369   plugin_register_complex_config ("tail", ctail_config);
370   plugin_register_init ("tail", ctail_init);
371   plugin_register_shutdown ("tail", ctail_shutdown);
372 } /* void module_register */
373
374 /* vim: set sw=2 sts=2 ts=8 : */