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