tail plugin: Implement the "GaugePersist" type.
[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
33 /*
34  *  <Plugin tail>
35  *    <File "/var/log/exim4/mainlog">
36  *      Instance "exim"
37  *      Interval 60
38  *      <Match>
39  *        Regex "S=([1-9][0-9]*)"
40  *        ExcludeRegex "U=root.*S="
41  *        DSType "CounterAdd"
42  *        Type "ipt_bytes"
43  *        Instance "total"
44  *      </Match>
45  *    </File>
46  *  </Plugin>
47  */
48
49 struct ctail_config_match_s
50 {
51   char *regex;
52   char *excluderegex;
53   int flags;
54   char *type;
55   char *type_instance;
56   cdtime_t interval;
57 };
58 typedef struct ctail_config_match_s ctail_config_match_t;
59
60 static cu_tail_match_t **tail_match_list = NULL;
61 static size_t tail_match_list_num = 0;
62 static cdtime_t tail_match_list_intervals[255];
63
64 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
65     oconfig_item_t *ci)
66 {
67   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
68   {
69     WARNING ("tail plugin: `DSType' needs exactly one string argument.");
70     return (-1);
71   }
72
73   if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
74   {
75     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
76     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
77       cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
78     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
79       cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
80     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
81       cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
82     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
83       cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
84     else if (strcasecmp ("GaugeInc", ci->values[0].value.string) == 0)
85       cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
86     else if (strcasecmp ("GaugeAdd", ci->values[0].value.string) == 0)
87       cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
88     else if (strcasecmp ("GaugePersist", ci->values[0].value.string) == 0)
89       cm->flags |= UTILS_MATCH_CF_GAUGE_PERSIST;
90     else
91       cm->flags = 0;
92   }
93   else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
94   {
95     cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
96     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
97       cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
98     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
99       cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
100     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
101       cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
102     else
103       cm->flags = 0;
104   }
105   else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
106   {
107     cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
108     if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
109       cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
110     else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
111       cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
112     else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
113       cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
114     else
115       cm->flags = 0;
116   }
117   else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
118   {
119     cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
120     if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
121       cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
122     else
123       cm->flags = 0;
124   }
125   else
126   {
127     cm->flags = 0;
128   }
129
130   if (cm->flags == 0)
131   {
132     WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
133         ci->values[0].value.string);
134     return (-1);
135   }
136
137   return (0);
138 } /* int ctail_config_add_match_dstype */
139
140 static int ctail_config_add_match (cu_tail_match_t *tm,
141     const char *plugin_instance, oconfig_item_t *ci, cdtime_t interval)
142 {
143   ctail_config_match_t cm = { 0 };
144   int status;
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 (int 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
228   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
229   {
230     WARNING ("tail plugin: `File' needs exactly one string argument.");
231     return (-1);
232   }
233
234   tm = tail_match_create (ci->values[0].value.string);
235   if (tm == NULL)
236   {
237     ERROR ("tail plugin: tail_match_create (%s) failed.",
238         ci->values[0].value.string);
239     return (-1);
240   }
241
242   for (int i = 0; i < ci->children_num; i++)
243   {
244     oconfig_item_t *option = ci->children + i;
245     int status = 0;
246
247     if (strcasecmp ("Instance", option->key) == 0)
248       status = cf_util_get_string (option, &plugin_instance);
249     else if (strcasecmp ("Interval", option->key) == 0)
250       cf_util_get_cdtime (option, &interval);
251     else if (strcasecmp ("Match", option->key) == 0)
252     {
253       status = ctail_config_add_match (tm, plugin_instance, option, interval);
254       if (status == 0)
255         num_matches++;
256       /* Be mild with failed matches.. */
257       status = 0;
258     }
259     else
260     {
261       status = -1;
262     }
263
264     if (status != 0)
265       break;
266   } /* for (i = 0; i < ci->children_num; i++) */
267
268   sfree (plugin_instance);
269
270   if (num_matches == 0)
271   {
272     ERROR ("tail plugin: No (valid) matches found for file `%s'.",
273         ci->values[0].value.string);
274     tail_match_destroy (tm);
275     return (-1);
276   }
277   else
278   {
279     cu_tail_match_t **temp;
280
281     temp = realloc (tail_match_list,
282         sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
283     if (temp == NULL)
284     {
285       ERROR ("tail plugin: realloc failed.");
286       tail_match_destroy (tm);
287       return (-1);
288     }
289
290     tail_match_list = temp;
291     tail_match_list[tail_match_list_num] = tm;
292     tail_match_list_intervals[tail_match_list_num] = interval;
293     tail_match_list_num++;
294   }
295
296   return (0);
297 } /* int ctail_config_add_file */
298
299 static int ctail_config (oconfig_item_t *ci)
300 {
301   for (int i = 0; i < ci->children_num; i++)
302   {
303     oconfig_item_t *option = ci->children + i;
304
305     if (strcasecmp ("File", option->key) == 0)
306       ctail_config_add_file (option);
307     else
308     {
309       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
310     }
311   } /* for (i = 0; i < ci->children_num; i++) */
312
313   return (0);
314 } /* int ctail_config */
315
316 static int ctail_read (user_data_t *ud)
317 {
318   int status;
319
320   status = tail_match_read ((cu_tail_match_t *)ud->data);
321   if (status != 0)
322   {
323     ERROR ("tail plugin: tail_match_read failed.");
324     return (-1);
325   }
326
327   return (0);
328 } /* int ctail_read */
329
330 static int ctail_init (void)
331 {
332   char str[255];
333
334   if (tail_match_list_num == 0)
335   {
336     WARNING ("tail plugin: File list is empty. Returning an error.");
337     return (-1);
338   }
339
340   for (size_t i = 0; i < tail_match_list_num; i++)
341   {
342     ssnprintf(str, sizeof(str), "tail-%zu", i);
343
344     plugin_register_complex_read (NULL, str, ctail_read, tail_match_list_intervals[i],
345         &(user_data_t) {
346           .data = tail_match_list[i],
347         });
348   }
349
350   return (0);
351 } /* int ctail_init */
352
353 static int ctail_shutdown (void)
354 {
355   for (size_t i = 0; i < tail_match_list_num; i++)
356   {
357     tail_match_destroy (tail_match_list[i]);
358     tail_match_list[i] = NULL;
359   }
360   sfree (tail_match_list);
361   tail_match_list_num = 0;
362
363   return (0);
364 } /* int ctail_shutdown */
365
366 void module_register (void)
367 {
368   plugin_register_complex_config ("tail", ctail_config);
369   plugin_register_init ("tail", ctail_init);
370   plugin_register_shutdown ("tail", ctail_shutdown);
371 } /* void module_register */
372
373 /* vim: set sw=2 sts=2 ts=8 : */