write_redis: remove unused variable from wr_write()
[collectd.git] / src / tail.c
1 /**
2  * collectd - src/tail.c
3  * Copyright (C) 2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_tail_match.h"
26
27 /*
28  *  <Plugin tail>
29  *    <File "/var/log/exim4/mainlog">
30  *      Instance "exim"
31  *      <Match>
32  *        Regex "S=([1-9][0-9]*)"
33  *        ExcludeRegex "U=root.*S="
34  *        DSType "CounterAdd"
35  *        Type "ipt_bytes"
36  *        Instance "total"
37  *      </Match>
38  *    </File>
39  *  </Plugin>
40  */
41
42 struct ctail_config_match_s
43 {
44   char *regex;
45   char *excluderegex;
46   int flags;
47   char *type;
48   char *type_instance;
49 };
50 typedef struct ctail_config_match_s ctail_config_match_t;
51
52 cu_tail_match_t **tail_match_list = NULL;
53 size_t tail_match_list_num = 0;
54
55 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
56     oconfig_item_t *ci)
57 {
58   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
59   {
60     WARNING ("tail plugin: `DSType' needs exactly one string argument.");
61     return (-1);
62   }
63
64   if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
65   {
66     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
67     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
68       cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
69     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
70       cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
71     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
72       cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
73     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
74       cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
75     else
76       cm->flags = 0;
77   }
78   else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
79   {
80     cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
81     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
82       cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
83     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
84       cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
85     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
86       cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
87     else
88       cm->flags = 0;
89   }
90   else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
91   {
92     cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
93     if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
94       cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
95     else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
96       cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
97     else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
98       cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
99     else
100       cm->flags = 0;
101   }
102   else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
103   {
104     cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
105     if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
106       cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
107     else
108       cm->flags = 0;
109   }
110   else
111   {
112     cm->flags = 0;
113   }
114
115   if (cm->flags == 0)
116   {
117     WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
118         ci->values[0].value.string);
119     return (-1);
120   }
121
122   return (0);
123 } /* int ctail_config_add_match_dstype */
124
125 static int ctail_config_add_match (cu_tail_match_t *tm,
126     const char *plugin_instance, oconfig_item_t *ci)
127 {
128   ctail_config_match_t cm;
129   int status;
130   int i;
131
132   memset (&cm, '\0', sizeof (cm));
133
134   if (ci->values_num != 0)
135   {
136     WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
137   }
138
139   status = 0;
140   for (i = 0; i < ci->children_num; i++)
141   {
142     oconfig_item_t *option = ci->children + i;
143
144     if (strcasecmp ("Regex", option->key) == 0)
145       status = cf_util_get_string (option, &cm.regex);
146     else if (strcasecmp ("ExcludeRegex", option->key) == 0)
147       status = cf_util_get_string (option, &cm.excluderegex);
148     else if (strcasecmp ("DSType", option->key) == 0)
149       status = ctail_config_add_match_dstype (&cm, option);
150     else if (strcasecmp ("Type", option->key) == 0)
151       status = cf_util_get_string (option, &cm.type);
152     else if (strcasecmp ("Instance", option->key) == 0)
153       status = cf_util_get_string (option, &cm.type_instance);
154     else
155     {
156       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
157       status = -1;
158     }
159
160     if (status != 0)
161       break;
162   } /* for (i = 0; i < ci->children_num; i++) */
163
164   while (status == 0)
165   {
166     if (cm.regex == NULL)
167     {
168       WARNING ("tail plugin: `Regex' missing in `Match' block.");
169       status = -1;
170       break;
171     }
172
173     if (cm.type == NULL)
174     {
175       WARNING ("tail plugin: `Type' missing in `Match' block.");
176       status = -1;
177       break;
178     }
179
180     if (cm.flags == 0)
181     {
182       WARNING ("tail plugin: `DSType' missing in `Match' block.");
183       status = -1;
184       break;
185     }
186
187     break;
188   } /* while (status == 0) */
189
190   if (status == 0)
191   {
192     status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
193         cm.flags, "tail", plugin_instance, cm.type, cm.type_instance);
194
195     if (status != 0)
196     {
197       ERROR ("tail plugin: tail_match_add_match_simple failed.");
198     }
199   }
200
201   sfree (cm.regex);
202   sfree (cm.excluderegex);
203   sfree (cm.type);
204   sfree (cm.type_instance);
205
206   return (status);
207 } /* int ctail_config_add_match */
208
209 static int ctail_config_add_file (oconfig_item_t *ci)
210 {
211   cu_tail_match_t *tm;
212   char *plugin_instance = NULL;
213   int num_matches = 0;
214   int i;
215
216   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
217   {
218     WARNING ("tail plugin: `File' needs exactly one string argument.");
219     return (-1);
220   }
221
222   tm = tail_match_create (ci->values[0].value.string);
223   if (tm == NULL)
224   {
225     ERROR ("tail plugin: tail_match_create (%s) failed.",
226         ci->values[0].value.string);
227     return (-1);
228   }
229
230   for (i = 0; i < ci->children_num; i++)
231   {
232     oconfig_item_t *option = ci->children + i;
233     int status;
234
235     if (strcasecmp ("Match", option->key) == 0)
236     {
237       status = ctail_config_add_match (tm, plugin_instance, option);
238       if (status == 0)
239         num_matches++;
240       /* Be mild with failed matches.. */
241       status = 0;
242     }
243     else if (strcasecmp ("Instance", option->key) == 0)
244       status = cf_util_get_string (option, &plugin_instance);
245     else
246     {
247       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
248       status = -1;
249     }
250
251     if (status != 0)
252       break;
253   } /* for (i = 0; i < ci->children_num; i++) */
254
255   if (num_matches == 0)
256   {
257     ERROR ("tail plugin: No (valid) matches found for file `%s'.",
258         ci->values[0].value.string);
259     tail_match_destroy (tm);
260     return (-1);
261   }
262   else
263   {
264     cu_tail_match_t **temp;
265
266     temp = (cu_tail_match_t **) realloc (tail_match_list,
267         sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
268     if (temp == NULL)
269     {
270       ERROR ("tail plugin: realloc failed.");
271       tail_match_destroy (tm);
272       return (-1);
273     }
274
275     tail_match_list = temp;
276     tail_match_list[tail_match_list_num] = tm;
277     tail_match_list_num++;
278   }
279
280   return (0);
281 } /* int ctail_config_add_file */
282
283 static int ctail_config (oconfig_item_t *ci)
284 {
285   int i;
286
287   for (i = 0; i < ci->children_num; i++)
288   {
289     oconfig_item_t *option = ci->children + i;
290
291     if (strcasecmp ("File", option->key) == 0)
292       ctail_config_add_file (option);
293     else
294     {
295       WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
296     }
297   } /* for (i = 0; i < ci->children_num; i++) */
298
299   return (0);
300 } /* int ctail_config */
301
302 static int ctail_init (void)
303 {
304   if (tail_match_list_num == 0)
305   {
306     WARNING ("tail plugin: File list is empty. Returning an error.");
307     return (-1);
308   }
309
310   return (0);
311 } /* int ctail_init */
312
313 static int ctail_read (void)
314 {
315   int success = 0;
316   size_t i;
317
318   for (i = 0; i < tail_match_list_num; i++)
319   {
320     int status;
321
322     status = tail_match_read (tail_match_list[i]);
323     if (status != 0)
324     {
325       ERROR ("tail plugin: tail_match_read[%zu] failed.", i);
326     }
327     else
328     {
329       success++;
330     }
331   }
332
333   if (success == 0)
334     return (-1);
335   return (0);
336 } /* int ctail_read */
337
338 static int ctail_shutdown (void)
339 {
340   size_t i;
341
342   for (i = 0; i < tail_match_list_num; i++)
343   {
344     tail_match_destroy (tail_match_list[i]);
345     tail_match_list[i] = NULL;
346   }
347   sfree (tail_match_list);
348   tail_match_list_num = 0;
349
350   return (0);
351 } /* int ctail_shutdown */
352
353 void module_register (void)
354 {
355   plugin_register_complex_config ("tail", ctail_config);
356   plugin_register_init ("tail", ctail_init);
357   plugin_register_read ("tail", ctail_read);
358   plugin_register_shutdown ("tail", ctail_shutdown);
359 } /* void module_register */
360
361 /* vim: set sw=2 sts=2 ts=8 : */