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