+ User_data destroy callback added to match_create_callback() in utils_match
[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_EXCLUDE_REGEX 0x02
37
38 struct cu_match_s
39 {
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 {
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 static void match_simple_free (void *data)
230 {
231   free (data);
232 } /* void match_simple_free */
233
234 /*
235  * Public functions
236  */
237 cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
238                 int (*callback) (const char *str,
239                   char * const *matches, size_t matches_num, void *user_data),
240                 void *user_data, void (*free_user_data) (void *user_data))
241 {
242   cu_match_t *obj;
243   int status;
244
245   DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s",
246          regex, excluderegex);
247
248   obj = calloc (1, sizeof (*obj));
249   if (obj == NULL)
250     return (NULL);
251
252   status = regcomp (&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
253   if (status != 0)
254   {
255     ERROR ("Compiling the regular expression \"%s\" failed.", regex);
256     sfree (obj);
257     return (NULL);
258   }
259
260   if (excluderegex && strcmp(excluderegex, "") != 0) {
261     status = regcomp (&obj->excluderegex, excluderegex, REG_EXTENDED);
262     if (status != 0)
263     {
264         ERROR ("Compiling the excluding regular expression \"%s\" failed.",
265                excluderegex);
266         sfree (obj);
267         return (NULL);
268     }
269     obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
270   }
271
272   obj->callback = callback;
273   obj->user_data = user_data;
274   obj->free = free_user_data;
275
276   return (obj);
277 } /* cu_match_t *match_create_callback */
278
279 cu_match_t *match_create_simple (const char *regex,
280                                  const char *excluderegex, int match_ds_type)
281 {
282   cu_match_value_t *user_data;
283   cu_match_t *obj;
284
285   user_data = calloc (1, sizeof (*user_data));
286   if (user_data == NULL)
287     return (NULL);
288   user_data->ds_type = match_ds_type;
289
290   obj = match_create_callback (regex, excluderegex,
291                                default_callback, user_data, match_simple_free);
292   if (obj == NULL)
293   {
294     sfree (user_data);
295     return (NULL);
296   }
297   return (obj);
298 } /* cu_match_t *match_create_simple */
299
300 void match_value_reset (cu_match_value_t *mv)
301 {
302   if (mv == NULL)
303     return;
304
305   /* Reset GAUGE metrics only and except GAUGE_PERSIST. */
306   if ((mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
307       && !(mv->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST))
308   {
309     mv->value.gauge = NAN;
310     mv->values_num = 0;
311   }
312 } /* }}} void match_value_reset */
313
314 void match_destroy (cu_match_t *obj)
315 {
316   if (obj == NULL)
317     return;
318
319   if ((obj->user_data != NULL) && (obj->free != NULL))
320       (*obj->free) (obj->user_data);
321
322   sfree (obj);
323 } /* void match_destroy */
324
325 int match_apply (cu_match_t *obj, const char *str)
326 {
327   int status;
328   regmatch_t re_match[32];
329   char *matches[32] = { 0 };
330   size_t matches_num;
331
332   if ((obj == NULL) || (str == NULL))
333     return (-1);
334
335   if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
336     status = regexec (&obj->excluderegex, str,
337                       STATIC_ARRAY_SIZE (re_match), re_match,
338                       /* eflags = */ 0);
339     /* Regex did match, so exclude this line */
340     if (status == 0) {
341       DEBUG("ExludeRegex matched, don't count that line\n");
342       return (0);
343     }
344   }
345
346   status = regexec (&obj->regex, str,
347       STATIC_ARRAY_SIZE (re_match), re_match,
348       /* eflags = */ 0);
349
350   /* Regex did not match */
351   if (status != 0)
352     return (0);
353
354   for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE (matches); matches_num++)
355   {
356     if ((re_match[matches_num].rm_so < 0)
357         || (re_match[matches_num].rm_eo < 0))
358       break;
359
360     matches[matches_num] = match_substr (str,
361         re_match[matches_num].rm_so, re_match[matches_num].rm_eo);
362     if (matches[matches_num] == NULL)
363     {
364       status = -1;
365       break;
366     }
367   }
368
369   if (status != 0)
370   {
371     ERROR ("utils_match: match_apply: match_substr failed.");
372   }
373   else
374   {
375     status = obj->callback (str, matches, matches_num, obj->user_data);
376     if (status != 0)
377     {
378       ERROR ("utils_match: match_apply: callback failed.");
379     }
380   }
381
382   for (size_t i = 0; i < matches_num; i++)
383   {
384     sfree (matches[i]);
385   }
386
387   return (status);
388 } /* int match_apply */
389
390 void *match_get_user_data (cu_match_t *obj)
391 {
392   if (obj == NULL)
393     return (NULL);
394   return (obj->user_data);
395 } /* void *match_get_user_data */
396
397 /* vim: set sw=2 sts=2 ts=8 : */