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 typedef struct threshold_s
36 char host[DATA_MAX_NAME_LEN];
37 char plugin[DATA_MAX_NAME_LEN];
38 char plugin_instance[DATA_MAX_NAME_LEN];
39 char type[DATA_MAX_NAME_LEN];
40 char type_instance[DATA_MAX_NAME_LEN];
48 * Private (static) variables
50 static avl_tree_t *threshold_tree = NULL;
51 static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
55 * Threshold management
56 * ====================
57 * The following functions add, delete, search, etc. configured thresholds to
58 * the underlying AVL trees.
60 static int ut_threshold_add (const threshold_t *th)
62 char name[6 * DATA_MAX_NAME_LEN];
67 if (format_name (name, sizeof (name), th->host,
68 th->plugin, th->plugin_instance,
69 th->type, th->type_instance) != 0)
71 ERROR ("ut_threshold_add: format_name failed.");
75 name_copy = strdup (name);
76 if (name_copy == NULL)
78 ERROR ("ut_threshold_add: strdup failed.");
82 th_copy = (threshold_t *) malloc (sizeof (threshold_t));
86 ERROR ("ut_threshold_add: malloc failed.");
89 memcpy (th_copy, th, sizeof (threshold_t));
91 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
93 pthread_mutex_lock (&threshold_lock);
94 status = avl_insert (threshold_tree, name_copy, th_copy);
95 pthread_mutex_unlock (&threshold_lock);
99 ERROR ("ut_threshold_add: avl_insert (%s) failed.", name);
105 } /* int ut_threshold_add */
107 * End of the threshold management functions
113 * The following approximately two hundred functions are used to handle the
114 * configuration and fill the threshold list.
116 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
118 if ((ci->values_num != 1)
119 || (ci->values[0].type != OCONFIG_TYPE_STRING))
121 WARNING ("threshold values: The `Instance' option needs exactly one "
126 strncpy (th->type_instance, ci->values[0].value.string,
127 sizeof (th->type_instance));
128 th->type_instance[sizeof (th->type_instance) - 1] = '\0';
131 } /* int ut_config_type_instance */
133 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
135 if ((ci->values_num != 1)
136 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
138 WARNING ("threshold values: The `Max' option needs exactly one "
143 th->max = ci->values[0].value.number;
146 } /* int ut_config_type_max */
148 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
150 if ((ci->values_num != 1)
151 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
153 WARNING ("threshold values: The `Min' option needs exactly one "
158 th->min = ci->values[0].value.number;
161 } /* int ut_config_type_min */
163 static int ut_config_type_invert (threshold_t *th, oconfig_item_t *ci)
165 if ((ci->values_num != 1)
166 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
168 WARNING ("threshold values: The `Invert' option needs exactly one "
169 "boolean argument.");
173 th->invert = (ci->values[0].value.boolean) ? 1 : 0;
176 } /* int ut_config_type_invert */
178 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
184 if ((ci->values_num != 1)
185 || (ci->values[0].type != OCONFIG_TYPE_STRING))
187 WARNING ("threshold values: The `Type' block needs exactly one string "
192 if (ci->children_num < 1)
194 WARNING ("threshold values: The `Type' block needs at least one option.");
198 memcpy (&th, th_orig, sizeof (th));
199 strncpy (th.type, ci->values[0].value.string, sizeof (th.type));
200 th.type[sizeof (th.type) - 1] = '\0';
202 for (i = 0; i < ci->children_num; i++)
204 oconfig_item_t *option = ci->children + i;
207 if (strcasecmp ("Instance", option->key) == 0)
208 status = ut_config_type_instance (&th, option);
209 else if (strcasecmp ("Max", option->key) == 0)
210 status = ut_config_type_max (&th, option);
211 else if (strcasecmp ("Min", option->key) == 0)
212 status = ut_config_type_min (&th, option);
213 else if (strcasecmp ("Invert", option->key) == 0)
214 status = ut_config_type_invert (&th, option);
217 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
218 "block.", option->key);
228 status = ut_threshold_add (&th);
232 } /* int ut_config_type */
234 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
236 if ((ci->values_num != 1)
237 || (ci->values[0].type != OCONFIG_TYPE_STRING))
239 WARNING ("threshold values: The `Instance' option needs exactly one "
244 strncpy (th->plugin_instance, ci->values[0].value.string,
245 sizeof (th->plugin_instance));
246 th->plugin_instance[sizeof (th->plugin_instance) - 1] = '\0';
249 } /* int ut_config_plugin_instance */
251 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
257 if ((ci->values_num != 1)
258 || (ci->values[0].type != OCONFIG_TYPE_STRING))
260 WARNING ("threshold values: The `Plugin' block needs exactly one string "
265 if (ci->children_num < 1)
267 WARNING ("threshold values: The `Plugin' block needs at least one nested "
272 memcpy (&th, th_orig, sizeof (th));
273 strncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
274 th.plugin[sizeof (th.plugin) - 1] = '\0';
276 for (i = 0; i < ci->children_num; i++)
278 oconfig_item_t *option = ci->children + i;
281 if (strcasecmp ("Type", option->key) == 0)
282 status = ut_config_type (&th, option);
283 else if (strcasecmp ("Instance", option->key) == 0)
284 status = ut_config_plugin_instance (&th, option);
287 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
288 "block.", option->key);
297 } /* int ut_config_plugin */
299 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
305 if ((ci->values_num != 1)
306 || (ci->values[0].type != OCONFIG_TYPE_STRING))
308 WARNING ("threshold values: The `Host' block needs exactly one string "
313 if (ci->children_num < 1)
315 WARNING ("threshold values: The `Host' block needs at least one nested "
320 memcpy (&th, th_orig, sizeof (th));
321 strncpy (th.host, ci->values[0].value.string, sizeof (th.host));
322 th.host[sizeof (th.host) - 1] = '\0';
324 for (i = 0; i < ci->children_num; i++)
326 oconfig_item_t *option = ci->children + i;
329 if (strcasecmp ("Type", option->key) == 0)
330 status = ut_config_type (&th, option);
331 else if (strcasecmp ("Plugin", option->key) == 0)
332 status = ut_config_plugin (&th, option);
335 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
336 "block.", option->key);
345 } /* int ut_config_host */
347 int ut_config (const oconfig_item_t *ci)
354 if (threshold_tree == NULL)
356 threshold_tree = avl_create ((void *) strcmp);
357 if (threshold_tree == NULL)
359 ERROR ("ut_config: avl_create failed.");
364 memset (&th, '\0', sizeof (th));
368 for (i = 0; i < ci->children_num; i++)
370 oconfig_item_t *option = ci->children + i;
373 if (strcasecmp ("Type", option->key) == 0)
374 status = ut_config_type (&th, option);
375 else if (strcasecmp ("Plugin", option->key) == 0)
376 status = ut_config_plugin (&th, option);
377 else if (strcasecmp ("Host", option->key) == 0)
378 status = ut_config_host (&th, option);
381 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
390 } /* int um_config */
392 * End of the functions used to configure threshold values.
396 static threshold_t *threshold_get (const char *hostname,
397 const char *plugin, const char *plugin_instance,
398 const char *type, const char *type_instance)
400 char name[6 * DATA_MAX_NAME_LEN];
401 threshold_t *th = NULL;
403 format_name (name, sizeof (name),
404 (hostname == NULL) ? "" : hostname,
405 (plugin == NULL) ? "" : plugin, plugin_instance,
406 (type == NULL) ? "" : type, type_instance);
407 name[sizeof (name) - 1] = '\0';
409 if (avl_get (threshold_tree, name, (void *) &th) == 0)
413 } /* threshold_t *threshold_get */
415 static threshold_t *threshold_search (const data_set_t *ds,
416 const value_list_t *vl)
420 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
421 ds->type, vl->type_instance)) != NULL)
423 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
424 ds->type, NULL)) != NULL)
426 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
427 ds->type, vl->type_instance)) != NULL)
429 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
430 ds->type, NULL)) != NULL)
432 else if ((th = threshold_get (vl->host, "", NULL,
433 ds->type, vl->type_instance)) != NULL)
435 else if ((th = threshold_get (vl->host, "", NULL,
436 ds->type, NULL)) != NULL)
438 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
439 ds->type, vl->type_instance)) != NULL)
441 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
442 ds->type, NULL)) != NULL)
444 else if ((th = threshold_get ("", vl->plugin, NULL,
445 ds->type, vl->type_instance)) != NULL)
447 else if ((th = threshold_get ("", vl->plugin, NULL,
448 ds->type, NULL)) != NULL)
450 else if ((th = threshold_get ("", "", NULL,
451 ds->type, vl->type_instance)) != NULL)
453 else if ((th = threshold_get ("", "", NULL,
454 ds->type, NULL)) != NULL)
458 } /* threshold_t *threshold_search */
460 int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
466 if (threshold_tree == NULL)
468 pthread_mutex_lock (&threshold_lock);
469 th = threshold_search (ds, vl);
470 pthread_mutex_unlock (&threshold_lock);
474 DEBUG ("Found matching threshold");
476 values = uc_get_rate (ds, vl);
480 for (i = 0; i < ds->ds_num; i++)
482 if ((th->min > values[i]) || (th->max < values[i]))
489 WARNING ("ut_check_threshold: ds[%s]: %lf <= !%lf <= %lf",
490 ds->ds[i].name, th->min, values[i], th->max);
493 bufsize = sizeof (n.message);
495 status = snprintf (buf, bufsize, "Host %s, plugin %s",
496 vl->host, vl->plugin);
500 if (vl->plugin_instance[0] != '\0')
502 status = snprintf (buf, bufsize, " (instance %s)",
503 vl->plugin_instance);
508 status = snprintf (buf, bufsize, " type %s", ds->type);
512 if (vl->type_instance[0] != '\0')
514 status = snprintf (buf, bufsize, " (instance %s)",
520 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
521 "%lf. That is %s the configured threshold of %lf.",
522 ds->ds[i].name, values[i],
523 (values[i] < th->min) ? "below" : "above",
524 (values[i] < th->min) ? th->min : th->max);
528 n.severity = NOTIF_FAILURE;
531 strncpy (n.host, vl->host, sizeof (n.host));
532 n.host[sizeof (n.host) - 1] = '\0';
534 plugin_dispatch_notification (&n);
536 } /* for (i = 0; i < ds->ds_num; i++) */
541 } /* int ut_check_threshold */
543 /* vim: set sw=2 ts=8 sts=2 tw=78 fdm=marker : */