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