rrdtool plugin: Changes after review
[collectd.git] / src / daemon / 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_FREE_USER_DATA 0x01
37 #define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
38 #define UTILS_MATCH_FLAGS_REGEX 0x04
39
40 struct cu_match_s {
41   regex_t regex;
42   regex_t excluderegex;
43   int flags;
44
45   int (*callback)(const char *str, char *const *matches, size_t matches_num,
46                   void *user_data);
47   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->values_num == 0) ||
98         (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST)) {
99       data->value.gauge = value;
100     } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE) {
101       double f = ((double)data->values_num) / ((double)(data->values_num + 1));
102       data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
103     } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN) {
104       if (data->value.gauge > value)
105         data->value.gauge = value;
106     } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX) {
107       if (data->value.gauge < value)
108         data->value.gauge = value;
109     } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD) {
110       data->value.gauge += value;
111     } else {
112       ERROR("utils_match: default_callback: obj->ds_type is invalid!");
113       return (-1);
114     }
115
116     data->values_num++;
117   } else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER) {
118     counter_t value;
119     char *endptr = NULL;
120
121     if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC) {
122       data->value.counter++;
123       data->values_num++;
124       return (0);
125     }
126
127     if (matches_num < 2)
128       return (-1);
129
130     value = (counter_t)strtoull(matches[1], &endptr, 0);
131     if (matches[1] == endptr)
132       return (-1);
133
134     if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
135       data->value.counter = value;
136     else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
137       data->value.counter += value;
138     else {
139       ERROR("utils_match: default_callback: obj->ds_type is invalid!");
140       return (-1);
141     }
142
143     data->values_num++;
144   } else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE) {
145     derive_t value;
146     char *endptr = NULL;
147
148     if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC) {
149       data->value.derive++;
150       data->values_num++;
151       return (0);
152     }
153
154     if (matches_num < 2)
155       return (-1);
156
157     value = (derive_t)strtoll(matches[1], &endptr, 0);
158     if (matches[1] == endptr)
159       return (-1);
160
161     if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
162       data->value.derive = value;
163     else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
164       data->value.derive += value;
165     else {
166       ERROR("utils_match: default_callback: obj->ds_type is invalid!");
167       return (-1);
168     }
169
170     data->values_num++;
171   } else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE) {
172     absolute_t value;
173     char *endptr = NULL;
174
175     if (matches_num < 2)
176       return (-1);
177
178     value = (absolute_t)strtoull(matches[1], &endptr, 0);
179     if (matches[1] == endptr)
180       return (-1);
181
182     if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
183       data->value.absolute = value;
184     else {
185       ERROR("utils_match: default_callback: obj->ds_type is invalid!");
186       return (-1);
187     }
188
189     data->values_num++;
190   } else {
191     ERROR("utils_match: default_callback: obj->ds_type is invalid!");
192     return (-1);
193   }
194
195   return (0);
196 } /* int default_callback */
197
198 /*
199  * Public functions
200  */
201 cu_match_t *
202 match_create_callback(const char *regex, const char *excluderegex,
203                       int (*callback)(const char *str, char *const *matches,
204                                       size_t matches_num, void *user_data),
205                       void *user_data) {
206   cu_match_t *obj;
207   int status;
208
209   DEBUG("utils_match: match_create_callback: regex = %s, excluderegex = %s",
210         regex, excluderegex);
211
212   obj = calloc(1, sizeof(*obj));
213   if (obj == NULL)
214     return (NULL);
215
216   status = regcomp(&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
217   if (status != 0) {
218     ERROR("Compiling the regular expression \"%s\" failed.", regex);
219     sfree(obj);
220     return (NULL);
221   }
222   obj->flags |= UTILS_MATCH_FLAGS_REGEX;
223
224   if (excluderegex && strcmp(excluderegex, "") != 0) {
225     status = regcomp(&obj->excluderegex, excluderegex, REG_EXTENDED);
226     if (status != 0) {
227       ERROR("Compiling the excluding regular expression \"%s\" failed.",
228             excluderegex);
229       sfree(obj);
230       return (NULL);
231     }
232     obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
233   }
234
235   obj->callback = callback;
236   obj->user_data = user_data;
237
238   return (obj);
239 } /* cu_match_t *match_create_callback */
240
241 cu_match_t *match_create_simple(const char *regex, const char *excluderegex,
242                                 int match_ds_type) {
243   cu_match_value_t *user_data;
244   cu_match_t *obj;
245
246   user_data = calloc(1, sizeof(*user_data));
247   if (user_data == NULL)
248     return (NULL);
249   user_data->ds_type = match_ds_type;
250
251   obj = match_create_callback(regex, excluderegex, default_callback, user_data);
252   if (obj == NULL) {
253     sfree(user_data);
254     return (NULL);
255   }
256
257   obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
258
259   return (obj);
260 } /* cu_match_t *match_create_simple */
261
262 void match_value_reset(cu_match_value_t *mv) {
263   if (mv == NULL)
264     return;
265
266   if (mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) {
267     mv->value.gauge = NAN;
268     mv->values_num = 0;
269   }
270 } /* }}} void match_value_reset */
271
272 void match_destroy(cu_match_t *obj) {
273   if (obj == NULL)
274     return;
275
276   if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
277     sfree(obj->user_data);
278   if (obj->flags & UTILS_MATCH_FLAGS_REGEX)
279     regfree(&obj->regex);
280   if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX)
281     regfree(&obj->excluderegex);
282
283   sfree(obj);
284 } /* void match_destroy */
285
286 int match_apply(cu_match_t *obj, const char *str) {
287   int status;
288   regmatch_t re_match[32];
289   char *matches[32] = {0};
290   size_t matches_num;
291
292   if ((obj == NULL) || (str == NULL))
293     return (-1);
294
295   if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
296     status =
297         regexec(&obj->excluderegex, str, STATIC_ARRAY_SIZE(re_match), re_match,
298                 /* eflags = */ 0);
299     /* Regex did match, so exclude this line */
300     if (status == 0) {
301       DEBUG("ExludeRegex matched, don't count that line\n");
302       return (0);
303     }
304   }
305
306   status = regexec(&obj->regex, str, STATIC_ARRAY_SIZE(re_match), re_match,
307                    /* eflags = */ 0);
308
309   /* Regex did not match */
310   if (status != 0)
311     return (0);
312
313   for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE(matches);
314        matches_num++) {
315     if ((re_match[matches_num].rm_so < 0) || (re_match[matches_num].rm_eo < 0))
316       break;
317
318     matches[matches_num] = match_substr(str, re_match[matches_num].rm_so,
319                                         re_match[matches_num].rm_eo);
320     if (matches[matches_num] == NULL) {
321       status = -1;
322       break;
323     }
324   }
325
326   if (status != 0) {
327     ERROR("utils_match: match_apply: match_substr failed.");
328   } else {
329     status = obj->callback(str, matches, matches_num, obj->user_data);
330     if (status != 0) {
331       ERROR("utils_match: match_apply: callback failed.");
332     }
333   }
334
335   for (size_t i = 0; i < matches_num; i++) {
336     sfree(matches[i]);
337   }
338
339   return (status);
340 } /* int match_apply */
341
342 void *match_get_user_data(cu_match_t *obj) {
343   if (obj == NULL)
344     return (NULL);
345   return (obj->user_data);
346 } /* void *match_get_user_data */
347
348 /* vim: set sw=2 sts=2 ts=8 : */