2 * collectd - src/tail.c
3 * Copyright (C) 2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_tail_match.h"
29 * <File "/var/log/exim4/mainlog">
32 * Regex "S=([1-9][0-9]*)"
33 * ExcludeRegex "U=root.*S="
42 struct ctail_config_match_s
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)
57 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
59 WARNING ("tail plugin: `%s' needs exactly one string argument.", name);
64 *dest = strdup (ci->values[0].value.string);
69 } /* int ctail_config_add_string */
71 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
74 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
76 WARNING ("tail plugin: `DSType' needs exactly one string argument.");
80 if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
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;
94 else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
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;
106 else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
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;
118 else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
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;
133 WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
134 ci->values[0].value.string);
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)
144 ctail_config_match_t cm;
148 memset (&cm, '\0', sizeof (cm));
150 if (ci->values_num != 0)
152 WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
156 for (i = 0; i < ci->children_num; i++)
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,
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);
173 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
179 } /* for (i = 0; i < ci->children_num; i++) */
183 if (cm.regex == NULL)
185 WARNING ("tail plugin: `Regex' missing in `Match' block.");
192 WARNING ("tail plugin: `Type' missing in `Match' block.");
199 WARNING ("tail plugin: `DSType' missing in `Match' block.");
205 } /* while (status == 0) */
209 status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
210 cm.flags, "tail", plugin_instance, cm.type, cm.type_instance);
214 ERROR ("tail plugin: tail_match_add_match_simple failed.");
219 sfree (cm.excluderegex);
221 sfree (cm.type_instance);
224 } /* int ctail_config_add_match */
226 static int ctail_config_add_file (oconfig_item_t *ci)
229 char *plugin_instance = NULL;
234 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
236 WARNING ("tail plugin: `File' needs exactly one string argument.");
240 tm = tail_match_create (ci->values[0].value.string);
243 ERROR ("tail plugin: tail_match_create (%s) failed.",
244 ci->values[0].value.string);
249 for (i = 0; i < ci->children_num; i++)
251 oconfig_item_t *option = ci->children + i;
253 if (strcasecmp ("Match", option->key) == 0)
255 status = ctail_config_add_match (tm, plugin_instance, option);
258 /* Be mild with failed matches.. */
261 else if (strcasecmp ("Instance", option->key) == 0)
262 status = ctail_config_add_string ("Instance", &plugin_instance, option);
265 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
271 } /* for (i = 0; i < ci->children_num; i++) */
273 if (num_matches == 0)
275 ERROR ("tail plugin: No (valid) matches found for file `%s'.",
276 ci->values[0].value.string);
277 tail_match_destroy (tm);
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));
288 ERROR ("tail plugin: realloc failed.");
289 tail_match_destroy (tm);
293 tail_match_list = temp;
294 tail_match_list[tail_match_list_num] = tm;
295 tail_match_list_num++;
299 } /* int ctail_config_add_file */
301 static int ctail_config (oconfig_item_t *ci)
305 for (i = 0; i < ci->children_num; i++)
307 oconfig_item_t *option = ci->children + i;
309 if (strcasecmp ("File", option->key) == 0)
310 ctail_config_add_file (option);
313 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
315 } /* for (i = 0; i < ci->children_num; i++) */
318 } /* int ctail_config */
320 static int ctail_init (void)
322 if (tail_match_list_num == 0)
324 WARNING ("tail plugin: File list is empty. Returning an error.");
329 } /* int ctail_init */
331 static int ctail_read (void)
336 for (i = 0; i < tail_match_list_num; i++)
340 status = tail_match_read (tail_match_list[i]);
343 ERROR ("tail plugin: tail_match_read[%zu] failed.", i);
354 } /* int ctail_read */
356 static int ctail_shutdown (void)
360 for (i = 0; i < tail_match_list_num; i++)
362 tail_match_destroy (tail_match_list[i]);
363 tail_match_list[i] = NULL;
365 sfree (tail_match_list);
366 tail_match_list_num = 0;
369 } /* int ctail_shutdown */
371 void module_register (void)
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 : */