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_EXCLUDE_REGEX 0x02
37 #define UTILS_MATCH_FLAGS_REGEX 0x04
44 int (*callback)(const char *str, char *const *matches, size_t matches_num,
47 void (*free)(void *user_data);
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->ds_type & UTILS_MATCH_CF_GAUGE_DIST) {
98 latency_counter_add(data->latency, DOUBLE_TO_CDTIME_T(value));
103 if ((data->values_num == 0) ||
104 (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST) ||
105 (data->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST)) {
106 data->value.gauge = value;
107 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE) {
108 double f = ((double)data->values_num) / ((double)(data->values_num + 1));
109 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
110 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN) {
111 if (data->value.gauge > value)
112 data->value.gauge = value;
113 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX) {
114 if (data->value.gauge < value)
115 data->value.gauge = value;
116 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD) {
117 data->value.gauge += value;
119 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
124 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER) {
128 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC) {
129 data->value.counter++;
137 value = (counter_t)strtoull(matches[1], &endptr, 0);
138 if (matches[1] == endptr)
141 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
142 data->value.counter = value;
143 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
144 data->value.counter += value;
146 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
151 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE) {
155 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC) {
156 data->value.derive++;
164 value = (derive_t)strtoll(matches[1], &endptr, 0);
165 if (matches[1] == endptr)
168 if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
169 data->value.derive = value;
170 else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
171 data->value.derive += value;
173 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
178 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE) {
185 value = (absolute_t)strtoull(matches[1], &endptr, 0);
186 if (matches[1] == endptr)
189 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
190 data->value.absolute = value;
192 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
198 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
203 } /* int default_callback */
205 static void match_simple_free(void *data) {
206 cu_match_value_t *user_data = (cu_match_value_t *)data;
207 if (user_data->latency)
208 latency_counter_destroy(user_data->latency);
211 } /* void match_simple_free */
217 match_create_callback(const char *regex, const char *excluderegex,
218 int (*callback)(const char *str, char *const *matches,
219 size_t matches_num, void *user_data),
221 void (*free_user_data)(void *user_data)) {
225 DEBUG("utils_match: match_create_callback: regex = %s, excluderegex = %s",
226 regex, excluderegex);
228 obj = calloc(1, sizeof(*obj));
232 status = regcomp(&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
234 ERROR("Compiling the regular expression \"%s\" failed.", regex);
238 obj->flags |= UTILS_MATCH_FLAGS_REGEX;
240 if (excluderegex && strcmp(excluderegex, "") != 0) {
241 status = regcomp(&obj->excluderegex, excluderegex, REG_EXTENDED);
243 ERROR("Compiling the excluding regular expression \"%s\" failed.",
248 obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
251 obj->callback = callback;
252 obj->user_data = user_data;
253 obj->free = free_user_data;
256 } /* cu_match_t *match_create_callback */
258 cu_match_t *match_create_simple(const char *regex, const char *excluderegex,
260 cu_match_value_t *user_data;
263 user_data = calloc(1, sizeof(*user_data));
264 if (user_data == NULL)
266 user_data->ds_type = match_ds_type;
268 if ((match_ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
269 (match_ds_type & UTILS_MATCH_CF_GAUGE_DIST)) {
270 user_data->latency = latency_counter_create();
271 if (user_data->latency == NULL) {
272 ERROR("match_create_simple(): latency_counter_create() failed.");
278 obj = match_create_callback(regex, excluderegex, default_callback, user_data,
281 if (user_data->latency)
282 latency_counter_destroy(user_data->latency);
288 } /* cu_match_t *match_create_simple */
290 void match_value_reset(cu_match_value_t *mv) {
294 /* Reset GAUGE metrics only and except GAUGE_PERSIST. */
295 if ((mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
296 !(mv->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST)) {
297 mv->value.gauge = (mv->ds_type & UTILS_MATCH_CF_GAUGE_INC) ? 0 : NAN;
300 } /* }}} void match_value_reset */
302 void match_destroy(cu_match_t *obj) {
306 if (obj->flags & UTILS_MATCH_FLAGS_REGEX)
307 regfree(&obj->regex);
308 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX)
309 regfree(&obj->excluderegex);
310 if ((obj->user_data != NULL) && (obj->free != NULL))
311 (*obj->free)(obj->user_data);
314 } /* void match_destroy */
316 int match_apply(cu_match_t *obj, const char *str) {
318 regmatch_t re_match[32];
319 char *matches[32] = {0};
322 if ((obj == NULL) || (str == NULL))
325 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
327 regexec(&obj->excluderegex, str, STATIC_ARRAY_SIZE(re_match), re_match,
329 /* Regex did match, so exclude this line */
331 DEBUG("ExludeRegex matched, don't count that line\n");
336 status = regexec(&obj->regex, str, STATIC_ARRAY_SIZE(re_match), re_match,
339 /* Regex did not match */
343 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE(matches);
345 if ((re_match[matches_num].rm_so < 0) || (re_match[matches_num].rm_eo < 0))
348 matches[matches_num] = match_substr(str, re_match[matches_num].rm_so,
349 re_match[matches_num].rm_eo);
350 if (matches[matches_num] == NULL) {
357 ERROR("utils_match: match_apply: match_substr failed.");
359 status = obj->callback(str, matches, matches_num, obj->user_data);
361 ERROR("utils_match: match_apply: callback failed.");
365 for (size_t i = 0; i < matches_num; i++) {
370 } /* int match_apply */
372 void *match_get_user_data(cu_match_t *obj) {
375 return obj->user_data;
376 } /* void *match_get_user_data */