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_latency_config.h"
37 #include "utils_match.h"
38 #include "utils_tail.h"
39 #include "utils_tail_match.h"
41 struct cu_tail_match_simple_s {
42 char plugin[DATA_MAX_NAME_LEN];
43 char plugin_instance[DATA_MAX_NAME_LEN];
44 char type[DATA_MAX_NAME_LEN];
45 char type_instance[DATA_MAX_NAME_LEN];
47 latency_config_t latency_config;
49 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
51 struct cu_tail_match_match_s {
54 int (*submit)(cu_match_t *match, void *user_data);
55 void (*free)(void *user_data);
57 typedef struct cu_tail_match_match_s cu_tail_match_match_t;
59 struct cu_tail_match_s {
64 cu_tail_match_match_t *matches;
71 static int simple_submit_match(cu_match_t *match, void *user_data) {
72 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *)user_data;
73 cu_match_value_t *match_value;
74 value_list_t vl = VALUE_LIST_INIT;
77 match_value = (cu_match_value_t *)match_get_user_data(match);
78 if (match_value == NULL)
81 if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
82 (match_value->values_num == 0))
83 values[0].gauge = NAN;
85 values[0] = match_value->value;
89 sstrncpy(vl.plugin, data->plugin, sizeof(vl.plugin));
90 sstrncpy(vl.plugin_instance, data->plugin_instance,
91 sizeof(vl.plugin_instance));
92 sstrncpy(vl.type, data->type, sizeof(vl.type));
93 sstrncpy(vl.type_instance, data->type_instance, sizeof(vl.type_instance));
95 vl.interval = data->interval;
96 plugin_dispatch_values(&vl);
98 match_value_reset(match_value);
100 } /* int simple_submit_match */
102 static int latency_submit_match(cu_match_t *match, void *user_data) {
103 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *)user_data;
104 cu_match_value_t *match_value;
105 value_list_t vl = VALUE_LIST_INIT;
107 match_value = (cu_match_value_t *)match_get_user_data(match);
108 if (match_value == NULL)
111 sstrncpy(vl.plugin, data->plugin, sizeof(vl.plugin));
112 sstrncpy(vl.plugin_instance, data->plugin_instance,
113 sizeof(vl.plugin_instance));
114 vl.interval = data->interval;
117 /* Submit percentiles */
118 sstrncpy(vl.type, data->type, sizeof(vl.type));
119 for (size_t i = 0; i < data->latency_config.percentile_num; i++) {
120 if (strlen(data->type_instance) != 0)
121 snprintf(vl.type_instance, sizeof(vl.type_instance), "%.117s-%.2f",
122 data->type_instance, data->latency_config.percentile[i]);
124 snprintf(vl.type_instance, sizeof(vl.type_instance), "%.0f",
125 data->latency_config.percentile[i]);
127 vl.values = &(value_t){
129 (match_value->values_num != 0)
130 ? CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(
131 match_value->latency, data->latency_config.percentile[i]))
136 plugin_dispatch_values(&vl);
140 if (data->latency_config.bucket_type != NULL)
141 sstrncpy(vl.type, data->latency_config.bucket_type, sizeof(vl.type));
143 sstrncpy(vl.type, "bucket", sizeof(vl.type));
145 for (size_t i = 0; i < data->latency_config.buckets_num; i++) {
146 latency_bucket_t bucket = data->latency_config.buckets[i];
148 double lower_bound = CDTIME_T_TO_DOUBLE(bucket.lower_bound);
150 bucket.upper_bound ? CDTIME_T_TO_DOUBLE(bucket.upper_bound) : INFINITY;
152 if (strlen(data->type_instance) != 0)
153 snprintf(vl.type_instance, sizeof(vl.type_instance),
154 "%.54s-%.54s-%.2g_%.2g", data->type, data->type_instance,
155 lower_bound, upper_bound);
157 snprintf(vl.type_instance, sizeof(vl.type_instance), "%.107s-%.2g_%.2g",
158 data->type, lower_bound, upper_bound);
160 vl.values = &(value_t){
162 latency_counter_get_rate(match_value->latency, bucket.lower_bound,
163 bucket.upper_bound, vl.time),
167 plugin_dispatch_values(&vl);
170 match_value->value.gauge = NAN;
171 match_value->values_num = 0;
172 latency_counter_reset(match_value->latency);
175 } /* int latency_submit_match */
177 static int tail_callback(void *data, char *buf,
178 int __attribute__((unused)) buflen) {
179 cu_tail_match_t *obj = (cu_tail_match_t *)data;
181 for (size_t i = 0; i < obj->matches_num; i++)
182 match_apply(obj->matches[i].match, buf);
185 } /* int tail_callback */
187 static void tail_match_simple_free(void *data) {
188 cu_tail_match_simple_t *user_data = (cu_tail_match_simple_t *)data;
189 latency_config_free(user_data->latency_config);
191 } /* void tail_match_simple_free */
196 cu_tail_match_t *tail_match_create(const char *filename) {
197 cu_tail_match_t *obj;
199 obj = calloc(1, sizeof(*obj));
203 obj->tail = cu_tail_create(filename);
204 if (obj->tail == NULL) {
210 } /* cu_tail_match_t *tail_match_create */
212 void tail_match_destroy(cu_tail_match_t *obj) {
216 if (obj->tail != NULL) {
217 cu_tail_destroy(obj->tail);
221 for (size_t i = 0; i < obj->matches_num; i++) {
222 cu_tail_match_match_t *match = obj->matches + i;
223 if (match->match != NULL) {
224 match_destroy(match->match);
228 if ((match->user_data != NULL) && (match->free != NULL))
229 (*match->free)(match->user_data);
230 match->user_data = NULL;
235 } /* void tail_match_destroy */
237 int tail_match_add_match(cu_tail_match_t *obj, cu_match_t *match,
238 int (*submit_match)(cu_match_t *match,
241 void (*free_user_data)(void *user_data)) {
242 cu_tail_match_match_t *temp;
244 temp = realloc(obj->matches,
245 sizeof(cu_tail_match_match_t) * (obj->matches_num + 1));
252 DEBUG("tail_match_add_match interval %lf",
253 CDTIME_T_TO_DOUBLE(((cu_tail_match_simple_t *)user_data)->interval));
254 temp = obj->matches + (obj->matches_num - 1);
257 temp->user_data = user_data;
258 temp->submit = submit_match;
259 temp->free = free_user_data;
262 } /* int tail_match_add_match */
264 int tail_match_add_match_simple(cu_tail_match_t *obj, const char *regex,
265 const char *excluderegex, int ds_type,
266 const char *plugin, const char *plugin_instance,
267 const char *type, const char *type_instance,
268 const latency_config_t latency_cfg,
269 const cdtime_t interval) {
271 cu_tail_match_simple_t *user_data;
274 match = match_create_simple(regex, excluderegex, ds_type);
278 user_data = calloc(1, sizeof(*user_data));
279 if (user_data == NULL) {
280 match_destroy(match);
284 sstrncpy(user_data->plugin, plugin, sizeof(user_data->plugin));
285 if (plugin_instance != NULL)
286 sstrncpy(user_data->plugin_instance, plugin_instance,
287 sizeof(user_data->plugin_instance));
289 sstrncpy(user_data->type, type, sizeof(user_data->type));
290 if (type_instance != NULL)
291 sstrncpy(user_data->type_instance, type_instance,
292 sizeof(user_data->type_instance));
294 user_data->interval = interval;
296 if ((ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
297 (ds_type & UTILS_MATCH_CF_GAUGE_DIST)) {
298 status = latency_config_copy(&user_data->latency_config, latency_cfg);
300 ERROR("tail_match_add_match_simple: latency_config_copy() failed.");
305 status = tail_match_add_match(obj, match, latency_submit_match, user_data,
306 tail_match_simple_free);
309 tail_match_add_match(obj, match, simple_submit_match, user_data, free);
314 tail_match_simple_free(user_data);
315 match_destroy(match);
319 } /* int tail_match_add_match_simple */
321 int tail_match_read(cu_tail_match_t *obj) {
325 status = cu_tail_read(obj->tail, buffer, sizeof(buffer), tail_callback,
328 ERROR("tail_match: cu_tail_read failed.");
332 for (size_t i = 0; i < obj->matches_num; i++) {
333 cu_tail_match_match_t *lt_match = obj->matches + i;
335 if (lt_match->submit == NULL)
338 (*lt_match->submit)(lt_match->match, lt_match->user_data);
342 } /* int tail_match_read */