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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_tail_match.h"
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 */
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;
52 cu_tail_match_t **tail_match_list = NULL;
53 size_t tail_match_list_num = 0;
55 static int ctail_config_add_string (const char *name, char **dest, oconfig_item_t *ci)
56 {
57 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
58 {
59 WARNING ("tail plugin: `%s' needs exactly one string argument.", name);
60 return (-1);
61 }
63 sfree (*dest);
64 *dest = strdup (ci->values[0].value.string);
65 if (*dest == NULL)
66 return (-1);
68 return (0);
69 } /* int ctail_config_add_string */
71 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
72 oconfig_item_t *ci)
73 {
74 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
75 {
76 WARNING ("tail plugin: `DSType' needs exactly one string argument.");
77 return (-1);
78 }
80 if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
81 {
82 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
83 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
84 cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
85 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
86 cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
87 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
88 cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
89 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
90 cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
91 else
92 cm->flags = 0;
93 }
94 else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
95 {
96 cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
97 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
98 cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
99 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
100 cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
101 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
102 cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
103 else
104 cm->flags = 0;
105 }
106 else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
107 {
108 cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
109 if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
110 cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
111 else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
112 cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
113 else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
114 cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
115 else
116 cm->flags = 0;
117 }
118 else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
119 {
120 cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
121 if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
122 cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
123 else
124 cm->flags = 0;
125 }
126 else
127 {
128 cm->flags = 0;
129 }
131 if (cm->flags == 0)
132 {
133 WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
134 ci->values[0].value.string);
135 return (-1);
136 }
138 return (0);
139 } /* int ctail_config_add_match_dstype */
141 static int ctail_config_add_match (cu_tail_match_t *tm,
142 const char *plugin_instance, oconfig_item_t *ci)
143 {
144 ctail_config_match_t cm;
145 int status;
146 int i;
148 memset (&cm, '\0', sizeof (cm));
150 if (ci->values_num != 0)
151 {
152 WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
153 }
155 status = 0;
156 for (i = 0; i < ci->children_num; i++)
157 {
158 oconfig_item_t *option = ci->children + i;
160 if (strcasecmp ("Regex", option->key) == 0)
161 status = ctail_config_add_string ("Regex", &cm.regex, option);
162 else if (strcasecmp ("ExcludeRegex", option->key) == 0)
163 status = ctail_config_add_string ("ExcludeRegex", &cm.excluderegex,
164 option);
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 = ctail_config_add_string ("Type", &cm.type, option);
169 else if (strcasecmp ("Instance", option->key) == 0)
170 status = ctail_config_add_string ("Instance", &cm.type_instance, option);
171 else
172 {
173 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
174 status = -1;
175 }
177 if (status != 0)
178 break;
179 } /* for (i = 0; i < ci->children_num; i++) */
181 while (status == 0)
182 {
183 if (cm.regex == NULL)
184 {
185 WARNING ("tail plugin: `Regex' missing in `Match' block.");
186 status = -1;
187 break;
188 }
190 if (cm.type == NULL)
191 {
192 WARNING ("tail plugin: `Type' missing in `Match' block.");
193 status = -1;
194 break;
195 }
197 if (cm.flags == 0)
198 {
199 WARNING ("tail plugin: `DSType' missing in `Match' block.");
200 status = -1;
201 break;
202 }
204 break;
205 } /* while (status == 0) */
207 if (status == 0)
208 {
209 status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
210 cm.flags, "tail", plugin_instance, cm.type, cm.type_instance);
212 if (status != 0)
213 {
214 ERROR ("tail plugin: tail_match_add_match_simple failed.");
215 }
216 }
218 sfree (cm.regex);
219 sfree (cm.excluderegex);
220 sfree (cm.type);
221 sfree (cm.type_instance);
223 return (status);
224 } /* int ctail_config_add_match */
226 static int ctail_config_add_file (oconfig_item_t *ci)
227 {
228 cu_tail_match_t *tm;
229 char *plugin_instance = NULL;
230 int num_matches = 0;
231 int status;
232 int i;
234 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
235 {
236 WARNING ("tail plugin: `File' needs exactly one string argument.");
237 return (-1);
238 }
240 tm = tail_match_create (ci->values[0].value.string);
241 if (tm == NULL)
242 {
243 ERROR ("tail plugin: tail_match_create (%s) failed.",
244 ci->values[0].value.string);
245 return (-1);
246 }
248 status = 0;
249 for (i = 0; i < ci->children_num; i++)
250 {
251 oconfig_item_t *option = ci->children + i;
253 if (strcasecmp ("Match", option->key) == 0)
254 {
255 status = ctail_config_add_match (tm, plugin_instance, option);
256 if (status == 0)
257 num_matches++;
258 /* Be mild with failed matches.. */
259 status = 0;
260 }
261 else if (strcasecmp ("Instance", option->key) == 0)
262 status = ctail_config_add_string ("Instance", &plugin_instance, option);
263 else
264 {
265 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
266 status = -1;
267 }
269 if (status != 0)
270 break;
271 } /* for (i = 0; i < ci->children_num; i++) */
273 if (num_matches == 0)
274 {
275 ERROR ("tail plugin: No (valid) matches found for file `%s'.",
276 ci->values[0].value.string);
277 tail_match_destroy (tm);
278 return (-1);
279 }
280 else
281 {
282 cu_tail_match_t **temp;
284 temp = (cu_tail_match_t **) realloc (tail_match_list,
285 sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
286 if (temp == NULL)
287 {
288 ERROR ("tail plugin: realloc failed.");
289 tail_match_destroy (tm);
290 return (-1);
291 }
293 tail_match_list = temp;
294 tail_match_list[tail_match_list_num] = tm;
295 tail_match_list_num++;
296 }
298 return (0);
299 } /* int ctail_config_add_file */
301 static int ctail_config (oconfig_item_t *ci)
302 {
303 int i;
305 for (i = 0; i < ci->children_num; i++)
306 {
307 oconfig_item_t *option = ci->children + i;
309 if (strcasecmp ("File", option->key) == 0)
310 ctail_config_add_file (option);
311 else
312 {
313 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
314 }
315 } /* for (i = 0; i < ci->children_num; i++) */
317 return (0);
318 } /* int ctail_config */
320 static int ctail_init (void)
321 {
322 if (tail_match_list_num == 0)
323 {
324 WARNING ("tail plugin: File list is empty. Returning an error.");
325 return (-1);
326 }
328 return (0);
329 } /* int ctail_init */
331 static int ctail_read (void)
332 {
333 int success = 0;
334 size_t i;
336 for (i = 0; i < tail_match_list_num; i++)
337 {
338 int status;
340 status = tail_match_read (tail_match_list[i]);
341 if (status != 0)
342 {
343 ERROR ("tail plugin: tail_match_read[%zu] failed.", i);
344 }
345 else
346 {
347 success++;
348 }
349 }
351 if (success == 0)
352 return (-1);
353 return (0);
354 } /* int ctail_read */
356 static int ctail_shutdown (void)
357 {
358 size_t i;
360 for (i = 0; i < tail_match_list_num; i++)
361 {
362 tail_match_destroy (tail_match_list[i]);
363 tail_match_list[i] = NULL;
364 }
365 sfree (tail_match_list);
366 tail_match_list_num = 0;
368 return (0);
369 } /* int ctail_shutdown */
371 void module_register (void)
372 {
373 plugin_register_complex_config ("tail", ctail_config);
374 plugin_register_init ("tail", ctail_init);
375 plugin_register_read ("tail", ctail_read);
376 plugin_register_shutdown ("tail", ctail_shutdown);
377 } /* void module_register */
379 /* vim: set sw=2 sts=2 ts=8 : */