Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / tail.c
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  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_latency_config.h"
32 #include "utils_tail_match.h"
33
34 /*
35  *  <Plugin tail>
36  *    <File "/var/log/exim4/mainlog">
37  *      Plugin "mail"
38  *      Instance "exim"
39  *      Interval 60
40  *      <Match>
41  *        Regex "S=([1-9][0-9]*)"
42  *        ExcludeRegex "U=root.*S="
43  *        DSType "CounterAdd"
44  *        Type "ipt_bytes"
45  *        Instance "total"
46  *      </Match>
47  *    </File>
48  *  </Plugin>
49  */
50
51 struct ctail_config_match_s {
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;
61
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];
65
66 static int ctail_config_add_match_dstype(ctail_config_match_t *cm,
67                                          oconfig_item_t *ci) {
68   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
69     WARNING("tail plugin: `DSType' needs exactly one string argument.");
70     return -1;
71   }
72
73   char const *ds_type = ci->values[0].value.string;
74   if (strncasecmp("Gauge", ds_type, strlen("Gauge")) == 0) {
75     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
76     if (strcasecmp("GaugeAverage", ds_type) == 0)
77       cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
78     else if (strcasecmp("GaugeMin", ds_type) == 0)
79       cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
80     else if (strcasecmp("GaugeMax", ds_type) == 0)
81       cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
82     else if (strcasecmp("GaugeLast", ds_type) == 0)
83       cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
84     else if (strcasecmp("GaugeInc", ds_type) == 0)
85       cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
86     else if (strcasecmp("GaugeAdd", ds_type) == 0)
87       cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
88     else if (strcasecmp("GaugePersist", ci->values[0].value.string) == 0)
89       cm->flags |= UTILS_MATCH_CF_GAUGE_PERSIST;
90     else
91       cm->flags = 0;
92   } else if (strcasecmp("Distribution", ds_type) == 0) {
93     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE | UTILS_MATCH_CF_GAUGE_DIST;
94
95     int status = latency_config(&cm->latency, ci, "tail");
96     if (status != 0)
97       return status;
98   } else if (strncasecmp("Counter", ds_type, strlen("Counter")) == 0) {
99     cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
100     if (strcasecmp("CounterSet", ds_type) == 0)
101       cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
102     else if (strcasecmp("CounterAdd", ds_type) == 0)
103       cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
104     else if (strcasecmp("CounterInc", ds_type) == 0)
105       cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
106     else
107       cm->flags = 0;
108   } else if (strncasecmp("Derive", ds_type, strlen("Derive")) == 0) {
109     cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
110     if (strcasecmp("DeriveSet", ds_type) == 0)
111       cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
112     else if (strcasecmp("DeriveAdd", ds_type) == 0)
113       cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
114     else if (strcasecmp("DeriveInc", ds_type) == 0)
115       cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
116     else
117       cm->flags = 0;
118   } else if (strncasecmp("Absolute", ds_type, strlen("Absolute")) == 0) {
119     cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
120     if (strcasecmp("AbsoluteSet", ds_type) == 0)
121       cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
122     else
123       cm->flags = 0;
124   } else {
125     cm->flags = 0;
126   }
127
128   if (cm->flags == 0) {
129     WARNING("tail plugin: `%s' is not a valid argument to `DSType'.",
130             ci->values[0].value.string);
131     return -1;
132   }
133
134   return 0;
135 } /* int ctail_config_add_match_dstype */
136
137 static int ctail_config_add_match(cu_tail_match_t *tm,
138                                   const char *plugin_name,
139                                   const char *plugin_instance,
140                                   oconfig_item_t *ci, cdtime_t interval) {
141   ctail_config_match_t cm = {0};
142   int status;
143
144   if (ci->values_num != 0) {
145     WARNING("tail plugin: Ignoring arguments for the `Match' block.");
146   }
147
148   status = 0;
149   for (int i = 0; i < ci->children_num; i++) {
150     oconfig_item_t *option = ci->children + i;
151
152     if (strcasecmp("Regex", option->key) == 0)
153       status = cf_util_get_string(option, &cm.regex);
154     else if (strcasecmp("ExcludeRegex", option->key) == 0)
155       status = cf_util_get_string(option, &cm.excluderegex);
156     else if (strcasecmp("DSType", option->key) == 0)
157       status = ctail_config_add_match_dstype(&cm, option);
158     else if (strcasecmp("Type", option->key) == 0)
159       status = cf_util_get_string(option, &cm.type);
160     else if (strcasecmp("Instance", option->key) == 0)
161       status = cf_util_get_string(option, &cm.type_instance);
162     else {
163       WARNING("tail plugin: Option `%s' not allowed here.", option->key);
164       status = -1;
165     }
166
167     if (status != 0)
168       break;
169   } /* for (i = 0; i < ci->children_num; i++) */
170
171   while (status == 0) {
172     if (cm.regex == NULL) {
173       WARNING("tail plugin: `Regex' missing in `Match' block.");
174       status = -1;
175       break;
176     }
177
178     if (cm.type == NULL) {
179       WARNING("tail plugin: `Type' missing in `Match' block.");
180       status = -1;
181       break;
182     }
183
184     if (cm.flags == 0) {
185       WARNING("tail plugin: `DSType' missing in `Match' block.");
186       status = -1;
187       break;
188     }
189
190     break;
191   } /* while (status == 0) */
192
193   if (status == 0) {
194     // TODO(octo): there's nothing "simple" about the latency stuff …
195     status = tail_match_add_match_simple(
196         tm, cm.regex, cm.excluderegex, cm.flags,
197         (plugin_name != NULL) ? plugin_name : "tail", plugin_instance,
198         cm.type, cm.type_instance, cm.latency, interval);
199
200     if (status != 0)
201       ERROR("tail plugin: tail_match_add_match_simple failed.");
202   }
203
204   sfree(cm.regex);
205   sfree(cm.excluderegex);
206   sfree(cm.type);
207   sfree(cm.type_instance);
208   latency_config_free(cm.latency);
209
210   return status;
211 } /* int ctail_config_add_match */
212
213 static int ctail_config_add_file(oconfig_item_t *ci) {
214   cu_tail_match_t *tm;
215   cdtime_t interval = 0;
216   char *plugin_name = NULL;
217   char *plugin_instance = NULL;
218   int num_matches = 0;
219
220   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
221     WARNING("tail plugin: `File' needs exactly one string argument.");
222     return -1;
223   }
224
225   tm = tail_match_create(ci->values[0].value.string);
226   if (tm == NULL) {
227     ERROR("tail plugin: tail_match_create (%s) failed.",
228           ci->values[0].value.string);
229     return -1;
230   }
231
232   for (int i = 0; i < ci->children_num; i++) {
233     oconfig_item_t *option = ci->children + i;
234     int status = 0;
235
236     if (strcasecmp("Plugin", option->key) == 0)
237       status = cf_util_get_string (option, &plugin_name);
238     else if (strcasecmp("Instance", option->key) == 0)
239       status = cf_util_get_string(option, &plugin_instance);
240     else if (strcasecmp("Interval", option->key) == 0)
241       cf_util_get_cdtime(option, &interval);
242     else if (strcasecmp("Match", option->key) == 0) {
243       status = ctail_config_add_match(tm, plugin_name, plugin_instance, option,
244                                       interval);
245       if (status == 0)
246         num_matches++;
247       /* Be mild with failed matches.. */
248       status = 0;
249     } else {
250       status = -1;
251     }
252
253     if (status != 0)
254       break;
255   } /* for (i = 0; i < ci->children_num; i++) */
256
257   sfree(plugin_name);
258   sfree(plugin_instance);
259
260   if (num_matches == 0) {
261     ERROR("tail plugin: No (valid) matches found for file `%s'.",
262           ci->values[0].value.string);
263     tail_match_destroy(tm);
264     return -1;
265   } else {
266     cu_tail_match_t **temp;
267
268     temp = realloc(tail_match_list,
269                    sizeof(cu_tail_match_t *) * (tail_match_list_num + 1));
270     if (temp == NULL) {
271       ERROR("tail plugin: realloc failed.");
272       tail_match_destroy(tm);
273       return -1;
274     }
275
276     tail_match_list = temp;
277     tail_match_list[tail_match_list_num] = tm;
278     tail_match_list_intervals[tail_match_list_num] = interval;
279     tail_match_list_num++;
280   }
281
282   return 0;
283 } /* int ctail_config_add_file */
284
285 static int ctail_config(oconfig_item_t *ci) {
286   for (int i = 0; i < ci->children_num; i++) {
287     oconfig_item_t *option = ci->children + i;
288
289     if (strcasecmp("File", option->key) == 0)
290       ctail_config_add_file(option);
291     else {
292       WARNING("tail plugin: Option `%s' not allowed here.", option->key);
293     }
294   } /* for (i = 0; i < ci->children_num; i++) */
295
296   return 0;
297 } /* int ctail_config */
298
299 static int ctail_read(user_data_t *ud) {
300   int status;
301
302   status = tail_match_read((cu_tail_match_t *)ud->data);
303   if (status != 0) {
304     ERROR("tail plugin: tail_match_read failed.");
305     return -1;
306   }
307
308   return 0;
309 } /* int ctail_read */
310
311 static int ctail_init(void) {
312   char str[255];
313
314   if (tail_match_list_num == 0) {
315     WARNING("tail plugin: File list is empty. Returning an error.");
316     return -1;
317   }
318
319   for (size_t i = 0; i < tail_match_list_num; i++) {
320     snprintf(str, sizeof(str), "tail-%zu", i);
321
322     plugin_register_complex_read(NULL, str, ctail_read,
323                                  tail_match_list_intervals[i],
324                                  &(user_data_t){
325                                      .data = tail_match_list[i],
326                                  });
327   }
328
329   return 0;
330 } /* int ctail_init */
331
332 static int ctail_shutdown(void) {
333   for (size_t i = 0; i < tail_match_list_num; i++) {
334     tail_match_destroy(tail_match_list[i]);
335     tail_match_list[i] = NULL;
336   }
337   sfree(tail_match_list);
338   tail_match_list_num = 0;
339
340   return 0;
341 } /* int ctail_shutdown */
342
343 void module_register(void) {
344   plugin_register_complex_config("tail", ctail_config);
345   plugin_register_init("tail", ctail_init);
346   plugin_register_shutdown("tail", ctail_shutdown);
347 } /* void module_register */