2 * collectd - src/utils_threshold.c
3 * Copyright (C) 2007 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];
51 * Private (static) variables
53 static avl_tree_t *threshold_tree = NULL;
54 static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
58 * Threshold management
59 * ====================
60 * The following functions add, delete, search, etc. configured thresholds to
61 * the underlying AVL trees.
63 static int ut_threshold_add (const threshold_t *th)
65 char name[6 * DATA_MAX_NAME_LEN];
70 if (format_name (name, sizeof (name), th->host,
71 th->plugin, th->plugin_instance,
72 th->type, th->type_instance) != 0)
74 ERROR ("ut_threshold_add: format_name failed.");
78 name_copy = strdup (name);
79 if (name_copy == NULL)
81 ERROR ("ut_threshold_add: strdup failed.");
85 th_copy = (threshold_t *) malloc (sizeof (threshold_t));
89 ERROR ("ut_threshold_add: malloc failed.");
92 memcpy (th_copy, th, sizeof (threshold_t));
94 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
96 pthread_mutex_lock (&threshold_lock);
97 status = avl_insert (threshold_tree, name_copy, th_copy);
98 pthread_mutex_unlock (&threshold_lock);
102 ERROR ("ut_threshold_add: avl_insert (%s) failed.", name);
108 } /* int ut_threshold_add */
110 * End of the threshold management functions
116 * The following approximately two hundred functions are used to handle the
117 * configuration and fill the threshold list.
119 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
121 if ((ci->values_num != 1)
122 || (ci->values[0].type != OCONFIG_TYPE_STRING))
124 WARNING ("threshold values: The `Instance' option needs exactly one "
129 strncpy (th->type_instance, ci->values[0].value.string,
130 sizeof (th->type_instance));
131 th->type_instance[sizeof (th->type_instance) - 1] = '\0';
134 } /* int ut_config_type_instance */
136 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
138 if ((ci->values_num != 1)
139 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
141 WARNING ("threshold values: The `Max' option needs exactly one "
146 th->max = ci->values[0].value.number;
149 } /* int ut_config_type_max */
151 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
153 if ((ci->values_num != 1)
154 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
156 WARNING ("threshold values: The `Min' option needs exactly one "
161 th->min = ci->values[0].value.number;
164 } /* int ut_config_type_min */
166 static int ut_config_type_invert (threshold_t *th, oconfig_item_t *ci)
168 if ((ci->values_num != 1)
169 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
171 WARNING ("threshold values: The `Invert' option needs exactly one "
172 "boolean argument.");
176 if (ci->values[0].value.boolean)
177 th->flags |= UT_FLAG_INVERT;
179 th->flags &= ~UT_FLAG_INVERT;
182 } /* int ut_config_type_invert */
184 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
190 if ((ci->values_num != 1)
191 || (ci->values[0].type != OCONFIG_TYPE_STRING))
193 WARNING ("threshold values: The `Type' block needs exactly one string "
198 if (ci->children_num < 1)
200 WARNING ("threshold values: The `Type' block needs at least one option.");
204 memcpy (&th, th_orig, sizeof (th));
205 strncpy (th.type, ci->values[0].value.string, sizeof (th.type));
206 th.type[sizeof (th.type) - 1] = '\0';
211 for (i = 0; i < ci->children_num; i++)
213 oconfig_item_t *option = ci->children + i;
216 if (strcasecmp ("Instance", option->key) == 0)
217 status = ut_config_type_instance (&th, option);
218 else if (strcasecmp ("Max", option->key) == 0)
219 status = ut_config_type_max (&th, option);
220 else if (strcasecmp ("Min", option->key) == 0)
221 status = ut_config_type_min (&th, option);
222 else if (strcasecmp ("Invert", option->key) == 0)
223 status = ut_config_type_invert (&th, option);
226 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
227 "block.", option->key);
237 status = ut_threshold_add (&th);
241 } /* int ut_config_type */
243 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
245 if ((ci->values_num != 1)
246 || (ci->values[0].type != OCONFIG_TYPE_STRING))
248 WARNING ("threshold values: The `Instance' option needs exactly one "
253 strncpy (th->plugin_instance, ci->values[0].value.string,
254 sizeof (th->plugin_instance));
255 th->plugin_instance[sizeof (th->plugin_instance) - 1] = '\0';
258 } /* int ut_config_plugin_instance */
260 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
266 if ((ci->values_num != 1)
267 || (ci->values[0].type != OCONFIG_TYPE_STRING))
269 WARNING ("threshold values: The `Plugin' block needs exactly one string "
274 if (ci->children_num < 1)
276 WARNING ("threshold values: The `Plugin' block needs at least one nested "
281 memcpy (&th, th_orig, sizeof (th));
282 strncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
283 th.plugin[sizeof (th.plugin) - 1] = '\0';
285 for (i = 0; i < ci->children_num; i++)
287 oconfig_item_t *option = ci->children + i;
290 if (strcasecmp ("Type", option->key) == 0)
291 status = ut_config_type (&th, option);
292 else if (strcasecmp ("Instance", option->key) == 0)
293 status = ut_config_plugin_instance (&th, option);
296 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
297 "block.", option->key);
306 } /* int ut_config_plugin */
308 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
314 if ((ci->values_num != 1)
315 || (ci->values[0].type != OCONFIG_TYPE_STRING))
317 WARNING ("threshold values: The `Host' block needs exactly one string "
322 if (ci->children_num < 1)
324 WARNING ("threshold values: The `Host' block needs at least one nested "
329 memcpy (&th, th_orig, sizeof (th));
330 strncpy (th.host, ci->values[0].value.string, sizeof (th.host));
331 th.host[sizeof (th.host) - 1] = '\0';
333 for (i = 0; i < ci->children_num; i++)
335 oconfig_item_t *option = ci->children + i;
338 if (strcasecmp ("Type", option->key) == 0)
339 status = ut_config_type (&th, option);
340 else if (strcasecmp ("Plugin", option->key) == 0)
341 status = ut_config_plugin (&th, option);
344 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
345 "block.", option->key);
354 } /* int ut_config_host */
356 int ut_config (const oconfig_item_t *ci)
363 if (threshold_tree == NULL)
365 threshold_tree = avl_create ((void *) strcmp);
366 if (threshold_tree == NULL)
368 ERROR ("ut_config: avl_create failed.");
373 memset (&th, '\0', sizeof (th));
377 for (i = 0; i < ci->children_num; i++)
379 oconfig_item_t *option = ci->children + i;
382 if (strcasecmp ("Type", option->key) == 0)
383 status = ut_config_type (&th, option);
384 else if (strcasecmp ("Plugin", option->key) == 0)
385 status = ut_config_plugin (&th, option);
386 else if (strcasecmp ("Host", option->key) == 0)
387 status = ut_config_host (&th, option);
390 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
399 } /* int um_config */
401 * End of the functions used to configure threshold values.
405 static threshold_t *threshold_get (const char *hostname,
406 const char *plugin, const char *plugin_instance,
407 const char *type, const char *type_instance)
409 char name[6 * DATA_MAX_NAME_LEN];
410 threshold_t *th = NULL;
412 format_name (name, sizeof (name),
413 (hostname == NULL) ? "" : hostname,
414 (plugin == NULL) ? "" : plugin, plugin_instance,
415 (type == NULL) ? "" : type, type_instance);
416 name[sizeof (name) - 1] = '\0';
418 if (avl_get (threshold_tree, name, (void *) &th) == 0)
422 } /* threshold_t *threshold_get */
424 static threshold_t *threshold_search (const data_set_t *ds,
425 const value_list_t *vl)
429 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
430 ds->type, vl->type_instance)) != NULL)
432 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
433 ds->type, NULL)) != NULL)
435 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
436 ds->type, vl->type_instance)) != NULL)
438 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
439 ds->type, NULL)) != NULL)
441 else if ((th = threshold_get (vl->host, "", NULL,
442 ds->type, vl->type_instance)) != NULL)
444 else if ((th = threshold_get (vl->host, "", NULL,
445 ds->type, NULL)) != NULL)
447 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
448 ds->type, vl->type_instance)) != NULL)
450 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
451 ds->type, NULL)) != NULL)
453 else if ((th = threshold_get ("", vl->plugin, NULL,
454 ds->type, vl->type_instance)) != NULL)
456 else if ((th = threshold_get ("", vl->plugin, NULL,
457 ds->type, NULL)) != NULL)
459 else if ((th = threshold_get ("", "", NULL,
460 ds->type, vl->type_instance)) != NULL)
462 else if ((th = threshold_get ("", "", NULL,
463 ds->type, NULL)) != NULL)
467 } /* threshold_t *threshold_search */
469 int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
475 if (threshold_tree == NULL)
477 pthread_mutex_lock (&threshold_lock);
478 th = threshold_search (ds, vl);
479 pthread_mutex_unlock (&threshold_lock);
483 DEBUG ("ut_check_threshold: Found matching threshold");
485 values = uc_get_rate (ds, vl);
489 for (i = 0; i < ds->ds_num; i++)
491 int out_of_range = 0;
494 if ((th->flags & UT_FLAG_INVERT) != 0)
496 if ((!isnan (th->min) && (th->min > values[i]))
497 || (!isnan (th->max) && (th->max < values[i])))
500 /* If only one of these conditions is true, there is a problem */
501 if ((out_of_range + is_inverted) == 1)
508 WARNING ("ut_check_threshold: ds[%s]: %lf <= !%lf <= %lf (invert: %s)",
509 ds->ds[i].name, th->min, values[i], th->max,
510 is_inverted ? "true" : "false");
513 bufsize = sizeof (n.message);
515 status = snprintf (buf, bufsize, "Host %s, plugin %s",
516 vl->host, vl->plugin);
520 if (vl->plugin_instance[0] != '\0')
522 status = snprintf (buf, bufsize, " (instance %s)",
523 vl->plugin_instance);
528 status = snprintf (buf, bufsize, " type %s", ds->type);
532 if (vl->type_instance[0] != '\0')
534 status = snprintf (buf, bufsize, " (instance %s)",
542 if (!isnan (th->min) && !isnan (th->max))
544 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
545 "%lf. That is within the critical region of %lf and %lf.",
546 ds->ds[i].name, values[i],
551 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
552 "%lf. That is %s the configured threshold of %lf.",
553 ds->ds[i].name, values[i],
554 isnan (th->min) ? "below" : "above",
555 isnan (th->min) ? th->max : th->min);
558 else /* (!is_inverted) */
560 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
561 "%lf. That is %s the configured threshold of %lf.",
562 ds->ds[i].name, values[i],
563 (values[i] < th->min) ? "below" : "above",
564 (values[i] < th->min) ? th->min : th->max);
569 n.severity = NOTIF_FAILURE;
572 strncpy (n.host, vl->host, sizeof (n.host));
573 n.host[sizeof (n.host) - 1] = '\0';
575 plugin_dispatch_notification (&n);
577 } /* for (i = 0; i < ds->ds_num; i++) */
582 } /* int ut_check_threshold */
584 int ut_check_interesting (const char *name)
586 char *name_copy = NULL;
589 char *plugin_instance = NULL;
591 char *type_instance = NULL;
597 /* If there is no tree nothing is interesting. */
598 if (threshold_tree == NULL)
601 name_copy = strdup (name);
602 if (name_copy == NULL)
604 ERROR ("ut_check_interesting: strdup failed.");
608 status = parse_identifier (name_copy, &host,
609 &plugin, &plugin_instance, &type, &type_instance);
612 ERROR ("ut_check_interesting: parse_identifier failed.");
616 memset (&ds, '\0', sizeof (ds));
617 memset (&vl, '\0', sizeof (vl));
619 strncpy (vl.host, host, sizeof (vl.host));
620 vl.host[sizeof (vl.host) - 1] = '\0';
621 strncpy (vl.plugin, plugin, sizeof (vl.plugin));
622 vl.plugin[sizeof (vl.plugin) - 1] = '\0';
623 if (plugin_instance != NULL)
625 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
626 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
628 strncpy (ds.type, type, sizeof (ds.type));
629 ds.type[sizeof (ds.type) - 1] = '\0';
630 if (type_instance != NULL)
632 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
633 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
637 host = plugin = plugin_instance = type = type_instance = NULL;
639 th = threshold_search (&ds, &vl);
644 } /* int ut_check_interesting */
646 /* vim: set sw=2 ts=8 sts=2 tw=78 fdm=marker : */