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