0be8cfe28e26cda47cc03d173d0d1b734b1deb9e
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 */
32 #include "collectd.h"
34 #include "common.h"
35 #include "plugin.h"
36 #include "utils_match.h"
37 #include "utils_tail.h"
38 #include "utils_tail_match.h"
39 #include "utils_latency_config.h"
41 struct cu_tail_match_simple_s
42 {
43 char plugin[DATA_MAX_NAME_LEN];
44 char plugin_instance[DATA_MAX_NAME_LEN];
45 char type[DATA_MAX_NAME_LEN];
46 char type_instance[DATA_MAX_NAME_LEN];
47 cdtime_t interval;
48 latency_config_t latency_config;
49 };
50 typedef struct cu_tail_match_simple_s cu_tail_match_simple_t;
52 struct cu_tail_match_match_s
53 {
54 cu_match_t *match;
55 void *user_data;
56 int (*submit) (cu_match_t *match, void *user_data);
57 void (*free) (void *user_data);
58 };
59 typedef struct cu_tail_match_match_s cu_tail_match_match_t;
61 struct cu_tail_match_s
62 {
63 int flags;
64 cu_tail_t *tail;
66 cdtime_t interval;
67 cu_tail_match_match_t *matches;
68 size_t matches_num;
69 };
71 /*
72 * Private functions
73 */
74 static int simple_submit_match (cu_match_t *match, void *user_data)
75 {
76 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *) user_data;
77 cu_match_value_t *match_value;
78 value_list_t vl = VALUE_LIST_INIT;
79 value_t values[1];
81 match_value = (cu_match_value_t *) match_get_user_data (match);
82 if (match_value == NULL)
83 return (-1);
85 if ((match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
86 && (match_value->values_num == 0))
87 values[0].gauge = NAN;
88 else
89 values[0] = match_value->value;
91 vl.values = values;
92 vl.values_len = 1;
93 sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
94 sstrncpy (vl.plugin_instance, data->plugin_instance,
95 sizeof (vl.plugin_instance));
96 sstrncpy (vl.type, data->type, sizeof (vl.type));
97 sstrncpy (vl.type_instance, data->type_instance,
98 sizeof (vl.type_instance));
100 vl.interval = data->interval;
101 plugin_dispatch_values (&vl);
103 match_value_reset (match_value);
104 return (0);
105 } /* int simple_submit_match */
107 static int simple_submit_latency (cu_match_t *match, void *user_data)
108 {
109 cu_tail_match_simple_t *data = (cu_tail_match_simple_t *) user_data;
110 cu_match_value_t *match_value;
111 value_list_t vl = VALUE_LIST_INIT;
112 value_t values[1];
114 match_value = (cu_match_value_t *) match_get_user_data (match);
115 if (match_value == NULL)
116 return (-1);
118 vl.values = values;
119 vl.values_len = 1;
120 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
121 sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
122 sstrncpy (vl.plugin_instance, data->plugin_instance,
123 sizeof (vl.plugin_instance));
124 sstrncpy (vl.type, data->type, sizeof (vl.type));
125 vl.interval = data->interval;
126 vl.time = cdtime ();
128 if (data->latency_config.lower) {
129 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
130 "lower");
131 values[0].gauge = (match_value->values_num != 0)
132 ? CDTIME_T_TO_DOUBLE (latency_counter_get_min (match_value->latency))
133 : NAN;
134 plugin_dispatch_values (&vl);
135 }
137 if (data->latency_config.avg) {
138 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
139 "average");
140 values[0].gauge = (match_value->values_num != 0)
141 ? CDTIME_T_TO_DOUBLE (latency_counter_get_average (match_value->latency))
142 : NAN;
143 plugin_dispatch_values (&vl);
144 }
146 if (data->latency_config.upper) {
147 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
148 "upper");
149 values[0].gauge = (match_value->values_num != 0)
150 ? CDTIME_T_TO_DOUBLE (latency_counter_get_max (match_value->latency))
151 : NAN;
152 plugin_dispatch_values (&vl);
153 }
155 size_t i;
156 /* Submit percentiles */
157 if (data->latency_config.percentile_type != NULL)
158 sstrncpy (vl.type, data->latency_config.percentile_type, sizeof (vl.type));
159 for (i = 0; i < data->latency_config.percentile_num; i++)
160 {
161 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
162 "percentile-%.0f", data->latency_config.percentile[i]);
163 values[0].gauge = (match_value->values_num != 0)
164 ? CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (match_value->latency,
165 data->latency_config.percentile[i]))
166 : NAN;
167 plugin_dispatch_values (&vl);
168 }
170 /* Submit rates */
171 sstrncpy (vl.type, data->type, sizeof (vl.type));
172 if (data->latency_config.rates_type != NULL)
173 sstrncpy (vl.type, data->latency_config.rates_type, sizeof (vl.type));
174 for (i = 0; i < data->latency_config.rates_num; i++)
175 {
176 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
177 "rate-%.3f-%.3f",
178 CDTIME_T_TO_DOUBLE(data->latency_config.rates[i * 2]),
179 CDTIME_T_TO_DOUBLE(data->latency_config.rates[i * 2 + 1]));
180 values[0].gauge = (match_value->values_num != 0)
181 ? latency_counter_get_rate (match_value->latency,
182 data->latency_config.rates[i * 2],
183 data->latency_config.rates[i * 2 + 1],
184 vl.time)
185 : NAN;
186 plugin_dispatch_values (&vl);
187 }
188 latency_counter_reset (match_value->latency);
190 match_value->value.gauge = NAN;
191 match_value->values_num = 0;
193 return (0);
194 } /* int simple_submit_latency */
196 static int tail_callback (void *data, char *buf,
197 int __attribute__((unused)) buflen)
198 {
199 cu_tail_match_t *obj = (cu_tail_match_t *) data;
201 for (size_t i = 0; i < obj->matches_num; i++)
202 match_apply (obj->matches[i].match, buf);
204 return (0);
205 } /* int tail_callback */
207 static void tail_match_simple_free (void *data)
208 {
209 cu_tail_match_simple_t *user_data = (cu_tail_match_simple_t *) data;
210 latency_config_free(user_data->latency_config);
211 sfree (user_data);
212 } /* void tail_match_simple_free */
214 /*
215 * Public functions
216 */
217 cu_tail_match_t *tail_match_create (const char *filename)
218 {
219 cu_tail_match_t *obj;
221 obj = calloc (1, sizeof (*obj));
222 if (obj == NULL)
223 return (NULL);
225 obj->tail = cu_tail_create (filename);
226 if (obj->tail == NULL)
227 {
228 sfree (obj);
229 return (NULL);
230 }
232 return (obj);
233 } /* cu_tail_match_t *tail_match_create */
235 void tail_match_destroy (cu_tail_match_t *obj)
236 {
237 if (obj == NULL)
238 return;
240 if (obj->tail != NULL)
241 {
242 cu_tail_destroy (obj->tail);
243 obj->tail = NULL;
244 }
246 for (size_t i = 0; i < obj->matches_num; i++)
247 {
248 cu_tail_match_match_t *match = obj->matches + i;
249 if (match->match != NULL)
250 {
251 match_destroy (match->match);
252 match->match = NULL;
253 }
255 if ((match->user_data != NULL)
256 && (match->free != NULL))
257 (*match->free) (match->user_data);
258 match->user_data = NULL;
259 }
261 sfree (obj->matches);
262 sfree (obj);
263 } /* void tail_match_destroy */
265 int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
266 int (*submit_match) (cu_match_t *match, void *user_data),
267 void *user_data,
268 void (*free_user_data) (void *user_data))
269 {
270 cu_tail_match_match_t *temp;
272 temp = realloc (obj->matches,
273 sizeof (cu_tail_match_match_t) * (obj->matches_num + 1));
274 if (temp == NULL)
275 return (-1);
277 obj->matches = temp;
278 obj->matches_num++;
280 DEBUG ("tail_match_add_match interval %lf", CDTIME_T_TO_DOUBLE(((cu_tail_match_simple_t *)user_data)->interval));
281 temp = obj->matches + (obj->matches_num - 1);
283 temp->match = match;
284 temp->user_data = user_data;
285 temp->submit = submit_match;
286 temp->free = free_user_data;
288 return (0);
289 } /* int tail_match_add_match */
291 int tail_match_add_match_simple (cu_tail_match_t *obj,
292 const char *regex, const char *excluderegex, int ds_type,
293 const char *plugin, const char *plugin_instance,
294 const char *type, const char *type_instance,
295 const latency_config_t latency_cfg,
296 const cdtime_t interval)
297 {
298 cu_match_t *match;
299 cu_tail_match_simple_t *user_data;
300 int status;
302 match = match_create_simple (regex, excluderegex, ds_type);
303 if (match == NULL)
304 return (-1);
306 user_data = calloc (1, sizeof (*user_data));
307 if (user_data == NULL)
308 {
309 match_destroy (match);
310 return (-1);
311 }
313 sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin));
314 if (plugin_instance != NULL)
315 sstrncpy (user_data->plugin_instance, plugin_instance,
316 sizeof (user_data->plugin_instance));
318 sstrncpy (user_data->type, type, sizeof (user_data->type));
319 if (type_instance != NULL)
320 sstrncpy (user_data->type_instance, type_instance,
321 sizeof (user_data->type_instance));
323 user_data->interval = interval;
325 if ((ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
326 && (ds_type & UTILS_MATCH_CF_GAUGE_LATENCY))
327 {
328 status = latency_config_copy(&user_data->latency_config, latency_cfg);
329 if (status != 0)
330 {
331 ERROR ("tail_match_add_match_simple: latency_config_copy() failed.");
332 status = -1;
333 goto out;
334 }
336 status = tail_match_add_match (obj, match, simple_submit_latency,
337 user_data, tail_match_simple_free);
338 } else {
339 status = tail_match_add_match (obj, match, simple_submit_match,
340 user_data, free);
341 }
343 out:
344 if (status != 0)
345 {
346 tail_match_simple_free(user_data);
347 match_destroy (match);
348 }
350 return (status);
351 } /* int tail_match_add_match_simple */
353 int tail_match_read (cu_tail_match_t *obj)
354 {
355 char buffer[4096];
356 int status;
358 status = cu_tail_read (obj->tail, buffer, sizeof (buffer), tail_callback,
359 (void *) obj);
360 if (status != 0)
361 {
362 ERROR ("tail_match: cu_tail_read failed.");
363 return (status);
364 }
366 for (size_t i = 0; i < obj->matches_num; i++)
367 {
368 cu_tail_match_match_t *lt_match = obj->matches + i;
370 if (lt_match->submit == NULL)
371 continue;
373 (*lt_match->submit) (lt_match->match, lt_match->user_data);
374 }
376 return (0);
377 } /* int tail_match_read */
379 /* vim: set sw=2 sts=2 ts=8 : */