1 /**
2 * collectd - src/tail.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_tail_match.h"
33 /*
34 * <Plugin tail>
35 * <File "/var/log/exim4/mainlog">
36 * Instance "exim"
37 * Interval 60
38 * <Match>
39 * Regex "S=([1-9][0-9]*)"
40 * ExcludeRegex "U=root.*S="
41 * DSType "CounterAdd"
42 * Type "ipt_bytes"
43 * Instance "total"
44 * </Match>
45 * </File>
46 * </Plugin>
47 */
49 struct ctail_config_match_s {
50 char *regex;
51 char *excluderegex;
52 int flags;
53 char *type;
54 char *type_instance;
55 cdtime_t interval;
56 };
57 typedef struct ctail_config_match_s ctail_config_match_t;
59 static cu_tail_match_t **tail_match_list = NULL;
60 static size_t tail_match_list_num = 0;
61 static cdtime_t tail_match_list_intervals[255];
63 static int ctail_config_add_match_dstype(ctail_config_match_t *cm,
64 oconfig_item_t *ci) {
65 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
66 WARNING("tail plugin: `DSType' needs exactly one string argument.");
67 return (-1);
68 }
70 if (strncasecmp("Gauge", ci->values[0].value.string, strlen("Gauge")) == 0) {
71 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
72 if (strcasecmp("GaugeAverage", ci->values[0].value.string) == 0)
73 cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
74 else if (strcasecmp("GaugeMin", ci->values[0].value.string) == 0)
75 cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
76 else if (strcasecmp("GaugeMax", ci->values[0].value.string) == 0)
77 cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
78 else if (strcasecmp("GaugeLast", ci->values[0].value.string) == 0)
79 cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
80 else if (strcasecmp("GaugeInc", ci->values[0].value.string) == 0)
81 cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
82 else if (strcasecmp("GaugeAdd", ci->values[0].value.string) == 0)
83 cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
84 else
85 cm->flags = 0;
86 } else if (strncasecmp("Counter", ci->values[0].value.string,
87 strlen("Counter")) == 0) {
88 cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
89 if (strcasecmp("CounterSet", ci->values[0].value.string) == 0)
90 cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
91 else if (strcasecmp("CounterAdd", ci->values[0].value.string) == 0)
92 cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
93 else if (strcasecmp("CounterInc", ci->values[0].value.string) == 0)
94 cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
95 else
96 cm->flags = 0;
97 } else if (strncasecmp("Derive", ci->values[0].value.string,
98 strlen("Derive")) == 0) {
99 cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
100 if (strcasecmp("DeriveSet", ci->values[0].value.string) == 0)
101 cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
102 else if (strcasecmp("DeriveAdd", ci->values[0].value.string) == 0)
103 cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
104 else if (strcasecmp("DeriveInc", ci->values[0].value.string) == 0)
105 cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
106 else
107 cm->flags = 0;
108 } else if (strncasecmp("Absolute", ci->values[0].value.string,
109 strlen("Absolute")) == 0) {
110 cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
111 if (strcasecmp("AbsoluteSet", ci->values[0].value.string) == 0)
112 cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
113 else
114 cm->flags = 0;
115 } else {
116 cm->flags = 0;
117 }
119 if (cm->flags == 0) {
120 WARNING("tail plugin: `%s' is not a valid argument to `DSType'.",
121 ci->values[0].value.string);
122 return (-1);
123 }
125 return (0);
126 } /* int ctail_config_add_match_dstype */
128 static int ctail_config_add_match(cu_tail_match_t *tm,
129 const char *plugin_instance,
130 oconfig_item_t *ci, cdtime_t interval) {
131 ctail_config_match_t cm = {0};
132 int status;
134 if (ci->values_num != 0) {
135 WARNING("tail plugin: Ignoring arguments for the `Match' block.");
136 }
138 status = 0;
139 for (int i = 0; i < ci->children_num; i++) {
140 oconfig_item_t *option = ci->children + i;
142 if (strcasecmp("Regex", option->key) == 0)
143 status = cf_util_get_string(option, &cm.regex);
144 else if (strcasecmp("ExcludeRegex", option->key) == 0)
145 status = cf_util_get_string(option, &cm.excluderegex);
146 else if (strcasecmp("DSType", option->key) == 0)
147 status = ctail_config_add_match_dstype(&cm, option);
148 else if (strcasecmp("Type", option->key) == 0)
149 status = cf_util_get_string(option, &cm.type);
150 else if (strcasecmp("Instance", option->key) == 0)
151 status = cf_util_get_string(option, &cm.type_instance);
152 else {
153 WARNING("tail plugin: Option `%s' not allowed here.", option->key);
154 status = -1;
155 }
157 if (status != 0)
158 break;
159 } /* for (i = 0; i < ci->children_num; i++) */
161 while (status == 0) {
162 if (cm.regex == NULL) {
163 WARNING("tail plugin: `Regex' missing in `Match' block.");
164 status = -1;
165 break;
166 }
168 if (cm.type == NULL) {
169 WARNING("tail plugin: `Type' missing in `Match' block.");
170 status = -1;
171 break;
172 }
174 if (cm.flags == 0) {
175 WARNING("tail plugin: `DSType' missing in `Match' block.");
176 status = -1;
177 break;
178 }
180 break;
181 } /* while (status == 0) */
183 if (status == 0) {
184 status = tail_match_add_match_simple(tm, cm.regex, cm.excluderegex,
185 cm.flags, "tail", plugin_instance,
186 cm.type, cm.type_instance, interval);
188 if (status != 0) {
189 ERROR("tail plugin: tail_match_add_match_simple failed.");
190 }
191 }
193 sfree(cm.regex);
194 sfree(cm.excluderegex);
195 sfree(cm.type);
196 sfree(cm.type_instance);
198 return (status);
199 } /* int ctail_config_add_match */
201 static int ctail_config_add_file(oconfig_item_t *ci) {
202 cu_tail_match_t *tm;
203 cdtime_t interval = 0;
204 char *plugin_instance = NULL;
205 int num_matches = 0;
207 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
208 WARNING("tail plugin: `File' needs exactly one string argument.");
209 return (-1);
210 }
212 tm = tail_match_create(ci->values[0].value.string);
213 if (tm == NULL) {
214 ERROR("tail plugin: tail_match_create (%s) failed.",
215 ci->values[0].value.string);
216 return (-1);
217 }
219 for (int i = 0; i < ci->children_num; i++) {
220 oconfig_item_t *option = ci->children + i;
221 int status = 0;
223 if (strcasecmp("Instance", option->key) == 0)
224 status = cf_util_get_string(option, &plugin_instance);
225 else if (strcasecmp("Interval", option->key) == 0)
226 cf_util_get_cdtime(option, &interval);
227 else if (strcasecmp("Match", option->key) == 0) {
228 status = ctail_config_add_match(tm, plugin_instance, option, interval);
229 if (status == 0)
230 num_matches++;
231 /* Be mild with failed matches.. */
232 status = 0;
233 } else {
234 status = -1;
235 }
237 if (status != 0)
238 break;
239 } /* for (i = 0; i < ci->children_num; i++) */
241 sfree(plugin_instance);
243 if (num_matches == 0) {
244 ERROR("tail plugin: No (valid) matches found for file `%s'.",
245 ci->values[0].value.string);
246 tail_match_destroy(tm);
247 return (-1);
248 } else {
249 cu_tail_match_t **temp;
251 temp = realloc(tail_match_list,
252 sizeof(cu_tail_match_t *) * (tail_match_list_num + 1));
253 if (temp == NULL) {
254 ERROR("tail plugin: realloc failed.");
255 tail_match_destroy(tm);
256 return (-1);
257 }
259 tail_match_list = temp;
260 tail_match_list[tail_match_list_num] = tm;
261 tail_match_list_intervals[tail_match_list_num] = interval;
262 tail_match_list_num++;
263 }
265 return (0);
266 } /* int ctail_config_add_file */
268 static int ctail_config(oconfig_item_t *ci) {
269 for (int i = 0; i < ci->children_num; i++) {
270 oconfig_item_t *option = ci->children + i;
272 if (strcasecmp("File", option->key) == 0)
273 ctail_config_add_file(option);
274 else {
275 WARNING("tail plugin: Option `%s' not allowed here.", option->key);
276 }
277 } /* for (i = 0; i < ci->children_num; i++) */
279 return (0);
280 } /* int ctail_config */
282 static int ctail_read(user_data_t *ud) {
283 int status;
285 status = tail_match_read((cu_tail_match_t *)ud->data);
286 if (status != 0) {
287 ERROR("tail plugin: tail_match_read failed.");
288 return (-1);
289 }
291 return (0);
292 } /* int ctail_read */
294 static int ctail_init(void) {
295 char str[255];
297 if (tail_match_list_num == 0) {
298 WARNING("tail plugin: File list is empty. Returning an error.");
299 return (-1);
300 }
302 for (size_t i = 0; i < tail_match_list_num; i++) {
303 ssnprintf(str, sizeof(str), "tail-%zu", i);
305 user_data_t ud = {.data = tail_match_list[i]};
307 plugin_register_complex_read(NULL, str, ctail_read,
308 tail_match_list_intervals[i], &ud);
309 }
311 return (0);
312 } /* int ctail_init */
314 static int ctail_shutdown(void) {
315 for (size_t i = 0; i < tail_match_list_num; i++) {
316 tail_match_destroy(tail_match_list[i]);
317 tail_match_list[i] = NULL;
318 }
319 sfree(tail_match_list);
320 tail_match_list_num = 0;
322 return (0);
323 } /* int ctail_shutdown */
325 void module_register(void) {
326 plugin_register_complex_config("tail", ctail_config);
327 plugin_register_init("tail", ctail_init);
328 plugin_register_shutdown("tail", ctail_shutdown);
329 } /* void module_register */
331 /* vim: set sw=2 sts=2 ts=8 : */