1 /**
2 * collectd - src/aggregation.c
3 * Copyright (C) 2012 Florian Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include <pthread.h>
31 #include "plugin.h"
32 #include "common.h"
33 #include "configfile.h"
34 #include "meta_data.h"
35 #include "utils_cache.h" /* for uc_get_rate() */
36 #include "utils_subst.h"
37 #include "utils_vl_lookup.h"
39 #define AGG_MATCHES_ALL(str) (strcmp ("/.*/", str) == 0)
40 #define AGG_FUNC_PLACEHOLDER "%{aggregation}"
42 struct aggregation_s /* {{{ */
43 {
44 identifier_t ident;
45 unsigned int group_by;
47 unsigned int regex_fields;
49 char *set_host;
50 char *set_plugin;
51 char *set_plugin_instance;
52 char *set_type_instance;
54 _Bool calc_num;
55 _Bool calc_sum;
56 _Bool calc_average;
57 _Bool calc_min;
58 _Bool calc_max;
59 _Bool calc_stddev;
60 }; /* }}} */
61 typedef struct aggregation_s aggregation_t;
63 struct agg_instance_s;
64 typedef struct agg_instance_s agg_instance_t;
65 struct agg_instance_s /* {{{ */
66 {
67 pthread_mutex_t lock;
68 identifier_t ident;
70 int ds_type;
72 derive_t num;
73 gauge_t sum;
74 gauge_t squares_sum;
76 gauge_t min;
77 gauge_t max;
79 rate_to_value_state_t *state_num;
80 rate_to_value_state_t *state_sum;
81 rate_to_value_state_t *state_average;
82 rate_to_value_state_t *state_min;
83 rate_to_value_state_t *state_max;
84 rate_to_value_state_t *state_stddev;
86 agg_instance_t *next;
87 }; /* }}} */
89 static lookup_t *lookup = NULL;
91 static pthread_mutex_t agg_instance_list_lock = PTHREAD_MUTEX_INITIALIZER;
92 static agg_instance_t *agg_instance_list_head = NULL;
94 static _Bool agg_is_regex (char const *str) /* {{{ */
95 {
96 size_t len;
98 if (str == NULL)
99 return (0);
101 len = strlen (str);
102 if (len < 3)
103 return (0);
105 if ((str[0] == '/') && (str[len - 1] == '/'))
106 return (1);
107 else
108 return (0);
109 } /* }}} _Bool agg_is_regex */
111 static void agg_destroy (aggregation_t *agg) /* {{{ */
112 {
113 sfree (agg);
114 } /* }}} void agg_destroy */
116 /* Frees all dynamically allocated memory within the instance. */
117 static void agg_instance_destroy (agg_instance_t *inst) /* {{{ */
118 {
119 if (inst == NULL)
120 return;
122 /* Remove this instance from the global list of instances. */
123 pthread_mutex_lock (&agg_instance_list_lock);
124 if (agg_instance_list_head == inst)
125 agg_instance_list_head = inst->next;
126 else if (agg_instance_list_head != NULL)
127 {
128 agg_instance_t *prev = agg_instance_list_head;
129 while ((prev != NULL) && (prev->next != inst))
130 prev = prev->next;
131 if (prev != NULL)
132 prev->next = inst->next;
133 }
134 pthread_mutex_unlock (&agg_instance_list_lock);
136 sfree (inst->state_num);
137 sfree (inst->state_sum);
138 sfree (inst->state_average);
139 sfree (inst->state_min);
140 sfree (inst->state_max);
141 sfree (inst->state_stddev);
143 memset (inst, 0, sizeof (*inst));
144 inst->ds_type = -1;
145 inst->min = NAN;
146 inst->max = NAN;
147 } /* }}} void agg_instance_destroy */
149 static int agg_instance_create_name (agg_instance_t *inst, /* {{{ */
150 value_list_t const *vl, aggregation_t const *agg)
151 {
152 #define COPY_FIELD(buffer, buffer_size, field, group_mask, all_value) do { \
153 if (agg->set_ ## field != NULL) \
154 sstrncpy (buffer, agg->set_ ## field, buffer_size); \
155 else if ((agg->regex_fields & group_mask) \
156 && (agg->group_by & group_mask)) \
157 sstrncpy (buffer, vl->field, buffer_size); \
158 else if ((agg->regex_fields & group_mask) \
159 && (AGG_MATCHES_ALL (agg->ident.field))) \
160 sstrncpy (buffer, all_value, buffer_size); \
161 else \
162 sstrncpy (buffer, agg->ident.field, buffer_size); \
163 } while (0)
165 /* Host */
166 COPY_FIELD (inst->ident.host, sizeof (inst->ident.host),
167 host, LU_GROUP_BY_HOST, "global");
169 /* Plugin */
170 if (agg->set_plugin != NULL)
171 sstrncpy (inst->ident.plugin, agg->set_plugin,
172 sizeof (inst->ident.plugin));
173 else
174 sstrncpy (inst->ident.plugin, "aggregation", sizeof (inst->ident.plugin));
176 /* Plugin instance */
177 if (agg->set_plugin_instance != NULL)
178 sstrncpy (inst->ident.plugin_instance, agg->set_plugin_instance,
179 sizeof (inst->ident.plugin_instance));
180 else
181 {
182 char tmp_plugin[DATA_MAX_NAME_LEN];
183 char tmp_plugin_instance[DATA_MAX_NAME_LEN] = "";
185 if ((agg->regex_fields & LU_GROUP_BY_PLUGIN)
186 && (agg->group_by & LU_GROUP_BY_PLUGIN))
187 sstrncpy (tmp_plugin, vl->plugin, sizeof (tmp_plugin));
188 else if ((agg->regex_fields & LU_GROUP_BY_PLUGIN)
189 && (AGG_MATCHES_ALL (agg->ident.plugin)))
190 sstrncpy (tmp_plugin, "", sizeof (tmp_plugin));
191 else
192 sstrncpy (tmp_plugin, agg->ident.plugin, sizeof (tmp_plugin));
194 if ((agg->regex_fields & LU_GROUP_BY_PLUGIN_INSTANCE)
195 && (agg->group_by & LU_GROUP_BY_PLUGIN_INSTANCE))
196 sstrncpy (tmp_plugin_instance, vl->plugin_instance,
197 sizeof (tmp_plugin_instance));
198 else if ((agg->regex_fields & LU_GROUP_BY_PLUGIN_INSTANCE)
199 && (AGG_MATCHES_ALL (agg->ident.plugin_instance)))
200 sstrncpy (tmp_plugin_instance, "", sizeof (tmp_plugin_instance));
201 else
202 sstrncpy (tmp_plugin_instance, agg->ident.plugin_instance,
203 sizeof (tmp_plugin_instance));
205 if ((strcmp ("", tmp_plugin) == 0)
206 && (strcmp ("", tmp_plugin_instance) == 0))
207 sstrncpy (inst->ident.plugin_instance, AGG_FUNC_PLACEHOLDER,
208 sizeof (inst->ident.plugin_instance));
209 else if (strcmp ("", tmp_plugin) != 0)
210 ssnprintf (inst->ident.plugin_instance,
211 sizeof (inst->ident.plugin_instance),
212 "%s-%s", tmp_plugin, AGG_FUNC_PLACEHOLDER);
213 else if (strcmp ("", tmp_plugin_instance) != 0)
214 ssnprintf (inst->ident.plugin_instance,
215 sizeof (inst->ident.plugin_instance),
216 "%s-%s", tmp_plugin_instance, AGG_FUNC_PLACEHOLDER);
217 else
218 ssnprintf (inst->ident.plugin_instance,
219 sizeof (inst->ident.plugin_instance),
220 "%s-%s-%s", tmp_plugin, tmp_plugin_instance, AGG_FUNC_PLACEHOLDER);
221 }
223 /* Type */
224 sstrncpy (inst->ident.type, agg->ident.type, sizeof (inst->ident.type));
226 /* Type instance */
227 COPY_FIELD (inst->ident.type_instance, sizeof (inst->ident.type_instance),
228 type_instance, LU_GROUP_BY_TYPE_INSTANCE, "");
230 #undef COPY_FIELD
232 return (0);
233 } /* }}} int agg_instance_create_name */
235 /* Create a new aggregation instance. */
236 static agg_instance_t *agg_instance_create (data_set_t const *ds, /* {{{ */
237 value_list_t const *vl, aggregation_t *agg)
238 {
239 agg_instance_t *inst;
241 DEBUG ("aggregation plugin: Creating new instance.");
243 inst = calloc (1, sizeof (*inst));
244 if (inst == NULL)
245 {
246 ERROR ("aggregation plugin: calloc() failed.");
247 return (NULL);
248 }
249 pthread_mutex_init (&inst->lock, /* attr = */ NULL);
251 inst->ds_type = ds->ds[0].type;
253 agg_instance_create_name (inst, vl, agg);
255 inst->min = NAN;
256 inst->max = NAN;
258 #define INIT_STATE(field) do { \
259 inst->state_ ## field = NULL; \
260 if (agg->calc_ ## field) { \
261 inst->state_ ## field = calloc (1, sizeof (*inst->state_ ## field)); \
262 if (inst->state_ ## field == NULL) { \
263 agg_instance_destroy (inst); \
264 free (inst); \
265 ERROR ("aggregation plugin: calloc() failed."); \
266 return (NULL); \
267 } \
268 } \
269 } while (0)
271 INIT_STATE (num);
272 INIT_STATE (sum);
273 INIT_STATE (average);
274 INIT_STATE (min);
275 INIT_STATE (max);
276 INIT_STATE (stddev);
278 #undef INIT_STATE
280 pthread_mutex_lock (&agg_instance_list_lock);
281 inst->next = agg_instance_list_head;
282 agg_instance_list_head = inst;
283 pthread_mutex_unlock (&agg_instance_list_lock);
285 return (inst);
286 } /* }}} agg_instance_t *agg_instance_create */
288 /* Update the num, sum, min, max, ... fields of the aggregation instance, if
289 * the rate of the value list is available. Value lists with more than one data
290 * source are not supported and will return an error. Returns zero on success
291 * and non-zero otherwise. */
292 static int agg_instance_update (agg_instance_t *inst, /* {{{ */
293 data_set_t const *ds, value_list_t const *vl)
294 {
295 gauge_t *rate;
297 if (ds->ds_num != 1)
298 {
299 ERROR ("aggregation plugin: The \"%s\" type (data set) has more than one "
300 "data source. This is currently not supported by this plugin. "
301 "Sorry.", ds->type);
302 return (EINVAL);
303 }
305 rate = uc_get_rate (ds, vl);
306 if (rate == NULL)
307 {
308 char ident[6 * DATA_MAX_NAME_LEN];
309 FORMAT_VL (ident, sizeof (ident), vl);
310 ERROR ("aggregation plugin: Unable to read the current rate of \"%s\".",
311 ident);
312 return (ENOENT);
313 }
315 if (isnan (rate[0]))
316 {
317 sfree (rate);
318 return (0);
319 }
321 pthread_mutex_lock (&inst->lock);
323 inst->num++;
324 inst->sum += rate[0];
325 inst->squares_sum += (rate[0] * rate[0]);
327 if (isnan (inst->min) || (inst->min > rate[0]))
328 inst->min = rate[0];
329 if (isnan (inst->max) || (inst->max < rate[0]))
330 inst->max = rate[0];
332 pthread_mutex_unlock (&inst->lock);
334 sfree (rate);
335 return (0);
336 } /* }}} int agg_instance_update */
338 static int agg_instance_read_func (agg_instance_t *inst, /* {{{ */
339 char const *func, gauge_t rate, rate_to_value_state_t *state,
340 value_list_t *vl, char const *pi_prefix, cdtime_t t)
341 {
342 value_t v;
343 int status;
345 if (pi_prefix[0] != 0)
346 subst_string (vl->plugin_instance, sizeof (vl->plugin_instance),
347 pi_prefix, AGG_FUNC_PLACEHOLDER, func);
348 else
349 sstrncpy (vl->plugin_instance, func, sizeof (vl->plugin_instance));
351 memset (&v, 0, sizeof (v));
352 status = rate_to_value (&v, rate, state, inst->ds_type, t);
353 if (status != 0)
354 {
355 /* If this is the first iteration and rate_to_value() was asked to return a
356 * COUNTER or a DERIVE, it will return EAGAIN. Catch this and handle
357 * gracefully. */
358 if (status == EAGAIN)
359 return (0);
361 WARNING ("aggregation plugin: rate_to_value failed with status %i.",
362 status);
363 return (-1);
364 }
366 vl->values = &v;
367 vl->values_len = 1;
369 plugin_dispatch_values (vl);
371 vl->values = NULL;
372 vl->values_len = 0;
374 return (0);
375 } /* }}} int agg_instance_read_func */
377 static int agg_instance_read (agg_instance_t *inst, cdtime_t t) /* {{{ */
378 {
379 value_list_t vl = VALUE_LIST_INIT;
381 /* Pre-set all the fields in the value list that will not change per
382 * aggregation type (sum, average, ...). The struct will be re-used and must
383 * therefore be dispatched using the "secure" function. */
385 vl.time = t;
386 vl.interval = 0;
388 vl.meta = meta_data_create ();
389 if (vl.meta == NULL)
390 {
391 ERROR ("aggregation plugin: meta_data_create failed.");
392 return (-1);
393 }
394 meta_data_add_boolean (vl.meta, "aggregation:created", 1);
396 sstrncpy (vl.host, inst->ident.host, sizeof (vl.host));
397 sstrncpy (vl.plugin, inst->ident.plugin, sizeof (vl.plugin));
398 sstrncpy (vl.type, inst->ident.type, sizeof (vl.type));
399 sstrncpy (vl.type_instance, inst->ident.type_instance,
400 sizeof (vl.type_instance));
402 #define READ_FUNC(func, rate) do { \
403 if (inst->state_ ## func != NULL) { \
404 agg_instance_read_func (inst, #func, rate, \
405 inst->state_ ## func, &vl, inst->ident.plugin_instance, t); \
406 } \
407 } while (0)
409 pthread_mutex_lock (&inst->lock);
411 READ_FUNC (num, (gauge_t) inst->num);
413 /* All other aggregations are only defined when there have been any values
414 * at all. */
415 if (inst->num > 0)
416 {
417 READ_FUNC (sum, inst->sum);
418 READ_FUNC (average, (inst->sum / ((gauge_t) inst->num)));
419 READ_FUNC (min, inst->min);
420 READ_FUNC (max, inst->max);
421 READ_FUNC (stddev, sqrt((((gauge_t) inst->num) * inst->squares_sum)
422 - (inst->sum * inst->sum)) / ((gauge_t) inst->num));
423 }
425 /* Reset internal state. */
426 inst->num = 0;
427 inst->sum = 0.0;
428 inst->squares_sum = 0.0;
429 inst->min = NAN;
430 inst->max = NAN;
432 pthread_mutex_unlock (&inst->lock);
434 meta_data_destroy (vl.meta);
435 vl.meta = NULL;
437 return (0);
438 } /* }}} int agg_instance_read */
440 /* lookup_class_callback_t for utils_vl_lookup */
441 static void *agg_lookup_class_callback ( /* {{{ */
442 data_set_t const *ds, value_list_t const *vl, void *user_class)
443 {
444 return (agg_instance_create (ds, vl, (aggregation_t *) user_class));
445 } /* }}} void *agg_class_callback */
447 /* lookup_obj_callback_t for utils_vl_lookup */
448 static int agg_lookup_obj_callback (data_set_t const *ds, /* {{{ */
449 value_list_t const *vl,
450 __attribute__((unused)) void *user_class,
451 void *user_obj)
452 {
453 return (agg_instance_update ((agg_instance_t *) user_obj, ds, vl));
454 } /* }}} int agg_lookup_obj_callback */
456 /* lookup_free_class_callback_t for utils_vl_lookup */
457 static void agg_lookup_free_class_callback (void *user_class) /* {{{ */
458 {
459 agg_destroy ((aggregation_t *) user_class);
460 } /* }}} void agg_lookup_free_class_callback */
462 /* lookup_free_obj_callback_t for utils_vl_lookup */
463 static void agg_lookup_free_obj_callback (void *user_obj) /* {{{ */
464 {
465 agg_instance_destroy ((agg_instance_t *) user_obj);
466 } /* }}} void agg_lookup_free_obj_callback */
468 /*
469 * <Plugin "aggregation">
470 * <Aggregation>
471 * Plugin "cpu"
472 * Type "cpu"
473 *
474 * GroupBy Host
475 * GroupBy TypeInstance
476 *
477 * CalculateNum true
478 * CalculateSum true
479 * CalculateAverage true
480 * CalculateMinimum true
481 * CalculateMaximum true
482 * CalculateStddev true
483 * </Aggregation>
484 * </Plugin>
485 */
486 static int agg_config_handle_group_by (oconfig_item_t const *ci, /* {{{ */
487 aggregation_t *agg)
488 {
489 int i;
491 for (i = 0; i < ci->values_num; i++)
492 {
493 char const *value;
495 if (ci->values[i].type != OCONFIG_TYPE_STRING)
496 {
497 ERROR ("aggregation plugin: Argument %i of the \"GroupBy\" option "
498 "is not a string.", i + 1);
499 continue;
500 }
502 value = ci->values[i].value.string;
504 if (strcasecmp ("Host", value) == 0)
505 agg->group_by |= LU_GROUP_BY_HOST;
506 else if (strcasecmp ("Plugin", value) == 0)
507 agg->group_by |= LU_GROUP_BY_PLUGIN;
508 else if (strcasecmp ("PluginInstance", value) == 0)
509 agg->group_by |= LU_GROUP_BY_PLUGIN_INSTANCE;
510 else if (strcasecmp ("TypeInstance", value) == 0)
511 agg->group_by |= LU_GROUP_BY_TYPE_INSTANCE;
512 else if (strcasecmp ("Type", value) == 0)
513 ERROR ("aggregation plugin: Grouping by type is not supported.");
514 else
515 WARNING ("aggregation plugin: The \"%s\" argument to the \"GroupBy\" "
516 "option is invalid and will be ignored.", value);
517 } /* for (ci->values) */
519 return (0);
520 } /* }}} int agg_config_handle_group_by */
522 static int agg_config_aggregation (oconfig_item_t *ci) /* {{{ */
523 {
524 aggregation_t *agg;
525 _Bool is_valid;
526 int status;
527 int i;
529 agg = calloc (1, sizeof (*agg));
530 if (agg == NULL)
531 {
532 ERROR ("aggregation plugin: calloc failed.");
533 return (-1);
534 }
536 sstrncpy (agg->ident.host, "/.*/", sizeof (agg->ident.host));
537 sstrncpy (agg->ident.plugin, "/.*/", sizeof (agg->ident.plugin));
538 sstrncpy (agg->ident.plugin_instance, "/.*/",
539 sizeof (agg->ident.plugin_instance));
540 sstrncpy (agg->ident.type, "/.*/", sizeof (agg->ident.type));
541 sstrncpy (agg->ident.type_instance, "/.*/",
542 sizeof (agg->ident.type_instance));
544 for (i = 0; i < ci->children_num; i++)
545 {
546 oconfig_item_t *child = ci->children + i;
548 if (strcasecmp ("Host", child->key) == 0)
549 cf_util_get_string_buffer (child, agg->ident.host,
550 sizeof (agg->ident.host));
551 else if (strcasecmp ("Plugin", child->key) == 0)
552 cf_util_get_string_buffer (child, agg->ident.plugin,
553 sizeof (agg->ident.plugin));
554 else if (strcasecmp ("PluginInstance", child->key) == 0)
555 cf_util_get_string_buffer (child, agg->ident.plugin_instance,
556 sizeof (agg->ident.plugin_instance));
557 else if (strcasecmp ("Type", child->key) == 0)
558 cf_util_get_string_buffer (child, agg->ident.type,
559 sizeof (agg->ident.type));
560 else if (strcasecmp ("TypeInstance", child->key) == 0)
561 cf_util_get_string_buffer (child, agg->ident.type_instance,
562 sizeof (agg->ident.type_instance));
563 else if (strcasecmp ("SetHost", child->key) == 0)
564 cf_util_get_string (child, &agg->set_host);
565 else if (strcasecmp ("SetPlugin", child->key) == 0)
566 cf_util_get_string (child, &agg->set_plugin);
567 else if (strcasecmp ("SetPluginInstance", child->key) == 0)
568 cf_util_get_string (child, &agg->set_plugin_instance);
569 else if (strcasecmp ("SetTypeInstance", child->key) == 0)
570 cf_util_get_string (child, &agg->set_type_instance);
571 else if (strcasecmp ("GroupBy", child->key) == 0)
572 agg_config_handle_group_by (child, agg);
573 else if (strcasecmp ("CalculateNum", child->key) == 0)
574 cf_util_get_boolean (child, &agg->calc_num);
575 else if (strcasecmp ("CalculateSum", child->key) == 0)
576 cf_util_get_boolean (child, &agg->calc_sum);
577 else if (strcasecmp ("CalculateAverage", child->key) == 0)
578 cf_util_get_boolean (child, &agg->calc_average);
579 else if (strcasecmp ("CalculateMinimum", child->key) == 0)
580 cf_util_get_boolean (child, &agg->calc_min);
581 else if (strcasecmp ("CalculateMaximum", child->key) == 0)
582 cf_util_get_boolean (child, &agg->calc_max);
583 else if (strcasecmp ("CalculateStddev", child->key) == 0)
584 cf_util_get_boolean (child, &agg->calc_stddev);
585 else
586 WARNING ("aggregation plugin: The \"%s\" key is not allowed inside "
587 "<Aggregation /> blocks and will be ignored.", child->key);
588 }
590 if (agg_is_regex (agg->ident.host))
591 agg->regex_fields |= LU_GROUP_BY_HOST;
592 if (agg_is_regex (agg->ident.plugin))
593 agg->regex_fields |= LU_GROUP_BY_PLUGIN;
594 if (agg_is_regex (agg->ident.plugin_instance))
595 agg->regex_fields |= LU_GROUP_BY_PLUGIN_INSTANCE;
596 if (agg_is_regex (agg->ident.type_instance))
597 agg->regex_fields |= LU_GROUP_BY_TYPE_INSTANCE;
599 /* Sanity checking */
600 is_valid = 1;
601 if (strcmp ("/.*/", agg->ident.type) == 0) /* {{{ */
602 {
603 ERROR ("aggregation plugin: It appears you did not specify the required "
604 "\"Type\" option in this aggregation. "
605 "(Host \"%s\", Plugin \"%s\", PluginInstance \"%s\", "
606 "Type \"%s\", TypeInstance \"%s\")",
607 agg->ident.host, agg->ident.plugin, agg->ident.plugin_instance,
608 agg->ident.type, agg->ident.type_instance);
609 is_valid = 0;
610 }
611 else if (strchr (agg->ident.type, '/') != NULL)
612 {
613 ERROR ("aggregation plugin: The \"Type\" may not contain the '/' "
614 "character. Especially, it may not be a regex. The current "
615 "value is \"%s\".", agg->ident.type);
616 is_valid = 0;
617 } /* }}} */
619 /* Check that there is at least one regex field without a grouping. {{{ */
620 if ((agg->regex_fields & ~agg->group_by) == 0)
621 {
622 ERROR ("aggregation plugin: An aggregation must contain at least one "
623 "wildcard. This is achieved by leaving at least one of the \"Host\", "
624 "\"Plugin\", \"PluginInstance\" and \"TypeInstance\" options blank "
625 "or using a regular expression and not grouping by that field. "
626 "(Host \"%s\", Plugin \"%s\", PluginInstance \"%s\", "
627 "Type \"%s\", TypeInstance \"%s\")",
628 agg->ident.host, agg->ident.plugin, agg->ident.plugin_instance,
629 agg->ident.type, agg->ident.type_instance);
630 is_valid = 0;
631 } /* }}} */
633 /* Check that all grouping fields are regular expressions. {{{ */
634 if (agg->group_by & ~agg->regex_fields)
635 {
636 ERROR ("aggregation plugin: Only wildcard fields (fields for which a "
637 "regular expression is configured or which are left blank) can be "
638 "specified in the \"GroupBy\" option. "
639 "(Host \"%s\", Plugin \"%s\", PluginInstance \"%s\", "
640 "Type \"%s\", TypeInstance \"%s\")",
641 agg->ident.host, agg->ident.plugin, agg->ident.plugin_instance,
642 agg->ident.type, agg->ident.type_instance);
643 is_valid = 0;
644 } /* }}} */
646 if (!agg->calc_num && !agg->calc_sum && !agg->calc_average /* {{{ */
647 && !agg->calc_min && !agg->calc_max && !agg->calc_stddev)
648 {
649 ERROR ("aggregation plugin: No aggregation function has been specified. "
650 "Without this, I don't know what I should be calculating. "
651 "(Host \"%s\", Plugin \"%s\", PluginInstance \"%s\", "
652 "Type \"%s\", TypeInstance \"%s\")",
653 agg->ident.host, agg->ident.plugin, agg->ident.plugin_instance,
654 agg->ident.type, agg->ident.type_instance);
655 is_valid = 0;
656 } /* }}} */
658 if (!is_valid) /* {{{ */
659 {
660 sfree (agg);
661 return (-1);
662 } /* }}} */
664 status = lookup_add (lookup, &agg->ident, agg->group_by, agg);
665 if (status != 0)
666 {
667 ERROR ("aggregation plugin: lookup_add failed with status %i.", status);
668 sfree (agg);
669 return (-1);
670 }
672 DEBUG ("aggregation plugin: Successfully added aggregation: "
673 "(Host \"%s\", Plugin \"%s\", PluginInstance \"%s\", "
674 "Type \"%s\", TypeInstance \"%s\")",
675 agg->ident.host, agg->ident.plugin, agg->ident.plugin_instance,
676 agg->ident.type, agg->ident.type_instance);
677 return (0);
678 } /* }}} int agg_config_aggregation */
680 static int agg_config (oconfig_item_t *ci) /* {{{ */
681 {
682 int i;
684 pthread_mutex_lock (&agg_instance_list_lock);
686 if (lookup == NULL)
687 {
688 lookup = lookup_create (agg_lookup_class_callback,
689 agg_lookup_obj_callback,
690 agg_lookup_free_class_callback,
691 agg_lookup_free_obj_callback);
692 if (lookup == NULL)
693 {
694 pthread_mutex_unlock (&agg_instance_list_lock);
695 ERROR ("aggregation plugin: lookup_create failed.");
696 return (-1);
697 }
698 }
700 for (i = 0; i < ci->children_num; i++)
701 {
702 oconfig_item_t *child = ci->children + i;
704 if (strcasecmp ("Aggregation", child->key) == 0)
705 agg_config_aggregation (child);
706 else
707 WARNING ("aggregation plugin: The \"%s\" key is not allowed inside "
708 "<Plugin aggregation /> blocks and will be ignored.", child->key);
709 }
711 pthread_mutex_unlock (&agg_instance_list_lock);
713 return (0);
714 } /* }}} int agg_config */
716 static int agg_read (void) /* {{{ */
717 {
718 agg_instance_t *this;
719 cdtime_t t;
720 int success;
722 t = cdtime ();
723 success = 0;
725 pthread_mutex_lock (&agg_instance_list_lock);
727 /* agg_instance_list_head only holds data, after the "write" callback has
728 * been called with a matching value list at least once. So on startup,
729 * there's a race between the aggregations read() and write() callback. If
730 * the read() callback is called first, agg_instance_list_head is NULL and
731 * "success" may be zero. This is expected and should not result in an error.
732 * Therefore we need to handle this case separately. */
733 if (agg_instance_list_head == NULL)
734 {
735 pthread_mutex_unlock (&agg_instance_list_lock);
736 return (0);
737 }
739 for (this = agg_instance_list_head; this != NULL; this = this->next)
740 {
741 int status;
743 status = agg_instance_read (this, t);
744 if (status != 0)
745 WARNING ("aggregation plugin: Reading an aggregation instance "
746 "failed with status %i.", status);
747 else
748 success++;
749 }
751 pthread_mutex_unlock (&agg_instance_list_lock);
753 return ((success > 0) ? 0 : -1);
754 } /* }}} int agg_read */
756 static int agg_write (data_set_t const *ds, value_list_t const *vl, /* {{{ */
757 __attribute__((unused)) user_data_t *user_data)
758 {
759 _Bool created_by_aggregation = 0;
760 int status;
762 /* Ignore values that were created by the aggregation plugin to avoid weird
763 * effects. */
764 (void) meta_data_get_boolean (vl->meta, "aggregation:created",
765 &created_by_aggregation);
766 if (created_by_aggregation)
767 return (0);
769 if (lookup == NULL)
770 status = ENOENT;
771 else
772 {
773 status = lookup_search (lookup, ds, vl);
774 if (status > 0)
775 status = 0;
776 }
778 return (status);
779 } /* }}} int agg_write */
781 void module_register (void)
782 {
783 plugin_register_complex_config ("aggregation", agg_config);
784 plugin_register_read ("aggregation", agg_read);
785 plugin_register_write ("aggregation", agg_write, /* user_data = */ NULL);
786 }
788 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */