2 * collectd - src/utils_match.c
3 * Copyright (C) 2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
27 #include "utils_match.h"
31 #define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01
32 #define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
40 int (*callback) (const char *str, char * const *matches, size_t matches_num,
48 static char *match_substr (const char *str, int begin, int end)
53 if ((begin < 0) || (end < 0) || (begin >= end))
55 if ((size_t) end > (strlen (str) + 1))
57 ERROR ("utils_match: match_substr: `end' points after end of string.");
61 ret_len = end - begin;
62 ret = (char *) malloc (sizeof (char) * (ret_len + 1));
65 ERROR ("utils_match: match_substr: malloc failed.");
69 sstrncpy (ret, str + begin, ret_len + 1);
71 } /* char *match_substr */
73 static int default_callback (const char __attribute__((unused)) *str,
74 char * const *matches, size_t matches_num, void *user_data)
76 cu_match_value_t *data = (cu_match_value_t *) user_data;
78 if (data->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
83 if (data->ds_type & UTILS_MATCH_CF_GAUGE_INC)
85 data->value.gauge = isnan (data->value.gauge) ? 1 : data->value.gauge + 1;
93 value = (gauge_t) strtod (matches[1], &endptr);
94 if (matches[1] == endptr)
97 if ((data->values_num == 0)
98 || (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST))
100 data->value.gauge = value;
102 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE)
104 double f = ((double) data->values_num)
105 / ((double) (data->values_num + 1));
106 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
108 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN)
110 if (data->value.gauge > value)
111 data->value.gauge = value;
113 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX)
115 if (data->value.gauge < value)
116 data->value.gauge = value;
118 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD)
120 data->value.gauge += value;
124 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
130 else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER)
135 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC)
137 data->value.counter++;
145 value = (counter_t) strtoull (matches[1], &endptr, 0);
146 if (matches[1] == endptr)
149 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
150 data->value.counter = value;
151 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
152 data->value.counter += value;
155 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
161 else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE)
166 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC)
168 data->value.counter++;
176 value = (derive_t) strtoll (matches[1], &endptr, 0);
177 if (matches[1] == endptr)
180 if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
181 data->value.derive = value;
182 else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
183 data->value.derive += value;
186 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
192 else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE)
200 value = (absolute_t) strtoull (matches[1], &endptr, 0);
201 if (matches[1] == endptr)
204 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
205 data->value.absolute = value;
208 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
216 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
221 } /* int default_callback */
226 cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
227 int (*callback) (const char *str,
228 char * const *matches, size_t matches_num, void *user_data),
234 DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s",
235 regex, excluderegex);
237 obj = (cu_match_t *) malloc (sizeof (cu_match_t));
240 memset (obj, '\0', sizeof (cu_match_t));
242 status = regcomp (&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
245 ERROR ("Compiling the regular expression \"%s\" failed.", regex);
250 if (excluderegex && strcmp(excluderegex, "") != 0) {
251 status = regcomp (&obj->excluderegex, excluderegex, REG_EXTENDED);
254 ERROR ("Compiling the excluding regular expression \"%s\" failed.",
259 obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
262 obj->callback = callback;
263 obj->user_data = user_data;
266 } /* cu_match_t *match_create_callback */
268 cu_match_t *match_create_simple (const char *regex,
269 const char *excluderegex, int match_ds_type)
271 cu_match_value_t *user_data;
274 user_data = (cu_match_value_t *) malloc (sizeof (cu_match_value_t));
275 if (user_data == NULL)
277 memset (user_data, '\0', sizeof (cu_match_value_t));
278 user_data->ds_type = match_ds_type;
280 obj = match_create_callback (regex, excluderegex,
281 default_callback, user_data);
288 obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
291 } /* cu_match_t *match_create_simple */
293 void match_destroy (cu_match_t *obj)
298 if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
300 sfree (obj->user_data);
304 } /* void match_destroy */
306 int match_apply (cu_match_t *obj, const char *str)
309 regmatch_t re_match[32];
314 if ((obj == NULL) || (str == NULL))
317 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
318 status = regexec (&obj->excluderegex, str,
319 STATIC_ARRAY_SIZE (re_match), re_match,
321 /* Regex did match, so exclude this line */
323 DEBUG("ExludeRegex matched, don't count that line\n");
328 status = regexec (&obj->regex, str,
329 STATIC_ARRAY_SIZE (re_match), re_match,
332 /* Regex did not match */
336 memset (matches, '\0', sizeof (matches));
337 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE (matches); matches_num++)
339 if ((re_match[matches_num].rm_so < 0)
340 || (re_match[matches_num].rm_eo < 0))
343 matches[matches_num] = match_substr (str,
344 re_match[matches_num].rm_so, re_match[matches_num].rm_eo);
345 if (matches[matches_num] == NULL)
354 ERROR ("utils_match: match_apply: match_substr failed.");
358 status = obj->callback (str, matches, matches_num, obj->user_data);
361 ERROR ("utils_match: match_apply: callback failed.");
365 for (i = 0; i < matches_num; i++)
371 } /* int match_apply */
373 void *match_get_user_data (cu_match_t *obj)
377 return (obj->user_data);
378 } /* void *match_get_user_data */
380 /* vim: set sw=2 sts=2 ts=8 : */