+ liblatency: Added utils_latency_config code
[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   if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
76   {
77     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
78     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
79       cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
80     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
81       cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
82     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
83       cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
84     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
85       cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
86     else if (strcasecmp ("GaugeInc", ci->values[0].value.string) == 0)
87       cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
88     else if (strcasecmp ("GaugeAdd", ci->values[0].value.string) == 0)
89       cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
90     else
91       cm->flags = 0;
92   }
93   else if (strcasecmp ("Latency", ci->values[0].value.string) == 0)
94   {
95     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
96     cm->flags |= UTILS_MATCH_CF_GAUGE_LATENCY;
97   }
98   else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
99   {
100     cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
101     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
102       cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
103     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
104       cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
105     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
106       cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
107     else
108       cm->flags = 0;
109   }
110   else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
111   {
112     cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
113     if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
114       cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
115     else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
116       cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
117     else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
118       cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
119     else
120       cm->flags = 0;
121   }
122   else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
123   {
124     cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
125     if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
126       cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
127     else
128       cm->flags = 0;
129   }
130   else
131   {
132     cm->flags = 0;
133   }
134
135   if (cm->flags == 0)
136   {
137     WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
138         ci->values[0].value.string);
139     return (-1);
140   }
141
142   return (0);
143 } /* int ctail_config_add_match_dstype */
144
145 static int ctail_config_add_match (cu_tail_match_t *tm,
146     const char *plugin_instance, oconfig_item_t *ci, cdtime_t interval)
147 {
148   ctail_config_match_t cm = { 0 };
149   int status;
150
151   if (ci->values_num != 0)
152   {
153     WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
154   }
155
156   status = 0;
157   for (int i = 0; i < ci->children_num; i++)
158   {
159     oconfig_item_t *option = ci->children + i;
160
161     if (strcasecmp ("Regex", option->key) == 0)
162       status = cf_util_get_string (option, &cm.regex);
163     else if (strcasecmp ("ExcludeRegex", option->key) == 0)
164       status = cf_util_get_string (option, &cm.excluderegex);
165     else if (strcasecmp ("DSType", option->key) == 0)
166       status = ctail_config_add_match_dstype (&cm, option);
167     else if (strcasecmp ("Type", option->key) == 0)
168       status = cf_util_get_string (option, &cm.type);
169     else if (strcasecmp ("Instance", option->key) == 0)
170       status = cf_util_get_string (option, &cm.type_instance);
171     else if (strncasecmp ("Latency", option->key, strlen ("Latency")) == 0)
172     {
173       if (strcasecmp ("LatencyPercentile", option->key) == 0)
174         status = latency_config_add_percentile ("tail", &cm.latency, option);
175       else if (strcasecmp ("LatencyPercentileType", option->key) == 0)
176         status = cf_util_get_string (option, &cm.latency.percentile_type);
177       else if (strcasecmp ("LatencyRate", option->key) == 0)
178         status = latency_config_add_rate ("tail", &cm.latency, option);
179       else if (strcasecmp ("LatencyRateType", option->key) == 0)
180         status = cf_util_get_string (option, &cm.latency.rates_type);
181       else if (strcasecmp ("LatencyLower", option->key) == 0)
182         status = cf_util_get_boolean (option, &cm.latency.lower);
183       else if (strcasecmp ("LatencyUpper", option->key) == 0)
184         status = cf_util_get_boolean (option, &cm.latency.upper);
185       else if (strcasecmp ("LatencyAvg", option->key) == 0)
186         status = cf_util_get_boolean (option, &cm.latency.avg);
187       else 
188       {
189         WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
190         status = -1;
191       }
192     }
193     else
194     {
195       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
196       status = -1;
197     }
198
199     if (status != 0)
200       break;
201   } /* for (i = 0; i < ci->children_num; i++) */
202
203   while (status == 0)
204   {
205     if (cm.regex == NULL)
206     {
207       WARNING ("tail plugin: `Regex' missing in `Match' block.");
208       status = -1;
209       break;
210     }
211
212     if (cm.type == NULL)
213     {
214       WARNING ("tail plugin: `Type' missing in `Match' block.");
215       status = -1;
216       break;
217     }
218
219     if (cm.flags == 0)
220     {
221       WARNING ("tail plugin: `DSType' missing in `Match' block.");
222       status = -1;
223       break;
224     }
225
226     if ((cm.flags & UTILS_MATCH_DS_TYPE_GAUGE)
227         && (cm.flags & UTILS_MATCH_CF_GAUGE_LATENCY))
228     {
229
230       if (cm.type_instance != NULL)
231       {
232         WARNING ("tail plugin: `DSType Latency' and `Instance %s' in `Match' "
233                  "block could not be used together.", cm.type_instance);
234         status = -1;
235         break;
236       }
237
238       if (cm.latency.percentile_num == 0 && cm.latency.rates_num == 0)
239       {
240         WARNING ("tail plugin: `Match' with `DSType Latency' has no "
241                  "`LatencyPercentile' or `LatencyRate' options.");
242         status = -1;
243         break;
244       }
245     }
246
247     break;
248   } /* while (status == 0) */
249
250   if (status == 0)
251   {
252     status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
253       cm.flags, "tail", plugin_instance, cm.type, cm.type_instance,
254       cm.latency, interval);
255
256     if (status != 0)
257     {
258       ERROR ("tail plugin: tail_match_add_match_simple failed.");
259     }
260   }
261
262   sfree (cm.regex);
263   sfree (cm.excluderegex);
264   sfree (cm.type);
265   sfree (cm.type_instance);
266   latency_config_free(cm.latency);
267
268   return (status);
269 } /* int ctail_config_add_match */
270
271 static int ctail_config_add_file (oconfig_item_t *ci)
272 {
273   cu_tail_match_t *tm;
274   cdtime_t interval = 0;
275   char *plugin_instance = NULL;
276   int num_matches = 0;
277
278   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
279   {
280     WARNING ("tail plugin: `File' needs exactly one string argument.");
281     return (-1);
282   }
283
284   tm = tail_match_create (ci->values[0].value.string);
285   if (tm == NULL)
286   {
287     ERROR ("tail plugin: tail_match_create (%s) failed.",
288         ci->values[0].value.string);
289     return (-1);
290   }
291
292   for (int i = 0; i < ci->children_num; i++)
293   {
294     oconfig_item_t *option = ci->children + i;
295     int status = 0;
296
297     if (strcasecmp ("Instance", option->key) == 0)
298       status = cf_util_get_string (option, &plugin_instance);
299     else if (strcasecmp ("Interval", option->key) == 0)
300       cf_util_get_cdtime (option, &interval);
301     else if (strcasecmp ("Match", option->key) == 0)
302     {
303       status = ctail_config_add_match (tm, plugin_instance, option, interval);
304       if (status == 0)
305         num_matches++;
306       /* Be mild with failed matches.. */
307       status = 0;
308     }
309     else
310     {
311       status = -1;
312     }
313
314     if (status != 0)
315       break;
316   } /* for (i = 0; i < ci->children_num; i++) */
317
318   sfree (plugin_instance);
319
320   if (num_matches == 0)
321   {
322     ERROR ("tail plugin: No (valid) matches found for file `%s'.",
323         ci->values[0].value.string);
324     tail_match_destroy (tm);
325     return (-1);
326   }
327   else
328   {
329     cu_tail_match_t **temp;
330
331     temp = realloc (tail_match_list,
332         sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
333     if (temp == NULL)
334     {
335       ERROR ("tail plugin: realloc failed.");
336       tail_match_destroy (tm);
337       return (-1);
338     }
339
340     tail_match_list = temp;
341     tail_match_list[tail_match_list_num] = tm;
342     tail_match_list_intervals[tail_match_list_num] = interval;
343     tail_match_list_num++;
344   }
345
346   return (0);
347 } /* int ctail_config_add_file */
348
349 static int ctail_config (oconfig_item_t *ci)
350 {
351   for (int i = 0; i < ci->children_num; i++)
352   {
353     oconfig_item_t *option = ci->children + i;
354
355     if (strcasecmp ("File", option->key) == 0)
356       ctail_config_add_file (option);
357     else
358     {
359       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
360     }
361   } /* for (i = 0; i < ci->children_num; i++) */
362
363   return (0);
364 } /* int ctail_config */
365
366 static int ctail_read (user_data_t *ud)
367 {
368   int status;
369
370   status = tail_match_read ((cu_tail_match_t *)ud->data);
371   if (status != 0)
372   {
373     ERROR ("tail plugin: tail_match_read failed.");
374     return (-1);
375   }
376
377   return (0);
378 } /* int ctail_read */
379
380 static int ctail_init (void)
381 {
382   char str[255];
383
384   if (tail_match_list_num == 0)
385   {
386     WARNING ("tail plugin: File list is empty. Returning an error.");
387     return (-1);
388   }
389
390   for (size_t i = 0; i < tail_match_list_num; i++)
391   {
392     ssnprintf(str, sizeof(str), "tail-%zu", i);
393
394     plugin_register_complex_read (NULL, str, ctail_read, tail_match_list_intervals[i],
395         &(user_data_t) {
396           .data = tail_match_list[i],
397         });
398   }
399
400   return (0);
401 } /* int ctail_init */
402
403 static int ctail_shutdown (void)
404 {
405   for (size_t i = 0; i < tail_match_list_num; i++)
406   {
407     tail_match_destroy (tail_match_list[i]);
408     tail_match_list[i] = NULL;
409   }
410   sfree (tail_match_list);
411   tail_match_list_num = 0;
412
413   return (0);
414 } /* int ctail_shutdown */
415
416 void module_register (void)
417 {
418   plugin_register_complex_config ("tail", ctail_config);
419   plugin_register_init ("tail", ctail_init);
420   plugin_register_shutdown ("tail", ctail_shutdown);
421 } /* void module_register */
422
423 /* vim: set sw=2 sts=2 ts=8 : */