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;
111 WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
112 ci->values[0].value.string);
117 } /* int ctail_config_add_match_dstype */
119 static int ctail_config_add_match (cu_tail_match_t *tm,
120 const char *plugin_instance, oconfig_item_t *ci)
122 ctail_config_match_t cm;
126 memset (&cm, '\0', sizeof (cm));
128 if (ci->values_num != 0)
130 WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
134 for (i = 0; i < ci->children_num; i++)
136 oconfig_item_t *option = ci->children + i;
138 if (strcasecmp ("Regex", option->key) == 0)
139 status = ctail_config_add_string ("Regex", &cm.regex, option);
140 else if (strcasecmp ("DSType", option->key) == 0)
141 status = ctail_config_add_match_dstype (&cm, option);
142 else if (strcasecmp ("Type", option->key) == 0)
143 status = ctail_config_add_string ("Type", &cm.type, option);
144 else if (strcasecmp ("Instance", option->key) == 0)
145 status = ctail_config_add_string ("Instance", &cm.type_instance, option);
148 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
154 } /* for (i = 0; i < ci->children_num; i++) */
158 if (cm.regex == NULL)
160 WARNING ("tail plugin: `Regex' missing in `Match' block.");
167 WARNING ("tail plugin: `Type' missing in `Match' block.");
174 WARNING ("tail plugin: `DSType' missing in `Match' block.");
180 } /* while (status == 0) */
184 status = tail_match_add_match_simple (tm, cm.regex, cm.flags,
185 "tail", plugin_instance, cm.type, cm.type_instance);
189 ERROR ("tail plugin: tail_match_add_match_simple failed.");
195 sfree (cm.type_instance);
198 } /* int ctail_config_add_match */
200 static int ctail_config_add_file (oconfig_item_t *ci)
203 char *plugin_instance = NULL;
208 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
210 WARNING ("tail plugin: `File' needs exactly one string argument.");
214 tm = tail_match_create (ci->values[0].value.string);
217 ERROR ("tail plugin: tail_match_create (%s) failed.",
218 ci->values[0].value.string);
223 for (i = 0; i < ci->children_num; i++)
225 oconfig_item_t *option = ci->children + i;
227 if (strcasecmp ("Match", option->key) == 0)
229 status = ctail_config_add_match (tm, plugin_instance, option);
232 /* Be mild with failed matches.. */
235 else if (strcasecmp ("Instance", option->key) == 0)
236 status = ctail_config_add_string ("Instance", &plugin_instance, option);
239 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
245 } /* for (i = 0; i < ci->children_num; i++) */
247 if (num_matches == 0)
249 ERROR ("tail plugin: No (valid) matches found for file `%s'.",
250 ci->values[0].value.string);
251 tail_match_destroy (tm);
256 cu_tail_match_t **temp;
258 temp = (cu_tail_match_t **) realloc (tail_match_list,
259 sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
262 ERROR ("tail plugin: realloc failed.");
263 tail_match_destroy (tm);
267 tail_match_list = temp;
268 tail_match_list[tail_match_list_num] = tm;
269 tail_match_list_num++;
273 } /* int ctail_config_add_file */
275 static int ctail_config (oconfig_item_t *ci)
279 for (i = 0; i < ci->children_num; i++)
281 oconfig_item_t *option = ci->children + i;
283 if (strcasecmp ("File", option->key) == 0)
284 ctail_config_add_file (option);
287 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
289 } /* for (i = 0; i < ci->children_num; i++) */
292 } /* int ctail_config */
294 static int ctail_init (void)
296 if (tail_match_list_num == 0)
298 WARNING ("tail plugin: File list is empty. Returning an error.");
303 } /* int ctail_init */
305 static int ctail_read (void)
310 for (i = 0; i < tail_match_list_num; i++)
314 status = tail_match_read (tail_match_list[i]);
317 ERROR ("tail plugin: tail_match_read[%i] failed.", i);
328 } /* int ctail_read */
330 static int ctail_shutdown (void)
334 for (i = 0; i < tail_match_list_num; i++)
336 tail_match_destroy (tail_match_list[i]);
337 tail_match_list[i] = NULL;
339 sfree (tail_match_list);
340 tail_match_list_num = 0;
343 } /* int ctail_shutdown */
345 void module_register (void)
347 plugin_register_complex_config ("tail", ctail_config);
348 plugin_register_init ("tail", ctail_init);
349 plugin_register_read ("tail", ctail_read);
350 plugin_register_shutdown ("tail", ctail_shutdown);
351 } /* void module_register */
353 /* vim: set sw=2 sts=2 ts=8 : */