1 /**
2 * collectd - src/threshold.c
3 * Copyright (C) 2007-2010 Florian Forster
4 * Copyright (C) 2008-2009 Sebastian Harl
5 * Copyright (C) 2009 Andrés J. Díaz
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Author:
21 * Florian octo Forster <octo at collectd.org>
22 * Sebastian Harl <sh at tokkee.org>
23 * Andrés J. Díaz <ajdiaz at connectical.com>
24 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "utils_avltree.h"
30 #include "utils_cache.h"
31 #include "utils_threshold.h"
33 #include <assert.h>
34 #include <pthread.h>
36 /*
37 * Threshold management
38 * ====================
39 * The following functions add, delete, search, etc. configured thresholds to
40 * the underlying AVL trees.
41 */
43 /*
44 * int ut_threshold_add
45 *
46 * Adds a threshold configuration to the list of thresholds. The threshold_t
47 * structure is copied and may be destroyed after this call. Returns zero on
48 * success, non-zero otherwise.
49 */
50 static int ut_threshold_add (const threshold_t *th)
51 { /* {{{ */
52 char name[6 * DATA_MAX_NAME_LEN];
53 char *name_copy;
54 threshold_t *th_copy;
55 threshold_t *th_ptr;
56 int status = 0;
58 if (format_name (name, sizeof (name), th->host,
59 th->plugin, th->plugin_instance,
60 th->type, th->type_instance) != 0)
61 {
62 ERROR ("ut_threshold_add: format_name failed.");
63 return (-1);
64 }
66 name_copy = strdup (name);
67 if (name_copy == NULL)
68 {
69 ERROR ("ut_threshold_add: strdup failed.");
70 return (-1);
71 }
73 th_copy = (threshold_t *) malloc (sizeof (threshold_t));
74 if (th_copy == NULL)
75 {
76 sfree (name_copy);
77 ERROR ("ut_threshold_add: malloc failed.");
78 return (-1);
79 }
80 memcpy (th_copy, th, sizeof (threshold_t));
81 th_ptr = NULL;
83 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
85 pthread_mutex_lock (&threshold_lock);
87 th_ptr = threshold_get (th->host, th->plugin, th->plugin_instance,
88 th->type, th->type_instance);
90 while ((th_ptr != NULL) && (th_ptr->next != NULL))
91 th_ptr = th_ptr->next;
93 if (th_ptr == NULL) /* no such threshold yet */
94 {
95 status = c_avl_insert (threshold_tree, name_copy, th_copy);
96 }
97 else /* th_ptr points to the last threshold in the list */
98 {
99 th_ptr->next = th_copy;
100 /* name_copy isn't needed */
101 sfree (name_copy);
102 }
104 pthread_mutex_unlock (&threshold_lock);
106 if (status != 0)
107 {
108 ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
109 sfree (name_copy);
110 sfree (th_copy);
111 }
113 return (status);
114 } /* }}} int ut_threshold_add */
116 /*
117 * Configuration
118 * =============
119 * The following approximately two hundred functions are used to handle the
120 * configuration and fill the threshold list.
121 * {{{ */
122 static int ut_config_type_datasource (threshold_t *th, oconfig_item_t *ci)
123 {
124 if ((ci->values_num != 1)
125 || (ci->values[0].type != OCONFIG_TYPE_STRING))
126 {
127 WARNING ("threshold values: The `DataSource' option needs exactly one "
128 "string argument.");
129 return (-1);
130 }
132 sstrncpy (th->data_source, ci->values[0].value.string,
133 sizeof (th->data_source));
135 return (0);
136 } /* int ut_config_type_datasource */
138 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
139 {
140 if ((ci->values_num != 1)
141 || (ci->values[0].type != OCONFIG_TYPE_STRING))
142 {
143 WARNING ("threshold values: The `Instance' option needs exactly one "
144 "string argument.");
145 return (-1);
146 }
148 sstrncpy (th->type_instance, ci->values[0].value.string,
149 sizeof (th->type_instance));
151 return (0);
152 } /* int ut_config_type_instance */
154 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
155 {
156 if ((ci->values_num != 1)
157 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
158 {
159 WARNING ("threshold values: The `%s' option needs exactly one "
160 "number argument.", ci->key);
161 return (-1);
162 }
164 if (strcasecmp (ci->key, "WarningMax") == 0)
165 th->warning_max = ci->values[0].value.number;
166 else
167 th->failure_max = ci->values[0].value.number;
169 return (0);
170 } /* int ut_config_type_max */
172 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
173 {
174 if ((ci->values_num != 1)
175 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
176 {
177 WARNING ("threshold values: The `%s' option needs exactly one "
178 "number argument.", ci->key);
179 return (-1);
180 }
182 if (strcasecmp (ci->key, "WarningMin") == 0)
183 th->warning_min = ci->values[0].value.number;
184 else
185 th->failure_min = ci->values[0].value.number;
187 return (0);
188 } /* int ut_config_type_min */
190 static int ut_config_type_hits (threshold_t *th, oconfig_item_t *ci)
191 {
192 if ((ci->values_num != 1)
193 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
194 {
195 WARNING ("threshold values: The `%s' option needs exactly one "
196 "number argument.", ci->key);
197 return (-1);
198 }
200 th->hits = ci->values[0].value.number;
202 return (0);
203 } /* int ut_config_type_hits */
205 static int ut_config_type_hysteresis (threshold_t *th, oconfig_item_t *ci)
206 {
207 if ((ci->values_num != 1)
208 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
209 {
210 WARNING ("threshold values: The `%s' option needs exactly one "
211 "number argument.", ci->key);
212 return (-1);
213 }
215 th->hysteresis = ci->values[0].value.number;
217 return (0);
218 } /* int ut_config_type_hysteresis */
220 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
221 {
222 int i;
223 threshold_t th;
224 int status = 0;
226 if ((ci->values_num != 1)
227 || (ci->values[0].type != OCONFIG_TYPE_STRING))
228 {
229 WARNING ("threshold values: The `Type' block needs exactly one string "
230 "argument.");
231 return (-1);
232 }
234 if (ci->children_num < 1)
235 {
236 WARNING ("threshold values: The `Type' block needs at least one option.");
237 return (-1);
238 }
240 memcpy (&th, th_orig, sizeof (th));
241 sstrncpy (th.type, ci->values[0].value.string, sizeof (th.type));
243 th.warning_min = NAN;
244 th.warning_max = NAN;
245 th.failure_min = NAN;
246 th.failure_max = NAN;
247 th.hits = 0;
248 th.hysteresis = 0;
249 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
251 for (i = 0; i < ci->children_num; i++)
252 {
253 oconfig_item_t *option = ci->children + i;
254 status = 0;
256 if (strcasecmp ("Instance", option->key) == 0)
257 status = ut_config_type_instance (&th, option);
258 else if (strcasecmp ("DataSource", option->key) == 0)
259 status = ut_config_type_datasource (&th, option);
260 else if ((strcasecmp ("WarningMax", option->key) == 0)
261 || (strcasecmp ("FailureMax", option->key) == 0))
262 status = ut_config_type_max (&th, option);
263 else if ((strcasecmp ("WarningMin", option->key) == 0)
264 || (strcasecmp ("FailureMin", option->key) == 0))
265 status = ut_config_type_min (&th, option);
266 else if (strcasecmp ("Interesting", option->key) == 0)
267 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INTERESTING);
268 else if (strcasecmp ("Invert", option->key) == 0)
269 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INVERT);
270 else if (strcasecmp ("Persist", option->key) == 0)
271 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST);
272 else if (strcasecmp ("PersistOK", option->key) == 0)
273 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST_OK);
274 else if (strcasecmp ("Percentage", option->key) == 0)
275 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERCENTAGE);
276 else if (strcasecmp ("Hits", option->key) == 0)
277 status = ut_config_type_hits (&th, option);
278 else if (strcasecmp ("Hysteresis", option->key) == 0)
279 status = ut_config_type_hysteresis (&th, option);
280 else
281 {
282 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
283 "block.", option->key);
284 status = -1;
285 }
287 if (status != 0)
288 break;
289 }
291 if (status == 0)
292 {
293 status = ut_threshold_add (&th);
294 }
296 return (status);
297 } /* int ut_config_type */
299 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
300 {
301 if ((ci->values_num != 1)
302 || (ci->values[0].type != OCONFIG_TYPE_STRING))
303 {
304 WARNING ("threshold values: The `Instance' option needs exactly one "
305 "string argument.");
306 return (-1);
307 }
309 sstrncpy (th->plugin_instance, ci->values[0].value.string,
310 sizeof (th->plugin_instance));
312 return (0);
313 } /* int ut_config_plugin_instance */
315 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
316 {
317 int i;
318 threshold_t th;
319 int status = 0;
321 if ((ci->values_num != 1)
322 || (ci->values[0].type != OCONFIG_TYPE_STRING))
323 {
324 WARNING ("threshold values: The `Plugin' block needs exactly one string "
325 "argument.");
326 return (-1);
327 }
329 if (ci->children_num < 1)
330 {
331 WARNING ("threshold values: The `Plugin' block needs at least one nested "
332 "block.");
333 return (-1);
334 }
336 memcpy (&th, th_orig, sizeof (th));
337 sstrncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
339 for (i = 0; i < ci->children_num; i++)
340 {
341 oconfig_item_t *option = ci->children + i;
342 status = 0;
344 if (strcasecmp ("Type", option->key) == 0)
345 status = ut_config_type (&th, option);
346 else if (strcasecmp ("Instance", option->key) == 0)
347 status = ut_config_plugin_instance (&th, option);
348 else
349 {
350 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
351 "block.", option->key);
352 status = -1;
353 }
355 if (status != 0)
356 break;
357 }
359 return (status);
360 } /* int ut_config_plugin */
362 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
363 {
364 int i;
365 threshold_t th;
366 int status = 0;
368 if ((ci->values_num != 1)
369 || (ci->values[0].type != OCONFIG_TYPE_STRING))
370 {
371 WARNING ("threshold values: The `Host' block needs exactly one string "
372 "argument.");
373 return (-1);
374 }
376 if (ci->children_num < 1)
377 {
378 WARNING ("threshold values: The `Host' block needs at least one nested "
379 "block.");
380 return (-1);
381 }
383 memcpy (&th, th_orig, sizeof (th));
384 sstrncpy (th.host, ci->values[0].value.string, sizeof (th.host));
386 for (i = 0; i < ci->children_num; i++)
387 {
388 oconfig_item_t *option = ci->children + i;
389 status = 0;
391 if (strcasecmp ("Type", option->key) == 0)
392 status = ut_config_type (&th, option);
393 else if (strcasecmp ("Plugin", option->key) == 0)
394 status = ut_config_plugin (&th, option);
395 else
396 {
397 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
398 "block.", option->key);
399 status = -1;
400 }
402 if (status != 0)
403 break;
404 }
406 return (status);
407 } /* int ut_config_host */
408 /*
409 * End of the functions used to configure threshold values.
410 */
411 /* }}} */
413 /*
414 * int ut_report_state
415 *
416 * Checks if the `state' differs from the old state and creates a notification
417 * if appropriate.
418 * Does not fail.
419 */
420 static int ut_report_state (const data_set_t *ds,
421 const value_list_t *vl,
422 const threshold_t *th,
423 const gauge_t *values,
424 int ds_index,
425 int state)
426 { /* {{{ */
427 int state_old;
428 notification_t n;
430 char *buf;
431 size_t bufsize;
433 int status;
435 /* Check if hits matched */
436 if ( (th->hits != 0) )
437 {
438 int hits = uc_get_hits(ds,vl);
439 /* STATE_OKAY resets hits unless PERSIST_OK flag is set. Hits resets if
440 * threshold is hit. */
441 if ( ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) ) || (hits > th->hits) )
442 {
443 DEBUG("ut_report_state: reset uc_get_hits = 0");
444 uc_set_hits(ds,vl,0); /* reset hit counter and notify */
445 } else {
446 DEBUG("ut_report_state: th->hits = %d, uc_get_hits = %d",th->hits,uc_get_hits(ds,vl));
447 (void) uc_inc_hits(ds,vl,1); /* increase hit counter */
448 return (0);
449 }
450 } /* end check hits */
452 state_old = uc_get_state (ds, vl);
454 /* If the state didn't change, report if `persistent' is specified. If the
455 * state is `okay', then only report if `persist_ok` flag is set. */
456 if (state == state_old)
457 {
458 if ((th->flags & UT_FLAG_PERSIST) == 0)
459 return (0);
460 else if ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) )
461 return (0);
462 }
464 if (state != state_old)
465 uc_set_state (ds, vl, state);
467 NOTIFICATION_INIT_VL (&n, vl);
469 buf = n.message;
470 bufsize = sizeof (n.message);
472 if (state == STATE_OKAY)
473 n.severity = NOTIF_OKAY;
474 else if (state == STATE_WARNING)
475 n.severity = NOTIF_WARNING;
476 else
477 n.severity = NOTIF_FAILURE;
479 n.time = vl->time;
481 status = ssnprintf (buf, bufsize, "Host %s, plugin %s",
482 vl->host, vl->plugin);
483 buf += status;
484 bufsize -= status;
486 if (vl->plugin_instance[0] != '\0')
487 {
488 status = ssnprintf (buf, bufsize, " (instance %s)",
489 vl->plugin_instance);
490 buf += status;
491 bufsize -= status;
492 }
494 status = ssnprintf (buf, bufsize, " type %s", vl->type);
495 buf += status;
496 bufsize -= status;
498 if (vl->type_instance[0] != '\0')
499 {
500 status = ssnprintf (buf, bufsize, " (instance %s)",
501 vl->type_instance);
502 buf += status;
503 bufsize -= status;
504 }
506 plugin_notification_meta_add_string (&n, "DataSource",
507 ds->ds[ds_index].name);
508 plugin_notification_meta_add_double (&n, "CurrentValue", values[ds_index]);
509 plugin_notification_meta_add_double (&n, "WarningMin", th->warning_min);
510 plugin_notification_meta_add_double (&n, "WarningMax", th->warning_max);
511 plugin_notification_meta_add_double (&n, "FailureMin", th->failure_min);
512 plugin_notification_meta_add_double (&n, "FailureMax", th->failure_max);
514 /* Send an okay notification */
515 if (state == STATE_OKAY)
516 {
517 if (state_old == STATE_MISSING)
518 status = ssnprintf (buf, bufsize,
519 ": Value is no longer missing.");
520 else
521 status = ssnprintf (buf, bufsize,
522 ": All data sources are within range again.");
523 buf += status;
524 bufsize -= status;
525 }
526 else
527 {
528 double min;
529 double max;
531 min = (state == STATE_ERROR) ? th->failure_min : th->warning_min;
532 max = (state == STATE_ERROR) ? th->failure_max : th->warning_max;
534 if (th->flags & UT_FLAG_INVERT)
535 {
536 if (!isnan (min) && !isnan (max))
537 {
538 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
539 "%f. That is within the %s region of %f%s and %f%s.",
540 ds->ds[ds_index].name, values[ds_index],
541 (state == STATE_ERROR) ? "failure" : "warning",
542 min, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "",
543 max, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
544 }
545 else
546 {
547 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
548 "%f. That is %s the %s threshold of %f%s.",
549 ds->ds[ds_index].name, values[ds_index],
550 isnan (min) ? "below" : "above",
551 (state == STATE_ERROR) ? "failure" : "warning",
552 isnan (min) ? max : min,
553 ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
554 }
555 }
556 else if (th->flags & UT_FLAG_PERCENTAGE)
557 {
558 gauge_t value;
559 gauge_t sum;
560 int i;
562 sum = 0.0;
563 for (i = 0; i < vl->values_len; i++)
564 {
565 if (isnan (values[i]))
566 continue;
568 sum += values[i];
569 }
571 if (sum == 0.0)
572 value = NAN;
573 else
574 value = 100.0 * values[ds_index] / sum;
576 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
577 "%g (%.2f%%). That is %s the %s threshold of %.2f%%.",
578 ds->ds[ds_index].name, values[ds_index], value,
579 (value < min) ? "below" : "above",
580 (state == STATE_ERROR) ? "failure" : "warning",
581 (value < min) ? min : max);
582 }
583 else /* is not inverted */
584 {
585 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
586 "%f. That is %s the %s threshold of %f.",
587 ds->ds[ds_index].name, values[ds_index],
588 (values[ds_index] < min) ? "below" : "above",
589 (state == STATE_ERROR) ? "failure" : "warning",
590 (values[ds_index] < min) ? min : max);
591 }
592 buf += status;
593 bufsize -= status;
594 }
596 plugin_dispatch_notification (&n);
598 plugin_notification_meta_free (n.meta);
599 return (0);
600 } /* }}} int ut_report_state */
602 /*
603 * int ut_check_one_data_source
604 *
605 * Checks one data source against the given threshold configuration. If the
606 * `DataSource' option is set in the threshold, and the name does NOT match,
607 * `okay' is returned. If the threshold does match, its failure and warning
608 * min and max values are checked and `failure' or `warning' is returned if
609 * appropriate.
610 * Does not fail.
611 */
612 static int ut_check_one_data_source (const data_set_t *ds,
613 const value_list_t __attribute__((unused)) *vl,
614 const threshold_t *th,
615 const gauge_t *values,
616 int ds_index)
617 { /* {{{ */
618 const char *ds_name;
619 int is_warning = 0;
620 int is_failure = 0;
621 int prev_state = STATE_OKAY;
623 /* check if this threshold applies to this data source */
624 if (ds != NULL)
625 {
626 ds_name = ds->ds[ds_index].name;
627 if ((th->data_source[0] != 0)
628 && (strcmp (ds_name, th->data_source) != 0))
629 return (STATE_OKAY);
630 }
632 if ((th->flags & UT_FLAG_INVERT) != 0)
633 {
634 is_warning--;
635 is_failure--;
636 }
638 /* XXX: This is an experimental code, not optimized, not fast, not reliable,
639 * and probably, do not work as you expect. Enjoy! :D */
640 if ( (th->hysteresis > 0) && ((prev_state = uc_get_state(ds,vl)) != STATE_OKAY) )
641 {
642 switch(prev_state)
643 {
644 case STATE_ERROR:
645 if ( (!isnan (th->failure_min) && ((th->failure_min + th->hysteresis) < values[ds_index])) ||
646 (!isnan (th->failure_max) && ((th->failure_max - th->hysteresis) > values[ds_index])) )
647 return (STATE_OKAY);
648 else
649 is_failure++;
650 case STATE_WARNING:
651 if ( (!isnan (th->warning_min) && ((th->warning_min + th->hysteresis) < values[ds_index])) ||
652 (!isnan (th->warning_max) && ((th->warning_max - th->hysteresis) > values[ds_index])) )
653 return (STATE_OKAY);
654 else
655 is_warning++;
656 }
657 }
658 else { /* no hysteresis */
659 if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
660 || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
661 is_failure++;
663 if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
664 || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
665 is_warning++;
666 }
668 if (is_failure != 0)
669 return (STATE_ERROR);
671 if (is_warning != 0)
672 return (STATE_WARNING);
674 return (STATE_OKAY);
675 } /* }}} int ut_check_one_data_source */
677 /*
678 * int ut_check_one_threshold
679 *
680 * Checks all data sources of a value list against the given threshold, using
681 * the ut_check_one_data_source function above. Returns the worst status,
682 * which is `okay' if nothing has failed.
683 * Returns less than zero if the data set doesn't have any data sources.
684 */
685 static int ut_check_one_threshold (const data_set_t *ds,
686 const value_list_t *vl,
687 const threshold_t *th,
688 const gauge_t *values,
689 int *ret_ds_index)
690 { /* {{{ */
691 int ret = -1;
692 int ds_index = -1;
693 int i;
694 gauge_t values_copy[ds->ds_num];
696 memcpy (values_copy, values, sizeof (values_copy));
698 if ((th->flags & UT_FLAG_PERCENTAGE) != 0)
699 {
700 int num = 0;
701 gauge_t sum=0.0;
703 if (ds->ds_num == 1)
704 {
705 WARNING ("ut_check_one_threshold: The %s type has only one data "
706 "source, but you have configured to check this as a percentage. "
707 "That doesn't make much sense, because the percentage will always "
708 "be 100%%!", ds->type);
709 }
711 /* Prepare `sum' and `num'. */
712 for (i = 0; i < ds->ds_num; i++)
713 if (!isnan (values[i]))
714 {
715 num++;
716 sum += values[i];
717 }
719 if ((num == 0) /* All data sources are undefined. */
720 || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */
721 {
722 for (i = 0; i < ds->ds_num; i++)
723 values_copy[i] = NAN;
724 }
725 else /* We can actually calculate the percentage. */
726 {
727 for (i = 0; i < ds->ds_num; i++)
728 values_copy[i] = 100.0 * values[i] / sum;
729 }
730 } /* if (UT_FLAG_PERCENTAGE) */
732 for (i = 0; i < ds->ds_num; i++)
733 {
734 int status;
736 status = ut_check_one_data_source (ds, vl, th, values_copy, i);
737 if (ret < status)
738 {
739 ret = status;
740 ds_index = i;
741 }
742 } /* for (ds->ds_num) */
744 if (ret_ds_index != NULL)
745 *ret_ds_index = ds_index;
747 return (ret);
748 } /* }}} int ut_check_one_threshold */
750 /*
751 * int ut_check_threshold
752 *
753 * Gets a list of matching thresholds and searches for the worst status by one
754 * of the thresholds. Then reports that status using the ut_report_state
755 * function above.
756 * Returns zero on success and if no threshold has been configured. Returns
757 * less than zero on failure.
758 */
759 static int ut_check_threshold (const data_set_t *ds, const value_list_t *vl,
760 __attribute__((unused)) user_data_t *ud)
761 { /* {{{ */
762 threshold_t *th;
763 gauge_t *values;
764 int status;
766 int worst_state = -1;
767 threshold_t *worst_th = NULL;
768 int worst_ds_index = -1;
770 if (threshold_tree == NULL)
771 return (0);
773 /* Is this lock really necessary? So far, thresholds are only inserted at
774 * startup. -octo */
775 pthread_mutex_lock (&threshold_lock);
776 th = threshold_search (vl);
777 pthread_mutex_unlock (&threshold_lock);
778 if (th == NULL)
779 return (0);
781 DEBUG ("ut_check_threshold: Found matching threshold(s)");
783 values = uc_get_rate (ds, vl);
784 if (values == NULL)
785 return (0);
787 while (th != NULL)
788 {
789 int ds_index = -1;
791 status = ut_check_one_threshold (ds, vl, th, values, &ds_index);
792 if (status < 0)
793 {
794 ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
795 sfree (values);
796 return (-1);
797 }
799 if (worst_state < status)
800 {
801 worst_state = status;
802 worst_th = th;
803 worst_ds_index = ds_index;
804 }
806 th = th->next;
807 } /* while (th) */
809 status = ut_report_state (ds, vl, worst_th, values,
810 worst_ds_index, worst_state);
811 if (status != 0)
812 {
813 ERROR ("ut_check_threshold: ut_report_state failed.");
814 sfree (values);
815 return (-1);
816 }
818 sfree (values);
820 return (0);
821 } /* }}} int ut_check_threshold */
823 /*
824 * int ut_missing
825 *
826 * This function is called whenever a value goes "missing".
827 */
828 static int ut_missing (const value_list_t *vl,
829 __attribute__((unused)) user_data_t *ud)
830 { /* {{{ */
831 threshold_t *th;
832 cdtime_t missing_time;
833 char identifier[6 * DATA_MAX_NAME_LEN];
834 notification_t n;
835 cdtime_t now;
837 if (threshold_tree == NULL)
838 return (0);
840 th = threshold_search (vl);
841 /* dispatch notifications for "interesting" values only */
842 if ((th == NULL) || ((th->flags & UT_FLAG_INTERESTING) == 0))
843 return (0);
845 now = cdtime ();
846 missing_time = now - vl->time;
847 FORMAT_VL (identifier, sizeof (identifier), vl);
849 NOTIFICATION_INIT_VL (&n, vl);
850 ssnprintf (n.message, sizeof (n.message),
851 "%s has not been updated for %.3f seconds.",
852 identifier, CDTIME_T_TO_DOUBLE (missing_time));
853 n.time = now;
855 plugin_dispatch_notification (&n);
857 return (0);
858 } /* }}} int ut_missing */
860 int ut_config (oconfig_item_t *ci)
861 { /* {{{ */
862 int i;
863 int status = 0;
865 threshold_t th;
867 if (threshold_tree == NULL)
868 {
869 threshold_tree = c_avl_create ((void *) strcmp);
870 if (threshold_tree == NULL)
871 {
872 ERROR ("ut_config: c_avl_create failed.");
873 return (-1);
874 }
875 }
877 memset (&th, '\0', sizeof (th));
878 th.warning_min = NAN;
879 th.warning_max = NAN;
880 th.failure_min = NAN;
881 th.failure_max = NAN;
883 th.hits = 0;
884 th.hysteresis = 0;
885 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
887 for (i = 0; i < ci->children_num; i++)
888 {
889 oconfig_item_t *option = ci->children + i;
890 status = 0;
892 if (strcasecmp ("Type", option->key) == 0)
893 status = ut_config_type (&th, option);
894 else if (strcasecmp ("Plugin", option->key) == 0)
895 status = ut_config_plugin (&th, option);
896 else if (strcasecmp ("Host", option->key) == 0)
897 status = ut_config_host (&th, option);
898 else
899 {
900 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
901 status = -1;
902 }
904 if (status != 0)
905 break;
906 }
908 if (c_avl_size (threshold_tree) > 0) {
909 plugin_register_missing ("threshold", ut_missing,
910 /* user data = */ NULL);
911 plugin_register_write ("threshold", ut_check_threshold,
912 /* user data = */ NULL);
913 }
915 return (status);
916 } /* }}} int um_config */
918 void module_register (void)
919 {
920 plugin_register_complex_config ("threshold", ut_config);
921 }
923 /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */