src/utils_{cache,threshold}.c: Fix the concept of failed and missing values.
[collectd.git] / src / utils_threshold.c
1 /**
2  * collectd - src/utils_threshold.c
3  * Copyright (C) 2007,2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
27
28 #include <assert.h>
29 #include <pthread.h>
30
31 /*
32  * Private data structures
33  * {{{ */
34 #define UT_FLAG_INVERT  0x01
35 #define UT_FLAG_PERSIST 0x02
36
37 typedef struct threshold_s
38 {
39   char host[DATA_MAX_NAME_LEN];
40   char plugin[DATA_MAX_NAME_LEN];
41   char plugin_instance[DATA_MAX_NAME_LEN];
42   char type[DATA_MAX_NAME_LEN];
43   char type_instance[DATA_MAX_NAME_LEN];
44   gauge_t warning_min;
45   gauge_t warning_max;
46   gauge_t failure_min;
47   gauge_t failure_max;
48   int flags;
49 } threshold_t;
50 /* }}} */
51
52 /*
53  * Private (static) variables
54  * {{{ */
55 static c_avl_tree_t   *threshold_tree = NULL;
56 static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
57 /* }}} */
58
59 /*
60  * Threshold management
61  * ====================
62  * The following functions add, delete, search, etc. configured thresholds to
63  * the underlying AVL trees.
64  * {{{ */
65 static int ut_threshold_add (const threshold_t *th)
66 {
67   char name[6 * DATA_MAX_NAME_LEN];
68   char *name_copy;
69   threshold_t *th_copy;
70   int status = 0;
71
72   if (format_name (name, sizeof (name), th->host,
73         th->plugin, th->plugin_instance,
74         th->type, th->type_instance) != 0)
75   {
76     ERROR ("ut_threshold_add: format_name failed.");
77     return (-1);
78   }
79
80   name_copy = strdup (name);
81   if (name_copy == NULL)
82   {
83     ERROR ("ut_threshold_add: strdup failed.");
84     return (-1);
85   }
86
87   th_copy = (threshold_t *) malloc (sizeof (threshold_t));
88   if (th_copy == NULL)
89   {
90     sfree (name_copy);
91     ERROR ("ut_threshold_add: malloc failed.");
92     return (-1);
93   }
94   memcpy (th_copy, th, sizeof (threshold_t));
95
96   DEBUG ("ut_threshold_add: Adding entry `%s'", name);
97
98   pthread_mutex_lock (&threshold_lock);
99   status = c_avl_insert (threshold_tree, name_copy, th_copy);
100   pthread_mutex_unlock (&threshold_lock);
101
102   if (status != 0)
103   {
104     ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
105     sfree (name_copy);
106     sfree (th_copy);
107   }
108
109   return (status);
110 } /* int ut_threshold_add */
111 /*
112  * End of the threshold management functions
113  * }}} */
114
115 /*
116  * Configuration
117  * =============
118  * The following approximately two hundred functions are used to handle the
119  * configuration and fill the threshold list.
120  * {{{ */
121 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
122 {
123   if ((ci->values_num != 1)
124       || (ci->values[0].type != OCONFIG_TYPE_STRING))
125   {
126     WARNING ("threshold values: The `Instance' option needs exactly one "
127         "string argument.");
128     return (-1);
129   }
130
131   strncpy (th->type_instance, ci->values[0].value.string,
132       sizeof (th->type_instance));
133   th->type_instance[sizeof (th->type_instance) - 1] = '\0';
134
135   return (0);
136 } /* int ut_config_type_instance */
137
138 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
139 {
140   if ((ci->values_num != 1)
141       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
142   {
143     WARNING ("threshold values: The `%s' option needs exactly one "
144         "number argument.", ci->key);
145     return (-1);
146   }
147
148   if (strcasecmp (ci->key, "WarningMax") == 0)
149     th->warning_max = ci->values[0].value.number;
150   else
151     th->failure_max = ci->values[0].value.number;
152
153   return (0);
154 } /* int ut_config_type_max */
155
156 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
157 {
158   if ((ci->values_num != 1)
159       || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
160   {
161     WARNING ("threshold values: The `%s' option needs exactly one "
162         "number argument.", ci->key);
163     return (-1);
164   }
165
166   if (strcasecmp (ci->key, "WarningMin") == 0)
167     th->warning_min = ci->values[0].value.number;
168   else
169     th->failure_min = ci->values[0].value.number;
170
171   return (0);
172 } /* int ut_config_type_min */
173
174 static int ut_config_type_invert (threshold_t *th, oconfig_item_t *ci)
175 {
176   if ((ci->values_num != 1)
177       || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
178   {
179     WARNING ("threshold values: The `Invert' option needs exactly one "
180         "boolean argument.");
181     return (-1);
182   }
183
184   if (ci->values[0].value.boolean)
185     th->flags |= UT_FLAG_INVERT;
186   else
187     th->flags &= ~UT_FLAG_INVERT;
188
189   return (0);
190 } /* int ut_config_type_invert */
191
192 static int ut_config_type_persist (threshold_t *th, oconfig_item_t *ci)
193 {
194   if ((ci->values_num != 1)
195       || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
196   {
197     WARNING ("threshold values: The `Persist' option needs exactly one "
198         "boolean argument.");
199     return (-1);
200   }
201
202   if (ci->values[0].value.boolean)
203     th->flags |= UT_FLAG_PERSIST;
204   else
205     th->flags &= ~UT_FLAG_PERSIST;
206
207   return (0);
208 } /* int ut_config_type_persist */
209
210 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
211 {
212   int i;
213   threshold_t th;
214   int status = 0;
215
216   if ((ci->values_num != 1)
217       || (ci->values[0].type != OCONFIG_TYPE_STRING))
218   {
219     WARNING ("threshold values: The `Type' block needs exactly one string "
220         "argument.");
221     return (-1);
222   }
223
224   if (ci->children_num < 1)
225   {
226     WARNING ("threshold values: The `Type' block needs at least one option.");
227     return (-1);
228   }
229
230   memcpy (&th, th_orig, sizeof (th));
231   strncpy (th.type, ci->values[0].value.string, sizeof (th.type));
232   th.type[sizeof (th.type) - 1] = '\0';
233
234   th.warning_min = NAN;
235   th.warning_max = NAN;
236   th.failure_min = NAN;
237   th.failure_max = NAN;
238
239   for (i = 0; i < ci->children_num; i++)
240   {
241     oconfig_item_t *option = ci->children + i;
242     status = 0;
243
244     if (strcasecmp ("Instance", option->key) == 0)
245       status = ut_config_type_instance (&th, option);
246     else if ((strcasecmp ("WarningMax", option->key) == 0)
247         || (strcasecmp ("FailureMax", option->key) == 0))
248       status = ut_config_type_max (&th, option);
249     else if ((strcasecmp ("WarningMin", option->key) == 0)
250         || (strcasecmp ("FailureMin", option->key) == 0))
251       status = ut_config_type_min (&th, option);
252     else if (strcasecmp ("Invert", option->key) == 0)
253       status = ut_config_type_invert (&th, option);
254     else if (strcasecmp ("Persist", option->key) == 0)
255       status = ut_config_type_persist (&th, option);
256     else
257     {
258       WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
259           "block.", option->key);
260       status = -1;
261     }
262
263     if (status != 0)
264       break;
265   }
266
267   if (status == 0)
268   {
269     status = ut_threshold_add (&th);
270   }
271
272   return (status);
273 } /* int ut_config_type */
274
275 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
276 {
277   if ((ci->values_num != 1)
278       || (ci->values[0].type != OCONFIG_TYPE_STRING))
279   {
280     WARNING ("threshold values: The `Instance' option needs exactly one "
281         "string argument.");
282     return (-1);
283   }
284
285   strncpy (th->plugin_instance, ci->values[0].value.string,
286       sizeof (th->plugin_instance));
287   th->plugin_instance[sizeof (th->plugin_instance) - 1] = '\0';
288
289   return (0);
290 } /* int ut_config_plugin_instance */
291
292 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
293 {
294   int i;
295   threshold_t th;
296   int status = 0;
297
298   if ((ci->values_num != 1)
299       || (ci->values[0].type != OCONFIG_TYPE_STRING))
300   {
301     WARNING ("threshold values: The `Plugin' block needs exactly one string "
302         "argument.");
303     return (-1);
304   }
305
306   if (ci->children_num < 1)
307   {
308     WARNING ("threshold values: The `Plugin' block needs at least one nested "
309         "block.");
310     return (-1);
311   }
312
313   memcpy (&th, th_orig, sizeof (th));
314   strncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
315   th.plugin[sizeof (th.plugin) - 1] = '\0';
316
317   for (i = 0; i < ci->children_num; i++)
318   {
319     oconfig_item_t *option = ci->children + i;
320     status = 0;
321
322     if (strcasecmp ("Type", option->key) == 0)
323       status = ut_config_type (&th, option);
324     else if (strcasecmp ("Instance", option->key) == 0)
325       status = ut_config_plugin_instance (&th, option);
326     else
327     {
328       WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
329           "block.", option->key);
330       status = -1;
331     }
332
333     if (status != 0)
334       break;
335   }
336
337   return (status);
338 } /* int ut_config_plugin */
339
340 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
341 {
342   int i;
343   threshold_t th;
344   int status = 0;
345
346   if ((ci->values_num != 1)
347       || (ci->values[0].type != OCONFIG_TYPE_STRING))
348   {
349     WARNING ("threshold values: The `Host' block needs exactly one string "
350         "argument.");
351     return (-1);
352   }
353
354   if (ci->children_num < 1)
355   {
356     WARNING ("threshold values: The `Host' block needs at least one nested "
357         "block.");
358     return (-1);
359   }
360
361   memcpy (&th, th_orig, sizeof (th));
362   strncpy (th.host, ci->values[0].value.string, sizeof (th.host));
363   th.host[sizeof (th.host) - 1] = '\0';
364
365   for (i = 0; i < ci->children_num; i++)
366   {
367     oconfig_item_t *option = ci->children + i;
368     status = 0;
369
370     if (strcasecmp ("Type", option->key) == 0)
371       status = ut_config_type (&th, option);
372     else if (strcasecmp ("Plugin", option->key) == 0)
373       status = ut_config_plugin (&th, option);
374     else
375     {
376       WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
377           "block.", option->key);
378       status = -1;
379     }
380
381     if (status != 0)
382       break;
383   }
384
385   return (status);
386 } /* int ut_config_host */
387
388 int ut_config (const oconfig_item_t *ci)
389 {
390   int i;
391   int status = 0;
392
393   threshold_t th;
394
395   if (ci->values_num != 0)
396   {
397     ERROR ("threshold values: The `Threshold' block may not have any "
398         "arguments.");
399     return (-1);
400   }
401
402   if (threshold_tree == NULL)
403   {
404     threshold_tree = c_avl_create ((void *) strcmp);
405     if (threshold_tree == NULL)
406     {
407       ERROR ("ut_config: c_avl_create failed.");
408       return (-1);
409     }
410   }
411
412   memset (&th, '\0', sizeof (th));
413   th.warning_min = NAN;
414   th.warning_max = NAN;
415   th.failure_min = NAN;
416   th.failure_max = NAN;
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 ("Plugin", option->key) == 0)
426       status = ut_config_plugin (&th, option);
427     else if (strcasecmp ("Host", option->key) == 0)
428       status = ut_config_host (&th, option);
429     else
430     {
431       WARNING ("threshold values: Option `%s' not allowed here.", option->key);
432       status = -1;
433     }
434
435     if (status != 0)
436       break;
437   }
438
439   return (status);
440 } /* int um_config */
441 /*
442  * End of the functions used to configure threshold values.
443  */
444 /* }}} */
445
446 static threshold_t *threshold_get (const char *hostname,
447     const char *plugin, const char *plugin_instance,
448     const char *type, const char *type_instance)
449 {
450   char name[6 * DATA_MAX_NAME_LEN];
451   threshold_t *th = NULL;
452
453   format_name (name, sizeof (name),
454       (hostname == NULL) ? "" : hostname,
455       (plugin == NULL) ? "" : plugin, plugin_instance,
456       (type == NULL) ? "" : type, type_instance);
457   name[sizeof (name) - 1] = '\0';
458
459   if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
460     return (th);
461   else
462     return (NULL);
463 } /* threshold_t *threshold_get */
464
465 static threshold_t *threshold_search (const data_set_t *ds,
466     const value_list_t *vl)
467 {
468   threshold_t *th;
469
470   if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
471           ds->type, vl->type_instance)) != NULL)
472     return (th);
473   else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
474           ds->type, NULL)) != NULL)
475     return (th);
476   else if ((th = threshold_get (vl->host, vl->plugin, NULL,
477           ds->type, vl->type_instance)) != NULL)
478     return (th);
479   else if ((th = threshold_get (vl->host, vl->plugin, NULL,
480           ds->type, NULL)) != NULL)
481     return (th);
482   else if ((th = threshold_get (vl->host, "", NULL,
483           ds->type, vl->type_instance)) != NULL)
484     return (th);
485   else if ((th = threshold_get (vl->host, "", NULL,
486           ds->type, NULL)) != NULL)
487     return (th);
488   else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
489           ds->type, vl->type_instance)) != NULL)
490     return (th);
491   else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
492           ds->type, NULL)) != NULL)
493     return (th);
494   else if ((th = threshold_get ("", vl->plugin, NULL,
495           ds->type, vl->type_instance)) != NULL)
496     return (th);
497   else if ((th = threshold_get ("", vl->plugin, NULL,
498           ds->type, NULL)) != NULL)
499     return (th);
500   else if ((th = threshold_get ("", "", NULL,
501           ds->type, vl->type_instance)) != NULL)
502     return (th);
503   else if ((th = threshold_get ("", "", NULL,
504           ds->type, NULL)) != NULL)
505     return (th);
506
507   return (NULL);
508 } /* threshold_t *threshold_search */
509
510 int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
511 {
512   notification_t n;
513   threshold_t *th;
514   gauge_t *values;
515   int i;
516
517   int state_orig;
518   int state_new = STATE_OKAY;
519   int ds_index = 0;
520
521   char *buf;
522   size_t bufsize;
523   int status;
524
525   if (threshold_tree == NULL)
526     return (0);
527
528   /* Is this lock really necessary? So far, thresholds are only inserted at
529    * startup. -octo */
530   pthread_mutex_lock (&threshold_lock);
531   th = threshold_search (ds, vl);
532   pthread_mutex_unlock (&threshold_lock);
533   if (th == NULL)
534     return (0);
535
536   DEBUG ("ut_check_threshold: Found matching threshold");
537
538   values = uc_get_rate (ds, vl);
539   if (values == NULL)
540     return (0);
541
542   state_orig = uc_get_state (ds, vl);
543
544   for (i = 0; i < ds->ds_num; i++)
545   {
546     int is_inverted = 0;
547     int is_warning = 0;
548     int is_failure = 0;
549
550     if ((th->flags & UT_FLAG_INVERT) != 0)
551       is_inverted = 1;
552     if ((!isnan (th->failure_min) && (th->failure_min > values[i]))
553         || (!isnan (th->failure_max) && (th->failure_max < values[i])))
554       is_failure = is_inverted - 1;
555     if ((!isnan (th->warning_min) && (th->warning_min > values[i]))
556         || (!isnan (th->warning_max) && (th->warning_max < values[i])))
557       is_warning = is_inverted - 1;
558
559     if ((is_failure != 0) && (state_new != STATE_ERROR))
560     {
561       state_new = STATE_ERROR;
562       ds_index = i;
563     }
564     else if ((is_warning != 0)
565         && (state_new != STATE_ERROR)
566         && (state_new != STATE_WARNING))
567     {
568       state_new = STATE_WARNING;
569       ds_index = i;
570     }
571   }
572
573   if (state_new != state_orig)
574     uc_set_state (ds, vl, state_new);
575
576   /* Return here if we're not going to send a notification */
577   if ((state_new == state_orig)
578       && ((state_new == STATE_OKAY)
579         || ((th->flags & UT_FLAG_PERSIST) == 0)))
580   {
581     sfree (values);
582     return (0);
583   }
584
585   NOTIFICATION_INIT_VL (&n, vl, ds);
586   {
587     /* Copy the associative members */
588     if (state_new == STATE_OKAY)
589       n.severity = NOTIF_OKAY;
590     else if (state_new == STATE_WARNING)
591       n.severity = NOTIF_WARNING;
592     else
593       n.severity = NOTIF_FAILURE;
594
595     n.time = vl->time;
596
597     buf = n.message;
598     bufsize = sizeof (n.message);
599
600     status = snprintf (buf, bufsize, "Host %s, plugin %s",
601         vl->host, vl->plugin);
602     buf += status;
603     bufsize -= status;
604
605     if (vl->plugin_instance[0] != '\0')
606     {
607       status = snprintf (buf, bufsize, " (instance %s)",
608           vl->plugin_instance);
609       buf += status;
610       bufsize -= status;
611     }
612
613     status = snprintf (buf, bufsize, " type %s", ds->type);
614     buf += status;
615     bufsize -= status;
616
617     if (vl->type_instance[0] != '\0')
618     {
619       status = snprintf (buf, bufsize, " (instance %s)",
620           vl->type_instance);
621       buf += status;
622       bufsize -= status;
623     }
624   }
625
626   /* Send a okay notification */
627   if (state_new == STATE_OKAY)
628   {
629     status = snprintf (buf, bufsize, ": All data sources are within range again.");
630     buf += status;
631     bufsize -= status;
632   }
633   else
634   {
635     double min;
636     double max;
637
638     min = (state_new == STATE_ERROR) ? th->failure_min : th->warning_min;
639     max = (state_new == STATE_ERROR) ? th->failure_max : th->warning_max;
640
641     if (th->flags & UT_FLAG_INVERT)
642     {
643       if (!isnan (min) && !isnan (max))
644       {
645         status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
646             "%f. That is within the %s region of %f and %f.",
647             ds->ds[ds_index].name, values[ds_index],
648             (state_new == STATE_ERROR) ? "failure" : "warning",
649             min, min);
650       }
651       else
652       {
653         status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
654             "%f. That is %s the %s threshold of %f.",
655             ds->ds[ds_index].name, values[ds_index],
656             isnan (min) ? "below" : "above",
657             (state_new == STATE_ERROR) ? "failure" : "warning",
658             isnan (min) ? max : min);
659       }
660     }
661     else /* is not inverted */
662     {
663       status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
664           "%f. That is %s the %s threshold of %f.",
665           ds->ds[ds_index].name, values[ds_index],
666           (values[ds_index] < min) ? "below" : "above",
667           (state_new == STATE_ERROR) ? "failure" : "warning",
668           (values[ds_index] < min) ? min : max);
669     }
670     buf += status;
671     bufsize -= status;
672   }
673
674   plugin_dispatch_notification (&n);
675
676   sfree (values);
677
678   return (0);
679 } /* int ut_check_threshold */
680
681 int ut_check_interesting (const char *name)
682 {
683   char *name_copy = NULL;
684   char *host = NULL;
685   char *plugin = NULL;
686   char *plugin_instance = NULL;
687   char *type = NULL;
688   char *type_instance = NULL;
689   int status;
690   data_set_t ds;
691   value_list_t vl;
692   threshold_t *th;
693
694   /* If there is no tree nothing is interesting. */
695   if (threshold_tree == NULL)
696     return (0);
697
698   name_copy = strdup (name);
699   if (name_copy == NULL)
700   {
701     ERROR ("ut_check_interesting: strdup failed.");
702     return (-1);
703   }
704
705   status = parse_identifier (name_copy, &host,
706       &plugin, &plugin_instance, &type, &type_instance);
707   if (status != 0)
708   {
709     ERROR ("ut_check_interesting: parse_identifier failed.");
710     return (-1);
711   }
712
713   memset (&ds, '\0', sizeof (ds));
714   memset (&vl, '\0', sizeof (vl));
715
716   strncpy (vl.host, host, sizeof (vl.host));
717   vl.host[sizeof (vl.host) - 1] = '\0';
718   strncpy (vl.plugin, plugin, sizeof (vl.plugin));
719   vl.plugin[sizeof (vl.plugin) - 1] = '\0';
720   if (plugin_instance != NULL)
721   {
722     strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
723     vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
724   }
725   strncpy (ds.type, type, sizeof (ds.type));
726   ds.type[sizeof (ds.type) - 1] = '\0';
727   if (type_instance != NULL)
728   {
729     strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
730     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
731   }
732
733   sfree (name_copy);
734   host = plugin = plugin_instance = type = type_instance = NULL;
735
736   th = threshold_search (&ds, &vl);
737   if (th == NULL)
738     return (0);
739   if ((th->flags & UT_FLAG_PERSIST) == 0)
740     return (1);
741   return (2);
742 } /* int ut_check_interesting */
743
744 /* vim: set sw=2 ts=8 sts=2 tw=78 fdm=marker : */