Fix compile time issues
[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 "plugin.h"
35 #include "utils/common/common.h"
36 #include "utils/latency/latency_config.h"
37 #include "utils/match/match.h"
38 #include "utils/tail/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   latency_config_t latency_config;
47 };
48 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
49
50 struct cu_tail_match_match_s {
51   cu_match_t *match;
52   void *user_data;
53   int (*submit)(cu_match_t *match, void *user_data);
54   void (*free)(void *user_data);
55 };
56 typedef struct cu_tail_match_match_s cu_tail_match_match_t;
57
58 struct cu_tail_match_s {
59   cu_tail_t *tail;
60   cu_tail_match_match_t *matches;
61   size_t matches_num;
62 };
63
64 /*
65  * Private functions
66  */
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;
71   value_t values[1];
72
73   match_value = (cu_match_value_t *)match_get_user_data(match);
74   if (match_value == NULL)
75     return -1;
76
77   if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE) &&
78       (match_value->values_num == 0))
79     values[0].gauge = NAN;
80   else
81     values[0] = match_value->value;
82
83   vl.values = values;
84   vl.values_len = 1;
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));
90
91   plugin_dispatch_values(&vl);
92
93   match_value_reset(match_value);
94   return 0;
95 } /* int simple_submit_match */
96
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;
101
102   match_value = (cu_match_value_t *)match_get_user_data(match);
103   if (match_value == NULL)
104     return -1;
105
106   sstrncpy(vl.plugin, data->plugin, sizeof(vl.plugin));
107   sstrncpy(vl.plugin_instance, data->plugin_instance,
108            sizeof(vl.plugin_instance));
109   vl.time = cdtime();
110
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]);
117     else
118       snprintf(vl.type_instance, sizeof(vl.type_instance), "%.5g",
119                data->latency_config.percentile[i]);
120
121     vl.values = &(value_t){
122         .gauge =
123             (match_value->values_num != 0)
124                 ? CDTIME_T_TO_DOUBLE(latency_counter_get_percentile(
125                       match_value->latency, data->latency_config.percentile[i]))
126                 : NAN,
127     };
128     vl.values_len = 1;
129
130     plugin_dispatch_values(&vl);
131   }
132
133   /* Submit buckets */
134   if (data->latency_config.bucket_type != NULL)
135     sstrncpy(vl.type, data->latency_config.bucket_type, sizeof(vl.type));
136   else
137     sstrncpy(vl.type, "bucket", sizeof(vl.type));
138
139   for (size_t i = 0; i < data->latency_config.buckets_num; i++) {
140     latency_bucket_t bucket = data->latency_config.buckets[i];
141
142     double lower_bound = CDTIME_T_TO_DOUBLE(bucket.lower_bound);
143     double upper_bound =
144         bucket.upper_bound ? CDTIME_T_TO_DOUBLE(bucket.upper_bound) : INFINITY;
145
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);
149     else
150       snprintf(vl.type_instance, sizeof(vl.type_instance), "%.50s-%g_%g",
151                data->type, lower_bound, upper_bound);
152
153     vl.values = &(value_t){
154         .gauge =
155             latency_counter_get_rate(match_value->latency, bucket.lower_bound,
156                                      bucket.upper_bound, vl.time),
157     };
158     vl.values_len = 1;
159
160     plugin_dispatch_values(&vl);
161   }
162
163   match_value->value.gauge = NAN;
164   match_value->values_num = 0;
165   latency_counter_reset(match_value->latency);
166
167   return 0;
168 } /* int latency_submit_match */
169
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;
173
174   for (size_t i = 0; i < obj->matches_num; i++)
175     match_apply(obj->matches[i].match, buf);
176
177   return 0;
178 } /* int tail_callback */
179
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);
183   sfree(user_data);
184 } /* void tail_match_simple_free */
185
186 /*
187  * Public functions
188  */
189 cu_tail_match_t *tail_match_create(const char *filename) {
190   cu_tail_match_t *obj;
191
192   obj = calloc(1, sizeof(*obj));
193   if (obj == NULL)
194     return NULL;
195
196   obj->tail = cu_tail_create(filename);
197   if (obj->tail == NULL) {
198     sfree(obj);
199     return NULL;
200   }
201
202   return obj;
203 } /* cu_tail_match_t *tail_match_create */
204
205 void tail_match_destroy(cu_tail_match_t *obj) {
206   if (obj == NULL)
207     return;
208
209   if (obj->tail != NULL) {
210     cu_tail_destroy(obj->tail);
211     obj->tail = NULL;
212   }
213
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);
218       match->match = NULL;
219     }
220
221     if ((match->user_data != NULL) && (match->free != NULL))
222       (*match->free)(match->user_data);
223     match->user_data = NULL;
224   }
225
226   sfree(obj->matches);
227   sfree(obj);
228 } /* void tail_match_destroy */
229
230 int tail_match_add_match(cu_tail_match_t *obj, cu_match_t *match,
231                          int (*submit_match)(cu_match_t *match,
232                                              void *user_data),
233                          void *user_data,
234                          void (*free_user_data)(void *user_data)) {
235   cu_tail_match_match_t *temp;
236
237   temp = realloc(obj->matches,
238                  sizeof(cu_tail_match_match_t) * (obj->matches_num + 1));
239   if (temp == NULL)
240     return -1;
241
242   obj->matches = temp;
243   obj->matches_num++;
244
245   temp = obj->matches + (obj->matches_num - 1);
246
247   temp->match = match;
248   temp->user_data = user_data;
249   temp->submit = submit_match;
250   temp->free = free_user_data;
251
252   return 0;
253 } /* int tail_match_add_match */
254
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) {
260   cu_match_t *match;
261   cu_tail_match_simple_t *user_data;
262   int status;
263
264   match = match_create_simple(regex, excluderegex, ds_type);
265   if (match == NULL)
266     return -1;
267
268   user_data = calloc(1, sizeof(*user_data));
269   if (user_data == NULL) {
270     match_destroy(match);
271     return -1;
272   }
273
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));
278
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));
283
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);
287     if (status != 0) {
288       ERROR("tail_match_add_match_simple: latency_config_copy() failed.");
289       status = -1;
290       goto out;
291     }
292
293     status = tail_match_add_match(obj, match, latency_submit_match, user_data,
294                                   tail_match_simple_free);
295   } else {
296     status =
297         tail_match_add_match(obj, match, simple_submit_match, user_data, free);
298   }
299
300 out:
301   if (status != 0) {
302     tail_match_simple_free(user_data);
303     match_destroy(match);
304   }
305
306   return status;
307 } /* int tail_match_add_match_simple */
308
309 int tail_match_read(cu_tail_match_t *obj) {
310   char buffer[4096];
311   int status;
312
313   status = cu_tail_read(obj->tail, buffer, sizeof(buffer), tail_callback,
314                         (void *)obj);
315   if (status != 0) {
316     ERROR("tail_match: cu_tail_read failed.");
317     return status;
318   }
319
320   for (size_t i = 0; i < obj->matches_num; i++) {
321     cu_tail_match_match_t *lt_match = obj->matches + i;
322
323     if (lt_match->submit == NULL)
324       continue;
325
326     (*lt_match->submit)(lt_match->match, lt_match->user_data);
327   }
328
329   return 0;
330 } /* int tail_match_read */