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
38 #define UTILS_MATCH_FLAGS_REGEX 0x04
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) {
57 if ((begin < 0) || (end < 0) || (begin >= end))
59 if ((size_t)end > (strlen(str) + 1)) {
60 ERROR("utils_match: match_substr: `end' points after end of string.");
64 ret_len = end - begin;
65 ret = malloc(ret_len + 1);
67 ERROR("utils_match: match_substr: malloc failed.");
71 sstrncpy(ret, str + begin, ret_len + 1);
73 } /* char *match_substr */
75 static int default_callback(const char __attribute__((unused)) * str,
76 char *const *matches, size_t matches_num,
78 cu_match_value_t *data = (cu_match_value_t *)user_data;
80 if (data->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) {
84 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)) {
99 data->value.gauge = value;
100 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE) {
101 double f = ((double)data->values_num) / ((double)(data->values_num + 1));
102 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
103 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN) {
104 if (data->value.gauge > value)
105 data->value.gauge = value;
106 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX) {
107 if (data->value.gauge < value)
108 data->value.gauge = value;
109 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD) {
110 data->value.gauge += value;
112 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
117 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER) {
121 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC) {
122 data->value.counter++;
130 value = (counter_t)strtoull(matches[1], &endptr, 0);
131 if (matches[1] == endptr)
134 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
135 data->value.counter = value;
136 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
137 data->value.counter += value;
139 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
144 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE) {
148 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC) {
149 data->value.derive++;
157 value = (derive_t)strtoll(matches[1], &endptr, 0);
158 if (matches[1] == endptr)
161 if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
162 data->value.derive = value;
163 else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
164 data->value.derive += value;
166 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
171 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE) {
178 value = (absolute_t)strtoull(matches[1], &endptr, 0);
179 if (matches[1] == endptr)
182 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
183 data->value.absolute = value;
185 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
191 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
196 } /* int default_callback */
202 match_create_callback(const char *regex, const char *excluderegex,
203 int (*callback)(const char *str, char *const *matches,
204 size_t matches_num, void *user_data),
209 DEBUG("utils_match: match_create_callback: regex = %s, excluderegex = %s",
210 regex, excluderegex);
212 obj = calloc(1, sizeof(*obj));
216 status = regcomp(&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
218 ERROR("Compiling the regular expression \"%s\" failed.", regex);
222 obj->flags |= UTILS_MATCH_FLAGS_REGEX;
224 if (excluderegex && strcmp(excluderegex, "") != 0) {
225 status = regcomp(&obj->excluderegex, excluderegex, REG_EXTENDED);
227 ERROR("Compiling the excluding regular expression \"%s\" failed.",
232 obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
235 obj->callback = callback;
236 obj->user_data = user_data;
239 } /* cu_match_t *match_create_callback */
241 cu_match_t *match_create_simple(const char *regex, const char *excluderegex,
243 cu_match_value_t *user_data;
246 user_data = calloc(1, sizeof(*user_data));
247 if (user_data == NULL)
249 user_data->ds_type = match_ds_type;
251 obj = match_create_callback(regex, excluderegex, default_callback, user_data);
257 obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
260 } /* cu_match_t *match_create_simple */
262 void match_value_reset(cu_match_value_t *mv) {
266 if (mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) {
267 mv->value.gauge = NAN;
270 } /* }}} void match_value_reset */
272 void match_destroy(cu_match_t *obj) {
276 if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
277 sfree(obj->user_data);
278 if (obj->flags & UTILS_MATCH_FLAGS_REGEX)
279 regfree(&obj->regex);
280 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX)
281 regfree(&obj->excluderegex);
284 } /* void match_destroy */
286 int match_apply(cu_match_t *obj, const char *str) {
288 regmatch_t re_match[32];
289 char *matches[32] = {0};
292 if ((obj == NULL) || (str == NULL))
295 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
297 regexec(&obj->excluderegex, str, STATIC_ARRAY_SIZE(re_match), re_match,
299 /* Regex did match, so exclude this line */
301 DEBUG("ExludeRegex matched, don't count that line\n");
306 status = regexec(&obj->regex, str, STATIC_ARRAY_SIZE(re_match), re_match,
309 /* Regex did not match */
313 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE(matches);
315 if ((re_match[matches_num].rm_so < 0) || (re_match[matches_num].rm_eo < 0))
318 matches[matches_num] = match_substr(str, re_match[matches_num].rm_so,
319 re_match[matches_num].rm_eo);
320 if (matches[matches_num] == NULL) {
327 ERROR("utils_match: match_apply: match_substr failed.");
329 status = obj->callback(str, matches, matches_num, obj->user_data);
331 ERROR("utils_match: match_apply: callback failed.");
335 for (size_t i = 0; i < matches_num; i++) {
340 } /* int match_apply */
342 void *match_get_user_data(cu_match_t *obj) {
345 return (obj->user_data);
346 } /* void *match_get_user_data */
348 /* vim: set sw=2 sts=2 ts=8 : */