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
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) {
56 if ((begin < 0) || (end < 0) || (begin >= end))
58 if ((size_t)end > (strlen(str) + 1)) {
59 ERROR("utils_match: match_substr: `end' points after end of string.");
63 ret_len = end - begin;
64 ret = malloc(ret_len + 1);
66 ERROR("utils_match: match_substr: malloc failed.");
70 sstrncpy(ret, str + begin, ret_len + 1);
72 } /* char *match_substr */
74 static int default_callback(const char __attribute__((unused)) * str,
75 char *const *matches, size_t matches_num,
77 cu_match_value_t *data = (cu_match_value_t *)user_data;
79 if (data->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) {
83 if (data->ds_type & UTILS_MATCH_CF_GAUGE_INC) {
84 data->value.gauge = isnan(data->value.gauge) ? 1 : data->value.gauge + 1;
92 value = (gauge_t)strtod(matches[1], &endptr);
93 if (matches[1] == endptr)
96 if ((data->values_num == 0) ||
97 (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST)) {
98 data->value.gauge = value;
99 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE) {
100 double f = ((double)data->values_num) / ((double)(data->values_num + 1));
101 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
102 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN) {
103 if (data->value.gauge > value)
104 data->value.gauge = value;
105 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX) {
106 if (data->value.gauge < value)
107 data->value.gauge = value;
108 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD) {
109 data->value.gauge += value;
111 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
116 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER) {
120 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC) {
121 data->value.counter++;
129 value = (counter_t)strtoull(matches[1], &endptr, 0);
130 if (matches[1] == endptr)
133 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
134 data->value.counter = value;
135 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
136 data->value.counter += value;
138 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
143 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE) {
147 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC) {
148 data->value.derive++;
156 value = (derive_t)strtoll(matches[1], &endptr, 0);
157 if (matches[1] == endptr)
160 if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
161 data->value.derive = value;
162 else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
163 data->value.derive += value;
165 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
170 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE) {
177 value = (absolute_t)strtoull(matches[1], &endptr, 0);
178 if (matches[1] == endptr)
181 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
182 data->value.absolute = value;
184 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
190 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
195 } /* int default_callback */
201 match_create_callback(const char *regex, const char *excluderegex,
202 int (*callback)(const char *str, char *const *matches,
203 size_t matches_num, void *user_data),
208 DEBUG("utils_match: match_create_callback: regex = %s, excluderegex = %s",
209 regex, excluderegex);
211 obj = calloc(1, sizeof(*obj));
215 status = regcomp(&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
217 ERROR("Compiling the regular expression \"%s\" failed.", regex);
222 if (excluderegex && strcmp(excluderegex, "") != 0) {
223 status = regcomp(&obj->excluderegex, excluderegex, REG_EXTENDED);
225 ERROR("Compiling the excluding regular expression \"%s\" failed.",
230 obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
233 obj->callback = callback;
234 obj->user_data = user_data;
237 } /* cu_match_t *match_create_callback */
239 cu_match_t *match_create_simple(const char *regex, const char *excluderegex,
241 cu_match_value_t *user_data;
244 user_data = calloc(1, sizeof(*user_data));
245 if (user_data == NULL)
247 user_data->ds_type = match_ds_type;
249 obj = match_create_callback(regex, excluderegex, default_callback, user_data);
255 obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
258 } /* cu_match_t *match_create_simple */
260 void match_value_reset(cu_match_value_t *mv) {
264 if (mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) {
265 mv->value.gauge = NAN;
268 } /* }}} void match_value_reset */
270 void match_destroy(cu_match_t *obj) {
274 if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA) {
275 sfree(obj->user_data);
279 } /* void match_destroy */
281 int match_apply(cu_match_t *obj, const char *str) {
283 regmatch_t re_match[32];
284 char *matches[32] = {0};
287 if ((obj == NULL) || (str == NULL))
290 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
292 regexec(&obj->excluderegex, str, STATIC_ARRAY_SIZE(re_match), re_match,
294 /* Regex did match, so exclude this line */
296 DEBUG("ExludeRegex matched, don't count that line\n");
301 status = regexec(&obj->regex, str, STATIC_ARRAY_SIZE(re_match), re_match,
304 /* Regex did not match */
308 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE(matches);
310 if ((re_match[matches_num].rm_so < 0) || (re_match[matches_num].rm_eo < 0))
313 matches[matches_num] = match_substr(str, re_match[matches_num].rm_so,
314 re_match[matches_num].rm_eo);
315 if (matches[matches_num] == NULL) {
322 ERROR("utils_match: match_apply: match_substr failed.");
324 status = obj->callback(str, matches, matches_num, obj->user_data);
326 ERROR("utils_match: match_apply: callback failed.");
330 for (size_t i = 0; i < matches_num; i++) {
335 } /* int match_apply */
337 void *match_get_user_data(cu_match_t *obj) {
340 return (obj->user_data);
341 } /* void *match_get_user_data */
343 /* vim: set sw=2 sts=2 ts=8 : */