2 * collectd - src/utils_match.c
3 * Copyright (C) 2008-2014 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
31 #include "utils_match.h"
35 #define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01
36 #define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
44 int (*callback) (const char *str, char * const *matches, size_t matches_num,
52 static char *match_substr (const char *str, int begin, int end)
57 if ((begin < 0) || (end < 0) || (begin >= end))
59 if ((size_t) end > (strlen (str) + 1))
61 ERROR ("utils_match: match_substr: `end' points after end of string.");
65 ret_len = end - begin;
66 ret = malloc (ret_len + 1);
69 ERROR ("utils_match: match_substr: malloc failed.");
73 sstrncpy (ret, str + begin, ret_len + 1);
75 } /* char *match_substr */
77 static int default_callback (const char __attribute__((unused)) *str,
78 char * const *matches, size_t matches_num, void *user_data)
80 cu_match_value_t *data = (cu_match_value_t *) user_data;
82 if (data->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
87 if (data->ds_type & UTILS_MATCH_CF_GAUGE_INC)
89 data->value.gauge = isnan (data->value.gauge) ? 1 : data->value.gauge + 1;
97 value = (gauge_t) strtod (matches[1], &endptr);
98 if (matches[1] == endptr)
101 if ((data->values_num == 0)
102 || (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST))
104 data->value.gauge = value;
106 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE)
108 double f = ((double) data->values_num)
109 / ((double) (data->values_num + 1));
110 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
112 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN)
114 if (data->value.gauge > value)
115 data->value.gauge = value;
117 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX)
119 if (data->value.gauge < value)
120 data->value.gauge = value;
122 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD)
124 data->value.gauge += value;
128 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
134 else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER)
139 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC)
141 data->value.counter++;
149 value = (counter_t) strtoull (matches[1], &endptr, 0);
150 if (matches[1] == endptr)
153 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
154 data->value.counter = value;
155 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
156 data->value.counter += value;
159 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
165 else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE)
170 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC)
172 data->value.counter++;
180 value = (derive_t) strtoll (matches[1], &endptr, 0);
181 if (matches[1] == endptr)
184 if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
185 data->value.derive = value;
186 else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
187 data->value.derive += value;
190 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
196 else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE)
204 value = (absolute_t) strtoull (matches[1], &endptr, 0);
205 if (matches[1] == endptr)
208 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
209 data->value.absolute = value;
212 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
220 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
225 } /* int default_callback */
230 cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
231 int (*callback) (const char *str,
232 char * const *matches, size_t matches_num, void *user_data),
238 DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s",
239 regex, excluderegex);
241 obj = calloc (1, sizeof (*obj));
245 status = regcomp (&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
248 ERROR ("Compiling the regular expression \"%s\" failed.", regex);
253 if (excluderegex && strcmp(excluderegex, "") != 0) {
254 status = regcomp (&obj->excluderegex, excluderegex, REG_EXTENDED);
257 ERROR ("Compiling the excluding regular expression \"%s\" failed.",
262 obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
265 obj->callback = callback;
266 obj->user_data = user_data;
269 } /* cu_match_t *match_create_callback */
271 cu_match_t *match_create_simple (const char *regex,
272 const char *excluderegex, int match_ds_type)
274 cu_match_value_t *user_data;
277 user_data = calloc (1, sizeof (*user_data));
278 if (user_data == NULL)
280 user_data->ds_type = match_ds_type;
282 obj = match_create_callback (regex, excluderegex,
283 default_callback, user_data);
290 obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
293 } /* cu_match_t *match_create_simple */
295 void match_value_reset (cu_match_value_t *mv)
300 if (mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
302 mv->value.gauge = NAN;
305 } /* }}} void match_value_reset */
307 void match_destroy (cu_match_t *obj)
312 if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
314 sfree (obj->user_data);
318 } /* void match_destroy */
320 int match_apply (cu_match_t *obj, const char *str)
323 regmatch_t re_match[32];
328 if ((obj == NULL) || (str == NULL))
331 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
332 status = regexec (&obj->excluderegex, str,
333 STATIC_ARRAY_SIZE (re_match), re_match,
335 /* Regex did match, so exclude this line */
337 DEBUG("ExludeRegex matched, don't count that line\n");
342 status = regexec (&obj->regex, str,
343 STATIC_ARRAY_SIZE (re_match), re_match,
346 /* Regex did not match */
350 memset (matches, '\0', sizeof (matches));
351 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE (matches); matches_num++)
353 if ((re_match[matches_num].rm_so < 0)
354 || (re_match[matches_num].rm_eo < 0))
357 matches[matches_num] = match_substr (str,
358 re_match[matches_num].rm_so, re_match[matches_num].rm_eo);
359 if (matches[matches_num] == NULL)
368 ERROR ("utils_match: match_apply: match_substr failed.");
372 status = obj->callback (str, matches, matches_num, obj->user_data);
375 ERROR ("utils_match: match_apply: callback failed.");
379 for (i = 0; i < matches_num; i++)
385 } /* int match_apply */
387 void *match_get_user_data (cu_match_t *obj)
391 return (obj->user_data);
392 } /* void *match_get_user_data */
394 /* vim: set sw=2 sts=2 ts=8 : */