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
43 int (*callback)(const char *str, char *const *matches, size_t matches_num,
46 void (*free)(void *user_data);
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->ds_type & UTILS_MATCH_CF_GAUGE_DIST) {
97 latency_counter_add(data->latency, DOUBLE_TO_CDTIME_T(value));
102 if ((data->values_num == 0) ||
103 (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST) ||
104 (data->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST)) {
105 data->value.gauge = value;
106 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE) {
107 double f = ((double)data->values_num) / ((double)(data->values_num + 1));
108 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
109 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN) {
110 if (data->value.gauge > value)
111 data->value.gauge = value;
112 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX) {
113 if (data->value.gauge < value)
114 data->value.gauge = value;
115 } else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD) {
116 data->value.gauge += value;
118 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
123 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER) {
127 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC) {
128 data->value.counter++;
136 value = (counter_t)strtoull(matches[1], &endptr, 0);
137 if (matches[1] == endptr)
140 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
141 data->value.counter = value;
142 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
143 data->value.counter += value;
145 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
150 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE) {
154 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC) {
155 data->value.derive++;
163 value = (derive_t)strtoll(matches[1], &endptr, 0);
164 if (matches[1] == endptr)
167 if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
168 data->value.derive = value;
169 else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
170 data->value.derive += value;
172 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
177 } else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE) {
184 value = (absolute_t)strtoull(matches[1], &endptr, 0);
185 if (matches[1] == endptr)
188 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
189 data->value.absolute = value;
191 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
197 ERROR("utils_match: default_callback: obj->ds_type is invalid!");
202 } /* int default_callback */
204 static void match_simple_free(void *data) {
205 cu_match_value_t *user_data = (cu_match_value_t *)data;
206 if (user_data->latency)
207 latency_counter_destroy(user_data->latency);
210 } /* void match_simple_free */
216 match_create_callback(const char *regex, const char *excluderegex,
217 int (*callback)(const char *str, char *const *matches,
218 size_t matches_num, void *user_data),
220 void (*free_user_data)(void *user_data)) {
224 DEBUG("utils_match: match_create_callback: regex = %s, excluderegex = %s",
225 regex, excluderegex);
227 obj = calloc(1, sizeof(*obj));
231 status = regcomp(&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
233 ERROR("Compiling the regular expression \"%s\" failed.", regex);
238 if (excluderegex && strcmp(excluderegex, "") != 0) {
239 status = regcomp(&obj->excluderegex, excluderegex, REG_EXTENDED);
241 ERROR("Compiling the excluding regular expression \"%s\" failed.",
246 obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
249 obj->callback = callback;
250 obj->user_data = user_data;
251 obj->free = free_user_data;
254 } /* cu_match_t *match_create_callback */
256 cu_match_t *match_create_simple(const char *regex, const char *excluderegex,
258 cu_match_value_t *user_data;
261 user_data = calloc(1, sizeof(*user_data));
262 if (user_data == NULL)
264 user_data->ds_type = match_ds_type;
266 if ((match_ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
267 (match_ds_type & UTILS_MATCH_CF_GAUGE_DIST)) {
268 user_data->latency = latency_counter_create();
269 if (user_data->latency == NULL) {
270 ERROR("match_create_simple(): latency_counter_create() failed.");
276 obj = match_create_callback(regex, excluderegex, default_callback, user_data,
279 if (user_data->latency)
280 latency_counter_destroy(user_data->latency);
286 } /* cu_match_t *match_create_simple */
288 void match_value_reset(cu_match_value_t *mv) {
292 /* Reset GAUGE metrics only and except GAUGE_PERSIST. */
293 if ((mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
294 !(mv->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST)) {
295 mv->value.gauge = NAN;
298 } /* }}} void match_value_reset */
300 void match_destroy(cu_match_t *obj) {
304 if ((obj->user_data != NULL) && (obj->free != NULL))
305 (*obj->free)(obj->user_data);
308 } /* void match_destroy */
310 int match_apply(cu_match_t *obj, const char *str) {
312 regmatch_t re_match[32];
313 char *matches[32] = {0};
316 if ((obj == NULL) || (str == NULL))
319 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
321 regexec(&obj->excluderegex, str, STATIC_ARRAY_SIZE(re_match), re_match,
323 /* Regex did match, so exclude this line */
325 DEBUG("ExludeRegex matched, don't count that line\n");
330 status = regexec(&obj->regex, str, STATIC_ARRAY_SIZE(re_match), re_match,
333 /* Regex did not match */
337 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE(matches);
339 if ((re_match[matches_num].rm_so < 0) || (re_match[matches_num].rm_eo < 0))
342 matches[matches_num] = match_substr(str, re_match[matches_num].rm_so,
343 re_match[matches_num].rm_eo);
344 if (matches[matches_num] == NULL) {
351 ERROR("utils_match: match_apply: match_substr failed.");
353 status = obj->callback(str, matches, matches_num, obj->user_data);
355 ERROR("utils_match: match_apply: callback failed.");
359 for (size_t i = 0; i < matches_num; i++) {
364 } /* int match_apply */
366 void *match_get_user_data(cu_match_t *obj) {
369 return (obj->user_data);
370 } /* void *match_get_user_data */