5326e7c18c7910e197fd97d0a6ad43c8ca879a64
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"
32 #include "utils_latency_config.h"
34 /*
35 * <Plugin tail>
36 * <File "/var/log/exim4/mainlog">
37 * Instance "exim"
38 * Interval 60
39 * <Match>
40 * Regex "S=([1-9][0-9]*)"
41 * ExcludeRegex "U=root.*S="
42 * DSType "CounterAdd"
43 * Type "ipt_bytes"
44 * Instance "total"
45 * </Match>
46 * </File>
47 * </Plugin>
48 */
50 struct ctail_config_match_s
51 {
52 char *regex;
53 char *excluderegex;
54 int flags;
55 char *type;
56 char *type_instance;
57 cdtime_t interval;
58 latency_config_t latency;
59 };
60 typedef struct ctail_config_match_s ctail_config_match_t;
62 static cu_tail_match_t **tail_match_list = NULL;
63 static size_t tail_match_list_num = 0;
64 static cdtime_t tail_match_list_intervals[255];
66 static int ctail_config_add_match_dstype (ctail_config_match_t *cm,
67 oconfig_item_t *ci)
68 {
69 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
70 {
71 WARNING ("tail plugin: `DSType' needs exactly one string argument.");
72 return (-1);
73 }
75 if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
76 {
77 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
78 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
79 cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
80 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
81 cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
82 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
83 cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
84 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
85 cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
86 else if (strcasecmp ("GaugeInc", ci->values[0].value.string) == 0)
87 cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
88 else if (strcasecmp ("GaugeAdd", ci->values[0].value.string) == 0)
89 cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
90 else if (strcasecmp ("GaugePersist", ci->values[0].value.string) == 0)
91 cm->flags |= UTILS_MATCH_CF_GAUGE_PERSIST;
92 else
93 cm->flags = 0;
94 }
95 else if (strcasecmp ("Latency", ci->values[0].value.string) == 0)
96 {
97 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
98 cm->flags |= UTILS_MATCH_CF_GAUGE_LATENCY;
99 }
100 else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
101 {
102 cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
103 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
104 cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
105 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
106 cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
107 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
108 cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
109 else
110 cm->flags = 0;
111 }
112 else if (strncasecmp ("Derive", ci->values[0].value.string, strlen ("Derive")) == 0)
113 {
114 cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
115 if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
116 cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
117 else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
118 cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
119 else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
120 cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
121 else
122 cm->flags = 0;
123 }
124 else if (strncasecmp ("Absolute", ci->values[0].value.string, strlen ("Absolute")) == 0)
125 {
126 cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
127 if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0)
128 cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
129 else
130 cm->flags = 0;
131 }
132 else
133 {
134 cm->flags = 0;
135 }
137 if (cm->flags == 0)
138 {
139 WARNING ("tail plugin: `%s' is not a valid argument to `DSType'.",
140 ci->values[0].value.string);
141 return (-1);
142 }
144 return (0);
145 } /* int ctail_config_add_match_dstype */
147 static int ctail_config_add_match (cu_tail_match_t *tm,
148 const char *plugin_instance, oconfig_item_t *ci, cdtime_t interval)
149 {
150 ctail_config_match_t cm = { 0 };
151 int status;
153 if (ci->values_num != 0)
154 {
155 WARNING ("tail plugin: Ignoring arguments for the `Match' block.");
156 }
158 status = 0;
159 for (int i = 0; i < ci->children_num; i++)
160 {
161 oconfig_item_t *option = ci->children + i;
163 if (strcasecmp ("Regex", option->key) == 0)
164 status = cf_util_get_string (option, &cm.regex);
165 else if (strcasecmp ("ExcludeRegex", option->key) == 0)
166 status = cf_util_get_string (option, &cm.excluderegex);
167 else if (strcasecmp ("DSType", option->key) == 0)
168 status = ctail_config_add_match_dstype (&cm, option);
169 else if (strcasecmp ("Type", option->key) == 0)
170 status = cf_util_get_string (option, &cm.type);
171 else if (strcasecmp ("Instance", option->key) == 0)
172 status = cf_util_get_string (option, &cm.type_instance);
173 else if (strncasecmp ("Latency", option->key, strlen ("Latency")) == 0)
174 {
175 if (strcasecmp ("LatencyPercentile", option->key) == 0)
176 status = latency_config_add_percentile ("tail", &cm.latency, option);
177 else if (strcasecmp ("LatencyPercentileType", option->key) == 0)
178 status = cf_util_get_string (option, &cm.latency.percentile_type);
179 else if (strcasecmp ("LatencyRate", option->key) == 0)
180 status = latency_config_add_rate ("tail", &cm.latency, option);
181 else if (strcasecmp ("LatencyRateType", option->key) == 0)
182 status = cf_util_get_string (option, &cm.latency.rates_type);
183 else if (strcasecmp ("LatencyLower", option->key) == 0)
184 status = cf_util_get_boolean (option, &cm.latency.lower);
185 else if (strcasecmp ("LatencyUpper", option->key) == 0)
186 status = cf_util_get_boolean (option, &cm.latency.upper);
187 else if (strcasecmp ("LatencyAvg", option->key) == 0)
188 status = cf_util_get_boolean (option, &cm.latency.avg);
189 else
190 {
191 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
192 status = -1;
193 }
194 }
195 else
196 {
197 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
198 status = -1;
199 }
201 if (status != 0)
202 break;
203 } /* for (i = 0; i < ci->children_num; i++) */
205 while (status == 0)
206 {
207 if (cm.regex == NULL)
208 {
209 WARNING ("tail plugin: `Regex' missing in `Match' block.");
210 status = -1;
211 break;
212 }
214 if (cm.type == NULL)
215 {
216 WARNING ("tail plugin: `Type' missing in `Match' block.");
217 status = -1;
218 break;
219 }
221 if (cm.flags == 0)
222 {
223 WARNING ("tail plugin: `DSType' missing in `Match' block.");
224 status = -1;
225 break;
226 }
228 if ((cm.flags & UTILS_MATCH_DS_TYPE_GAUGE)
229 && (cm.flags & UTILS_MATCH_CF_GAUGE_LATENCY))
230 {
232 if (cm.type_instance != NULL)
233 {
234 WARNING ("tail plugin: `DSType Latency' and `Instance %s' in `Match' "
235 "block could not be used together.", cm.type_instance);
236 status = -1;
237 break;
238 }
240 if (cm.latency.percentile_num == 0 && cm.latency.rates_num == 0)
241 {
242 WARNING ("tail plugin: `Match' with `DSType Latency' has no "
243 "`LatencyPercentile' or `LatencyRate' options.");
244 status = -1;
245 break;
246 }
247 }
249 break;
250 } /* while (status == 0) */
252 if (status == 0)
253 {
254 status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
255 cm.flags, "tail", plugin_instance, cm.type, cm.type_instance,
256 cm.latency, interval);
258 if (status != 0)
259 {
260 ERROR ("tail plugin: tail_match_add_match_simple failed.");
261 }
262 }
264 sfree (cm.regex);
265 sfree (cm.excluderegex);
266 sfree (cm.type);
267 sfree (cm.type_instance);
268 latency_config_free(cm.latency);
270 return (status);
271 } /* int ctail_config_add_match */
273 static int ctail_config_add_file (oconfig_item_t *ci)
274 {
275 cu_tail_match_t *tm;
276 cdtime_t interval = 0;
277 char *plugin_instance = NULL;
278 int num_matches = 0;
280 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
281 {
282 WARNING ("tail plugin: `File' needs exactly one string argument.");
283 return (-1);
284 }
286 tm = tail_match_create (ci->values[0].value.string);
287 if (tm == NULL)
288 {
289 ERROR ("tail plugin: tail_match_create (%s) failed.",
290 ci->values[0].value.string);
291 return (-1);
292 }
294 for (int i = 0; i < ci->children_num; i++)
295 {
296 oconfig_item_t *option = ci->children + i;
297 int status = 0;
299 if (strcasecmp ("Instance", option->key) == 0)
300 status = cf_util_get_string (option, &plugin_instance);
301 else if (strcasecmp ("Interval", option->key) == 0)
302 cf_util_get_cdtime (option, &interval);
303 else if (strcasecmp ("Match", option->key) == 0)
304 {
305 status = ctail_config_add_match (tm, plugin_instance, option, interval);
306 if (status == 0)
307 num_matches++;
308 /* Be mild with failed matches.. */
309 status = 0;
310 }
311 else
312 {
313 status = -1;
314 }
316 if (status != 0)
317 break;
318 } /* for (i = 0; i < ci->children_num; i++) */
320 sfree (plugin_instance);
322 if (num_matches == 0)
323 {
324 ERROR ("tail plugin: No (valid) matches found for file `%s'.",
325 ci->values[0].value.string);
326 tail_match_destroy (tm);
327 return (-1);
328 }
329 else
330 {
331 cu_tail_match_t **temp;
333 temp = realloc (tail_match_list,
334 sizeof (cu_tail_match_t *) * (tail_match_list_num + 1));
335 if (temp == NULL)
336 {
337 ERROR ("tail plugin: realloc failed.");
338 tail_match_destroy (tm);
339 return (-1);
340 }
342 tail_match_list = temp;
343 tail_match_list[tail_match_list_num] = tm;
344 tail_match_list_intervals[tail_match_list_num] = interval;
345 tail_match_list_num++;
346 }
348 return (0);
349 } /* int ctail_config_add_file */
351 static int ctail_config (oconfig_item_t *ci)
352 {
353 for (int i = 0; i < ci->children_num; i++)
354 {
355 oconfig_item_t *option = ci->children + i;
357 if (strcasecmp ("File", option->key) == 0)
358 ctail_config_add_file (option);
359 else
360 {
361 WARNING ("tail plugin: Option `%s' not allowed here.", option->key);
362 }
363 } /* for (i = 0; i < ci->children_num; i++) */
365 return (0);
366 } /* int ctail_config */
368 static int ctail_read (user_data_t *ud)
369 {
370 int status;
372 status = tail_match_read ((cu_tail_match_t *)ud->data);
373 if (status != 0)
374 {
375 ERROR ("tail plugin: tail_match_read failed.");
376 return (-1);
377 }
379 return (0);
380 } /* int ctail_read */
382 static int ctail_init (void)
383 {
384 char str[255];
386 if (tail_match_list_num == 0)
387 {
388 WARNING ("tail plugin: File list is empty. Returning an error.");
389 return (-1);
390 }
392 for (size_t i = 0; i < tail_match_list_num; i++)
393 {
394 ssnprintf(str, sizeof(str), "tail-%zu", i);
396 plugin_register_complex_read (NULL, str, ctail_read, tail_match_list_intervals[i],
397 &(user_data_t) {
398 .data = tail_match_list[i],
399 });
400 }
402 return (0);
403 } /* int ctail_init */
405 static int ctail_shutdown (void)
406 {
407 for (size_t i = 0; i < tail_match_list_num; i++)
408 {
409 tail_match_destroy (tail_match_list[i]);
410 tail_match_list[i] = NULL;
411 }
412 sfree (tail_match_list);
413 tail_match_list_num = 0;
415 return (0);
416 } /* int ctail_shutdown */
418 void module_register (void)
419 {
420 plugin_register_complex_config ("tail", ctail_config);
421 plugin_register_init ("tail", ctail_init);
422 plugin_register_shutdown ("tail", ctail_shutdown);
423 } /* void module_register */
425 /* vim: set sw=2 sts=2 ts=8 : */