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