1 /**
2 * collectd - src/utils_match.c
3 * Copyright (C) 2008-2014 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #include "utils_match.h"
34 #include <regex.h>
36 #define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
38 struct cu_match_s
39 {
40 regex_t regex;
41 regex_t excluderegex;
42 int flags;
44 int (*callback) (const char *str, char * const *matches, size_t matches_num,
45 void *user_data);
46 void *user_data;
47 void (*free) (void *user_data);
48 };
50 /*
51 * Private functions
52 */
53 static char *match_substr (const char *str, int begin, int end)
54 {
55 char *ret;
56 size_t ret_len;
58 if ((begin < 0) || (end < 0) || (begin >= end))
59 return (NULL);
60 if ((size_t) end > (strlen (str) + 1))
61 {
62 ERROR ("utils_match: match_substr: `end' points after end of string.");
63 return (NULL);
64 }
66 ret_len = end - begin;
67 ret = malloc (ret_len + 1);
68 if (ret == NULL)
69 {
70 ERROR ("utils_match: match_substr: malloc failed.");
71 return (NULL);
72 }
74 sstrncpy (ret, str + begin, ret_len + 1);
75 return (ret);
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)
80 {
81 cu_match_value_t *data = (cu_match_value_t *) user_data;
83 if (data->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
84 {
85 gauge_t value;
86 char *endptr = NULL;
88 if (data->ds_type & UTILS_MATCH_CF_GAUGE_INC)
89 {
90 data->value.gauge = isnan (data->value.gauge) ? 1 : data->value.gauge + 1;
91 data->values_num++;
92 return(0);
93 }
95 if (matches_num < 2)
96 return (-1);
98 value = (gauge_t) strtod (matches[1], &endptr);
99 if (matches[1] == endptr)
100 return (-1);
102 if (data->ds_type & UTILS_MATCH_CF_GAUGE_DIST)
103 {
104 latency_counter_add(data->latency, DOUBLE_TO_CDTIME_T(value));
105 data->values_num++;
106 return (0);
107 }
109 if ((data->values_num == 0)
110 || (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST)
111 || (data->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST))
112 {
113 data->value.gauge = value;
114 }
115 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_AVERAGE)
116 {
117 double f = ((double) data->values_num)
118 / ((double) (data->values_num + 1));
119 data->value.gauge = (data->value.gauge * f) + (value * (1.0 - f));
120 }
121 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MIN)
122 {
123 if (data->value.gauge > value)
124 data->value.gauge = value;
125 }
126 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_MAX)
127 {
128 if (data->value.gauge < value)
129 data->value.gauge = value;
130 }
131 else if (data->ds_type & UTILS_MATCH_CF_GAUGE_ADD)
132 {
133 data->value.gauge += value;
134 }
135 else
136 {
137 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
138 return (-1);
139 }
141 data->values_num++;
142 }
143 else if (data->ds_type & UTILS_MATCH_DS_TYPE_COUNTER)
144 {
145 counter_t value;
146 char *endptr = NULL;
148 if (data->ds_type & UTILS_MATCH_CF_COUNTER_INC)
149 {
150 data->value.counter++;
151 data->values_num++;
152 return (0);
153 }
155 if (matches_num < 2)
156 return (-1);
158 value = (counter_t) strtoull (matches[1], &endptr, 0);
159 if (matches[1] == endptr)
160 return (-1);
162 if (data->ds_type & UTILS_MATCH_CF_COUNTER_SET)
163 data->value.counter = value;
164 else if (data->ds_type & UTILS_MATCH_CF_COUNTER_ADD)
165 data->value.counter += value;
166 else
167 {
168 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
169 return (-1);
170 }
172 data->values_num++;
173 }
174 else if (data->ds_type & UTILS_MATCH_DS_TYPE_DERIVE)
175 {
176 derive_t value;
177 char *endptr = NULL;
179 if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC)
180 {
181 data->value.derive++;
182 data->values_num++;
183 return (0);
184 }
186 if (matches_num < 2)
187 return (-1);
189 value = (derive_t) strtoll (matches[1], &endptr, 0);
190 if (matches[1] == endptr)
191 return (-1);
193 if (data->ds_type & UTILS_MATCH_CF_DERIVE_SET)
194 data->value.derive = value;
195 else if (data->ds_type & UTILS_MATCH_CF_DERIVE_ADD)
196 data->value.derive += value;
197 else
198 {
199 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
200 return (-1);
201 }
203 data->values_num++;
204 }
205 else if (data->ds_type & UTILS_MATCH_DS_TYPE_ABSOLUTE)
206 {
207 absolute_t value;
208 char *endptr = NULL;
210 if (matches_num < 2)
211 return (-1);
213 value = (absolute_t) strtoull (matches[1], &endptr, 0);
214 if (matches[1] == endptr)
215 return (-1);
217 if (data->ds_type & UTILS_MATCH_CF_ABSOLUTE_SET)
218 data->value.absolute = value;
219 else
220 {
221 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
222 return (-1);
223 }
225 data->values_num++;
226 }
227 else
228 {
229 ERROR ("utils_match: default_callback: obj->ds_type is invalid!");
230 return (-1);
231 }
233 return (0);
234 } /* int default_callback */
236 static void match_simple_free (void *data)
237 {
238 cu_match_value_t *user_data = (cu_match_value_t *) data;
239 if (user_data->latency)
240 latency_counter_destroy(user_data->latency);
242 free (data);
243 } /* void match_simple_free */
245 /*
246 * Public functions
247 */
248 cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
249 int (*callback) (const char *str,
250 char * const *matches, size_t matches_num, void *user_data),
251 void *user_data, void (*free_user_data) (void *user_data))
252 {
253 cu_match_t *obj;
254 int status;
256 DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s",
257 regex, excluderegex);
259 obj = calloc (1, sizeof (*obj));
260 if (obj == NULL)
261 return (NULL);
263 status = regcomp (&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
264 if (status != 0)
265 {
266 ERROR ("Compiling the regular expression \"%s\" failed.", regex);
267 sfree (obj);
268 return (NULL);
269 }
271 if (excluderegex && strcmp(excluderegex, "") != 0) {
272 status = regcomp (&obj->excluderegex, excluderegex, REG_EXTENDED);
273 if (status != 0)
274 {
275 ERROR ("Compiling the excluding regular expression \"%s\" failed.",
276 excluderegex);
277 sfree (obj);
278 return (NULL);
279 }
280 obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
281 }
283 obj->callback = callback;
284 obj->user_data = user_data;
285 obj->free = free_user_data;
287 return (obj);
288 } /* cu_match_t *match_create_callback */
290 cu_match_t *match_create_simple (const char *regex,
291 const char *excluderegex, int match_ds_type)
292 {
293 cu_match_value_t *user_data;
294 cu_match_t *obj;
296 user_data = calloc (1, sizeof (*user_data));
297 if (user_data == NULL)
298 return (NULL);
299 user_data->ds_type = match_ds_type;
301 if ((match_ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
302 && (match_ds_type & UTILS_MATCH_CF_GAUGE_DIST))
303 {
304 user_data->latency = latency_counter_create();
305 if (user_data->latency == NULL)
306 {
307 ERROR ("match_create_simple(): latency_counter_create() failed.");
308 free (user_data);
309 return (NULL);
310 }
311 }
313 obj = match_create_callback (regex, excluderegex,
314 default_callback, user_data, match_simple_free);
315 if (obj == NULL)
316 {
317 if (user_data->latency)
318 latency_counter_destroy(user_data->latency);
320 sfree (user_data);
321 return (NULL);
322 }
323 return (obj);
324 } /* cu_match_t *match_create_simple */
326 void match_value_reset (cu_match_value_t *mv)
327 {
328 if (mv == NULL)
329 return;
331 /* Reset GAUGE metrics only and except GAUGE_PERSIST. */
332 if ((mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
333 && !(mv->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST))
334 {
335 mv->value.gauge = NAN;
336 mv->values_num = 0;
337 }
338 } /* }}} void match_value_reset */
340 void match_destroy (cu_match_t *obj)
341 {
342 if (obj == NULL)
343 return;
345 if ((obj->user_data != NULL) && (obj->free != NULL))
346 (*obj->free) (obj->user_data);
348 sfree (obj);
349 } /* void match_destroy */
351 int match_apply (cu_match_t *obj, const char *str)
352 {
353 int status;
354 regmatch_t re_match[32];
355 char *matches[32] = { 0 };
356 size_t matches_num;
358 if ((obj == NULL) || (str == NULL))
359 return (-1);
361 if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
362 status = regexec (&obj->excluderegex, str,
363 STATIC_ARRAY_SIZE (re_match), re_match,
364 /* eflags = */ 0);
365 /* Regex did match, so exclude this line */
366 if (status == 0) {
367 DEBUG("ExludeRegex matched, don't count that line\n");
368 return (0);
369 }
370 }
372 status = regexec (&obj->regex, str,
373 STATIC_ARRAY_SIZE (re_match), re_match,
374 /* eflags = */ 0);
376 /* Regex did not match */
377 if (status != 0)
378 return (0);
380 for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE (matches); matches_num++)
381 {
382 if ((re_match[matches_num].rm_so < 0)
383 || (re_match[matches_num].rm_eo < 0))
384 break;
386 matches[matches_num] = match_substr (str,
387 re_match[matches_num].rm_so, re_match[matches_num].rm_eo);
388 if (matches[matches_num] == NULL)
389 {
390 status = -1;
391 break;
392 }
393 }
395 if (status != 0)
396 {
397 ERROR ("utils_match: match_apply: match_substr failed.");
398 }
399 else
400 {
401 status = obj->callback (str, matches, matches_num, obj->user_data);
402 if (status != 0)
403 {
404 ERROR ("utils_match: match_apply: callback failed.");
405 }
406 }
408 for (size_t i = 0; i < matches_num; i++)
409 {
410 sfree (matches[i]);
411 }
413 return (status);
414 } /* int match_apply */
416 void *match_get_user_data (cu_match_t *obj)
417 {
418 if (obj == NULL)
419 return (NULL);
420 return (obj->user_data);
421 } /* void *match_get_user_data */
423 /* vim: set sw=2 sts=2 ts=8 : */