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_latency_config.h"
32 #include "utils_tail_match.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 char *regex;
52 char *excluderegex;
53 int flags;
54 char *type;
55 char *type_instance;
56 cdtime_t interval;
57 latency_config_t latency;
58 };
59 typedef struct ctail_config_match_s ctail_config_match_t;
61 static cu_tail_match_t **tail_match_list = NULL;
62 static size_t tail_match_list_num = 0;
63 static cdtime_t tail_match_list_intervals[255];
65 static int ctail_config_add_match_dstype(ctail_config_match_t *cm,
66 oconfig_item_t *ci) {
67 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
68 WARNING("tail plugin: `DSType' needs exactly one string argument.");
69 return (-1);
70 }
72 char const *ds_type = ci->values[0].value.string;
73 if (strncasecmp("Gauge", ds_type, strlen("Gauge")) == 0) {
74 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
75 if (strcasecmp("GaugeAverage", ds_type) == 0)
76 cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
77 else if (strcasecmp("GaugeMin", ds_type) == 0)
78 cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
79 else if (strcasecmp("GaugeMax", ds_type) == 0)
80 cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
81 else if (strcasecmp("GaugeLast", ds_type) == 0)
82 cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
83 else if (strcasecmp("GaugeInc", ds_type) == 0)
84 cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
85 else if (strcasecmp("GaugeAdd", ds_type) == 0)
86 cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
87 else if (strcasecmp("GaugePersist", ci->values[0].value.string) == 0)
88 cm->flags |= UTILS_MATCH_CF_GAUGE_PERSIST;
89 else
90 cm->flags = 0;
91 } else if (strcasecmp("Distribution", ds_type) == 0) {
92 cm->flags = UTILS_MATCH_DS_TYPE_GAUGE | UTILS_MATCH_CF_GAUGE_DIST;
94 int status = latency_config(&cm->latency, ci, "tail");
95 if (status != 0)
96 return (status);
97 } else if (strncasecmp("Counter", ds_type, strlen("Counter")) == 0) {
98 cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
99 if (strcasecmp("CounterSet", ds_type) == 0)
100 cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
101 else if (strcasecmp("CounterAdd", ds_type) == 0)
102 cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
103 else if (strcasecmp("CounterInc", ds_type) == 0)
104 cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
105 else
106 cm->flags = 0;
107 } else if (strncasecmp("Derive", ds_type, strlen("Derive")) == 0) {
108 cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
109 if (strcasecmp("DeriveSet", ds_type) == 0)
110 cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
111 else if (strcasecmp("DeriveAdd", ds_type) == 0)
112 cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
113 else if (strcasecmp("DeriveInc", ds_type) == 0)
114 cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
115 else
116 cm->flags = 0;
117 } else if (strncasecmp("Absolute", ds_type, strlen("Absolute")) == 0) {
118 cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
119 if (strcasecmp("AbsoluteSet", ds_type) == 0)
120 cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
121 else
122 cm->flags = 0;
123 } else {
124 cm->flags = 0;
125 }
127 if (cm->flags == 0) {
128 WARNING("tail plugin: `%s' is not a valid argument to `DSType'.",
129 ci->values[0].value.string);
130 return (-1);
131 }
133 return (0);
134 } /* int ctail_config_add_match_dstype */
136 static int ctail_config_add_match(cu_tail_match_t *tm,
137 const char *plugin_instance,
138 oconfig_item_t *ci, cdtime_t interval) {
139 ctail_config_match_t cm = {0};
140 int status;
142 if (ci->values_num != 0) {
143 WARNING("tail plugin: Ignoring arguments for the `Match' block.");
144 }
146 status = 0;
147 for (int i = 0; i < ci->children_num; i++) {
148 oconfig_item_t *option = ci->children + i;
150 if (strcasecmp("Regex", option->key) == 0)
151 status = cf_util_get_string(option, &cm.regex);
152 else if (strcasecmp("ExcludeRegex", option->key) == 0)
153 status = cf_util_get_string(option, &cm.excluderegex);
154 else if (strcasecmp("DSType", option->key) == 0)
155 status = ctail_config_add_match_dstype(&cm, option);
156 else if (strcasecmp("Type", option->key) == 0)
157 status = cf_util_get_string(option, &cm.type);
158 else if (strcasecmp("Instance", option->key) == 0)
159 status = cf_util_get_string(option, &cm.type_instance);
160 else {
161 WARNING("tail plugin: Option `%s' not allowed here.", option->key);
162 status = -1;
163 }
165 if (status != 0)
166 break;
167 } /* for (i = 0; i < ci->children_num; i++) */
169 while (status == 0) {
170 if (cm.regex == NULL) {
171 WARNING("tail plugin: `Regex' missing in `Match' block.");
172 status = -1;
173 break;
174 }
176 if (cm.type == NULL) {
177 WARNING("tail plugin: `Type' missing in `Match' block.");
178 status = -1;
179 break;
180 }
182 if (cm.flags == 0) {
183 WARNING("tail plugin: `DSType' missing in `Match' block.");
184 status = -1;
185 break;
186 }
188 break;
189 } /* while (status == 0) */
191 if (status == 0) {
192 // TODO(octo): there's nothing "simple" about the latency stuff …
193 status = tail_match_add_match_simple(
194 tm, cm.regex, cm.excluderegex, cm.flags, "tail", plugin_instance,
195 cm.type, cm.type_instance, cm.latency, interval);
197 if (status != 0)
198 ERROR("tail plugin: tail_match_add_match_simple failed.");
199 }
201 sfree(cm.regex);
202 sfree(cm.excluderegex);
203 sfree(cm.type);
204 sfree(cm.type_instance);
205 latency_config_free(cm.latency);
207 return (status);
208 } /* int ctail_config_add_match */
210 static int ctail_config_add_file(oconfig_item_t *ci) {
211 cu_tail_match_t *tm;
212 cdtime_t interval = 0;
213 char *plugin_instance = NULL;
214 int num_matches = 0;
216 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
217 WARNING("tail plugin: `File' needs exactly one string argument.");
218 return (-1);
219 }
221 tm = tail_match_create(ci->values[0].value.string);
222 if (tm == NULL) {
223 ERROR("tail plugin: tail_match_create (%s) failed.",
224 ci->values[0].value.string);
225 return (-1);
226 }
228 for (int i = 0; i < ci->children_num; i++) {
229 oconfig_item_t *option = ci->children + i;
230 int status = 0;
232 if (strcasecmp("Instance", option->key) == 0)
233 status = cf_util_get_string(option, &plugin_instance);
234 else if (strcasecmp("Interval", option->key) == 0)
235 cf_util_get_cdtime(option, &interval);
236 else if (strcasecmp("Match", option->key) == 0) {
237 status = ctail_config_add_match(tm, plugin_instance, option, interval);
238 if (status == 0)
239 num_matches++;
240 /* Be mild with failed matches.. */
241 status = 0;
242 } else {
243 status = -1;
244 }
246 if (status != 0)
247 break;
248 } /* for (i = 0; i < ci->children_num; i++) */
250 sfree(plugin_instance);
252 if (num_matches == 0) {
253 ERROR("tail plugin: No (valid) matches found for file `%s'.",
254 ci->values[0].value.string);
255 tail_match_destroy(tm);
256 return (-1);
257 } else {
258 cu_tail_match_t **temp;
260 temp = realloc(tail_match_list,
261 sizeof(cu_tail_match_t *) * (tail_match_list_num + 1));
262 if (temp == NULL) {
263 ERROR("tail plugin: realloc failed.");
264 tail_match_destroy(tm);
265 return (-1);
266 }
268 tail_match_list = temp;
269 tail_match_list[tail_match_list_num] = tm;
270 tail_match_list_intervals[tail_match_list_num] = interval;
271 tail_match_list_num++;
272 }
274 return (0);
275 } /* int ctail_config_add_file */
277 static int ctail_config(oconfig_item_t *ci) {
278 for (int i = 0; i < ci->children_num; i++) {
279 oconfig_item_t *option = ci->children + i;
281 if (strcasecmp("File", option->key) == 0)
282 ctail_config_add_file(option);
283 else {
284 WARNING("tail plugin: Option `%s' not allowed here.", option->key);
285 }
286 } /* for (i = 0; i < ci->children_num; i++) */
288 return (0);
289 } /* int ctail_config */
291 static int ctail_read(user_data_t *ud) {
292 int status;
294 status = tail_match_read((cu_tail_match_t *)ud->data);
295 if (status != 0) {
296 ERROR("tail plugin: tail_match_read failed.");
297 return (-1);
298 }
300 return (0);
301 } /* int ctail_read */
303 static int ctail_init(void) {
304 char str[255];
306 if (tail_match_list_num == 0) {
307 WARNING("tail plugin: File list is empty. Returning an error.");
308 return (-1);
309 }
311 for (size_t i = 0; i < tail_match_list_num; i++) {
312 ssnprintf(str, sizeof(str), "tail-%zu", i);
314 plugin_register_complex_read(NULL, str, ctail_read,
315 tail_match_list_intervals[i],
316 &(user_data_t){
317 .data = tail_match_list[i],
318 });
319 }
321 return (0);
322 } /* int ctail_init */
324 static int ctail_shutdown(void) {
325 for (size_t i = 0; i < tail_match_list_num; i++) {
326 tail_match_destroy(tail_match_list[i]);
327 tail_match_list[i] = NULL;
328 }
329 sfree(tail_match_list);
330 tail_match_list_num = 0;
332 return (0);
333 } /* int ctail_shutdown */
335 void module_register(void) {
336 plugin_register_complex_config("tail", ctail_config);
337 plugin_register_init("tail", ctail_init);
338 plugin_register_shutdown("tail", ctail_shutdown);
339 } /* void module_register */