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