Merge remote-tracking branch 'github/pr/2412'
[collectd.git] / src / utils_tail_match.c
1 /*
2  * collectd - src/utils_tail_match.c
3  * Copyright (C) 2007-2008  C-Ware, Inc.
4  * Copyright (C) 2008       Florian Forster
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  *
24  * Author:
25  *   Luke Heberling <lukeh at c-ware.com>
26  *   Florian Forster <octo at collectd.org>
27  *
28  * Description:
29  *   Encapsulates useful code to plugins which must parse a log file.
30  */
31
32 #include "collectd.h"
33
34 #include "common.h"
35 #include "plugin.h"
36 #include "utils_latency_config.h"
37 #include "utils_match.h"
38 #include "utils_tail.h"
39 #include "utils_tail_match.h"
40
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   cdtime_t interval;
47   latency_config_t latency_config;
48 };
49 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
50
51 struct cu_tail_match_match_s {
52   cu_match_t *match;
53   void *user_data;
54   int (*submit)(cu_match_t *match, void *user_data);
55   void (*free)(void *user_data);
56 };
57 typedef struct cu_tail_match_match_s cu_tail_match_match_t;
58
59 struct cu_tail_match_s {
60   int flags;
61   cu_tail_t *tail;
62
63   cdtime_t interval;
64   cu_tail_match_match_t *matches;
65   size_t matches_num;
66 };
67
68 /*
69  * Private functions
70  */
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;
75   value_t values[1];
76
77   match_value = (cu_match_value_t *)match_get_user_data(match);
78   if (match_value == NULL)
79     return -1;
80
81   if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
82       (match_value->values_num == 0))
83     values[0].gauge = NAN;
84   else
85     values[0] = match_value->value;
86
87   vl.values = values;
88   vl.values_len = 1;
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));
94
95   vl.interval = data->interval;
96   plugin_dispatch_values(&vl);
97
98   match_value_reset(match_value);
99   return 0;
100 } /* int simple_submit_match */
101
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;
106
107   match_value = (cu_match_value_t *)match_get_user_data(match);
108   if (match_value == NULL)
109     return -1;
110
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;
115   vl.time = cdtime();
116
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), "%s-%.0f",
122                data->type_instance, data->latency_config.percentile[i]);
123     else
124       snprintf(vl.type_instance, sizeof(vl.type_instance), "%.0f",
125                data->latency_config.percentile[i]);
126
127     vl.values = &(value_t){
128         .gauge =
129             (match_value->values_num != 0)
130                 ? CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(
131                       match_value->latency, data->latency_config.percentile[i]))
132                 : NAN,
133     };
134     vl.values_len = 1;
135
136     plugin_dispatch_values(&vl);
137   }
138
139   /* Submit buckets */
140   sstrncpy(vl.type, "bucket", sizeof(vl.type));
141   for (size_t i = 0; i < data->latency_config.buckets_num; i++) {
142     latency_bucket_t bucket = data->latency_config.buckets[i];
143
144     double lower_bound = CDTIME_T_TO_DOUBLE(bucket.lower_bound);
145     double upper_bound =
146         bucket.upper_bound ? CDTIME_T_TO_DOUBLE(bucket.upper_bound) : INFINITY;
147
148     if (strlen(data->type_instance) != 0)
149       snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s-%g_%g",
150                data->type, data->type_instance, lower_bound, upper_bound);
151     else
152       snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%g_%g",
153                data->type, lower_bound, upper_bound);
154
155     vl.values = &(value_t){
156         .gauge =
157             latency_counter_get_rate(match_value->latency, bucket.lower_bound,
158                                      bucket.upper_bound, vl.time),
159     };
160     vl.values_len = 1;
161
162     plugin_dispatch_values(&vl);
163   }
164
165   match_value->value.gauge = NAN;
166   match_value->values_num = 0;
167   latency_counter_reset(match_value->latency);
168
169   return 0;
170 } /* int latency_submit_match */
171
172 static int tail_callback(void *data, char *buf,
173                          int __attribute__((unused)) buflen) {
174   cu_tail_match_t *obj = (cu_tail_match_t *)data;
175
176   for (size_t i = 0; i < obj->matches_num; i++)
177     match_apply(obj->matches[i].match, buf);
178
179   return 0;
180 } /* int tail_callback */
181
182 static void tail_match_simple_free(void *data) {
183   cu_tail_match_simple_t *user_data = (cu_tail_match_simple_t *)data;
184   latency_config_free(user_data->latency_config);
185   sfree(user_data);
186 } /* void tail_match_simple_free */
187
188 /*
189  * Public functions
190  */
191 cu_tail_match_t *tail_match_create(const char *filename) {
192   cu_tail_match_t *obj;
193
194   obj = calloc(1, sizeof(*obj));
195   if (obj == NULL)
196     return NULL;
197
198   obj->tail = cu_tail_create(filename);
199   if (obj->tail == NULL) {
200     sfree(obj);
201     return NULL;
202   }
203
204   return obj;
205 } /* cu_tail_match_t *tail_match_create */
206
207 void tail_match_destroy(cu_tail_match_t *obj) {
208   if (obj == NULL)
209     return;
210
211   if (obj->tail != NULL) {
212     cu_tail_destroy(obj->tail);
213     obj->tail = NULL;
214   }
215
216   for (size_t i = 0; i < obj->matches_num; i++) {
217     cu_tail_match_match_t *match = obj->matches + i;
218     if (match->match != NULL) {
219       match_destroy(match->match);
220       match->match = NULL;
221     }
222
223     if ((match->user_data != NULL) && (match->free != NULL))
224       (*match->free)(match->user_data);
225     match->user_data = NULL;
226   }
227
228   sfree(obj->matches);
229   sfree(obj);
230 } /* void tail_match_destroy */
231
232 int tail_match_add_match(cu_tail_match_t *obj, cu_match_t *match,
233                          int (*submit_match)(cu_match_t *match,
234                                              void *user_data),
235                          void *user_data,
236                          void (*free_user_data)(void *user_data)) {
237   cu_tail_match_match_t *temp;
238
239   temp = realloc(obj->matches,
240                  sizeof(cu_tail_match_match_t) * (obj->matches_num + 1));
241   if (temp == NULL)
242     return -1;
243
244   obj->matches = temp;
245   obj->matches_num++;
246
247   DEBUG("tail_match_add_match interval %lf",
248         CDTIME_T_TO_DOUBLE(((cu_tail_match_simple_t *)user_data)->interval));
249   temp = obj->matches + (obj->matches_num - 1);
250
251   temp->match = match;
252   temp->user_data = user_data;
253   temp->submit = submit_match;
254   temp->free = free_user_data;
255
256   return 0;
257 } /* int tail_match_add_match */
258
259 int tail_match_add_match_simple(cu_tail_match_t *obj, const char *regex,
260                                 const char *excluderegex, int ds_type,
261                                 const char *plugin, const char *plugin_instance,
262                                 const char *type, const char *type_instance,
263                                 const latency_config_t latency_cfg,
264                                 const cdtime_t interval) {
265   cu_match_t *match;
266   cu_tail_match_simple_t *user_data;
267   int status;
268
269   match = match_create_simple(regex, excluderegex, ds_type);
270   if (match == NULL)
271     return -1;
272
273   user_data = calloc(1, sizeof(*user_data));
274   if (user_data == NULL) {
275     match_destroy(match);
276     return -1;
277   }
278
279   sstrncpy(user_data->plugin, plugin, sizeof(user_data->plugin));
280   if (plugin_instance != NULL)
281     sstrncpy(user_data->plugin_instance, plugin_instance,
282              sizeof(user_data->plugin_instance));
283
284   sstrncpy(user_data->type, type, sizeof(user_data->type));
285   if (type_instance != NULL)
286     sstrncpy(user_data->type_instance, type_instance,
287              sizeof(user_data->type_instance));
288
289   user_data->interval = interval;
290
291   if ((ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
292       (ds_type & UTILS_MATCH_CF_GAUGE_DIST)) {
293     status = latency_config_copy(&user_data->latency_config, latency_cfg);
294     if (status != 0) {
295       ERROR("tail_match_add_match_simple: latency_config_copy() failed.");
296       status = -1;
297       goto out;
298     }
299
300     status = tail_match_add_match(obj, match, latency_submit_match, user_data,
301                                   tail_match_simple_free);
302   } else {
303     status =
304         tail_match_add_match(obj, match, simple_submit_match, user_data, free);
305   }
306
307 out:
308   if (status != 0) {
309     tail_match_simple_free(user_data);
310     match_destroy(match);
311   }
312
313   return status;
314 } /* int tail_match_add_match_simple */
315
316 int tail_match_read(cu_tail_match_t *obj) {
317   char buffer[4096];
318   int status;
319
320   status = cu_tail_read(obj->tail, buffer, sizeof(buffer), tail_callback,
321                         (void *)obj);
322   if (status != 0) {
323     ERROR("tail_match: cu_tail_read failed.");
324     return status;
325   }
326
327   for (size_t i = 0; i < obj->matches_num; i++) {
328     cu_tail_match_match_t *lt_match = obj->matches + i;
329
330     if (lt_match->submit == NULL)
331       continue;
332
333     (*lt_match->submit)(lt_match->match, lt_match->user_data);
334   }
335
336   return 0;
337 } /* int tail_match_read */