Merge branch 'collectd-4.2' into collectd-4.3
[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     {
552       is_inverted = 1;
553       is_warning--;
554       is_failure--;
555     }
556     if ((!isnan (th->failure_min) && (th->failure_min > values[i]))
557         || (!isnan (th->failure_max) && (th->failure_max < values[i])))
558       is_failure++;
559     if ((!isnan (th->warning_min) && (th->warning_min > values[i]))
560         || (!isnan (th->warning_max) && (th->warning_max < values[i])))
561       is_warning++;
562
563     if ((is_failure != 0) && (state_new != STATE_ERROR))
564     {
565       state_new = STATE_ERROR;
566       ds_index = i;
567     }
568     else if ((is_warning != 0)
569         && (state_new != STATE_ERROR)
570         && (state_new != STATE_WARNING))
571     {
572       state_new = STATE_WARNING;
573       ds_index = i;
574     }
575   }
576
577   if (state_new != state_orig)
578     uc_set_state (ds, vl, state_new);
579
580   /* Return here if we're not going to send a notification */
581   if ((state_new == state_orig)
582       && ((state_new == STATE_OKAY)
583         || ((th->flags & UT_FLAG_PERSIST) == 0)))
584   {
585     sfree (values);
586     return (0);
587   }
588
589   NOTIFICATION_INIT_VL (&n, vl, ds);
590   {
591     /* Copy the associative members */
592     if (state_new == STATE_OKAY)
593       n.severity = NOTIF_OKAY;
594     else if (state_new == STATE_WARNING)
595       n.severity = NOTIF_WARNING;
596     else
597       n.severity = NOTIF_FAILURE;
598
599     n.time = vl->time;
600
601     buf = n.message;
602     bufsize = sizeof (n.message);
603
604     status = snprintf (buf, bufsize, "Host %s, plugin %s",
605         vl->host, vl->plugin);
606     buf += status;
607     bufsize -= status;
608
609     if (vl->plugin_instance[0] != '\0')
610     {
611       status = snprintf (buf, bufsize, " (instance %s)",
612           vl->plugin_instance);
613       buf += status;
614       bufsize -= status;
615     }
616
617     status = snprintf (buf, bufsize, " type %s", ds->type);
618     buf += status;
619     bufsize -= status;
620
621     if (vl->type_instance[0] != '\0')
622     {
623       status = snprintf (buf, bufsize, " (instance %s)",
624           vl->type_instance);
625       buf += status;
626       bufsize -= status;
627     }
628   }
629
630   /* Send a okay notification */
631   if (state_new == STATE_OKAY)
632   {
633     status = snprintf (buf, bufsize, ": All data sources are within range again.");
634     buf += status;
635     bufsize -= status;
636   }
637   else
638   {
639     double min;
640     double max;
641
642     min = (state_new == STATE_ERROR) ? th->failure_min : th->warning_min;
643     max = (state_new == STATE_ERROR) ? th->failure_max : th->warning_max;
644
645     if (th->flags & UT_FLAG_INVERT)
646     {
647       if (!isnan (min) && !isnan (max))
648       {
649         status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
650             "%f. That is within the %s region of %f and %f.",
651             ds->ds[ds_index].name, values[ds_index],
652             (state_new == STATE_ERROR) ? "failure" : "warning",
653             min, min);
654       }
655       else
656       {
657         status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
658             "%f. That is %s the %s threshold of %f.",
659             ds->ds[ds_index].name, values[ds_index],
660             isnan (min) ? "below" : "above",
661             (state_new == STATE_ERROR) ? "failure" : "warning",
662             isnan (min) ? max : min);
663       }
664     }
665     else /* is not inverted */
666     {
667       status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
668           "%f. That is %s the %s threshold of %f.",
669           ds->ds[ds_index].name, values[ds_index],
670           (values[ds_index] < min) ? "below" : "above",
671           (state_new == STATE_ERROR) ? "failure" : "warning",
672           (values[ds_index] < min) ? min : max);
673     }
674     buf += status;
675     bufsize -= status;
676   }
677
678   plugin_dispatch_notification (&n);
679
680   sfree (values);
681
682   return (0);
683 } /* int ut_check_threshold */
684
685 int ut_check_interesting (const char *name)
686 {
687   char *name_copy = NULL;
688   char *host = NULL;
689   char *plugin = NULL;
690   char *plugin_instance = NULL;
691   char *type = NULL;
692   char *type_instance = NULL;
693   int status;
694   data_set_t ds;
695   value_list_t vl;
696   threshold_t *th;
697
698   /* If there is no tree nothing is interesting. */
699   if (threshold_tree == NULL)
700     return (0);
701
702   name_copy = strdup (name);
703   if (name_copy == NULL)
704   {
705     ERROR ("ut_check_interesting: strdup failed.");
706     return (-1);
707   }
708
709   status = parse_identifier (name_copy, &host,
710       &plugin, &plugin_instance, &type, &type_instance);
711   if (status != 0)
712   {
713     ERROR ("ut_check_interesting: parse_identifier failed.");
714     return (-1);
715   }
716
717   memset (&ds, '\0', sizeof (ds));
718   memset (&vl, '\0', sizeof (vl));
719
720   strncpy (vl.host, host, sizeof (vl.host));
721   vl.host[sizeof (vl.host) - 1] = '\0';
722   strncpy (vl.plugin, plugin, sizeof (vl.plugin));
723   vl.plugin[sizeof (vl.plugin) - 1] = '\0';
724   if (plugin_instance != NULL)
725   {
726     strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
727     vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
728   }
729   strncpy (ds.type, type, sizeof (ds.type));
730   ds.type[sizeof (ds.type) - 1] = '\0';
731   if (type_instance != NULL)
732   {
733     strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
734     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
735   }
736
737   sfree (name_copy);
738   host = plugin = plugin_instance = type = type_instance = NULL;
739
740   th = threshold_search (&ds, &vl);
741   if (th == NULL)
742     return (0);
743   if ((th->flags & UT_FLAG_PERSIST) == 0)
744     return (1);
745   return (2);
746 } /* int ut_check_interesting */
747
748 /* vim: set sw=2 ts=8 sts=2 tw=78 fdm=marker : */