2 * collectd - src/utils_match.c
3 * Copyright (C) 2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
27 #include "utils_match.h"
31 #define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01
38 int (*callback) (const char *str, char * const *matches, size_t matches_num,
46 static char *match_substr (const char *str, int begin, int end)
51 if ((begin < 0) || (end < 0) || (begin >= end))
53 if ((size_t) end > (strlen (str) + 1))
55 ERROR ("utils_match: match_substr: `end' points after end of string.");
59 ret_len = end - begin;
60 ret = (char *) malloc (sizeof (char) * (ret_len + 1));
63 ERROR ("utils_match: match_substr: malloc failed.");
67 sstrncpy (ret, str + begin, ret_len + 1);
69 } /* char *match_substr */
71 static int default_callback (const char __attribute__((unused)) *str,
72 char * const *matches, size_t matches_num, void *user_data)
74 cu_match_value_t *data = (cu_match_value_t *) user_data;
76 if (data->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
84 value = strtod (matches[1], &endptr);
85 if (matches[1] == endptr)
88 if ((data->values_num == 0)
89 || (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST))
91 data->value.gauge = value;
93 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE)
95 double f = ((double) data->values_num)
96 / ((double) (data->values_num + 1));
97 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
99 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN)
101 if (data->value.gauge > value)
102 data->value.gauge = value;
104 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX)
106 if (data->value.gauge < value)
107 data->value.gauge = value;
111 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
117 else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER)
122 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC)
124 data->value.counter++;
132 value = strtoll (matches[1], &endptr, 0);
133 if (matches[1] == endptr)
136 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
137 data->value.counter = value;
138 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
139 data->value.counter += value;
142 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
148 else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE)
153 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC)
155 data->value.counter++;
163 value = 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;
173 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
179 else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE)
187 value = strtoll (matches[1], &endptr, 0);
188 if (matches[1] == endptr)
191 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
192 data->value.absolute = value;
195 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
203 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
208 } /* int default_callback */
213 cu_match_t *match_create_callback (const char *regex,
214 int (*callback) (const char *str,
215 char * const *matches, size_t matches_num, void *user_data),
221 DEBUG ("utils_match: match_create_callback: regex = %s", regex);
223 obj = (cu_match_t *) malloc (sizeof (cu_match_t));
226 memset (obj, '\0', sizeof (cu_match_t));
228 status = regcomp (&obj->regex, regex, REG_EXTENDED);
231 ERROR ("Compiling the regular expression \"%s\" failed.", regex);
236 obj->callback = callback;
237 obj->user_data = user_data;
240 } /* cu_match_t *match_create_callback */
242 cu_match_t *match_create_simple (const char *regex, int match_ds_type)
244 cu_match_value_t *user_data;
247 user_data = (cu_match_value_t *) malloc (sizeof (cu_match_value_t));
248 if (user_data == NULL)
250 memset (user_data, '\0', sizeof (cu_match_value_t));
251 user_data->ds_type = match_ds_type;
253 obj = match_create_callback (regex, default_callback, user_data);
260 obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
263 } /* cu_match_t *match_create_simple */
265 void match_destroy (cu_match_t *obj)
270 if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
272 sfree (obj->user_data);
276 } /* void match_destroy */
278 int match_apply (cu_match_t *obj, const char *str)
281 regmatch_t re_match[32];
286 if ((obj == NULL) || (str == NULL))
289 status = regexec (&obj->regex, str,
290 STATIC_ARRAY_SIZE (re_match), re_match,
293 /* Regex did not match */
297 memset (matches, '\0', sizeof (matches));
298 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE (matches); matches_num++)
300 if ((re_match[matches_num].rm_so < 0)
301 || (re_match[matches_num].rm_eo < 0))
304 matches[matches_num] = match_substr (str,
305 re_match[matches_num].rm_so, re_match[matches_num].rm_eo);
306 if (matches[matches_num] == NULL)
315 ERROR ("utils_match: match_apply: match_substr failed.");
319 status = obj->callback (str, matches, matches_num, obj->user_data);
322 ERROR ("utils_match: match_apply: callback failed.");
326 for (i = 0; i < matches_num; i++)
332 } /* int match_apply */
334 void *match_get_user_data (cu_match_t *obj)
338 return (obj->user_data);
339 } /* void *match_get_user_data */
341 /* vim: set sw=2 sts=2 ts=8 : */