2 * collectd - src/utils_threshold.c
3 * Copyright (C) 2007,2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
32 * Private data structures
34 #define UT_FLAG_INVERT 0x01
35 #define UT_FLAG_PERSIST 0x02
37 typedef struct threshold_s
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];
53 * Private (static) variables
55 static c_avl_tree_t *threshold_tree = NULL;
56 static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
60 * Threshold management
61 * ====================
62 * The following functions add, delete, search, etc. configured thresholds to
63 * the underlying AVL trees.
65 static int ut_threshold_add (const threshold_t *th)
67 char name[6 * DATA_MAX_NAME_LEN];
72 if (format_name (name, sizeof (name), th->host,
73 th->plugin, th->plugin_instance,
74 th->type, th->type_instance) != 0)
76 ERROR ("ut_threshold_add: format_name failed.");
80 name_copy = strdup (name);
81 if (name_copy == NULL)
83 ERROR ("ut_threshold_add: strdup failed.");
87 th_copy = (threshold_t *) malloc (sizeof (threshold_t));
91 ERROR ("ut_threshold_add: malloc failed.");
94 memcpy (th_copy, th, sizeof (threshold_t));
96 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
98 pthread_mutex_lock (&threshold_lock);
99 status = c_avl_insert (threshold_tree, name_copy, th_copy);
100 pthread_mutex_unlock (&threshold_lock);
104 ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
110 } /* int ut_threshold_add */
112 * End of the threshold management functions
118 * The following approximately two hundred functions are used to handle the
119 * configuration and fill the threshold list.
121 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
123 if ((ci->values_num != 1)
124 || (ci->values[0].type != OCONFIG_TYPE_STRING))
126 WARNING ("threshold values: The `Instance' option needs exactly one "
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';
136 } /* int ut_config_type_instance */
138 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
140 if ((ci->values_num != 1)
141 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
143 WARNING ("threshold values: The `%s' option needs exactly one "
144 "number argument.", ci->key);
148 if (strcasecmp (ci->key, "WarningMax") == 0)
149 th->warning_max = ci->values[0].value.number;
151 th->failure_max = ci->values[0].value.number;
154 } /* int ut_config_type_max */
156 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
158 if ((ci->values_num != 1)
159 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
161 WARNING ("threshold values: The `%s' option needs exactly one "
162 "number argument.", ci->key);
166 if (strcasecmp (ci->key, "WarningMin") == 0)
167 th->warning_min = ci->values[0].value.number;
169 th->failure_min = ci->values[0].value.number;
172 } /* int ut_config_type_min */
174 static int ut_config_type_invert (threshold_t *th, oconfig_item_t *ci)
176 if ((ci->values_num != 1)
177 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
179 WARNING ("threshold values: The `Invert' option needs exactly one "
180 "boolean argument.");
184 if (ci->values[0].value.boolean)
185 th->flags |= UT_FLAG_INVERT;
187 th->flags &= ~UT_FLAG_INVERT;
190 } /* int ut_config_type_invert */
192 static int ut_config_type_persist (threshold_t *th, oconfig_item_t *ci)
194 if ((ci->values_num != 1)
195 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
197 WARNING ("threshold values: The `Persist' option needs exactly one "
198 "boolean argument.");
202 if (ci->values[0].value.boolean)
203 th->flags |= UT_FLAG_PERSIST;
205 th->flags &= ~UT_FLAG_PERSIST;
208 } /* int ut_config_type_persist */
210 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
216 if ((ci->values_num != 1)
217 || (ci->values[0].type != OCONFIG_TYPE_STRING))
219 WARNING ("threshold values: The `Type' block needs exactly one string "
224 if (ci->children_num < 1)
226 WARNING ("threshold values: The `Type' block needs at least one option.");
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';
234 th.warning_min = NAN;
235 th.warning_max = NAN;
236 th.failure_min = NAN;
237 th.failure_max = NAN;
239 for (i = 0; i < ci->children_num; i++)
241 oconfig_item_t *option = ci->children + i;
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);
258 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
259 "block.", option->key);
269 status = ut_threshold_add (&th);
273 } /* int ut_config_type */
275 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
277 if ((ci->values_num != 1)
278 || (ci->values[0].type != OCONFIG_TYPE_STRING))
280 WARNING ("threshold values: The `Instance' option needs exactly one "
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';
290 } /* int ut_config_plugin_instance */
292 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
298 if ((ci->values_num != 1)
299 || (ci->values[0].type != OCONFIG_TYPE_STRING))
301 WARNING ("threshold values: The `Plugin' block needs exactly one string "
306 if (ci->children_num < 1)
308 WARNING ("threshold values: The `Plugin' block needs at least one nested "
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';
317 for (i = 0; i < ci->children_num; i++)
319 oconfig_item_t *option = ci->children + i;
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);
328 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
329 "block.", option->key);
338 } /* int ut_config_plugin */
340 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
346 if ((ci->values_num != 1)
347 || (ci->values[0].type != OCONFIG_TYPE_STRING))
349 WARNING ("threshold values: The `Host' block needs exactly one string "
354 if (ci->children_num < 1)
356 WARNING ("threshold values: The `Host' block needs at least one nested "
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';
365 for (i = 0; i < ci->children_num; i++)
367 oconfig_item_t *option = ci->children + i;
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);
376 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
377 "block.", option->key);
386 } /* int ut_config_host */
388 int ut_config (const oconfig_item_t *ci)
395 if (ci->values_num != 0)
397 ERROR ("threshold values: The `Threshold' block may not have any "
402 if (threshold_tree == NULL)
404 threshold_tree = c_avl_create ((void *) strcmp);
405 if (threshold_tree == NULL)
407 ERROR ("ut_config: c_avl_create failed.");
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;
418 for (i = 0; i < ci->children_num; i++)
420 oconfig_item_t *option = ci->children + i;
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);
431 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
440 } /* int um_config */
442 * End of the functions used to configure threshold values.
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)
450 char name[6 * DATA_MAX_NAME_LEN];
451 threshold_t *th = NULL;
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';
459 if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
463 } /* threshold_t *threshold_get */
465 static threshold_t *threshold_search (const data_set_t *ds,
466 const value_list_t *vl)
470 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
471 ds->type, vl->type_instance)) != NULL)
473 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
474 ds->type, NULL)) != NULL)
476 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
477 ds->type, vl->type_instance)) != NULL)
479 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
480 ds->type, NULL)) != NULL)
482 else if ((th = threshold_get (vl->host, "", NULL,
483 ds->type, vl->type_instance)) != NULL)
485 else if ((th = threshold_get (vl->host, "", NULL,
486 ds->type, NULL)) != NULL)
488 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
489 ds->type, vl->type_instance)) != NULL)
491 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
492 ds->type, NULL)) != NULL)
494 else if ((th = threshold_get ("", vl->plugin, NULL,
495 ds->type, vl->type_instance)) != NULL)
497 else if ((th = threshold_get ("", vl->plugin, NULL,
498 ds->type, NULL)) != NULL)
500 else if ((th = threshold_get ("", "", NULL,
501 ds->type, vl->type_instance)) != NULL)
503 else if ((th = threshold_get ("", "", NULL,
504 ds->type, NULL)) != NULL)
508 } /* threshold_t *threshold_search */
510 int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
518 int state_new = STATE_OKAY;
525 if (threshold_tree == NULL)
528 /* Is this lock really necessary? So far, thresholds are only inserted at
530 pthread_mutex_lock (&threshold_lock);
531 th = threshold_search (ds, vl);
532 pthread_mutex_unlock (&threshold_lock);
536 DEBUG ("ut_check_threshold: Found matching threshold");
538 values = uc_get_rate (ds, vl);
542 state_orig = uc_get_state (ds, vl);
544 for (i = 0; i < ds->ds_num; i++)
550 if ((th->flags & UT_FLAG_INVERT) != 0)
556 if ((!isnan (th->failure_min) && (th->failure_min > values[i]))
557 || (!isnan (th->failure_max) && (th->failure_max < values[i])))
559 if ((!isnan (th->warning_min) && (th->warning_min > values[i]))
560 || (!isnan (th->warning_max) && (th->warning_max < values[i])))
563 if ((is_failure != 0) && (state_new != STATE_ERROR))
565 state_new = STATE_ERROR;
568 else if ((is_warning != 0)
569 && (state_new != STATE_ERROR)
570 && (state_new != STATE_WARNING))
572 state_new = STATE_WARNING;
577 if (state_new != state_orig)
578 uc_set_state (ds, vl, state_new);
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)))
589 NOTIFICATION_INIT_VL (&n, vl, ds);
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;
597 n.severity = NOTIF_FAILURE;
602 bufsize = sizeof (n.message);
604 status = snprintf (buf, bufsize, "Host %s, plugin %s",
605 vl->host, vl->plugin);
609 if (vl->plugin_instance[0] != '\0')
611 status = snprintf (buf, bufsize, " (instance %s)",
612 vl->plugin_instance);
617 status = snprintf (buf, bufsize, " type %s", ds->type);
621 if (vl->type_instance[0] != '\0')
623 status = snprintf (buf, bufsize, " (instance %s)",
630 /* Send a okay notification */
631 if (state_new == STATE_OKAY)
633 status = snprintf (buf, bufsize, ": All data sources are within range again.");
642 min = (state_new == STATE_ERROR) ? th->failure_min : th->warning_min;
643 max = (state_new == STATE_ERROR) ? th->failure_max : th->warning_max;
645 if (th->flags & UT_FLAG_INVERT)
647 if (!isnan (min) && !isnan (max))
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",
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);
665 else /* is not inverted */
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);
678 plugin_dispatch_notification (&n);
683 } /* int ut_check_threshold */
685 int ut_check_interesting (const char *name)
687 char *name_copy = NULL;
690 char *plugin_instance = NULL;
692 char *type_instance = NULL;
698 /* If there is no tree nothing is interesting. */
699 if (threshold_tree == NULL)
702 name_copy = strdup (name);
703 if (name_copy == NULL)
705 ERROR ("ut_check_interesting: strdup failed.");
709 status = parse_identifier (name_copy, &host,
710 &plugin, &plugin_instance, &type, &type_instance);
713 ERROR ("ut_check_interesting: parse_identifier failed.");
718 memset (&ds, '\0', sizeof (ds));
719 memset (&vl, '\0', sizeof (vl));
721 strncpy (vl.host, host, sizeof (vl.host));
722 vl.host[sizeof (vl.host) - 1] = '\0';
723 strncpy (vl.plugin, plugin, sizeof (vl.plugin));
724 vl.plugin[sizeof (vl.plugin) - 1] = '\0';
725 if (plugin_instance != NULL)
727 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
728 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
730 strncpy (ds.type, type, sizeof (ds.type));
731 ds.type[sizeof (ds.type) - 1] = '\0';
732 if (type_instance != NULL)
734 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
735 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
739 host = plugin = plugin_instance = type = type_instance = NULL;
741 th = threshold_search (&ds, &vl);
744 if ((th->flags & UT_FLAG_PERSIST) == 0)
747 } /* int ut_check_interesting */
749 /* vim: set sw=2 ts=8 sts=2 tw=78 fdm=marker : */