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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
28 #include <assert.h>
29 #include <pthread.h>
31 /*
32 * Private data structures
33 * {{{ */
34 #define UT_FLAG_INVERT 0x01
35 #define UT_FLAG_PERSIST 0x02
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 /* }}} */
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 /* }}} */
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;
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 }
80 name_copy = strdup (name);
81 if (name_copy == NULL)
82 {
83 ERROR ("ut_threshold_add: strdup failed.");
84 return (-1);
85 }
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));
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);
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 }
109 return (status);
110 } /* int ut_threshold_add */
111 /*
112 * End of the threshold management functions
113 * }}} */
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 }
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';
135 return (0);
136 } /* int ut_config_type_instance */
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 }
148 if (strcasecmp (ci->key, "WarningMax") == 0)
149 th->warning_min = ci->values[0].value.number;
150 else
151 th->failure_min = ci->values[0].value.number;
153 return (0);
154 } /* int ut_config_type_max */
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 }
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;
171 return (0);
172 } /* int ut_config_type_min */
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 }
184 if (ci->values[0].value.boolean)
185 th->flags |= UT_FLAG_INVERT;
186 else
187 th->flags &= ~UT_FLAG_INVERT;
189 return (0);
190 } /* int ut_config_type_invert */
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 }
202 if (ci->values[0].value.boolean)
203 th->flags |= UT_FLAG_PERSIST;
204 else
205 th->flags &= ~UT_FLAG_PERSIST;
207 return (0);
208 } /* int ut_config_type_persist */
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;
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 }
224 if (ci->children_num < 1)
225 {
226 WARNING ("threshold values: The `Type' block needs at least one option.");
227 return (-1);
228 }
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++)
240 {
241 oconfig_item_t *option = ci->children + i;
242 status = 0;
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 }
263 if (status != 0)
264 break;
265 }
267 if (status == 0)
268 {
269 status = ut_threshold_add (&th);
270 }
272 return (status);
273 } /* int ut_config_type */
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 }
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';
289 return (0);
290 } /* int ut_config_plugin_instance */
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;
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 }
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 }
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++)
318 {
319 oconfig_item_t *option = ci->children + i;
320 status = 0;
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 }
333 if (status != 0)
334 break;
335 }
337 return (status);
338 } /* int ut_config_plugin */
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;
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 }
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 }
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++)
366 {
367 oconfig_item_t *option = ci->children + i;
368 status = 0;
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 }
381 if (status != 0)
382 break;
383 }
385 return (status);
386 } /* int ut_config_host */
388 int ut_config (const oconfig_item_t *ci)
389 {
390 int i;
391 int status = 0;
393 threshold_t th;
395 if (ci->values_num != 0)
396 {
397 ERROR ("threshold values: The `Threshold' block may not have any "
398 "arguments.");
399 return (-1);
400 }
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 }
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++)
419 {
420 oconfig_item_t *option = ci->children + i;
421 status = 0;
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 }
435 if (status != 0)
436 break;
437 }
439 return (status);
440 } /* int um_config */
441 /*
442 * End of the functions used to configure threshold values.
443 */
444 /* }}} */
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;
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)
460 return (th);
461 else
462 return (NULL);
463 } /* threshold_t *threshold_get */
465 static threshold_t *threshold_search (const data_set_t *ds,
466 const value_list_t *vl)
467 {
468 threshold_t *th;
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);
507 return (NULL);
508 } /* threshold_t *threshold_search */
510 int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
511 {
512 threshold_t *th;
513 gauge_t *values;
514 int i;
516 if (threshold_tree == NULL)
517 return (0);
518 /* Is this lock really necessary? So far, thresholds are only inserted at
519 * startup. -octo */
520 pthread_mutex_lock (&threshold_lock);
521 th = threshold_search (ds, vl);
522 pthread_mutex_unlock (&threshold_lock);
523 if (th == NULL)
524 return (0);
526 DEBUG ("ut_check_threshold: Found matching threshold");
528 values = uc_get_rate (ds, vl);
529 if (values == NULL)
530 return (0);
532 for (i = 0; i < ds->ds_num; i++)
533 {
534 int is_inverted = 0;
535 int is_warning = 0;
536 int is_failure = 0;
538 if ((th->flags & UT_FLAG_INVERT) != 0)
539 is_inverted = 1;
540 if ((!isnan (th->failure_min) && (th->failure_min > values[i]))
541 || (!isnan (th->failure_max) && (th->failure_max < values[i])))
542 is_failure = is_inverted - 1;
543 if ((!isnan (th->warning_min) && (th->warning_min > values[i]))
544 || (!isnan (th->warning_max) && (th->warning_max < values[i])))
545 is_warning = is_inverted - 1;
547 if ((is_failure != 0) || (is_warning != 0))
548 {
549 notification_t n;
550 char *buf;
551 size_t bufsize;
552 int status;
554 double min;
555 double max;
557 min = (is_failure != 0) ? th->failure_min : th->warning_min;
558 max = (is_failure != 0) ? th->failure_max : th->warning_max;
560 DEBUG ("ut_check_threshold: ds[%s]: %lf <= !%lf <= %lf (invert: %s)",
561 ds->ds[i].name, min, values[i], max,
562 is_inverted ? "true" : "false");
564 /* Copy the associative members */
565 NOTIFICATION_INIT_VL (&n, vl, ds);
567 n.severity = (is_failure != 0) ? NOTIF_FAILURE : NOTIF_WARNING;
568 n.time = vl->time;
570 buf = n.message;
571 bufsize = sizeof (n.message);
573 status = snprintf (buf, bufsize, "Host %s, plugin %s",
574 vl->host, vl->plugin);
575 buf += status;
576 bufsize -= status;
578 if (vl->plugin_instance[0] != '\0')
579 {
580 status = snprintf (buf, bufsize, " (instance %s)",
581 vl->plugin_instance);
582 buf += status;
583 bufsize -= status;
584 }
586 status = snprintf (buf, bufsize, " type %s", ds->type);
587 buf += status;
588 bufsize -= status;
590 if (vl->type_instance[0] != '\0')
591 {
592 status = snprintf (buf, bufsize, " (instance %s)",
593 vl->type_instance);
594 buf += status;
595 bufsize -= status;
596 }
598 if (is_inverted)
599 {
600 if (!isnan (min) && !isnan (max))
601 {
602 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
603 "%f. That is within the %s region of %f and %f.",
604 ds->ds[i].name, values[i],
605 (is_failure != 0) ? "failure" : "warning",
606 min, min);
607 }
608 else
609 {
610 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
611 "%f. That is %s the %s threshold of %f.",
612 ds->ds[i].name, values[i],
613 isnan (min) ? "below" : "above",
614 (is_failure != 0) ? "failure" : "warning",
615 isnan (min) ? max : min);
616 }
617 }
618 else /* (!is_inverted) */
619 {
620 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
621 "%f. That is %s the %s threshold of %f.",
622 ds->ds[i].name, values[i],
623 (values[i] < min) ? "below" : "above",
624 (is_failure != 0) ? "failure" : "warning",
625 (values[i] < min) ? min : max);
626 }
627 buf += status;
628 bufsize -= status;
630 plugin_dispatch_notification (&n);
631 }
632 } /* for (i = 0; i < ds->ds_num; i++) */
634 sfree (values);
636 return (0);
637 } /* int ut_check_threshold */
639 int ut_check_interesting (const char *name)
640 {
641 char *name_copy = NULL;
642 char *host = NULL;
643 char *plugin = NULL;
644 char *plugin_instance = NULL;
645 char *type = NULL;
646 char *type_instance = NULL;
647 int status;
648 data_set_t ds;
649 value_list_t vl;
650 threshold_t *th;
652 /* If there is no tree nothing is interesting. */
653 if (threshold_tree == NULL)
654 return (0);
656 name_copy = strdup (name);
657 if (name_copy == NULL)
658 {
659 ERROR ("ut_check_interesting: strdup failed.");
660 return (-1);
661 }
663 status = parse_identifier (name_copy, &host,
664 &plugin, &plugin_instance, &type, &type_instance);
665 if (status != 0)
666 {
667 ERROR ("ut_check_interesting: parse_identifier failed.");
668 return (-1);
669 }
671 memset (&ds, '\0', sizeof (ds));
672 memset (&vl, '\0', sizeof (vl));
674 strncpy (vl.host, host, sizeof (vl.host));
675 vl.host[sizeof (vl.host) - 1] = '\0';
676 strncpy (vl.plugin, plugin, sizeof (vl.plugin));
677 vl.plugin[sizeof (vl.plugin) - 1] = '\0';
678 if (plugin_instance != NULL)
679 {
680 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
681 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
682 }
683 strncpy (ds.type, type, sizeof (ds.type));
684 ds.type[sizeof (ds.type) - 1] = '\0';
685 if (type_instance != NULL)
686 {
687 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
688 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
689 }
691 sfree (name_copy);
692 host = plugin = plugin_instance = type = type_instance = NULL;
694 th = threshold_search (&ds, &vl);
695 if (th == NULL)
696 return (0);
697 if ((th->flags & UT_FLAG_PERSIST) == 0)
698 return (1);
699 return (2);
700 } /* int ut_check_interesting */
702 /* vim: set sw=2 ts=8 sts=2 tw=78 fdm=marker : */