Merge pull request #2378 from cramertj/grpc-meta
[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.host, hostname_g, sizeof(vl.host));
112   sstrncpy(vl.plugin, data->plugin, sizeof(vl.plugin));
113   sstrncpy(vl.plugin_instance, data->plugin_instance,
114            sizeof(vl.plugin_instance));
115   vl.interval = data->interval;
116   vl.time = cdtime();
117
118   /* Submit percentiles */
119   sstrncpy(vl.type, data->type, sizeof(vl.type));
120   for (size_t i = 0; i < data->latency_config.percentile_num; i++) {
121     if (strlen(data->type_instance) != 0)
122       snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%.0f",
123                data->type_instance, data->latency_config.percentile[i]);
124     else
125       snprintf(vl.type_instance, sizeof(vl.type_instance), "%.0f",
126                data->latency_config.percentile[i]);
127
128     vl.values = &(value_t){
129         .gauge =
130             (match_value->values_num != 0)
131                 ? CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(
132                       match_value->latency, data->latency_config.percentile[i]))
133                 : NAN,
134     };
135     vl.values_len = 1;
136
137     plugin_dispatch_values(&vl);
138   }
139
140   /* Submit buckets */
141   sstrncpy(vl.type, "bucket", sizeof(vl.type));
142   for (size_t i = 0; i < data->latency_config.buckets_num; i++) {
143     latency_bucket_t bucket = data->latency_config.buckets[i];
144
145     double lower_bound = CDTIME_T_TO_DOUBLE(bucket.lower_bound);
146     double upper_bound =
147         bucket.upper_bound ? CDTIME_T_TO_DOUBLE(bucket.upper_bound) : INFINITY;
148
149     if (strlen(data->type_instance) != 0)
150       snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s-%g_%g",
151                data->type, data->type_instance, lower_bound, upper_bound);
152     else
153       snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%g_%g",
154                data->type, lower_bound, upper_bound);
155
156     vl.values = &(value_t){
157         .gauge =
158             latency_counter_get_rate(match_value->latency, bucket.lower_bound,
159                                      bucket.upper_bound, vl.time),
160     };
161     vl.values_len = 1;
162
163     plugin_dispatch_values(&vl);
164   }
165
166   match_value->value.gauge = NAN;
167   match_value->values_num = 0;
168   latency_counter_reset(match_value->latency);
169
170   return 0;
171 } /* int latency_submit_match */
172
173 static int tail_callback(void *data, char *buf,
174                          int __attribute__((unused)) buflen) {
175   cu_tail_match_t *obj = (cu_tail_match_t *)data;
176
177   for (size_t i = 0; i < obj->matches_num; i++)
178     match_apply(obj->matches[i].match, buf);
179
180   return 0;
181 } /* int tail_callback */
182
183 static void tail_match_simple_free(void *data) {
184   cu_tail_match_simple_t *user_data = (cu_tail_match_simple_t *)data;
185   latency_config_free(user_data->latency_config);
186   sfree(user_data);
187 } /* void tail_match_simple_free */
188
189 /*
190  * Public functions
191  */
192 cu_tail_match_t *tail_match_create(const char *filename) {
193   cu_tail_match_t *obj;
194
195   obj = calloc(1, sizeof(*obj));
196   if (obj == NULL)
197     return NULL;
198
199   obj->tail = cu_tail_create(filename);
200   if (obj->tail == NULL) {
201     sfree(obj);
202     return NULL;
203   }
204
205   return obj;
206 } /* cu_tail_match_t *tail_match_create */
207
208 void tail_match_destroy(cu_tail_match_t *obj) {
209   if (obj == NULL)
210     return;
211
212   if (obj->tail != NULL) {
213     cu_tail_destroy(obj->tail);
214     obj->tail = NULL;
215   }
216
217   for (size_t i = 0; i < obj->matches_num; i++) {
218     cu_tail_match_match_t *match = obj->matches + i;
219     if (match->match != NULL) {
220       match_destroy(match->match);
221       match->match = NULL;
222     }
223
224     if ((match->user_data != NULL) && (match->free != NULL))
225       (*match->free)(match->user_data);
226     match->user_data = NULL;
227   }
228
229   sfree(obj->matches);
230   sfree(obj);
231 } /* void tail_match_destroy */
232
233 int tail_match_add_match(cu_tail_match_t *obj, cu_match_t *match,
234                          int (*submit_match)(cu_match_t *match,
235                                              void *user_data),
236                          void *user_data,
237                          void (*free_user_data)(void *user_data)) {
238   cu_tail_match_match_t *temp;
239
240   temp = realloc(obj->matches,
241                  sizeof(cu_tail_match_match_t) * (obj->matches_num + 1));
242   if (temp == NULL)
243     return -1;
244
245   obj->matches = temp;
246   obj->matches_num++;
247
248   DEBUG("tail_match_add_match interval %lf",
249         CDTIME_T_TO_DOUBLE(((cu_tail_match_simple_t *)user_data)->interval));
250   temp = obj->matches + (obj->matches_num - 1);
251
252   temp->match = match;
253   temp->user_data = user_data;
254   temp->submit = submit_match;
255   temp->free = free_user_data;
256
257   return 0;
258 } /* int tail_match_add_match */
259
260 int tail_match_add_match_simple(cu_tail_match_t *obj, const char *regex,
261                                 const char *excluderegex, int ds_type,
262                                 const char *plugin, const char *plugin_instance,
263                                 const char *type, const char *type_instance,
264                                 const latency_config_t latency_cfg,
265                                 const cdtime_t interval) {
266   cu_match_t *match;
267   cu_tail_match_simple_t *user_data;
268   int status;
269
270   match = match_create_simple(regex, excluderegex, ds_type);
271   if (match == NULL)
272     return -1;
273
274   user_data = calloc(1, sizeof(*user_data));
275   if (user_data == NULL) {
276     match_destroy(match);
277     return -1;
278   }
279
280   sstrncpy(user_data->plugin, plugin, sizeof(user_data->plugin));
281   if (plugin_instance != NULL)
282     sstrncpy(user_data->plugin_instance, plugin_instance,
283              sizeof(user_data->plugin_instance));
284
285   sstrncpy(user_data->type, type, sizeof(user_data->type));
286   if (type_instance != NULL)
287     sstrncpy(user_data->type_instance, type_instance,
288              sizeof(user_data->type_instance));
289
290   user_data->interval = interval;
291
292   if ((ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
293       (ds_type & UTILS_MATCH_CF_GAUGE_DIST)) {
294     status = latency_config_copy(&user_data->latency_config, latency_cfg);
295     if (status != 0) {
296       ERROR("tail_match_add_match_simple: latency_config_copy() failed.");
297       status = -1;
298       goto out;
299     }
300
301     status = tail_match_add_match(obj, match, latency_submit_match, user_data,
302                                   tail_match_simple_free);
303   } else {
304     status =
305         tail_match_add_match(obj, match, simple_submit_match, user_data, free);
306   }
307
308 out:
309   if (status != 0) {
310     tail_match_simple_free(user_data);
311     match_destroy(match);
312   }
313
314   return status;
315 } /* int tail_match_add_match_simple */
316
317 int tail_match_read(cu_tail_match_t *obj) {
318   char buffer[4096];
319   int status;
320
321   status = cu_tail_read(obj->tail, buffer, sizeof(buffer), tail_callback,
322                         (void *)obj);
323   if (status != 0) {
324     ERROR("tail_match: cu_tail_read failed.");
325     return status;
326   }
327
328   for (size_t i = 0; i < obj->matches_num; i++) {
329     cu_tail_match_match_t *lt_match = obj->matches + i;
330
331     if (lt_match->submit == NULL)
332       continue;
333
334     (*lt_match->submit)(lt_match->match, lt_match->user_data);
335   }
336
337   return 0;
338 } /* int tail_match_read */