use kern.cp_times sysctl for FreeBSD smp support
[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 /* is not inverted */
731     {
732       status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
733           "%f. That is %s the %s threshold of %f%s.",
734           ds->ds[ds_index].name, values[ds_index],
735           (values[ds_index] < min) ? "below" : "above",
736           (state == STATE_ERROR) ? "failure" : "warning",
737           (values[ds_index] < min) ? min : max,
738           ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
739     }
740     buf += status;
741     bufsize -= status;
742   }
743
744   plugin_dispatch_notification (&n);
745
746   plugin_notification_meta_free (n.meta);
747   return (0);
748 } /* }}} int ut_report_state */
749
750 /*
751  * int ut_check_one_data_source
752  *
753  * Checks one data source against the given threshold configuration. If the
754  * `DataSource' option is set in the threshold, and the name does NOT match,
755  * `okay' is returned. If the threshold does match, its failure and warning
756  * min and max values are checked and `failure' or `warning' is returned if
757  * appropriate.
758  * Does not fail.
759  */
760 static int ut_check_one_data_source (const data_set_t *ds,
761     const value_list_t __attribute__((unused)) *vl,
762     const threshold_t *th,
763     const gauge_t *values,
764     int ds_index)
765 { /* {{{ */
766   const char *ds_name;
767   int is_warning = 0;
768   int is_failure = 0;
769   int prev_state = STATE_OKAY;
770
771   /* check if this threshold applies to this data source */
772   if (ds != NULL)
773   {
774     ds_name = ds->ds[ds_index].name;
775     if ((th->data_source[0] != 0)
776         && (strcmp (ds_name, th->data_source) != 0))
777       return (STATE_OKAY);
778   }
779
780   if ((th->flags & UT_FLAG_INVERT) != 0)
781   {
782     is_warning--;
783     is_failure--;
784   }
785
786   /* XXX: This is an experimental code, not optimized, not fast, not reliable,
787    * and probably, do not work as you expect. Enjoy! :D */
788   if ( (th->hysteresis > 0) && ((prev_state = uc_get_state(ds,vl)) != STATE_OKAY) )
789   {
790     switch(prev_state)
791     {
792       case STATE_ERROR:
793         if ( (!isnan (th->failure_min) && ((th->failure_min + th->hysteresis) < values[ds_index])) ||
794              (!isnan (th->failure_max) && ((th->failure_max - th->hysteresis) > values[ds_index])) )
795           return (STATE_OKAY);
796         else
797           is_failure++;
798       case STATE_WARNING:
799         if ( (!isnan (th->warning_min) && ((th->warning_min + th->hysteresis) < values[ds_index])) ||
800              (!isnan (th->warning_max) && ((th->warning_max - th->hysteresis) > values[ds_index])) )
801           return (STATE_OKAY);
802         else
803           is_warning++;
804      }
805   }
806   else { /* no hysteresis */
807     if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
808         || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
809       is_failure++;
810
811     if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
812         || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
813       is_warning++;
814  }
815
816   if (is_failure != 0)
817     return (STATE_ERROR);
818
819   if (is_warning != 0)
820     return (STATE_WARNING);
821
822   return (STATE_OKAY);
823 } /* }}} int ut_check_one_data_source */
824
825 /*
826  * int ut_check_one_threshold
827  *
828  * Checks all data sources of a value list against the given threshold, using
829  * the ut_check_one_data_source function above. Returns the worst status,
830  * which is `okay' if nothing has failed.
831  * Returns less than zero if the data set doesn't have any data sources.
832  */
833 static int ut_check_one_threshold (const data_set_t *ds,
834     const value_list_t *vl,
835     const threshold_t *th,
836     const gauge_t *values,
837     int *ret_ds_index)
838 { /* {{{ */
839   int ret = -1;
840   int ds_index = -1;
841   int i;
842   gauge_t values_copy[ds->ds_num];
843
844   memcpy (values_copy, values, sizeof (values_copy));
845
846   if ((th->flags & UT_FLAG_PERCENTAGE) != 0)
847   {
848     int num = 0;
849     gauge_t sum=0.0;
850
851     if (ds->ds_num == 1)
852     {
853       WARNING ("ut_check_one_threshold: The %s type has only one data "
854           "source, but you have configured to check this as a percentage. "
855           "That doesn't make much sense, because the percentage will always "
856           "be 100%%!", ds->type);
857     }
858
859     /* Prepare `sum' and `num'. */
860     for (i = 0; i < ds->ds_num; i++)
861       if (!isnan (values[i]))
862       {
863         num++;
864         sum += values[i];
865       }
866
867     if ((num == 0) /* All data sources are undefined. */
868         || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */
869     {
870       for (i = 0; i < ds->ds_num; i++)
871         values_copy[i] = NAN;
872     }
873     else /* We can actually calculate the percentage. */
874     {
875       for (i = 0; i < ds->ds_num; i++)
876         values_copy[i] = 100.0 * values[i] / sum;
877     }
878   } /* if (UT_FLAG_PERCENTAGE) */
879
880   for (i = 0; i < ds->ds_num; i++)
881   {
882     int status;
883
884     status = ut_check_one_data_source (ds, vl, th, values_copy, i);
885     if (ret < status)
886     {
887       ret = status;
888       ds_index = i;
889     }
890   } /* for (ds->ds_num) */
891
892   if (ret_ds_index != NULL)
893     *ret_ds_index = ds_index;
894
895   return (ret);
896 } /* }}} int ut_check_one_threshold */
897
898 /*
899  * int ut_check_threshold (PUBLIC)
900  *
901  * Gets a list of matching thresholds and searches for the worst status by one
902  * of the thresholds. Then reports that status using the ut_report_state
903  * function above. 
904  * Returns zero on success and if no threshold has been configured. Returns
905  * less than zero on failure.
906  */
907 int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
908 { /* {{{ */
909   threshold_t *th;
910   gauge_t *values;
911   int status;
912
913   int worst_state = -1;
914   threshold_t *worst_th = NULL;
915   int worst_ds_index = -1;
916
917   if (threshold_tree == NULL)
918     return (0);
919
920   /* Is this lock really necessary? So far, thresholds are only inserted at
921    * startup. -octo */
922   pthread_mutex_lock (&threshold_lock);
923   th = threshold_search (vl);
924   pthread_mutex_unlock (&threshold_lock);
925   if (th == NULL)
926     return (0);
927
928   DEBUG ("ut_check_threshold: Found matching threshold(s)");
929
930   values = uc_get_rate (ds, vl);
931   if (values == NULL)
932     return (0);
933
934   while (th != NULL)
935   {
936     int ds_index = -1;
937
938     status = ut_check_one_threshold (ds, vl, th, values, &ds_index);
939     if (status < 0)
940     {
941       ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
942       sfree (values);
943       return (-1);
944     }
945
946     if (worst_state < status)
947     {
948       worst_state = status;
949       worst_th = th;
950       worst_ds_index = ds_index;
951     }
952
953     th = th->next;
954   } /* while (th) */
955
956   status = ut_report_state (ds, vl, worst_th, values,
957       worst_ds_index, worst_state);
958   if (status != 0)
959   {
960     ERROR ("ut_check_threshold: ut_report_state failed.");
961     sfree (values);
962     return (-1);
963   }
964
965   sfree (values);
966
967   return (0);
968 } /* }}} int ut_check_threshold */
969
970 /*
971  * int ut_check_interesting (PUBLIC)
972  *
973  * Given an identification returns
974  * 0: No threshold is defined.
975  * 1: A threshold has been found. The flag `persist' is off.
976  * 2: A threshold has been found. The flag `persist' is on.
977  *    (That is, it is expected that many notifications are sent until the
978  *    problem disappears.)
979  */
980 int ut_check_interesting (const char *name)
981 { /* {{{ */
982   char *name_copy = NULL;
983   char *host = NULL;
984   char *plugin = NULL;
985   char *plugin_instance = NULL;
986   char *type = NULL;
987   char *type_instance = NULL;
988   int status;
989   data_set_t ds;
990   value_list_t vl;
991   threshold_t *th;
992
993   /* If there is no tree nothing is interesting. */
994   if (threshold_tree == NULL)
995     return (0);
996
997   name_copy = strdup (name);
998   if (name_copy == NULL)
999   {
1000     ERROR ("ut_check_interesting: strdup failed.");
1001     return (-1);
1002   }
1003
1004   status = parse_identifier (name_copy, &host,
1005       &plugin, &plugin_instance, &type, &type_instance);
1006   if (status != 0)
1007   {
1008     ERROR ("ut_check_interesting: parse_identifier failed.");
1009     sfree (name_copy);
1010     return (-1);
1011   }
1012
1013   memset (&ds, '\0', sizeof (ds));
1014   memset (&vl, '\0', sizeof (vl));
1015
1016   sstrncpy (vl.host, host, sizeof (vl.host));
1017   sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
1018   if (plugin_instance != NULL)
1019     sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
1020   sstrncpy (ds.type, type, sizeof (ds.type));
1021   sstrncpy (vl.type, type, sizeof (vl.type));
1022   if (type_instance != NULL)
1023     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
1024
1025   sfree (name_copy);
1026   host = plugin = plugin_instance = type = type_instance = NULL;
1027
1028   th = threshold_search (&vl);
1029   if (th == NULL)
1030     return (0);
1031   if ((th->flags & UT_FLAG_PERSIST) == 0)
1032     return (1);
1033   return (2);
1034 } /* }}} int ut_check_interesting */
1035
1036 int ut_search_threshold (const value_list_t *vl, /* {{{ */
1037     threshold_t *ret_threshold)
1038 {
1039   threshold_t *t;
1040
1041   if (vl == NULL)
1042     return (EINVAL);
1043
1044   t = threshold_search (vl);
1045   if (t == NULL)
1046     return (ENOENT);
1047
1048   memcpy (ret_threshold, t, sizeof (*ret_threshold));
1049   ret_threshold->next = NULL;
1050
1051   return (0);
1052 } /* }}} int ut_search_threshold */
1053
1054 /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */