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]*)"
41 struct ctail_config_match_s
48 typedef struct ctail_config_match_s ctail_config_match_t;
50 cu_tail_match_t **tail_match_list = NULL;
51 size_t tail_match_list_num = 0;
53 static int ctail_config_add_string (const char *name, char **dest, oconfig_item_t *ci)
55 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
57 WARNING ("tail plugin: `%s' needs exactly one string argument.", name);
62 *dest = strdup (ci->values[0].value.string);
67 } /* int ctail_config_add_string */
69 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
72 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
74 WARNING ("tail plugin: `DSType' needs exactly one string argument.");
78 if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
80 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
81 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
82 cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
83 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
84 cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
85 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
86 cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
87 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
88 cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
92 else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
94 cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
95 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
96 cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
97 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
98 cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
99 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
100 cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
104 else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
106 cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
107 if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
108 cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
109 else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
110 cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
111 else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
112 cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
116 else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
118 cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
119 if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
120 cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
131 WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
132 ci->values[0].value.string);
137 } /* int ctail_config_add_match_dstype */
139 static int ctail_config_add_match (cu_tail_match_t *tm,
140 const char *plugin_instance, oconfig_item_t *ci)
142 ctail_config_match_t cm;
146 memset (&cm, '\0', sizeof (cm));
148 if (ci->values_num != 0)
150 WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
154 for (i = 0; i < ci->children_num; i++)
156 oconfig_item_t *option = ci->children + i;
158 if (strcasecmp ("Regex", option->key) == 0)
159 status = ctail_config_add_string ("Regex", &cm.regex, option);
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 = ctail_config_add_string ("Type", &cm.type, option);
164 else if (strcasecmp ("Instance", option->key) == 0)
165 status = ctail_config_add_string ("Instance", &cm.type_instance, option);
168 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
174 } /* for (i = 0; i < ci->children_num; i++) */
178 if (cm.regex == NULL)
180 WARNING ("tail plugin: `Regex' missing in `Match' block.");
187 WARNING ("tail plugin: `Type' missing in `Match' block.");
194 WARNING ("tail plugin: `DSType' missing in `Match' block.");
200 } /* while (status == 0) */
204 status = tail_match_add_match_simple (tm, cm.regex, cm.flags,
205 "tail", plugin_instance, cm.type, cm.type_instance);
209 ERROR ("tail plugin: tail_match_add_match_simple failed.");
215 sfree (cm.type_instance);
218 } /* int ctail_config_add_match */
220 static int ctail_config_add_file (oconfig_item_t *ci)
223 char *plugin_instance = NULL;
228 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
230 WARNING ("tail plugin: `File' needs exactly one string argument.");
234 tm = tail_match_create (ci->values[0].value.string);
237 ERROR ("tail plugin: tail_match_create (%s) failed.",
238 ci->values[0].value.string);
243 for (i = 0; i < ci->children_num; i++)
245 oconfig_item_t *option = ci->children + i;
247 if (strcasecmp ("Match", option->key) == 0)
249 status = ctail_config_add_match (tm, plugin_instance, option);
252 /* Be mild with failed matches.. */
255 else if (strcasecmp ("Instance", option->key) == 0)
256 status = ctail_config_add_string ("Instance", &plugin_instance, option);
259 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
265 } /* for (i = 0; i < ci->children_num; i++) */
267 if (num_matches == 0)
269 ERROR ("tail plugin: No (valid) matches found for file `%s'.",
270 ci->values[0].value.string);
271 tail_match_destroy (tm);
276 cu_tail_match_t **temp;
278 temp = (cu_tail_match_t **) realloc (tail_match_list,
279 sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
282 ERROR ("tail plugin: realloc failed.");
283 tail_match_destroy (tm);
287 tail_match_list = temp;
288 tail_match_list[tail_match_list_num] = tm;
289 tail_match_list_num++;
293 } /* int ctail_config_add_file */
295 static int ctail_config (oconfig_item_t *ci)
299 for (i = 0; i < ci->children_num; i++)
301 oconfig_item_t *option = ci->children + i;
303 if (strcasecmp ("File", option->key) == 0)
304 ctail_config_add_file (option);
307 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
309 } /* for (i = 0; i < ci->children_num; i++) */
312 } /* int ctail_config */
314 static int ctail_init (void)
316 if (tail_match_list_num == 0)
318 WARNING ("tail plugin: File list is empty. Returning an error.");
323 } /* int ctail_init */
325 static int ctail_read (void)
330 for (i = 0; i < tail_match_list_num; i++)
334 status = tail_match_read (tail_match_list[i]);
337 ERROR ("tail plugin: tail_match_read[%zu] failed.", i);
348 } /* int ctail_read */
350 static int ctail_shutdown (void)
354 for (i = 0; i < tail_match_list_num; i++)
356 tail_match_destroy (tail_match_list[i]);
357 tail_match_list[i] = NULL;
359 sfree (tail_match_list);
360 tail_match_list_num = 0;
363 } /* int ctail_shutdown */
365 void module_register (void)
367 plugin_register_complex_config ("tail", ctail_config);
368 plugin_register_init ("tail", ctail_init);
369 plugin_register_read ("tail", ctail_read);
370 plugin_register_shutdown ("tail", ctail_shutdown);
371 } /* void module_register */
373 /* vim: set sw=2 sts=2 ts=8 : */