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];
46 latency_config_t latency_config;
48 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
50 struct cu_tail_match_match_s {
53 int (*submit)(cu_match_t *match, void *user_data);
54 void (*free)(void *user_data);
56 typedef struct cu_tail_match_match_s cu_tail_match_match_t;
58 struct cu_tail_match_s {
60 cu_tail_match_match_t *matches;
67 static int simple_submit_match(cu_match_t *match, void *user_data) {
68 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *)user_data;
69 cu_match_value_t *match_value;
70 value_list_t vl = VALUE_LIST_INIT;
73 match_value = (cu_match_value_t *)match_get_user_data(match);
74 if (match_value == NULL)
77 if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
78 (match_value->values_num == 0))
79 values[0].gauge = NAN;
81 values[0] = match_value->value;
85 sstrncpy(vl.plugin, data->plugin, sizeof(vl.plugin));
86 sstrncpy(vl.plugin_instance, data->plugin_instance,
87 sizeof(vl.plugin_instance));
88 sstrncpy(vl.type, data->type, sizeof(vl.type));
89 sstrncpy(vl.type_instance, data->type_instance, sizeof(vl.type_instance));
91 plugin_dispatch_values(&vl);
93 match_value_reset(match_value);
95 } /* int simple_submit_match */
97 static int latency_submit_match(cu_match_t *match, void *user_data) {
98 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *)user_data;
99 cu_match_value_t *match_value;
100 value_list_t vl = VALUE_LIST_INIT;
102 match_value = (cu_match_value_t *)match_get_user_data(match);
103 if (match_value == NULL)
106 sstrncpy(vl.plugin, data->plugin, sizeof(vl.plugin));
107 sstrncpy(vl.plugin_instance, data->plugin_instance,
108 sizeof(vl.plugin_instance));
111 /* Submit percentiles */
112 sstrncpy(vl.type, data->type, sizeof(vl.type));
113 for (size_t i = 0; i < data->latency_config.percentile_num; i++) {
114 if (strlen(data->type_instance) != 0)
115 snprintf(vl.type_instance, sizeof(vl.type_instance), "%.50s-%.5g",
116 data->type_instance, data->latency_config.percentile[i]);
118 snprintf(vl.type_instance, sizeof(vl.type_instance), "%.5g",
119 data->latency_config.percentile[i]);
121 vl.values = &(value_t){
123 (match_value->values_num != 0)
124 ? CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(
125 match_value->latency, data->latency_config.percentile[i]))
130 plugin_dispatch_values(&vl);
134 if (data->latency_config.bucket_type != NULL)
135 sstrncpy(vl.type, data->latency_config.bucket_type, sizeof(vl.type));
137 sstrncpy(vl.type, "bucket", sizeof(vl.type));
139 for (size_t i = 0; i < data->latency_config.buckets_num; i++) {
140 latency_bucket_t bucket = data->latency_config.buckets[i];
142 double lower_bound = CDTIME_T_TO_DOUBLE(bucket.lower_bound);
144 bucket.upper_bound ? CDTIME_T_TO_DOUBLE(bucket.upper_bound) : INFINITY;
146 if (strlen(data->type_instance) != 0)
147 snprintf(vl.type_instance, sizeof(vl.type_instance), "%.50s-%.50s-%g_%g",
148 data->type, data->type_instance, lower_bound, upper_bound);
150 snprintf(vl.type_instance, sizeof(vl.type_instance), "%.50s-%g_%g",
151 data->type, lower_bound, upper_bound);
153 vl.values = &(value_t){
155 latency_counter_get_rate(match_value->latency, bucket.lower_bound,
156 bucket.upper_bound, vl.time),
160 plugin_dispatch_values(&vl);
163 match_value->value.gauge = NAN;
164 match_value->values_num = 0;
165 latency_counter_reset(match_value->latency);
168 } /* int latency_submit_match */
170 static int tail_callback(void *data, char *buf,
171 int __attribute__((unused)) buflen) {
172 cu_tail_match_t *obj = (cu_tail_match_t *)data;
174 for (size_t i = 0; i < obj->matches_num; i++)
175 match_apply(obj->matches[i].match, buf);
178 } /* int tail_callback */
180 static void tail_match_simple_free(void *data) {
181 cu_tail_match_simple_t *user_data = (cu_tail_match_simple_t *)data;
182 latency_config_free(user_data->latency_config);
184 } /* void tail_match_simple_free */
189 cu_tail_match_t *tail_match_create(const char *filename) {
190 cu_tail_match_t *obj;
192 obj = calloc(1, sizeof(*obj));
196 obj->tail = cu_tail_create(filename);
197 if (obj->tail == NULL) {
203 } /* cu_tail_match_t *tail_match_create */
205 void tail_match_destroy(cu_tail_match_t *obj) {
209 if (obj->tail != NULL) {
210 cu_tail_destroy(obj->tail);
214 for (size_t i = 0; i < obj->matches_num; i++) {
215 cu_tail_match_match_t *match = obj->matches + i;
216 if (match->match != NULL) {
217 match_destroy(match->match);
221 if ((match->user_data != NULL) && (match->free != NULL))
222 (*match->free)(match->user_data);
223 match->user_data = NULL;
228 } /* void tail_match_destroy */
230 int tail_match_add_match(cu_tail_match_t *obj, cu_match_t *match,
231 int (*submit_match)(cu_match_t *match,
234 void (*free_user_data)(void *user_data)) {
235 cu_tail_match_match_t *temp;
237 temp = realloc(obj->matches,
238 sizeof(cu_tail_match_match_t) * (obj->matches_num + 1));
245 temp = obj->matches + (obj->matches_num - 1);
248 temp->user_data = user_data;
249 temp->submit = submit_match;
250 temp->free = free_user_data;
253 } /* int tail_match_add_match */
255 int tail_match_add_match_simple(cu_tail_match_t *obj, const char *regex,
256 const char *excluderegex, int ds_type,
257 const char *plugin, const char *plugin_instance,
258 const char *type, const char *type_instance,
259 const latency_config_t latency_cfg) {
261 cu_tail_match_simple_t *user_data;
264 match = match_create_simple(regex, excluderegex, ds_type);
268 user_data = calloc(1, sizeof(*user_data));
269 if (user_data == NULL) {
270 match_destroy(match);
274 sstrncpy(user_data->plugin, plugin, sizeof(user_data->plugin));
275 if (plugin_instance != NULL)
276 sstrncpy(user_data->plugin_instance, plugin_instance,
277 sizeof(user_data->plugin_instance));
279 sstrncpy(user_data->type, type, sizeof(user_data->type));
280 if (type_instance != NULL)
281 sstrncpy(user_data->type_instance, type_instance,
282 sizeof(user_data->type_instance));
284 if ((ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
285 (ds_type & UTILS_MATCH_CF_GAUGE_DIST)) {
286 status = latency_config_copy(&user_data->latency_config, latency_cfg);
288 ERROR("tail_match_add_match_simple: latency_config_copy() failed.");
293 status = tail_match_add_match(obj, match, latency_submit_match, user_data,
294 tail_match_simple_free);
297 tail_match_add_match(obj, match, simple_submit_match, user_data, free);
302 tail_match_simple_free(user_data);
303 match_destroy(match);
307 } /* int tail_match_add_match_simple */
309 int tail_match_read(cu_tail_match_t *obj) {
313 status = cu_tail_read(obj->tail, buffer, sizeof(buffer), tail_callback,
316 ERROR("tail_match: cu_tail_read failed.");
320 for (size_t i = 0; i < obj->matches_num; i++) {
321 cu_tail_match_match_t *lt_match = obj->matches + i;
323 if (lt_match->submit == NULL)
326 (*lt_match->submit)(lt_match->match, lt_match->user_data);
330 } /* int tail_match_read */