2 * collectd - src/tail.c
3 * Copyright (C) 2008 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
31 #include "utils_tail_match.h"
35 * <File "/var/log/exim4/mainlog">
39 * Regex "S=([1-9][0-9]*)"
40 * ExcludeRegex "U=root.*S="
49 struct ctail_config_match_s
58 typedef struct ctail_config_match_s ctail_config_match_t;
60 static cu_tail_match_t **tail_match_list = NULL;
61 static size_t tail_match_list_num = 0;
62 static cdtime_t tail_match_list_intervals[255];
64 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
67 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
69 WARNING ("tail plugin: `DSType' needs exactly one string argument.");
73 if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
75 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
76 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
77 cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
78 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
79 cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
80 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
81 cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
82 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
83 cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
84 else if (strcasecmp ("GaugeInc", ci->values[0].value.string) == 0)
85 cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
86 else if (strcasecmp ("GaugeAdd", ci->values[0].value.string) == 0)
87 cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
91 else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
93 cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
94 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
95 cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
96 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
97 cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
98 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
99 cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
103 else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
105 cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
106 if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
107 cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
108 else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
109 cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
110 else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
111 cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
115 else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
117 cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
118 if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
119 cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
130 WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
131 ci->values[0].value.string);
136 } /* int ctail_config_add_match_dstype */
138 static int ctail_config_add_match (cu_tail_match_t *tm,
139 const char *plugin_instance, oconfig_item_t *ci, cdtime_t interval)
141 ctail_config_match_t cm = { 0 };
144 if (ci->values_num != 0)
146 WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
150 for (int i = 0; i < ci->children_num; i++)
152 oconfig_item_t *option = ci->children + i;
154 if (strcasecmp ("Regex", option->key) == 0)
155 status = cf_util_get_string (option, &cm.regex);
156 else if (strcasecmp ("ExcludeRegex", option->key) == 0)
157 status = cf_util_get_string (option, &cm.excluderegex);
158 else if (strcasecmp ("DSType", option->key) == 0)
159 status = ctail_config_add_match_dstype (&cm, option);
160 else if (strcasecmp ("Type", option->key) == 0)
161 status = cf_util_get_string (option, &cm.type);
162 else if (strcasecmp ("Instance", option->key) == 0)
163 status = cf_util_get_string (option, &cm.type_instance);
166 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
172 } /* for (i = 0; i < ci->children_num; i++) */
176 if (cm.regex == NULL)
178 WARNING ("tail plugin: `Regex' missing in `Match' block.");
185 WARNING ("tail plugin: `Type' missing in `Match' block.");
192 WARNING ("tail plugin: `DSType' missing in `Match' block.");
198 } /* while (status == 0) */
202 status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
203 cm.flags, "tail", plugin_instance, cm.type, cm.type_instance, interval);
207 ERROR ("tail plugin: tail_match_add_match_simple failed.");
212 sfree (cm.excluderegex);
214 sfree (cm.type_instance);
217 } /* int ctail_config_add_match */
219 static int ctail_config_add_file (oconfig_item_t *ci)
222 cdtime_t interval = 0;
223 char *plugin_instance = NULL;
226 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
228 WARNING ("tail plugin: `File' needs exactly one string argument.");
232 tm = tail_match_create (ci->values[0].value.string);
235 ERROR ("tail plugin: tail_match_create (%s) failed.",
236 ci->values[0].value.string);
240 for (int i = 0; i < ci->children_num; i++)
242 oconfig_item_t *option = ci->children + i;
245 if (strcasecmp ("Instance", option->key) == 0)
246 status = cf_util_get_string (option, &plugin_instance);
247 else if (strcasecmp ("Interval", option->key) == 0)
248 cf_util_get_cdtime (option, &interval);
249 else if (strcasecmp ("Match", option->key) == 0)
251 status = ctail_config_add_match (tm, plugin_instance, option, interval);
254 /* Be mild with failed matches.. */
264 } /* for (i = 0; i < ci->children_num; i++) */
266 sfree (plugin_instance);
268 if (num_matches == 0)
270 ERROR ("tail plugin: No (valid) matches found for file `%s'.",
271 ci->values[0].value.string);
272 tail_match_destroy (tm);
277 cu_tail_match_t **temp;
279 temp = realloc (tail_match_list,
280 sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
283 ERROR ("tail plugin: realloc failed.");
284 tail_match_destroy (tm);
288 tail_match_list = temp;
289 tail_match_list[tail_match_list_num] = tm;
290 tail_match_list_intervals[tail_match_list_num] = interval;
291 tail_match_list_num++;
295 } /* int ctail_config_add_file */
297 static int ctail_config (oconfig_item_t *ci)
299 for (int 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_read (user_data_t *ud)
318 status = tail_match_read ((cu_tail_match_t *)ud->data);
321 ERROR ("tail plugin: tail_match_read failed.");
326 } /* int ctail_read */
328 static int ctail_init (void)
332 if (tail_match_list_num == 0)
334 WARNING ("tail plugin: File list is empty. Returning an error.");
338 for (size_t i = 0; i < tail_match_list_num; i++)
340 ssnprintf(str, sizeof(str), "tail-%zu", i);
342 plugin_register_complex_read (NULL, str, ctail_read, tail_match_list_intervals[i],
344 .data = tail_match_list[i],
349 } /* int ctail_init */
351 static int ctail_shutdown (void)
353 for (size_t i = 0; i < tail_match_list_num; i++)
355 tail_match_destroy (tail_match_list[i]);
356 tail_match_list[i] = NULL;
358 sfree (tail_match_list);
359 tail_match_list_num = 0;
362 } /* int ctail_shutdown */
364 void module_register (void)
366 plugin_register_complex_config ("tail", ctail_config);
367 plugin_register_init ("tail", ctail_init);
368 plugin_register_shutdown ("tail", ctail_shutdown);
369 } /* void module_register */
371 /* vim: set sw=2 sts=2 ts=8 : */