Merge pull request #3329 from efuss/fix-3311
[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 "plugin.h"
30 #include "utils/common/common.h"
31 #include "utils/latency/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   latency_config_t latency;
58 };
59 typedef struct ctail_config_match_s ctail_config_match_t;
60
61 static size_t tail_file_num;
62
63 static int ctail_read(user_data_t *ud);
64
65 static void ctail_match_free(void *arg) {
66   tail_match_destroy((cu_tail_match_t *)arg);
67 } /* void ctail_match_free */
68
69 static int ctail_config_add_match_dstype(ctail_config_match_t *cm,
70                                          oconfig_item_t *ci) {
71   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
72     WARNING("tail plugin: `DSType' needs exactly one string argument.");
73     return -1;
74   }
75
76   char const *ds_type = ci->values[0].value.string;
77   if (strncasecmp("Gauge", ds_type, strlen("Gauge")) == 0) {
78     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE;
79     if (strcasecmp("GaugeAverage", ds_type) == 0)
80       cm->flags |= UTILS_MATCH_CF_GAUGE_AVERAGE;
81     else if (strcasecmp("GaugeMin", ds_type) == 0)
82       cm->flags |= UTILS_MATCH_CF_GAUGE_MIN;
83     else if (strcasecmp("GaugeMax", ds_type) == 0)
84       cm->flags |= UTILS_MATCH_CF_GAUGE_MAX;
85     else if (strcasecmp("GaugeLast", ds_type) == 0)
86       cm->flags |= UTILS_MATCH_CF_GAUGE_LAST;
87     else if (strcasecmp("GaugeInc", ds_type) == 0)
88       cm->flags |= UTILS_MATCH_CF_GAUGE_INC;
89     else if (strcasecmp("GaugeAdd", ds_type) == 0)
90       cm->flags |= UTILS_MATCH_CF_GAUGE_ADD;
91     else if (strcasecmp("GaugePersist", ci->values[0].value.string) == 0)
92       cm->flags |= UTILS_MATCH_CF_GAUGE_PERSIST;
93     else
94       cm->flags = 0;
95   } else if (strcasecmp("Distribution", ds_type) == 0) {
96     cm->flags = UTILS_MATCH_DS_TYPE_GAUGE | UTILS_MATCH_CF_GAUGE_DIST;
97
98     int status = latency_config(&cm->latency, ci);
99     if (status != 0)
100       return status;
101   } else if (strncasecmp("Counter", ds_type, strlen("Counter")) == 0) {
102     cm->flags = UTILS_MATCH_DS_TYPE_COUNTER;
103     if (strcasecmp("CounterSet", ds_type) == 0)
104       cm->flags |= UTILS_MATCH_CF_COUNTER_SET;
105     else if (strcasecmp("CounterAdd", ds_type) == 0)
106       cm->flags |= UTILS_MATCH_CF_COUNTER_ADD;
107     else if (strcasecmp("CounterInc", ds_type) == 0)
108       cm->flags |= UTILS_MATCH_CF_COUNTER_INC;
109     else
110       cm->flags = 0;
111   } else if (strncasecmp("Derive", ds_type, strlen("Derive")) == 0) {
112     cm->flags = UTILS_MATCH_DS_TYPE_DERIVE;
113     if (strcasecmp("DeriveSet", ds_type) == 0)
114       cm->flags |= UTILS_MATCH_CF_DERIVE_SET;
115     else if (strcasecmp("DeriveAdd", ds_type) == 0)
116       cm->flags |= UTILS_MATCH_CF_DERIVE_ADD;
117     else if (strcasecmp("DeriveInc", ds_type) == 0)
118       cm->flags |= UTILS_MATCH_CF_DERIVE_INC;
119     else
120       cm->flags = 0;
121   } else if (strncasecmp("Absolute", ds_type, strlen("Absolute")) == 0) {
122     cm->flags = UTILS_MATCH_DS_TYPE_ABSOLUTE;
123     if (strcasecmp("AbsoluteSet", ds_type) == 0)
124       cm->flags |= UTILS_MATCH_CF_ABSOLUTE_SET;
125     else
126       cm->flags = 0;
127   } else {
128     cm->flags = 0;
129   }
130
131   if (cm->flags == 0) {
132     WARNING("tail plugin: `%s' is not a valid argument to `DSType'.",
133             ci->values[0].value.string);
134     return -1;
135   }
136
137   return 0;
138 } /* int ctail_config_add_match_dstype */
139
140 static int ctail_config_add_match(cu_tail_match_t *tm, const char *plugin_name,
141                                   const char *plugin_instance,
142                                   oconfig_item_t *ci) {
143   ctail_config_match_t cm = {0};
144   int status;
145
146   if (ci->values_num != 0) {
147     WARNING("tail plugin: Ignoring arguments for the `Match' block.");
148   }
149
150   status = 0;
151   for (int i = 0; i < ci->children_num; i++) {
152     oconfig_item_t *option = ci->children + i;
153
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);
164     else {
165       WARNING("tail plugin: Option `%s' not allowed here.", option->key);
166       status = -1;
167     }
168
169     if (status != 0)
170       break;
171   } /* for (i = 0; i < ci->children_num; i++) */
172
173   while (status == 0) {
174     if (cm.regex == NULL) {
175       WARNING("tail plugin: `Regex' missing in `Match' block.");
176       status = -1;
177       break;
178     }
179
180     if (cm.type == NULL) {
181       WARNING("tail plugin: `Type' missing in `Match' block.");
182       status = -1;
183       break;
184     }
185
186     if (cm.flags == 0) {
187       WARNING("tail plugin: `DSType' missing in `Match' block.");
188       status = -1;
189       break;
190     }
191
192     break;
193   } /* while (status == 0) */
194
195   if (status == 0) {
196     // TODO(octo): there's nothing "simple" about the latency stuff …
197     status = tail_match_add_match_simple(
198         tm, cm.regex, cm.excluderegex, cm.flags,
199         (plugin_name != NULL) ? plugin_name : "tail", plugin_instance, cm.type,
200         cm.type_instance, cm.latency);
201
202     if (status != 0)
203       ERROR("tail plugin: tail_match_add_match_simple failed.");
204   }
205
206   sfree(cm.regex);
207   sfree(cm.excluderegex);
208   sfree(cm.type);
209   sfree(cm.type_instance);
210   latency_config_free(cm.latency);
211
212   return status;
213 } /* int ctail_config_add_match */
214
215 static int ctail_config_add_file(oconfig_item_t *ci) {
216   cu_tail_match_t *tm;
217   cdtime_t interval = 0;
218   char *plugin_name = NULL;
219   char *plugin_instance = NULL;
220   int num_matches = 0;
221
222   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
223     WARNING("tail plugin: `File' needs exactly one string argument.");
224     return -1;
225   }
226
227   tm = tail_match_create(ci->values[0].value.string);
228   if (tm == NULL) {
229     ERROR("tail plugin: tail_match_create (%s) failed.",
230           ci->values[0].value.string);
231     return -1;
232   }
233
234   for (int i = 0; i < ci->children_num; i++) {
235     oconfig_item_t *option = ci->children + i;
236     int status = 0;
237
238     if (strcasecmp("Plugin", option->key) == 0)
239       status = cf_util_get_string(option, &plugin_name);
240     else if (strcasecmp("Instance", option->key) == 0)
241       status = cf_util_get_string(option, &plugin_instance);
242     else if (strcasecmp("Interval", option->key) == 0)
243       cf_util_get_cdtime(option, &interval);
244     else if (strcasecmp("Match", option->key) == 0) {
245       status = ctail_config_add_match(tm, plugin_name, plugin_instance, option);
246       if (status == 0)
247         num_matches++;
248       /* Be mild with failed matches.. */
249       status = 0;
250     } else {
251       status = -1;
252     }
253
254     if (status != 0)
255       break;
256   } /* for (i = 0; i < ci->children_num; i++) */
257
258   sfree(plugin_name);
259   sfree(plugin_instance);
260
261   if (num_matches == 0) {
262     ERROR("tail plugin: No (valid) matches found for file `%s'.",
263           ci->values[0].value.string);
264     tail_match_destroy(tm);
265     return -1;
266   }
267
268   char str[255];
269   snprintf(str, sizeof(str), "tail-%zu", tail_file_num++);
270
271   plugin_register_complex_read(
272       NULL, str, ctail_read, interval,
273       &(user_data_t){.data = tm, .free_func = ctail_match_free});
274
275   return 0;
276 } /* int ctail_config_add_file */
277
278 static int ctail_config(oconfig_item_t *ci) {
279   for (int i = 0; i < ci->children_num; i++) {
280     oconfig_item_t *option = ci->children + i;
281
282     if (strcasecmp("File", option->key) == 0)
283       ctail_config_add_file(option);
284     else {
285       WARNING("tail plugin: Option `%s' not allowed here.", option->key);
286     }
287   } /* for (i = 0; i < ci->children_num; i++) */
288
289   return 0;
290 } /* int ctail_config */
291
292 static int ctail_read(user_data_t *ud) {
293   int status;
294
295   status = tail_match_read((cu_tail_match_t *)ud->data);
296   if (status != 0) {
297     ERROR("tail plugin: tail_match_read failed.");
298     return -1;
299   }
300
301   return 0;
302 } /* int ctail_read */
303
304 void module_register(void) {
305   plugin_register_complex_config("tail", ctail_config);
306 } /* void module_register */