Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / utils_match.c
1 /**
2  * collectd - src/utils_match.c
3  * Copyright (C) 2008-2014  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
32 #include "utils_match.h"
33
34 #include <regex.h>
35
36 #define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
37 #define UTILS_MATCH_FLAGS_REGEX 0x04
38
39 struct cu_match_s {
40   regex_t regex;
41   regex_t excluderegex;
42   int flags;
43
44   int (*callback)(const char *str, char *const *matches, size_t matches_num,
45                   void *user_data);
46   void *user_data;
47   void (*free)(void *user_data);
48 };
49
50 /*
51  * Private functions
52  */
53 static char *match_substr(const char *str, int begin, int end) {
54   char *ret;
55   size_t ret_len;
56
57   if ((begin < 0) || (end < 0) || (begin >= end))
58     return NULL;
59   if ((size_t)end > (strlen(str) + 1)) {
60     ERROR("utils_match: match_substr: `end' points after end of string.");
61     return NULL;
62   }
63
64   ret_len = end - begin;
65   ret = malloc(ret_len + 1);
66   if (ret == NULL) {
67     ERROR("utils_match: match_substr: malloc failed.");
68     return NULL;
69   }
70
71   sstrncpy(ret, str + begin, ret_len + 1);
72   return ret;
73 } /* char *match_substr */
74
75 static int default_callback(const char __attribute__((unused)) * str,
76                             char *const *matches, size_t matches_num,
77                             void *user_data) {
78   cu_match_value_t *data = (cu_match_value_t *)user_data;
79
80   if (data->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) {
81     gauge_t value;
82     char *endptr = NULL;
83
84     if (data->ds_type & UTILS_MATCH_CF_GAUGE_INC) {
85       data->value.gauge = isnan(data->value.gauge) ? 1 : data->value.gauge + 1;
86       data->values_num++;
87       return 0;
88     }
89
90     if (matches_num < 2)
91       return -1;
92
93     value = (gauge_t)strtod(matches[1], &endptr);
94     if (matches[1] == endptr)
95       return -1;
96
97     if (data->ds_type & UTILS_MATCH_CF_GAUGE_DIST) {
98       latency_counter_add(data->latency, DOUBLE_TO_CDTIME_T(value));
99       data->values_num++;
100       return 0;
101     }
102
103     if ((data->values_num == 0) ||
104         (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST) ||
105         (data->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST)) {
106       data->value.gauge = value;
107     } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE) {
108       double f = ((double)data->values_num) / ((double)(data->values_num + 1));
109       data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
110     } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN) {
111       if (data->value.gauge > value)
112         data->value.gauge = value;
113     } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX) {
114       if (data->value.gauge < value)
115         data->value.gauge = value;
116     } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD) {
117       data->value.gauge += value;
118     } else {
119       ERROR("utils_match: default_callback: obj->ds_type is invalid!");
120       return -1;
121     }
122
123     data->values_num++;
124   } else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER) {
125     counter_t value;
126     char *endptr = NULL;
127
128     if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC) {
129       data->value.counter++;
130       data->values_num++;
131       return 0;
132     }
133
134     if (matches_num < 2)
135       return -1;
136
137     value = (counter_t)strtoull(matches[1], &endptr, 0);
138     if (matches[1] == endptr)
139       return -1;
140
141     if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
142       data->value.counter = value;
143     else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
144       data->value.counter += value;
145     else {
146       ERROR("utils_match: default_callback: obj->ds_type is invalid!");
147       return -1;
148     }
149
150     data->values_num++;
151   } else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE) {
152     derive_t value;
153     char *endptr = NULL;
154
155     if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC) {
156       data->value.derive++;
157       data->values_num++;
158       return 0;
159     }
160
161     if (matches_num < 2)
162       return -1;
163
164     value = (derive_t)strtoll(matches[1], &endptr, 0);
165     if (matches[1] == endptr)
166       return -1;
167
168     if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
169       data->value.derive = value;
170     else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
171       data->value.derive += value;
172     else {
173       ERROR("utils_match: default_callback: obj->ds_type is invalid!");
174       return -1;
175     }
176
177     data->values_num++;
178   } else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE) {
179     absolute_t value;
180     char *endptr = NULL;
181
182     if (matches_num < 2)
183       return -1;
184
185     value = (absolute_t)strtoull(matches[1], &endptr, 0);
186     if (matches[1] == endptr)
187       return -1;
188
189     if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
190       data->value.absolute = value;
191     else {
192       ERROR("utils_match: default_callback: obj->ds_type is invalid!");
193       return -1;
194     }
195
196     data->values_num++;
197   } else {
198     ERROR("utils_match: default_callback: obj->ds_type is invalid!");
199     return -1;
200   }
201
202   return 0;
203 } /* int default_callback */
204
205 static void match_simple_free(void *data) {
206   cu_match_value_t *user_data = (cu_match_value_t *)data;
207   if (user_data->latency)
208     latency_counter_destroy(user_data->latency);
209
210   free(data);
211 } /* void match_simple_free */
212
213 /*
214  * Public functions
215  */
216 cu_match_t *
217 match_create_callback(const char *regex, const char *excluderegex,
218                       int (*callback)(const char *str, char *const *matches,
219                                       size_t matches_num, void *user_data),
220                       void *user_data,
221                       void (*free_user_data)(void *user_data)) {
222   cu_match_t *obj;
223   int status;
224
225   DEBUG("utils_match: match_create_callback: regex = %s, excluderegex = %s",
226         regex, excluderegex);
227
228   obj = calloc(1, sizeof(*obj));
229   if (obj == NULL)
230     return NULL;
231
232   status = regcomp(&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
233   if (status != 0) {
234     ERROR("Compiling the regular expression \"%s\" failed.", regex);
235     sfree(obj);
236     return NULL;
237   }
238   obj->flags |= UTILS_MATCH_FLAGS_REGEX;
239
240   if (excluderegex && strcmp(excluderegex, "") != 0) {
241     status = regcomp(&obj->excluderegex, excluderegex, REG_EXTENDED);
242     if (status != 0) {
243       ERROR("Compiling the excluding regular expression \"%s\" failed.",
244             excluderegex);
245       sfree(obj);
246       return NULL;
247     }
248     obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
249   }
250
251   obj->callback = callback;
252   obj->user_data = user_data;
253   obj->free = free_user_data;
254
255   return obj;
256 } /* cu_match_t *match_create_callback */
257
258 cu_match_t *match_create_simple(const char *regex, const char *excluderegex,
259                                 int match_ds_type) {
260   cu_match_value_t *user_data;
261   cu_match_t *obj;
262
263   user_data = calloc(1, sizeof(*user_data));
264   if (user_data == NULL)
265     return NULL;
266   user_data->ds_type = match_ds_type;
267
268   if ((match_ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
269       (match_ds_type & UTILS_MATCH_CF_GAUGE_DIST)) {
270     user_data->latency = latency_counter_create();
271     if (user_data->latency == NULL) {
272       ERROR("match_create_simple(): latency_counter_create() failed.");
273       free(user_data);
274       return NULL;
275     }
276   }
277
278   obj = match_create_callback(regex, excluderegex, default_callback, user_data,
279                               match_simple_free);
280   if (obj == NULL) {
281     if (user_data->latency)
282       latency_counter_destroy(user_data->latency);
283
284     sfree(user_data);
285     return NULL;
286   }
287   return obj;
288 } /* cu_match_t *match_create_simple */
289
290 void match_value_reset(cu_match_value_t *mv) {
291   if (mv == NULL)
292     return;
293
294   /* Reset GAUGE metrics only and except GAUGE_PERSIST. */
295   if ((mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
296       !(mv->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST)) {
297     mv->value.gauge = (mv->ds_type & UTILS_MATCH_CF_GAUGE_INC) ? 0 : NAN;
298     mv->values_num = 0;
299   }
300 } /* }}} void match_value_reset */
301
302 void match_destroy(cu_match_t *obj) {
303   if (obj == NULL)
304     return;
305
306   if (obj->flags & UTILS_MATCH_FLAGS_REGEX)
307     regfree(&obj->regex);
308   if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX)
309     regfree(&obj->excluderegex);
310   if ((obj->user_data != NULL) && (obj->free != NULL))
311     (*obj->free)(obj->user_data);
312
313   sfree(obj);
314 } /* void match_destroy */
315
316 int match_apply(cu_match_t *obj, const char *str) {
317   int status;
318   regmatch_t re_match[32];
319   char *matches[32] = {0};
320   size_t matches_num;
321
322   if ((obj == NULL) || (str == NULL))
323     return -1;
324
325   if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
326     status =
327         regexec(&obj->excluderegex, str, STATIC_ARRAY_SIZE(re_match), re_match,
328                 /* eflags = */ 0);
329     /* Regex did match, so exclude this line */
330     if (status == 0) {
331       DEBUG("ExludeRegex matched, don't count that line\n");
332       return 0;
333     }
334   }
335
336   status = regexec(&obj->regex, str, STATIC_ARRAY_SIZE(re_match), re_match,
337                    /* eflags = */ 0);
338
339   /* Regex did not match */
340   if (status != 0)
341     return 0;
342
343   for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE(matches);
344        matches_num++) {
345     if ((re_match[matches_num].rm_so < 0) || (re_match[matches_num].rm_eo < 0))
346       break;
347
348     matches[matches_num] = match_substr(str, re_match[matches_num].rm_so,
349                                         re_match[matches_num].rm_eo);
350     if (matches[matches_num] == NULL) {
351       status = -1;
352       break;
353     }
354   }
355
356   if (status != 0) {
357     ERROR("utils_match: match_apply: match_substr failed.");
358   } else {
359     status = obj->callback(str, matches, matches_num, obj->user_data);
360     if (status != 0) {
361       ERROR("utils_match: match_apply: callback failed.");
362     }
363   }
364
365   for (size_t i = 0; i < matches_num; i++) {
366     sfree(matches[i]);
367   }
368
369   return status;
370 } /* int match_apply */
371
372 void *match_get_user_data(cu_match_t *obj) {
373   if (obj == NULL)
374     return NULL;
375   return obj->user_data;
376 } /* void *match_get_user_data */