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)
104 || (data->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST))
106 data->value.gauge = value;
108 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE)
110 double f = ((double) data->values_num)
111 / ((double) (data->values_num + 1));
112 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
114 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN)
116 if (data->value.gauge > value)
117 data->value.gauge = value;
119 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX)
121 if (data->value.gauge < value)
122 data->value.gauge = value;
124 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD)
126 data->value.gauge += value;
130 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
136 else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER)
141 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC)
143 data->value.counter++;
151 value = (counter_t) strtoull (matches[1], &endptr, 0);
152 if (matches[1] == endptr)
155 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
156 data->value.counter = value;
157 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
158 data->value.counter += value;
161 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
167 else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE)
172 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC)
174 data->value.derive++;
182 value = (derive_t) strtoll (matches[1], &endptr, 0);
183 if (matches[1] == endptr)
186 if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
187 data->value.derive = value;
188 else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
189 data->value.derive += value;
192 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
198 else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE)
206 value = (absolute_t) strtoull (matches[1], &endptr, 0);
207 if (matches[1] == endptr)
210 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
211 data->value.absolute = value;
214 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
222 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
227 } /* int default_callback */
232 cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
233 int (*callback) (const char *str,
234 char * const *matches, size_t matches_num, void *user_data),
240 DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s",
241 regex, excluderegex);
243 obj = calloc (1, sizeof (*obj));
247 status = regcomp (&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
250 ERROR ("Compiling the regular expression \"%s\" failed.", regex);
255 if (excluderegex && strcmp(excluderegex, "") != 0) {
256 status = regcomp (&obj->excluderegex, excluderegex, REG_EXTENDED);
259 ERROR ("Compiling the excluding regular expression \"%s\" failed.",
264 obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
267 obj->callback = callback;
268 obj->user_data = user_data;
271 } /* cu_match_t *match_create_callback */
273 cu_match_t *match_create_simple (const char *regex,
274 const char *excluderegex, int match_ds_type)
276 cu_match_value_t *user_data;
279 user_data = calloc (1, sizeof (*user_data));
280 if (user_data == NULL)
282 user_data->ds_type = match_ds_type;
284 obj = match_create_callback (regex, excluderegex,
285 default_callback, user_data);
292 obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
295 } /* cu_match_t *match_create_simple */
297 void match_value_reset (cu_match_value_t *mv)
302 /* Reset GAUGE metrics only and except GAUGE_PERSIST. */
303 if ((mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
304 && !(mv->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST))
306 mv->value.gauge = NAN;
309 } /* }}} void match_value_reset */
311 void match_destroy (cu_match_t *obj)
316 if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
318 sfree (obj->user_data);
322 } /* void match_destroy */
324 int match_apply (cu_match_t *obj, const char *str)
327 regmatch_t re_match[32];
328 char *matches[32] = { 0 };
331 if ((obj == NULL) || (str == NULL))
334 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
335 status = regexec (&obj->excluderegex, str,
336 STATIC_ARRAY_SIZE (re_match), re_match,
338 /* Regex did match, so exclude this line */
340 DEBUG("ExludeRegex matched, don't count that line\n");
345 status = regexec (&obj->regex, str,
346 STATIC_ARRAY_SIZE (re_match), re_match,
349 /* Regex did not match */
353 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE (matches); matches_num++)
355 if ((re_match[matches_num].rm_so < 0)
356 || (re_match[matches_num].rm_eo < 0))
359 matches[matches_num] = match_substr (str,
360 re_match[matches_num].rm_so, re_match[matches_num].rm_eo);
361 if (matches[matches_num] == NULL)
370 ERROR ("utils_match: match_apply: match_substr failed.");
374 status = obj->callback (str, matches, matches_num, obj->user_data);
377 ERROR ("utils_match: match_apply: callback failed.");
381 for (size_t i = 0; i < matches_num; i++)
387 } /* int match_apply */
389 void *match_get_user_data (cu_match_t *obj)
393 return (obj->user_data);
394 } /* void *match_get_user_data */
396 /* vim: set sw=2 sts=2 ts=8 : */