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">
33 * Regex "S=([1-9][0-9]*)"
34 * ExcludeRegex "U=root.*S="
43 struct ctail_config_match_s
52 typedef struct ctail_config_match_s ctail_config_match_t;
54 cu_tail_match_t **tail_match_list = NULL;
55 size_t tail_match_list_num = 0;
56 cdtime_t tail_match_list_intervals[255];
58 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
61 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
63 WARNING ("tail plugin: `DSType' needs exactly one string argument.");
67 if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
69 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
70 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
71 cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
72 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
73 cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
74 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
75 cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
76 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
77 cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
81 else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
83 cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
84 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
85 cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
86 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
87 cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
88 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
89 cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
93 else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
95 cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
96 if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
97 cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
98 else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
99 cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
100 else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
101 cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
105 else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
107 cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
108 if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
109 cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
120 WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
121 ci->values[0].value.string);
126 } /* int ctail_config_add_match_dstype */
128 static int ctail_config_add_match (cu_tail_match_t *tm,
129 const char *plugin_instance, oconfig_item_t *ci, cdtime_t interval)
131 ctail_config_match_t cm;
135 memset (&cm, '\0', sizeof (cm));
137 if (ci->values_num != 0)
139 WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
143 for (i = 0; i < ci->children_num; i++)
145 oconfig_item_t *option = ci->children + i;
147 if (strcasecmp ("Regex", option->key) == 0)
148 status = cf_util_get_string (option, &cm.regex);
149 else if (strcasecmp ("ExcludeRegex", option->key) == 0)
150 status = cf_util_get_string (option, &cm.excluderegex);
151 else if (strcasecmp ("DSType", option->key) == 0)
152 status = ctail_config_add_match_dstype (&cm, option);
153 else if (strcasecmp ("Type", option->key) == 0)
154 status = cf_util_get_string (option, &cm.type);
155 else if (strcasecmp ("Instance", option->key) == 0)
156 status = cf_util_get_string (option, &cm.type_instance);
159 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
165 } /* for (i = 0; i < ci->children_num; i++) */
169 if (cm.regex == NULL)
171 WARNING ("tail plugin: `Regex' missing in `Match' block.");
178 WARNING ("tail plugin: `Type' missing in `Match' block.");
185 WARNING ("tail plugin: `DSType' missing in `Match' block.");
191 } /* while (status == 0) */
195 status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
196 cm.flags, "tail", plugin_instance, cm.type, cm.type_instance, interval);
200 ERROR ("tail plugin: tail_match_add_match_simple failed.");
205 sfree (cm.excluderegex);
207 sfree (cm.type_instance);
210 } /* int ctail_config_add_match */
212 static int ctail_config_add_file (oconfig_item_t *ci)
215 cdtime_t interval = 0;
216 char *plugin_instance = NULL;
221 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
223 WARNING ("tail plugin: `File' needs exactly one string argument.");
227 tm = tail_match_create (ci->values[0].value.string);
230 ERROR ("tail plugin: tail_match_create (%s) failed.",
231 ci->values[0].value.string);
236 for (i = 0; i < ci->children_num; i++)
238 oconfig_item_t *option = ci->children + i;
240 if (strcasecmp ("Instance", option->key) == 0)
241 status = cf_util_get_string (option, &plugin_instance);
242 else if (strcasecmp ("Interval", option->key) == 0)
243 cf_util_get_cdtime (option, &interval);
244 else if (strcasecmp ("Match", option->key) == 0)
246 status = ctail_config_add_match (tm, plugin_instance, option, interval);
249 /* Be mild with failed matches.. */
259 } /* for (i = 0; i < ci->children_num; i++) */
261 if (num_matches == 0)
263 ERROR ("tail plugin: No (valid) matches found for file `%s'.",
264 ci->values[0].value.string);
265 tail_match_destroy (tm);
270 cu_tail_match_t **temp;
272 temp = (cu_tail_match_t **) realloc (tail_match_list,
273 sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
276 ERROR ("tail plugin: realloc failed.");
277 tail_match_destroy (tm);
281 tail_match_list = temp;
282 tail_match_list[tail_match_list_num] = tm;
283 tail_match_list_intervals[tail_match_list_num] = interval;
284 tail_match_list_num++;
288 } /* int ctail_config_add_file */
290 static int ctail_config (oconfig_item_t *ci)
294 for (i = 0; i < ci->children_num; i++)
296 oconfig_item_t *option = ci->children + i;
298 if (strcasecmp ("File", option->key) == 0)
299 ctail_config_add_file (option);
302 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
304 } /* for (i = 0; i < ci->children_num; i++) */
307 } /* int ctail_config */
309 static int ctail_read (user_data_t *ud)
313 status = tail_match_read ((cu_tail_match_t *)ud->data);
316 ERROR ("tail plugin: tail_match_read failed.");
321 } /* int ctail_read */
323 static int ctail_init (void)
325 struct timespec cb_interval;
330 if (tail_match_list_num == 0)
332 WARNING ("tail plugin: File list is empty. Returning an error.");
336 for (i = 0; i < tail_match_list_num; i++)
338 ud.data = (void *)tail_match_list[i];
339 ssnprintf(str, sizeof(str), "tail-%zu", i);
340 CDTIME_T_TO_TIMESPEC (tail_match_list_intervals[i], &cb_interval);
341 plugin_register_complex_read (NULL, str, ctail_read, &cb_interval, &ud);
345 } /* int ctail_init */
347 static int ctail_shutdown (void)
351 for (i = 0; i < tail_match_list_num; i++)
353 tail_match_destroy (tail_match_list[i]);
354 tail_match_list[i] = NULL;
356 sfree (tail_match_list);
357 tail_match_list_num = 0;
360 } /* int ctail_shutdown */
362 void module_register (void)
364 plugin_register_complex_config ("tail", ctail_config);
365 plugin_register_init ("tail", ctail_init);
366 plugin_register_shutdown ("tail", ctail_shutdown);
367 } /* void module_register */
369 /* vim: set sw=2 sts=2 ts=8 : */