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 = malloc (sizeof (*th_copy));
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));
82 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
84 pthread_mutex_lock (&threshold_lock);
86 th_ptr = threshold_get (th->host, th->plugin, th->plugin_instance,
87 th->type, th->type_instance);
89 while ((th_ptr != NULL) && (th_ptr->next != NULL))
90 th_ptr = th_ptr->next;
92 if (th_ptr == NULL) /* no such threshold yet */
93 {
94 status = c_avl_insert (threshold_tree, name_copy, th_copy);
95 }
96 else /* th_ptr points to the last threshold in the list */
97 {
98 th_ptr->next = th_copy;
99 /* name_copy isn't needed */
100 sfree (name_copy);
101 }
103 pthread_mutex_unlock (&threshold_lock);
105 if (status != 0)
106 {
107 ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
108 sfree (name_copy);
109 sfree (th_copy);
110 }
112 return (status);
113 } /* }}} int ut_threshold_add */
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_datasource (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 `DataSource' option needs exactly one "
127 "string argument.");
128 return (-1);
129 }
131 sstrncpy (th->data_source, ci->values[0].value.string,
132 sizeof (th->data_source));
134 return (0);
135 } /* int ut_config_type_datasource */
137 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
138 {
139 if ((ci->values_num != 1)
140 || (ci->values[0].type != OCONFIG_TYPE_STRING))
141 {
142 WARNING ("threshold values: The `Instance' option needs exactly one "
143 "string argument.");
144 return (-1);
145 }
147 sstrncpy (th->type_instance, ci->values[0].value.string,
148 sizeof (th->type_instance));
150 return (0);
151 } /* int ut_config_type_instance */
153 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
154 {
155 if ((ci->values_num != 1)
156 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
157 {
158 WARNING ("threshold values: The `%s' option needs exactly one "
159 "number argument.", ci->key);
160 return (-1);
161 }
163 if (strcasecmp (ci->key, "WarningMax") == 0)
164 th->warning_max = ci->values[0].value.number;
165 else
166 th->failure_max = ci->values[0].value.number;
168 return (0);
169 } /* int ut_config_type_max */
171 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
172 {
173 if ((ci->values_num != 1)
174 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
175 {
176 WARNING ("threshold values: The `%s' option needs exactly one "
177 "number argument.", ci->key);
178 return (-1);
179 }
181 if (strcasecmp (ci->key, "WarningMin") == 0)
182 th->warning_min = ci->values[0].value.number;
183 else
184 th->failure_min = ci->values[0].value.number;
186 return (0);
187 } /* int ut_config_type_min */
189 static int ut_config_type_hits (threshold_t *th, oconfig_item_t *ci)
190 {
191 if ((ci->values_num != 1)
192 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
193 {
194 WARNING ("threshold values: The `%s' option needs exactly one "
195 "number argument.", ci->key);
196 return (-1);
197 }
199 th->hits = ci->values[0].value.number;
201 return (0);
202 } /* int ut_config_type_hits */
204 static int ut_config_type_hysteresis (threshold_t *th, oconfig_item_t *ci)
205 {
206 if ((ci->values_num != 1)
207 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
208 {
209 WARNING ("threshold values: The `%s' option needs exactly one "
210 "number argument.", ci->key);
211 return (-1);
212 }
214 th->hysteresis = ci->values[0].value.number;
216 return (0);
217 } /* int ut_config_type_hysteresis */
219 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
220 {
221 int i;
222 threshold_t th;
223 int status = 0;
225 if ((ci->values_num != 1)
226 || (ci->values[0].type != OCONFIG_TYPE_STRING))
227 {
228 WARNING ("threshold values: The `Type' block needs exactly one string "
229 "argument.");
230 return (-1);
231 }
233 if (ci->children_num < 1)
234 {
235 WARNING ("threshold values: The `Type' block needs at least one option.");
236 return (-1);
237 }
239 memcpy (&th, th_orig, sizeof (th));
240 sstrncpy (th.type, ci->values[0].value.string, sizeof (th.type));
242 th.warning_min = NAN;
243 th.warning_max = NAN;
244 th.failure_min = NAN;
245 th.failure_max = NAN;
246 th.hits = 0;
247 th.hysteresis = 0;
248 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
250 for (i = 0; i < ci->children_num; i++)
251 {
252 oconfig_item_t *option = ci->children + i;
254 if (strcasecmp ("Instance", option->key) == 0)
255 status = ut_config_type_instance (&th, option);
256 else if (strcasecmp ("DataSource", option->key) == 0)
257 status = ut_config_type_datasource (&th, option);
258 else if ((strcasecmp ("WarningMax", option->key) == 0)
259 || (strcasecmp ("FailureMax", option->key) == 0))
260 status = ut_config_type_max (&th, option);
261 else if ((strcasecmp ("WarningMin", option->key) == 0)
262 || (strcasecmp ("FailureMin", option->key) == 0))
263 status = ut_config_type_min (&th, option);
264 else if (strcasecmp ("Interesting", option->key) == 0)
265 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INTERESTING);
266 else if (strcasecmp ("Invert", option->key) == 0)
267 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INVERT);
268 else if (strcasecmp ("Persist", option->key) == 0)
269 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST);
270 else if (strcasecmp ("PersistOK", option->key) == 0)
271 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST_OK);
272 else if (strcasecmp ("Percentage", option->key) == 0)
273 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERCENTAGE);
274 else if (strcasecmp ("Hits", option->key) == 0)
275 status = ut_config_type_hits (&th, option);
276 else if (strcasecmp ("Hysteresis", option->key) == 0)
277 status = ut_config_type_hysteresis (&th, option);
278 else
279 {
280 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
281 "block.", option->key);
282 status = -1;
283 }
285 if (status != 0)
286 break;
287 }
289 if (status == 0)
290 {
291 status = ut_threshold_add (&th);
292 }
294 return (status);
295 } /* int ut_config_type */
297 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
298 {
299 if ((ci->values_num != 1)
300 || (ci->values[0].type != OCONFIG_TYPE_STRING))
301 {
302 WARNING ("threshold values: The `Instance' option needs exactly one "
303 "string argument.");
304 return (-1);
305 }
307 sstrncpy (th->plugin_instance, ci->values[0].value.string,
308 sizeof (th->plugin_instance));
310 return (0);
311 } /* int ut_config_plugin_instance */
313 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
314 {
315 int i;
316 threshold_t th;
317 int status = 0;
319 if ((ci->values_num != 1)
320 || (ci->values[0].type != OCONFIG_TYPE_STRING))
321 {
322 WARNING ("threshold values: The `Plugin' block needs exactly one string "
323 "argument.");
324 return (-1);
325 }
327 if (ci->children_num < 1)
328 {
329 WARNING ("threshold values: The `Plugin' block needs at least one nested "
330 "block.");
331 return (-1);
332 }
334 memcpy (&th, th_orig, sizeof (th));
335 sstrncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
337 for (i = 0; i < ci->children_num; i++)
338 {
339 oconfig_item_t *option = ci->children + i;
341 if (strcasecmp ("Type", option->key) == 0)
342 status = ut_config_type (&th, option);
343 else if (strcasecmp ("Instance", option->key) == 0)
344 status = ut_config_plugin_instance (&th, option);
345 else
346 {
347 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
348 "block.", option->key);
349 status = -1;
350 }
352 if (status != 0)
353 break;
354 }
356 return (status);
357 } /* int ut_config_plugin */
359 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
360 {
361 int i;
362 threshold_t th;
363 int status = 0;
365 if ((ci->values_num != 1)
366 || (ci->values[0].type != OCONFIG_TYPE_STRING))
367 {
368 WARNING ("threshold values: The `Host' block needs exactly one string "
369 "argument.");
370 return (-1);
371 }
373 if (ci->children_num < 1)
374 {
375 WARNING ("threshold values: The `Host' block needs at least one nested "
376 "block.");
377 return (-1);
378 }
380 memcpy (&th, th_orig, sizeof (th));
381 sstrncpy (th.host, ci->values[0].value.string, sizeof (th.host));
383 for (i = 0; i < ci->children_num; i++)
384 {
385 oconfig_item_t *option = ci->children + i;
387 if (strcasecmp ("Type", option->key) == 0)
388 status = ut_config_type (&th, option);
389 else if (strcasecmp ("Plugin", option->key) == 0)
390 status = ut_config_plugin (&th, option);
391 else
392 {
393 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
394 "block.", option->key);
395 status = -1;
396 }
398 if (status != 0)
399 break;
400 }
402 return (status);
403 } /* int ut_config_host */
404 /*
405 * End of the functions used to configure threshold values.
406 */
407 /* }}} */
409 /*
410 * int ut_report_state
411 *
412 * Checks if the `state' differs from the old state and creates a notification
413 * if appropriate.
414 * Does not fail.
415 */
416 static int ut_report_state (const data_set_t *ds,
417 const value_list_t *vl,
418 const threshold_t *th,
419 const gauge_t *values,
420 int ds_index,
421 int state)
422 { /* {{{ */
423 int state_old;
424 notification_t n;
426 char *buf;
427 size_t bufsize;
429 int status;
431 /* Check if hits matched */
432 if ( (th->hits != 0) )
433 {
434 int hits = uc_get_hits(ds,vl);
435 /* STATE_OKAY resets hits unless PERSIST_OK flag is set. Hits resets if
436 * threshold is hit. */
437 if ( ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) ) || (hits > th->hits) )
438 {
439 DEBUG("ut_report_state: reset uc_get_hits = 0");
440 uc_set_hits(ds,vl,0); /* reset hit counter and notify */
441 } else {
442 DEBUG("ut_report_state: th->hits = %d, uc_get_hits = %d",th->hits,uc_get_hits(ds,vl));
443 (void) uc_inc_hits(ds,vl,1); /* increase hit counter */
444 return (0);
445 }
446 } /* end check hits */
448 state_old = uc_get_state (ds, vl);
450 /* If the state didn't change, report if `persistent' is specified. If the
451 * state is `okay', then only report if `persist_ok` flag is set. */
452 if (state == state_old)
453 {
454 if ((th->flags & UT_FLAG_PERSIST) == 0)
455 return (0);
456 else if ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) )
457 return (0);
458 }
460 if (state != state_old)
461 uc_set_state (ds, vl, state);
463 NOTIFICATION_INIT_VL (&n, vl);
465 buf = n.message;
466 bufsize = sizeof (n.message);
468 if (state == STATE_OKAY)
469 n.severity = NOTIF_OKAY;
470 else if (state == STATE_WARNING)
471 n.severity = NOTIF_WARNING;
472 else
473 n.severity = NOTIF_FAILURE;
475 n.time = vl->time;
477 status = ssnprintf (buf, bufsize, "Host %s, plugin %s",
478 vl->host, vl->plugin);
479 buf += status;
480 bufsize -= status;
482 if (vl->plugin_instance[0] != '\0')
483 {
484 status = ssnprintf (buf, bufsize, " (instance %s)",
485 vl->plugin_instance);
486 buf += status;
487 bufsize -= status;
488 }
490 status = ssnprintf (buf, bufsize, " type %s", vl->type);
491 buf += status;
492 bufsize -= status;
494 if (vl->type_instance[0] != '\0')
495 {
496 status = ssnprintf (buf, bufsize, " (instance %s)",
497 vl->type_instance);
498 buf += status;
499 bufsize -= status;
500 }
502 plugin_notification_meta_add_string (&n, "DataSource",
503 ds->ds[ds_index].name);
504 plugin_notification_meta_add_double (&n, "CurrentValue", values[ds_index]);
505 plugin_notification_meta_add_double (&n, "WarningMin", th->warning_min);
506 plugin_notification_meta_add_double (&n, "WarningMax", th->warning_max);
507 plugin_notification_meta_add_double (&n, "FailureMin", th->failure_min);
508 plugin_notification_meta_add_double (&n, "FailureMax", th->failure_max);
510 /* Send an okay notification */
511 if (state == STATE_OKAY)
512 {
513 if (state_old == STATE_MISSING)
514 ssnprintf (buf, bufsize, ": Value is no longer missing.");
515 else
516 ssnprintf (buf, bufsize,
517 ": All data sources are within range again. "
518 "Current value of \"%s\" is %f.",
519 ds->ds[ds_index].name, values[ds_index]);
520 }
521 else
522 {
523 double min;
524 double max;
526 min = (state == STATE_ERROR) ? th->failure_min : th->warning_min;
527 max = (state == STATE_ERROR) ? th->failure_max : th->warning_max;
529 if (th->flags & UT_FLAG_INVERT)
530 {
531 if (!isnan (min) && !isnan (max))
532 {
533 ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
534 "%f. That is within the %s region of %f%s and %f%s.",
535 ds->ds[ds_index].name, values[ds_index],
536 (state == STATE_ERROR) ? "failure" : "warning",
537 min, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "",
538 max, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
539 }
540 else
541 {
542 ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
543 "%f. That is %s the %s threshold of %f%s.",
544 ds->ds[ds_index].name, values[ds_index],
545 isnan (min) ? "below" : "above",
546 (state == STATE_ERROR) ? "failure" : "warning",
547 isnan (min) ? max : min,
548 ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
549 }
550 }
551 else if (th->flags & UT_FLAG_PERCENTAGE)
552 {
553 gauge_t value;
554 gauge_t sum;
555 size_t i;
557 sum = 0.0;
558 for (i = 0; i < vl->values_len; i++)
559 {
560 if (isnan (values[i]))
561 continue;
563 sum += values[i];
564 }
566 if (sum == 0.0)
567 value = NAN;
568 else
569 value = 100.0 * values[ds_index] / sum;
571 ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
572 "%g (%.2f%%). That is %s the %s threshold of %.2f%%.",
573 ds->ds[ds_index].name, values[ds_index], value,
574 (value < min) ? "below" : "above",
575 (state == STATE_ERROR) ? "failure" : "warning",
576 (value < min) ? min : max);
577 }
578 else /* is not inverted */
579 {
580 ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
581 "%f. That is %s the %s threshold of %f.",
582 ds->ds[ds_index].name, values[ds_index],
583 (values[ds_index] < min) ? "below" : "above",
584 (state == STATE_ERROR) ? "failure" : "warning",
585 (values[ds_index] < min) ? min : max);
586 }
587 }
589 plugin_dispatch_notification (&n);
591 plugin_notification_meta_free (n.meta);
592 return (0);
593 } /* }}} int ut_report_state */
595 /*
596 * int ut_check_one_data_source
597 *
598 * Checks one data source against the given threshold configuration. If the
599 * `DataSource' option is set in the threshold, and the name does NOT match,
600 * `okay' is returned. If the threshold does match, its failure and warning
601 * min and max values are checked and `failure' or `warning' is returned if
602 * appropriate.
603 * Does not fail.
604 */
605 static int ut_check_one_data_source (const data_set_t *ds,
606 const value_list_t __attribute__((unused)) *vl,
607 const threshold_t *th,
608 const gauge_t *values,
609 int ds_index)
610 { /* {{{ */
611 const char *ds_name;
612 int is_warning = 0;
613 int is_failure = 0;
614 int prev_state = STATE_OKAY;
616 /* check if this threshold applies to this data source */
617 if (ds != NULL)
618 {
619 ds_name = ds->ds[ds_index].name;
620 if ((th->data_source[0] != 0)
621 && (strcmp (ds_name, th->data_source) != 0))
622 return (STATE_OKAY);
623 }
625 if ((th->flags & UT_FLAG_INVERT) != 0)
626 {
627 is_warning--;
628 is_failure--;
629 }
631 /* XXX: This is an experimental code, not optimized, not fast, not reliable,
632 * and probably, do not work as you expect. Enjoy! :D */
633 if (th->hysteresis > 0)
634 {
635 prev_state = uc_get_state(ds,vl);
636 /* The purpose of hysteresis is elliminating flapping state when the value
637 * oscilates around the thresholds. In other words, what is important is
638 * the previous state; if the new value would trigger a transition, make
639 * sure that we artificially widen the range which is considered to apply
640 * for the previous state, and only trigger the notification if the value
641 * is outside of this expanded range.
642 *
643 * There is no hysteresis for the OKAY state.
644 * */
645 gauge_t hysteresis_for_warning = 0, hysteresis_for_failure = 0;
646 switch (prev_state)
647 {
648 case STATE_ERROR:
649 hysteresis_for_failure = th->hysteresis;
650 break;
651 case STATE_WARNING:
652 hysteresis_for_warning = th->hysteresis;
653 break;
654 case STATE_OKAY:
655 /* do nothing -- the hysteresis only applies to the non-normal states */
656 break;
657 }
659 if ((!isnan (th->failure_min) && (th->failure_min + hysteresis_for_failure > values[ds_index]))
660 || (!isnan (th->failure_max) && (th->failure_max - hysteresis_for_failure < values[ds_index])))
661 is_failure++;
663 if ((!isnan (th->warning_min) && (th->warning_min + hysteresis_for_warning > values[ds_index]))
664 || (!isnan (th->warning_max) && (th->warning_max - hysteresis_for_warning < values[ds_index])))
665 is_warning++;
667 }
668 else { /* no hysteresis */
669 if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
670 || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
671 is_failure++;
673 if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
674 || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
675 is_warning++;
676 }
678 if (is_failure != 0)
679 return (STATE_ERROR);
681 if (is_warning != 0)
682 return (STATE_WARNING);
684 return (STATE_OKAY);
685 } /* }}} int ut_check_one_data_source */
687 /*
688 * int ut_check_one_threshold
689 *
690 * Checks all data sources of a value list against the given threshold, using
691 * the ut_check_one_data_source function above. Returns the worst status,
692 * which is `okay' if nothing has failed.
693 * Returns less than zero if the data set doesn't have any data sources.
694 */
695 static int ut_check_one_threshold (const data_set_t *ds,
696 const value_list_t *vl,
697 const threshold_t *th,
698 const gauge_t *values,
699 int *ret_ds_index)
700 { /* {{{ */
701 int ret = -1;
702 int ds_index = -1;
703 size_t i;
704 gauge_t values_copy[ds->ds_num];
706 memcpy (values_copy, values, sizeof (values_copy));
708 if ((th->flags & UT_FLAG_PERCENTAGE) != 0)
709 {
710 int num = 0;
711 gauge_t sum=0.0;
713 if (ds->ds_num == 1)
714 {
715 WARNING ("ut_check_one_threshold: The %s type has only one data "
716 "source, but you have configured to check this as a percentage. "
717 "That doesn't make much sense, because the percentage will always "
718 "be 100%%!", ds->type);
719 }
721 /* Prepare `sum' and `num'. */
722 for (i = 0; i < ds->ds_num; i++)
723 if (!isnan (values[i]))
724 {
725 num++;
726 sum += values[i];
727 }
729 if ((num == 0) /* All data sources are undefined. */
730 || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */
731 {
732 for (i = 0; i < ds->ds_num; i++)
733 values_copy[i] = NAN;
734 }
735 else /* We can actually calculate the percentage. */
736 {
737 for (i = 0; i < ds->ds_num; i++)
738 values_copy[i] = 100.0 * values[i] / sum;
739 }
740 } /* if (UT_FLAG_PERCENTAGE) */
742 for (i = 0; i < ds->ds_num; i++)
743 {
744 int status;
746 status = ut_check_one_data_source (ds, vl, th, values_copy, i);
747 if (ret < status)
748 {
749 ret = status;
750 ds_index = i;
751 }
752 } /* for (ds->ds_num) */
754 if (ret_ds_index != NULL)
755 *ret_ds_index = ds_index;
757 return (ret);
758 } /* }}} int ut_check_one_threshold */
760 /*
761 * int ut_check_threshold
762 *
763 * Gets a list of matching thresholds and searches for the worst status by one
764 * of the thresholds. Then reports that status using the ut_report_state
765 * function above.
766 * Returns zero on success and if no threshold has been configured. Returns
767 * less than zero on failure.
768 */
769 static int ut_check_threshold (const data_set_t *ds, const value_list_t *vl,
770 __attribute__((unused)) user_data_t *ud)
771 { /* {{{ */
772 threshold_t *th;
773 gauge_t *values;
774 int status;
776 int worst_state = -1;
777 threshold_t *worst_th = NULL;
778 int worst_ds_index = -1;
780 if (threshold_tree == NULL)
781 return (0);
783 /* Is this lock really necessary? So far, thresholds are only inserted at
784 * startup. -octo */
785 pthread_mutex_lock (&threshold_lock);
786 th = threshold_search (vl);
787 pthread_mutex_unlock (&threshold_lock);
788 if (th == NULL)
789 return (0);
791 DEBUG ("ut_check_threshold: Found matching threshold(s)");
793 values = uc_get_rate (ds, vl);
794 if (values == NULL)
795 return (0);
797 while (th != NULL)
798 {
799 int ds_index = -1;
801 status = ut_check_one_threshold (ds, vl, th, values, &ds_index);
802 if (status < 0)
803 {
804 ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
805 sfree (values);
806 return (-1);
807 }
809 if (worst_state < status)
810 {
811 worst_state = status;
812 worst_th = th;
813 worst_ds_index = ds_index;
814 }
816 th = th->next;
817 } /* while (th) */
819 status = ut_report_state (ds, vl, worst_th, values,
820 worst_ds_index, worst_state);
821 if (status != 0)
822 {
823 ERROR ("ut_check_threshold: ut_report_state failed.");
824 sfree (values);
825 return (-1);
826 }
828 sfree (values);
830 return (0);
831 } /* }}} int ut_check_threshold */
833 /*
834 * int ut_missing
835 *
836 * This function is called whenever a value goes "missing".
837 */
838 static int ut_missing (const value_list_t *vl,
839 __attribute__((unused)) user_data_t *ud)
840 { /* {{{ */
841 threshold_t *th;
842 cdtime_t missing_time;
843 char identifier[6 * DATA_MAX_NAME_LEN];
844 notification_t n;
845 cdtime_t now;
847 if (threshold_tree == NULL)
848 return (0);
850 th = threshold_search (vl);
851 /* dispatch notifications for "interesting" values only */
852 if ((th == NULL) || ((th->flags & UT_FLAG_INTERESTING) == 0))
853 return (0);
855 now = cdtime ();
856 missing_time = now - vl->time;
857 FORMAT_VL (identifier, sizeof (identifier), vl);
859 NOTIFICATION_INIT_VL (&n, vl);
860 ssnprintf (n.message, sizeof (n.message),
861 "%s has not been updated for %.3f seconds.",
862 identifier, CDTIME_T_TO_DOUBLE (missing_time));
863 n.time = now;
865 plugin_dispatch_notification (&n);
867 return (0);
868 } /* }}} int ut_missing */
870 static int ut_config (oconfig_item_t *ci)
871 { /* {{{ */
872 int i;
873 int status = 0;
874 int old_size = c_avl_size (threshold_tree);
876 threshold_t th;
878 if (threshold_tree == NULL)
879 {
880 threshold_tree = c_avl_create ((void *) strcmp);
881 if (threshold_tree == NULL)
882 {
883 ERROR ("ut_config: c_avl_create failed.");
884 return (-1);
885 }
886 }
888 memset (&th, '\0', sizeof (th));
889 th.warning_min = NAN;
890 th.warning_max = NAN;
891 th.failure_min = NAN;
892 th.failure_max = NAN;
894 th.hits = 0;
895 th.hysteresis = 0;
896 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
898 for (i = 0; i < ci->children_num; i++)
899 {
900 oconfig_item_t *option = ci->children + i;
902 if (strcasecmp ("Type", option->key) == 0)
903 status = ut_config_type (&th, option);
904 else if (strcasecmp ("Plugin", option->key) == 0)
905 status = ut_config_plugin (&th, option);
906 else if (strcasecmp ("Host", option->key) == 0)
907 status = ut_config_host (&th, option);
908 else
909 {
910 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
911 status = -1;
912 }
914 if (status != 0)
915 break;
916 }
918 /* register callbacks if this is the first time we see a valid config */
919 if ((old_size == 0) && (c_avl_size (threshold_tree) > 0))
920 {
921 plugin_register_missing ("threshold", ut_missing,
922 /* user data = */ NULL);
923 plugin_register_write ("threshold", ut_check_threshold,
924 /* user data = */ NULL);
925 }
927 return (status);
928 } /* }}} int um_config */
930 void module_register (void)
931 {
932 plugin_register_complex_config ("threshold", ut_config);
933 }
935 /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */