src/utils_cmd_flush.c: Fix memory leak.
[collectd.git] / src / threshold.c
1 /**
2  * collectd - src/threshold.c
3  * Copyright (C) 2007-2010  Florian Forster
4  * Copyright (C) 2008-2009  Sebastian Harl
5  * Copyright (C) 2009       Andrés J. Díaz
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; only version 2 of the License is applicable.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Author:
21  *   Florian octo Forster <octo at collectd.org>
22  *   Sebastian Harl <sh at tokkee.org>
23  *   Andrés J. Díaz <ajdiaz at connectical.com>
24  **/
25
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "utils_avltree.h"
30 #include "utils_cache.h"
31
32 #include <assert.h>
33 #include <pthread.h>
34
35 /*
36  * Private data structures
37  * {{{ */
38 #define UT_FLAG_INVERT  0x01
39 #define UT_FLAG_PERSIST 0x02
40 #define UT_FLAG_PERCENTAGE 0x04
41 #define UT_FLAG_INTERESTING 0x08
42 #define UT_FLAG_PERSIST_OK 0x10
43 typedef struct threshold_s
44 {
45   char host[DATA_MAX_NAME_LEN];
46   char plugin[DATA_MAX_NAME_LEN];
47   char plugin_instance[DATA_MAX_NAME_LEN];
48   char type[DATA_MAX_NAME_LEN];
49   char type_instance[DATA_MAX_NAME_LEN];
50   char data_source[DATA_MAX_NAME_LEN];
51   gauge_t warning_min;
52   gauge_t warning_max;
53   gauge_t failure_min;
54   gauge_t failure_max;
55   gauge_t hysteresis;
56   unsigned int flags;
57   int hits;
58   struct threshold_s *next;
59 } threshold_t;
60 /* }}} */
61
62 /*
63  * Private (static) variables
64  * {{{ */
65 static c_avl_tree_t   *threshold_tree = NULL;
66 static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
67 /* }}} */
68
69 /*
70  * Threshold management
71  * ====================
72  * The following functions add, delete, search, etc. configured thresholds to
73  * the underlying AVL trees.
74  */
75 /*
76  * threshold_t *threshold_get
77  *
78  * Retrieve one specific threshold configuration. For looking up a threshold
79  * matching a value_list_t, see "threshold_search" below. Returns NULL if the
80  * specified threshold doesn't exist.
81  */
82 static threshold_t *threshold_get (const char *hostname,
83     const char *plugin, const char *plugin_instance,
84     const char *type, const char *type_instance)
85 { /* {{{ */
86   char name[6 * DATA_MAX_NAME_LEN];
87   threshold_t *th = NULL;
88
89   format_name (name, sizeof (name),
90       (hostname == NULL) ? "" : hostname,
91       (plugin == NULL) ? "" : plugin, plugin_instance,
92       (type == NULL) ? "" : type, type_instance);
93   name[sizeof (name) - 1] = '\0';
94
95   if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
96     return (th);
97   else
98     return (NULL);
99 } /* }}} threshold_t *threshold_get */
100
101 /*
102  * int ut_threshold_add
103  *
104  * Adds a threshold configuration to the list of thresholds. The threshold_t
105  * structure is copied and may be destroyed after this call. Returns zero on
106  * success, non-zero otherwise.
107  */
108 static int ut_threshold_add (const threshold_t *th)
109 { /* {{{ */
110   char name[6 * DATA_MAX_NAME_LEN];
111   char *name_copy;
112   threshold_t *th_copy;
113   threshold_t *th_ptr;
114   int status = 0;
115
116   if (format_name (name, sizeof (name), th->host,
117         th->plugin, th->plugin_instance,
118         th->type, th->type_instance) != 0)
119   {
120     ERROR ("ut_threshold_add: format_name failed.");
121     return (-1);
122   }
123
124   name_copy = strdup (name);
125   if (name_copy == NULL)
126   {
127     ERROR ("ut_threshold_add: strdup failed.");
128     return (-1);
129   }
130
131   th_copy = (threshold_t *) malloc (sizeof (threshold_t));
132   if (th_copy == NULL)
133   {
134     sfree (name_copy);
135     ERROR ("ut_threshold_add: malloc failed.");
136     return (-1);
137   }
138   memcpy (th_copy, th, sizeof (threshold_t));
139   th_ptr = NULL;
140
141   DEBUG ("ut_threshold_add: Adding entry `%s'", name);
142
143   pthread_mutex_lock (&threshold_lock);
144
145   th_ptr = threshold_get (th->host, th->plugin, th->plugin_instance,
146       th->type, th->type_instance);
147
148   while ((th_ptr != NULL) && (th_ptr->next != NULL))
149     th_ptr = th_ptr->next;
150
151   if (th_ptr == NULL) /* no such threshold yet */
152   {
153     status = c_avl_insert (threshold_tree, name_copy, th_copy);
154   }
155   else /* th_ptr points to the last threshold in the list */
156   {
157     th_ptr->next = th_copy;
158     /* name_copy isn't needed */
159     sfree (name_copy);
160   }
161
162   pthread_mutex_unlock (&threshold_lock);
163
164   if (status != 0)
165   {
166     ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
167     sfree (name_copy);
168     sfree (th_copy);
169   }
170
171   return (status);
172 } /* }}} int ut_threshold_add */
173
174 /* 
175  * threshold_t *threshold_search
176  *
177  * Searches for a threshold configuration using all the possible variations of
178  * "Host", "Plugin" and "Type" blocks. Returns NULL if no threshold could be
179  * found.
180  * XXX: This is likely the least efficient function in collectd.
181  */
182 static threshold_t *threshold_search (const value_list_t *vl)
183 { /* {{{ */
184   threshold_t *th;
185
186   if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
187           vl->type, vl->type_instance)) != NULL)
188     return (th);
189   else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
190           vl->type, NULL)) != NULL)
191     return (th);
192   else if ((th = threshold_get (vl->host, vl->plugin, NULL,
193           vl->type, vl->type_instance)) != NULL)
194     return (th);
195   else if ((th = threshold_get (vl->host, vl->plugin, NULL,
196           vl->type, NULL)) != NULL)
197     return (th);
198   else if ((th = threshold_get (vl->host, "", NULL,
199           vl->type, vl->type_instance)) != NULL)
200     return (th);
201   else if ((th = threshold_get (vl->host, "", NULL,
202           vl->type, NULL)) != NULL)
203     return (th);
204   else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
205           vl->type, vl->type_instance)) != NULL)
206     return (th);
207   else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
208           vl->type, NULL)) != NULL)
209     return (th);
210   else if ((th = threshold_get ("", vl->plugin, NULL,
211           vl->type, vl->type_instance)) != NULL)
212     return (th);
213   else if ((th = threshold_get ("", vl->plugin, NULL,
214           vl->type, NULL)) != NULL)
215     return (th);
216   else if ((th = threshold_get ("", "", NULL,
217           vl->type, vl->type_instance)) != NULL)
218     return (th);
219   else if ((th = threshold_get ("", "", NULL,
220           vl->type, NULL)) != NULL)
221     return (th);
222
223   return (NULL);
224 } /* }}} threshold_t *threshold_search */
225
226 /*
227  * Configuration
228  * =============
229  * The following approximately two hundred functions are used to handle the
230  * configuration and fill the threshold list.
231  * {{{ */
232 static int ut_config_type_datasource (threshold_t *th, oconfig_item_t *ci)
233 {
234   if ((ci->values_num != 1)
235       || (ci->values[0].type != OCONFIG_TYPE_STRING))
236   {
237     WARNING ("threshold values: The `DataSource' option needs exactly one "
238         "string argument.");
239     return (-1);
240   }
241
242   sstrncpy (th->data_source, ci->values[0].value.string,
243       sizeof (th->data_source));
244
245   return (0);
246 } /* int ut_config_type_datasource */
247
248 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
249 {
250   if ((ci->values_num != 1)
251       || (ci->values[0].type != OCONFIG_TYPE_STRING))
252   {
253     WARNING ("threshold values: The `Instance' option needs exactly one "
254         "string argument.");
255     return (-1);
256   }
257
258   sstrncpy (th->type_instance, ci->values[0].value.string,
259       sizeof (th->type_instance));
260
261   return (0);
262 } /* int ut_config_type_instance */
263
264 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
265 {
266   if ((ci->values_num != 1)
267       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
268   {
269     WARNING ("threshold values: The `%s' option needs exactly one "
270         "number argument.", ci->key);
271     return (-1);
272   }
273
274   if (strcasecmp (ci->key, "WarningMax") == 0)
275     th->warning_max = ci->values[0].value.number;
276   else
277     th->failure_max = ci->values[0].value.number;
278
279   return (0);
280 } /* int ut_config_type_max */
281
282 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
283 {
284   if ((ci->values_num != 1)
285       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
286   {
287     WARNING ("threshold values: The `%s' option needs exactly one "
288         "number argument.", ci->key);
289     return (-1);
290   }
291
292   if (strcasecmp (ci->key, "WarningMin") == 0)
293     th->warning_min = ci->values[0].value.number;
294   else
295     th->failure_min = ci->values[0].value.number;
296
297   return (0);
298 } /* int ut_config_type_min */
299
300 static int ut_config_type_hits (threshold_t *th, oconfig_item_t *ci)
301 {
302   if ((ci->values_num != 1)
303       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
304   {
305     WARNING ("threshold values: The `%s' option needs exactly one "
306       "number argument.", ci->key);
307     return (-1);
308   }
309
310   th->hits = ci->values[0].value.number;
311
312   return (0);
313 } /* int ut_config_type_hits */
314
315 static int ut_config_type_hysteresis (threshold_t *th, oconfig_item_t *ci)
316 {
317   if ((ci->values_num != 1)
318       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
319   {
320     WARNING ("threshold values: The `%s' option needs exactly one "
321       "number argument.", ci->key);
322     return (-1);
323   }
324
325   th->hysteresis = ci->values[0].value.number;
326
327   return (0);
328 } /* int ut_config_type_hysteresis */
329
330 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
331 {
332   int i;
333   threshold_t th;
334   int status = 0;
335
336   if ((ci->values_num != 1)
337       || (ci->values[0].type != OCONFIG_TYPE_STRING))
338   {
339     WARNING ("threshold values: The `Type' block needs exactly one string "
340         "argument.");
341     return (-1);
342   }
343
344   if (ci->children_num < 1)
345   {
346     WARNING ("threshold values: The `Type' block needs at least one option.");
347     return (-1);
348   }
349
350   memcpy (&th, th_orig, sizeof (th));
351   sstrncpy (th.type, ci->values[0].value.string, sizeof (th.type));
352
353   th.warning_min = NAN;
354   th.warning_max = NAN;
355   th.failure_min = NAN;
356   th.failure_max = NAN;
357   th.hits = 0;
358   th.hysteresis = 0;
359   th.flags = UT_FLAG_INTERESTING; /* interesting by default */
360
361   for (i = 0; i < ci->children_num; i++)
362   {
363     oconfig_item_t *option = ci->children + i;
364
365     if (strcasecmp ("Instance", option->key) == 0)
366       status = ut_config_type_instance (&th, option);
367     else if (strcasecmp ("DataSource", option->key) == 0)
368       status = ut_config_type_datasource (&th, option);
369     else if ((strcasecmp ("WarningMax", option->key) == 0)
370         || (strcasecmp ("FailureMax", option->key) == 0))
371       status = ut_config_type_max (&th, option);
372     else if ((strcasecmp ("WarningMin", option->key) == 0)
373         || (strcasecmp ("FailureMin", option->key) == 0))
374       status = ut_config_type_min (&th, option);
375     else if (strcasecmp ("Interesting", option->key) == 0)
376       status = cf_util_get_flag (option, &th.flags, UT_FLAG_INTERESTING);
377     else if (strcasecmp ("Invert", option->key) == 0)
378       status = cf_util_get_flag (option, &th.flags, UT_FLAG_INVERT);
379     else if (strcasecmp ("Persist", option->key) == 0)
380       status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST);
381     else if (strcasecmp ("PersistOK", option->key) == 0)
382       status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST_OK);
383     else if (strcasecmp ("Percentage", option->key) == 0)
384       status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERCENTAGE);
385     else if (strcasecmp ("Hits", option->key) == 0)
386       status = ut_config_type_hits (&th, option);
387     else if (strcasecmp ("Hysteresis", option->key) == 0)
388       status = ut_config_type_hysteresis (&th, option);
389     else
390     {
391       WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
392           "block.", option->key);
393       status = -1;
394     }
395
396     if (status != 0)
397       break;
398   }
399
400   if (status == 0)
401   {
402     status = ut_threshold_add (&th);
403   }
404
405   return (status);
406 } /* int ut_config_type */
407
408 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
409 {
410   if ((ci->values_num != 1)
411       || (ci->values[0].type != OCONFIG_TYPE_STRING))
412   {
413     WARNING ("threshold values: The `Instance' option needs exactly one "
414         "string argument.");
415     return (-1);
416   }
417
418   sstrncpy (th->plugin_instance, ci->values[0].value.string,
419       sizeof (th->plugin_instance));
420
421   return (0);
422 } /* int ut_config_plugin_instance */
423
424 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
425 {
426   int i;
427   threshold_t th;
428   int status = 0;
429
430   if ((ci->values_num != 1)
431       || (ci->values[0].type != OCONFIG_TYPE_STRING))
432   {
433     WARNING ("threshold values: The `Plugin' block needs exactly one string "
434         "argument.");
435     return (-1);
436   }
437
438   if (ci->children_num < 1)
439   {
440     WARNING ("threshold values: The `Plugin' block needs at least one nested "
441         "block.");
442     return (-1);
443   }
444
445   memcpy (&th, th_orig, sizeof (th));
446   sstrncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
447
448   for (i = 0; i < ci->children_num; i++)
449   {
450     oconfig_item_t *option = ci->children + i;
451
452     if (strcasecmp ("Type", option->key) == 0)
453       status = ut_config_type (&th, option);
454     else if (strcasecmp ("Instance", option->key) == 0)
455       status = ut_config_plugin_instance (&th, option);
456     else
457     {
458       WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
459           "block.", option->key);
460       status = -1;
461     }
462
463     if (status != 0)
464       break;
465   }
466
467   return (status);
468 } /* int ut_config_plugin */
469
470 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
471 {
472   int i;
473   threshold_t th;
474   int status = 0;
475
476   if ((ci->values_num != 1)
477       || (ci->values[0].type != OCONFIG_TYPE_STRING))
478   {
479     WARNING ("threshold values: The `Host' block needs exactly one string "
480         "argument.");
481     return (-1);
482   }
483
484   if (ci->children_num < 1)
485   {
486     WARNING ("threshold values: The `Host' block needs at least one nested "
487         "block.");
488     return (-1);
489   }
490
491   memcpy (&th, th_orig, sizeof (th));
492   sstrncpy (th.host, ci->values[0].value.string, sizeof (th.host));
493
494   for (i = 0; i < ci->children_num; i++)
495   {
496     oconfig_item_t *option = ci->children + i;
497
498     if (strcasecmp ("Type", option->key) == 0)
499       status = ut_config_type (&th, option);
500     else if (strcasecmp ("Plugin", option->key) == 0)
501       status = ut_config_plugin (&th, option);
502     else
503     {
504       WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
505           "block.", option->key);
506       status = -1;
507     }
508
509     if (status != 0)
510       break;
511   }
512
513   return (status);
514 } /* int ut_config_host */
515 /*
516  * End of the functions used to configure threshold values.
517  */
518 /* }}} */
519
520 /*
521  * int ut_report_state
522  *
523  * Checks if the `state' differs from the old state and creates a notification
524  * if appropriate.
525  * Does not fail.
526  */
527 static int ut_report_state (const data_set_t *ds,
528     const value_list_t *vl,
529     const threshold_t *th,
530     const gauge_t *values,
531     int ds_index,
532     int state)
533 { /* {{{ */
534   int state_old;
535   notification_t n;
536
537   char *buf;
538   size_t bufsize;
539
540   int status;
541
542   /* Check if hits matched */
543   if ( (th->hits != 0) )
544   {
545     int hits = uc_get_hits(ds,vl);
546     /* STATE_OKAY resets hits unless PERSIST_OK flag is set. Hits resets if
547      * threshold is hit. */
548     if ( ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) ) || (hits > th->hits) )
549     {
550         DEBUG("ut_report_state: reset uc_get_hits = 0");
551         uc_set_hits(ds,vl,0); /* reset hit counter and notify */
552     } else {
553       DEBUG("ut_report_state: th->hits = %d, uc_get_hits = %d",th->hits,uc_get_hits(ds,vl));
554       (void) uc_inc_hits(ds,vl,1); /* increase hit counter */
555       return (0);
556     }
557   } /* end check hits */
558
559   state_old = uc_get_state (ds, vl);
560
561   /* If the state didn't change, report if `persistent' is specified. If the
562    * state is `okay', then only report if `persist_ok` flag is set. */
563   if (state == state_old)
564   {
565     if ((th->flags & UT_FLAG_PERSIST) == 0)
566       return (0);
567     else if ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) )
568       return (0);
569   }
570
571   if (state != state_old)
572     uc_set_state (ds, vl, state);
573
574   NOTIFICATION_INIT_VL (&n, vl);
575
576   buf = n.message;
577   bufsize = sizeof (n.message);
578
579   if (state == STATE_OKAY)
580     n.severity = NOTIF_OKAY;
581   else if (state == STATE_WARNING)
582     n.severity = NOTIF_WARNING;
583   else
584     n.severity = NOTIF_FAILURE;
585
586   n.time = vl->time;
587
588   status = ssnprintf (buf, bufsize, "Host %s, plugin %s",
589       vl->host, vl->plugin);
590   buf += status;
591   bufsize -= status;
592
593   if (vl->plugin_instance[0] != '\0')
594   {
595     status = ssnprintf (buf, bufsize, " (instance %s)",
596         vl->plugin_instance);
597     buf += status;
598     bufsize -= status;
599   }
600
601   status = ssnprintf (buf, bufsize, " type %s", vl->type);
602   buf += status;
603   bufsize -= status;
604
605   if (vl->type_instance[0] != '\0')
606   {
607     status = ssnprintf (buf, bufsize, " (instance %s)",
608         vl->type_instance);
609     buf += status;
610     bufsize -= status;
611   }
612
613   plugin_notification_meta_add_string (&n, "DataSource",
614       ds->ds[ds_index].name);
615   plugin_notification_meta_add_double (&n, "CurrentValue", values[ds_index]);
616   plugin_notification_meta_add_double (&n, "WarningMin", th->warning_min);
617   plugin_notification_meta_add_double (&n, "WarningMax", th->warning_max);
618   plugin_notification_meta_add_double (&n, "FailureMin", th->failure_min);
619   plugin_notification_meta_add_double (&n, "FailureMax", th->failure_max);
620
621   /* Send an okay notification */
622   if (state == STATE_OKAY)
623   {
624     if (state_old == STATE_MISSING)
625       ssnprintf (buf, bufsize, ": Value is no longer missing.");
626     else
627       ssnprintf (buf, bufsize, ": All data sources are within range again.");
628   }
629   else
630   {
631     double min;
632     double max;
633
634     min = (state == STATE_ERROR) ? th->failure_min : th->warning_min;
635     max = (state == STATE_ERROR) ? th->failure_max : th->warning_max;
636
637     if (th->flags & UT_FLAG_INVERT)
638     {
639       if (!isnan (min) && !isnan (max))
640       {
641         ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
642             "%f. That is within the %s region of %f%s and %f%s.",
643             ds->ds[ds_index].name, values[ds_index],
644             (state == STATE_ERROR) ? "failure" : "warning",
645             min, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "",
646             max, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
647       }
648       else
649       {
650         ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
651             "%f. That is %s the %s threshold of %f%s.",
652             ds->ds[ds_index].name, values[ds_index],
653             isnan (min) ? "below" : "above",
654             (state == STATE_ERROR) ? "failure" : "warning",
655             isnan (min) ? max : min,
656             ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
657       }
658     }
659     else if (th->flags & UT_FLAG_PERCENTAGE)
660     {
661       gauge_t value;
662       gauge_t sum;
663       int i;
664
665       sum = 0.0;
666       for (i = 0; i < vl->values_len; i++)
667       {
668         if (isnan (values[i]))
669           continue;
670
671         sum += values[i];
672       }
673
674       if (sum == 0.0)
675         value = NAN;
676       else
677         value = 100.0 * values[ds_index] / sum;
678
679       ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
680           "%g (%.2f%%). That is %s the %s threshold of %.2f%%.",
681           ds->ds[ds_index].name, values[ds_index], value,
682           (value < min) ? "below" : "above",
683           (state == STATE_ERROR) ? "failure" : "warning",
684           (value < min) ? min : max);
685     }
686     else /* is not inverted */
687     {
688       ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
689           "%f. That is %s the %s threshold of %f.",
690           ds->ds[ds_index].name, values[ds_index],
691           (values[ds_index] < min) ? "below" : "above",
692           (state == STATE_ERROR) ? "failure" : "warning",
693           (values[ds_index] < min) ? min : max);
694     }
695   }
696
697   plugin_dispatch_notification (&n);
698
699   plugin_notification_meta_free (n.meta);
700   return (0);
701 } /* }}} int ut_report_state */
702
703 /*
704  * int ut_check_one_data_source
705  *
706  * Checks one data source against the given threshold configuration. If the
707  * `DataSource' option is set in the threshold, and the name does NOT match,
708  * `okay' is returned. If the threshold does match, its failure and warning
709  * min and max values are checked and `failure' or `warning' is returned if
710  * appropriate.
711  * Does not fail.
712  */
713 static int ut_check_one_data_source (const data_set_t *ds,
714     const value_list_t __attribute__((unused)) *vl,
715     const threshold_t *th,
716     const gauge_t *values,
717     int ds_index)
718 { /* {{{ */
719   const char *ds_name;
720   int is_warning = 0;
721   int is_failure = 0;
722   int prev_state = STATE_OKAY;
723
724   /* check if this threshold applies to this data source */
725   if (ds != NULL)
726   {
727     ds_name = ds->ds[ds_index].name;
728     if ((th->data_source[0] != 0)
729         && (strcmp (ds_name, th->data_source) != 0))
730       return (STATE_OKAY);
731   }
732
733   if ((th->flags & UT_FLAG_INVERT) != 0)
734   {
735     is_warning--;
736     is_failure--;
737   }
738
739   /* XXX: This is an experimental code, not optimized, not fast, not reliable,
740    * and probably, do not work as you expect. Enjoy! :D */
741   if ( (th->hysteresis > 0) && ((prev_state = uc_get_state(ds,vl)) != STATE_OKAY) )
742   {
743     switch(prev_state)
744     {
745       case STATE_ERROR:
746         if ( (!isnan (th->failure_min) && ((th->failure_min + th->hysteresis) < values[ds_index])) ||
747              (!isnan (th->failure_max) && ((th->failure_max - th->hysteresis) > values[ds_index])) )
748           return (STATE_OKAY);
749         else
750           is_failure++;
751       case STATE_WARNING:
752         if ( (!isnan (th->warning_min) && ((th->warning_min + th->hysteresis) < values[ds_index])) ||
753              (!isnan (th->warning_max) && ((th->warning_max - th->hysteresis) > values[ds_index])) )
754           return (STATE_OKAY);
755         else
756           is_warning++;
757      }
758   }
759   else { /* no hysteresis */
760     if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
761         || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
762       is_failure++;
763
764     if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
765         || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
766       is_warning++;
767  }
768
769   if (is_failure != 0)
770     return (STATE_ERROR);
771
772   if (is_warning != 0)
773     return (STATE_WARNING);
774
775   return (STATE_OKAY);
776 } /* }}} int ut_check_one_data_source */
777
778 /*
779  * int ut_check_one_threshold
780  *
781  * Checks all data sources of a value list against the given threshold, using
782  * the ut_check_one_data_source function above. Returns the worst status,
783  * which is `okay' if nothing has failed.
784  * Returns less than zero if the data set doesn't have any data sources.
785  */
786 static int ut_check_one_threshold (const data_set_t *ds,
787     const value_list_t *vl,
788     const threshold_t *th,
789     const gauge_t *values,
790     int *ret_ds_index)
791 { /* {{{ */
792   int ret = -1;
793   int ds_index = -1;
794   int i;
795   gauge_t values_copy[ds->ds_num];
796
797   memcpy (values_copy, values, sizeof (values_copy));
798
799   if ((th->flags & UT_FLAG_PERCENTAGE) != 0)
800   {
801     int num = 0;
802     gauge_t sum=0.0;
803
804     if (ds->ds_num == 1)
805     {
806       WARNING ("ut_check_one_threshold: The %s type has only one data "
807           "source, but you have configured to check this as a percentage. "
808           "That doesn't make much sense, because the percentage will always "
809           "be 100%%!", ds->type);
810     }
811
812     /* Prepare `sum' and `num'. */
813     for (i = 0; i < ds->ds_num; i++)
814       if (!isnan (values[i]))
815       {
816         num++;
817         sum += values[i];
818       }
819
820     if ((num == 0) /* All data sources are undefined. */
821         || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */
822     {
823       for (i = 0; i < ds->ds_num; i++)
824         values_copy[i] = NAN;
825     }
826     else /* We can actually calculate the percentage. */
827     {
828       for (i = 0; i < ds->ds_num; i++)
829         values_copy[i] = 100.0 * values[i] / sum;
830     }
831   } /* if (UT_FLAG_PERCENTAGE) */
832
833   for (i = 0; i < ds->ds_num; i++)
834   {
835     int status;
836
837     status = ut_check_one_data_source (ds, vl, th, values_copy, i);
838     if (ret < status)
839     {
840       ret = status;
841       ds_index = i;
842     }
843   } /* for (ds->ds_num) */
844
845   if (ret_ds_index != NULL)
846     *ret_ds_index = ds_index;
847
848   return (ret);
849 } /* }}} int ut_check_one_threshold */
850
851 /*
852  * int ut_check_threshold
853  *
854  * Gets a list of matching thresholds and searches for the worst status by one
855  * of the thresholds. Then reports that status using the ut_report_state
856  * function above. 
857  * Returns zero on success and if no threshold has been configured. Returns
858  * less than zero on failure.
859  */
860 static int ut_check_threshold (const data_set_t *ds, const value_list_t *vl,
861     __attribute__((unused)) user_data_t *ud)
862 { /* {{{ */
863   threshold_t *th;
864   gauge_t *values;
865   int status;
866
867   int worst_state = -1;
868   threshold_t *worst_th = NULL;
869   int worst_ds_index = -1;
870
871   if (threshold_tree == NULL)
872     return (0);
873
874   /* Is this lock really necessary? So far, thresholds are only inserted at
875    * startup. -octo */
876   pthread_mutex_lock (&threshold_lock);
877   th = threshold_search (vl);
878   pthread_mutex_unlock (&threshold_lock);
879   if (th == NULL)
880     return (0);
881
882   DEBUG ("ut_check_threshold: Found matching threshold(s)");
883
884   values = uc_get_rate (ds, vl);
885   if (values == NULL)
886     return (0);
887
888   while (th != NULL)
889   {
890     int ds_index = -1;
891
892     status = ut_check_one_threshold (ds, vl, th, values, &ds_index);
893     if (status < 0)
894     {
895       ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
896       sfree (values);
897       return (-1);
898     }
899
900     if (worst_state < status)
901     {
902       worst_state = status;
903       worst_th = th;
904       worst_ds_index = ds_index;
905     }
906
907     th = th->next;
908   } /* while (th) */
909
910   status = ut_report_state (ds, vl, worst_th, values,
911       worst_ds_index, worst_state);
912   if (status != 0)
913   {
914     ERROR ("ut_check_threshold: ut_report_state failed.");
915     sfree (values);
916     return (-1);
917   }
918
919   sfree (values);
920
921   return (0);
922 } /* }}} int ut_check_threshold */
923
924 /*
925  * int ut_missing
926  *
927  * This function is called whenever a value goes "missing".
928  */
929 static int ut_missing (const value_list_t *vl,
930     __attribute__((unused)) user_data_t *ud)
931 { /* {{{ */
932   threshold_t *th;
933   cdtime_t missing_time;
934   char identifier[6 * DATA_MAX_NAME_LEN];
935   notification_t n;
936   cdtime_t now;
937
938   if (threshold_tree == NULL)
939     return (0);
940
941   th = threshold_search (vl);
942   /* dispatch notifications for "interesting" values only */
943   if ((th == NULL) || ((th->flags & UT_FLAG_INTERESTING) == 0))
944     return (0);
945
946   now = cdtime ();
947   missing_time = now - vl->time;
948   FORMAT_VL (identifier, sizeof (identifier), vl);
949
950   NOTIFICATION_INIT_VL (&n, vl);
951   ssnprintf (n.message, sizeof (n.message),
952       "%s has not been updated for %.3f seconds.",
953       identifier, CDTIME_T_TO_DOUBLE (missing_time));
954   n.time = now;
955
956   plugin_dispatch_notification (&n);
957
958   return (0);
959 } /* }}} int ut_missing */
960
961 int ut_config (oconfig_item_t *ci)
962 { /* {{{ */
963   int i;
964   int status = 0;
965
966   threshold_t th;
967
968   if (threshold_tree == NULL)
969   {
970     threshold_tree = c_avl_create ((void *) strcmp);
971     if (threshold_tree == NULL)
972     {
973       ERROR ("ut_config: c_avl_create failed.");
974       return (-1);
975     }
976   }
977
978   memset (&th, '\0', sizeof (th));
979   th.warning_min = NAN;
980   th.warning_max = NAN;
981   th.failure_min = NAN;
982   th.failure_max = NAN;
983
984   th.hits = 0;
985   th.hysteresis = 0;
986   th.flags = UT_FLAG_INTERESTING; /* interesting by default */
987     
988   for (i = 0; i < ci->children_num; i++)
989   {
990     oconfig_item_t *option = ci->children + i;
991
992     if (strcasecmp ("Type", option->key) == 0)
993       status = ut_config_type (&th, option);
994     else if (strcasecmp ("Plugin", option->key) == 0)
995       status = ut_config_plugin (&th, option);
996     else if (strcasecmp ("Host", option->key) == 0)
997       status = ut_config_host (&th, option);
998     else
999     {
1000       WARNING ("threshold values: Option `%s' not allowed here.", option->key);
1001       status = -1;
1002     }
1003
1004     if (status != 0)
1005       break;
1006   }
1007
1008   if (c_avl_size (threshold_tree) > 0) {
1009     plugin_register_missing ("threshold", ut_missing,
1010         /* user data = */ NULL);
1011     plugin_register_write ("threshold", ut_check_threshold,
1012         /* user data = */ NULL);
1013   }
1014
1015   return (status);
1016 } /* }}} int um_config */
1017
1018 void module_register (void)
1019 {
1020   plugin_register_complex_config ("threshold", ut_config);
1021 }
1022
1023 /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */