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>
32 #include "utils_match.h"
36 #define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01
37 #define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
45 int (*callback) (const char *str, char * const *matches, size_t matches_num,
53 static char *match_substr (const char *str, int begin, int end)
58 if ((begin < 0) || (end < 0) || (begin >= end))
60 if ((size_t) end > (strlen (str) + 1))
62 ERROR ("utils_match: match_substr: `end' points after end of string.");
66 ret_len = end - begin;
67 ret = malloc (ret_len + 1);
70 ERROR ("utils_match: match_substr: malloc failed.");
74 sstrncpy (ret, str + begin, ret_len + 1);
76 } /* char *match_substr */
78 static int default_callback (const char __attribute__((unused)) *str,
79 char * const *matches, size_t matches_num, void *user_data)
81 cu_match_value_t *data = (cu_match_value_t *) user_data;
83 if (data->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
88 if (data->ds_type & UTILS_MATCH_CF_GAUGE_INC)
90 data->value.gauge = isnan (data->value.gauge) ? 1 : data->value.gauge + 1;
98 value = (gauge_t) strtod (matches[1], &endptr);
99 if (matches[1] == endptr)
102 if ((data->values_num == 0)
103 || (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST))
105 data->value.gauge = value;
107 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE)
109 double f = ((double) data->values_num)
110 / ((double) (data->values_num + 1));
111 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
113 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN)
115 if (data->value.gauge > value)
116 data->value.gauge = value;
118 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX)
120 if (data->value.gauge < value)
121 data->value.gauge = value;
123 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD)
125 data->value.gauge += value;
129 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
135 else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER)
140 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC)
142 data->value.counter++;
150 value = (counter_t) strtoull (matches[1], &endptr, 0);
151 if (matches[1] == endptr)
154 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
155 data->value.counter = value;
156 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
157 data->value.counter += value;
160 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
166 else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE)
171 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC)
173 data->value.counter++;
181 value = (derive_t) strtoll (matches[1], &endptr, 0);
182 if (matches[1] == endptr)
185 if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
186 data->value.derive = value;
187 else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
188 data->value.derive += value;
191 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
197 else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE)
205 value = (absolute_t) strtoull (matches[1], &endptr, 0);
206 if (matches[1] == endptr)
209 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
210 data->value.absolute = value;
213 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
221 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
226 } /* int default_callback */
231 cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
232 int (*callback) (const char *str,
233 char * const *matches, size_t matches_num, void *user_data),
239 DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s",
240 regex, excluderegex);
242 obj = calloc (1, sizeof (*obj));
246 status = regcomp (&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
249 ERROR ("Compiling the regular expression \"%s\" failed.", regex);
254 if (excluderegex && strcmp(excluderegex, "") != 0) {
255 status = regcomp (&obj->excluderegex, excluderegex, REG_EXTENDED);
258 ERROR ("Compiling the excluding regular expression \"%s\" failed.",
263 obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
266 obj->callback = callback;
267 obj->user_data = user_data;
270 } /* cu_match_t *match_create_callback */
272 cu_match_t *match_create_simple (const char *regex,
273 const char *excluderegex, int match_ds_type)
275 cu_match_value_t *user_data;
278 user_data = calloc (1, sizeof (*user_data));
279 if (user_data == NULL)
281 user_data->ds_type = match_ds_type;
283 obj = match_create_callback (regex, excluderegex,
284 default_callback, user_data);
291 obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
294 } /* cu_match_t *match_create_simple */
296 void match_value_reset (cu_match_value_t *mv)
301 if (mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
303 mv->value.gauge = NAN;
306 } /* }}} void match_value_reset */
308 void match_destroy (cu_match_t *obj)
313 if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
315 sfree (obj->user_data);
319 } /* void match_destroy */
321 int match_apply (cu_match_t *obj, const char *str)
324 regmatch_t re_match[32];
325 char *matches[32] = { 0 };
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 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE (matches); matches_num++)
352 if ((re_match[matches_num].rm_so < 0)
353 || (re_match[matches_num].rm_eo < 0))
356 matches[matches_num] = match_substr (str,
357 re_match[matches_num].rm_so, re_match[matches_num].rm_eo);
358 if (matches[matches_num] == NULL)
367 ERROR ("utils_match: match_apply: match_substr failed.");
371 status = obj->callback (str, matches, matches_num, obj->user_data);
374 ERROR ("utils_match: match_apply: callback failed.");
378 for (size_t i = 0; i < matches_num; i++)
384 } /* int match_apply */
386 void *match_get_user_data (cu_match_t *obj)
390 return (obj->user_data);
391 } /* void *match_get_user_data */
393 /* vim: set sw=2 sts=2 ts=8 : */