2 * collectd - src/utils_tail_match.c
3 * Copyright (C) 2007-2008 C-Ware, Inc.
4 * Copyright (C) 2008 Florian Forster
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Luke Heberling <lukeh at c-ware.com>
26 * Florian Forster <octo at collectd.org>
29 * Encapsulates useful code to plugins which must parse a log file.
36 #include "utils_match.h"
37 #include "utils_tail.h"
38 #include "utils_tail_match.h"
39 #include "utils_latency_config.h"
41 struct cu_tail_match_simple_s
43 char plugin[DATA_MAX_NAME_LEN];
44 char plugin_instance[DATA_MAX_NAME_LEN];
45 char type[DATA_MAX_NAME_LEN];
46 char type_instance[DATA_MAX_NAME_LEN];
48 latency_config_t latency_config;
50 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
52 struct cu_tail_match_match_s
56 int (*submit) (cu_match_t *match, void *user_data);
57 void (*free) (void *user_data);
59 typedef struct cu_tail_match_match_s cu_tail_match_match_t;
61 struct cu_tail_match_s
67 cu_tail_match_match_t *matches;
74 static int simple_submit_match (cu_match_t *match, void *user_data)
76 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *) user_data;
77 cu_match_value_t *match_value;
78 value_list_t vl = VALUE_LIST_INIT;
81 match_value = (cu_match_value_t *) match_get_user_data (match);
82 if (match_value == NULL)
85 if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
86 && (match_value->values_num == 0))
87 values[0].gauge = NAN;
89 values[0] = match_value->value;
93 sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
94 sstrncpy (vl.plugin_instance, data->plugin_instance,
95 sizeof (vl.plugin_instance));
96 sstrncpy (vl.type, data->type, sizeof (vl.type));
97 sstrncpy (vl.type_instance, data->type_instance,
98 sizeof (vl.type_instance));
100 vl.interval = data->interval;
101 plugin_dispatch_values (&vl);
103 match_value_reset (match_value);
105 } /* int simple_submit_match */
107 static int latency_submit_match(cu_match_t *match, void *user_data) {
108 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *)user_data;
109 cu_match_value_t *match_value;
110 value_list_t vl = VALUE_LIST_INIT;
112 match_value = (cu_match_value_t *)match_get_user_data(match);
113 if (match_value == NULL)
116 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
117 sstrncpy(vl.plugin, data->plugin, sizeof(vl.plugin));
118 sstrncpy(vl.plugin_instance, data->plugin_instance,
119 sizeof(vl.plugin_instance));
120 vl.interval = data->interval;
123 /* Submit percentiles */
124 sstrncpy(vl.type, data->type, sizeof(vl.type));
125 for (size_t i = 0; i < data->latency_config.percentile_num; i++) {
126 if (strlen(data->type_instance) != 0)
127 ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%.0f",
128 data->type_instance, data->latency_config.percentile[i]);
130 ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%.0f",
131 data->latency_config.percentile[i]);
133 vl.values = &(value_t){
135 (match_value->values_num != 0)
136 ? CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(
137 match_value->latency, data->latency_config.percentile[i]))
142 plugin_dispatch_values(&vl);
146 sstrncpy(vl.type, "bucket", sizeof(vl.type));
147 for (size_t i = 0; i < data->latency_config.buckets_num; i++) {
148 latency_bucket_t bucket = data->latency_config.buckets[i];
150 double lower_bound = CDTIME_T_TO_DOUBLE(bucket.lower_bound);
152 bucket.upper_bound ? CDTIME_T_TO_DOUBLE(bucket.upper_bound) : INFINITY;
154 if (strlen(data->type_instance) != 0)
155 ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s-%g_%g",
156 data->type, data->type_instance, lower_bound, upper_bound);
158 ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%g_%g",
159 data->type, lower_bound, upper_bound);
161 vl.values = &(value_t){
162 .gauge = latency_counter_get_rate(match_value->latency,
164 bucket.upper_bound, vl.time),
168 plugin_dispatch_values(&vl);
171 match_value->value.gauge = NAN;
172 match_value->values_num = 0;
173 latency_counter_reset(match_value->latency);
176 } /* int latency_submit_match */
178 static int tail_callback (void *data, char *buf,
179 int __attribute__((unused)) buflen)
181 cu_tail_match_t *obj = (cu_tail_match_t *) data;
183 for (size_t i = 0; i < obj->matches_num; i++)
184 match_apply (obj->matches[i].match, buf);
187 } /* int tail_callback */
189 static void tail_match_simple_free (void *data)
191 cu_tail_match_simple_t *user_data = (cu_tail_match_simple_t *) data;
192 latency_config_free(user_data->latency_config);
194 } /* void tail_match_simple_free */
199 cu_tail_match_t *tail_match_create (const char *filename)
201 cu_tail_match_t *obj;
203 obj = calloc (1, sizeof (*obj));
207 obj->tail = cu_tail_create (filename);
208 if (obj->tail == NULL)
215 } /* cu_tail_match_t *tail_match_create */
217 void tail_match_destroy (cu_tail_match_t *obj)
222 if (obj->tail != NULL)
224 cu_tail_destroy (obj->tail);
228 for (size_t i = 0; i < obj->matches_num; i++)
230 cu_tail_match_match_t *match = obj->matches + i;
231 if (match->match != NULL)
233 match_destroy (match->match);
237 if ((match->user_data != NULL)
238 && (match->free != NULL))
239 (*match->free) (match->user_data);
240 match->user_data = NULL;
243 sfree (obj->matches);
245 } /* void tail_match_destroy */
247 int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
248 int (*submit_match) (cu_match_t *match, void *user_data),
250 void (*free_user_data) (void *user_data))
252 cu_tail_match_match_t *temp;
254 temp = realloc (obj->matches,
255 sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
262 DEBUG ("tail_match_add_match interval %lf", CDTIME_T_TO_DOUBLE(((cu_tail_match_simple_t *)user_data)->interval));
263 temp = obj->matches + (obj->matches_num - 1);
266 temp->user_data = user_data;
267 temp->submit = submit_match;
268 temp->free = free_user_data;
271 } /* int tail_match_add_match */
273 int tail_match_add_match_simple (cu_tail_match_t *obj,
274 const char *regex, const char *excluderegex, int ds_type,
275 const char *plugin, const char *plugin_instance,
276 const char *type, const char *type_instance,
277 const latency_config_t latency_cfg,
278 const cdtime_t interval)
281 cu_tail_match_simple_t *user_data;
284 match = match_create_simple (regex, excluderegex, ds_type);
288 user_data = calloc (1, sizeof (*user_data));
289 if (user_data == NULL)
291 match_destroy (match);
295 sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
296 if (plugin_instance != NULL)
297 sstrncpy (user_data->plugin_instance, plugin_instance,
298 sizeof (user_data->plugin_instance));
300 sstrncpy (user_data->type, type, sizeof (user_data->type));
301 if (type_instance != NULL)
302 sstrncpy (user_data->type_instance, type_instance,
303 sizeof (user_data->type_instance));
305 user_data->interval = interval;
307 if ((ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
308 && (ds_type & UTILS_MATCH_CF_GAUGE_DIST))
310 status = latency_config_copy(&user_data->latency_config, latency_cfg);
313 ERROR ("tail_match_add_match_simple: latency_config_copy() failed.");
318 status = tail_match_add_match (obj, match, latency_submit_match,
319 user_data, tail_match_simple_free);
321 status = tail_match_add_match (obj, match, simple_submit_match,
328 tail_match_simple_free(user_data);
329 match_destroy (match);
333 } /* int tail_match_add_match_simple */
335 int tail_match_read (cu_tail_match_t *obj)
340 status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
344 ERROR ("tail_match: cu_tail_read failed.");
348 for (size_t i = 0; i < obj->matches_num; i++)
350 cu_tail_match_match_t *lt_match = obj->matches + i;
352 if (lt_match->submit == NULL)
355 (*lt_match->submit) (lt_match->match, lt_match->user_data);
359 } /* int tail_match_read */
361 /* vim: set sw=2 sts=2 ts=8 : */