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