From: Pierre-Yves Ritschard Date: Tue, 22 Jul 2014 11:55:49 +0000 (+0200) Subject: Merge branch 'master' into feature-riemann-threshold X-Git-Tag: collectd-5.5.0~291^2~2 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=639f00e946bb3182688661a99119aa5f5d6c81b6;hp=bcce4d5922302fb9204e6ef3fb1022388f366656;p=collectd.git Merge branch 'master' into feature-riemann-threshold --- diff --git a/src/Makefile.am b/src/Makefile.am index a9d85823..8060029a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -39,7 +39,9 @@ collectd_SOURCES = collectd.c collectd.h \ utils_subst.c utils_subst.h \ utils_tail.c utils_tail.h \ utils_time.c utils_time.h \ - types_list.c types_list.h + types_list.c types_list.h \ + utils_threshold.c utils_threshold.h + collectd_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) collectd_CFLAGS = $(AM_CFLAGS) @@ -133,9 +135,9 @@ collectd_tg_LDADD += libcollectdclient/libcollectdclient.la collectd_tg_DEPENDENCIES = libcollectdclient/libcollectdclient.la -pkglib_LTLIBRARIES = +pkglib_LTLIBRARIES = -BUILT_SOURCES = +BUILT_SOURCES = CLEANFILES = if BUILD_PLUGIN_AGGREGATION @@ -273,7 +275,7 @@ pkglib_LTLIBRARIES += cpu.la cpu_la_SOURCES = cpu.c cpu_la_CFLAGS = $(AM_CFLAGS) cpu_la_LDFLAGS = -module -avoid-version -cpu_la_LIBADD = +cpu_la_LIBADD = if BUILD_WITH_LIBKSTAT cpu_la_LIBADD += -lkstat endif @@ -371,7 +373,7 @@ pkglib_LTLIBRARIES += disk.la disk_la_SOURCES = disk.c disk_la_CFLAGS = $(AM_CFLAGS) disk_la_LDFLAGS = -module -avoid-version -disk_la_LIBADD = +disk_la_LIBADD = if BUILD_WITH_LIBKSTAT disk_la_LIBADD += -lkstat endif @@ -382,7 +384,7 @@ if BUILD_WITH_LIBIOKIT disk_la_LDFLAGS += -framework IOKit endif if BUILD_WITH_LIBSTATGRAB -disk_la_CFLAGS += $(BUILD_WITH_LIBSTATGRAB_CFLAGS) +disk_la_CFLAGS += $(BUILD_WITH_LIBSTATGRAB_CFLAGS) disk_la_LIBADD += $(BUILD_WITH_LIBSTATGRAB_LDFLAGS) endif if BUILD_WITH_PERFSTAT @@ -862,7 +864,7 @@ if BUILD_PLUGIN_OLSRD pkglib_LTLIBRARIES += olsrd.la olsrd_la_SOURCES = olsrd.c olsrd_la_LDFLAGS = -module -avoid-version -olsrd_la_LIBADD = +olsrd_la_LIBADD = if BUILD_WITH_LIBSOCKET olsrd_la_LIBADD += -lsocket endif @@ -1409,7 +1411,7 @@ endif if BUILD_PLUGIN_WRITE_RIEMANN pkglib_LTLIBRARIES += write_riemann.la -write_riemann_la_SOURCES = write_riemann.c +write_riemann_la_SOURCES = write_riemann.c write_riemann_threshold.c nodist_write_riemann_la_SOURCES = riemann.pb-c.c riemann.pb-c.h write_riemann_la_LDFLAGS = -module -avoid-version write_riemann_la_LIBADD = -lprotobuf-c diff --git a/src/threshold.c b/src/threshold.c index 7df4d616..922689d3 100644 --- a/src/threshold.c +++ b/src/threshold.c @@ -28,45 +28,12 @@ #include "plugin.h" #include "utils_avltree.h" #include "utils_cache.h" +#include "utils_threshold.h" #include #include /* - * Private data structures - * {{{ */ -#define UT_FLAG_INVERT 0x01 -#define UT_FLAG_PERSIST 0x02 -#define UT_FLAG_PERCENTAGE 0x04 -#define UT_FLAG_INTERESTING 0x08 -#define UT_FLAG_PERSIST_OK 0x10 -typedef struct threshold_s -{ - char host[DATA_MAX_NAME_LEN]; - char plugin[DATA_MAX_NAME_LEN]; - char plugin_instance[DATA_MAX_NAME_LEN]; - char type[DATA_MAX_NAME_LEN]; - char type_instance[DATA_MAX_NAME_LEN]; - char data_source[DATA_MAX_NAME_LEN]; - gauge_t warning_min; - gauge_t warning_max; - gauge_t failure_min; - gauge_t failure_max; - gauge_t hysteresis; - unsigned int flags; - int hits; - struct threshold_s *next; -} threshold_t; -/* }}} */ - -/* - * Private (static) variables - * {{{ */ -static c_avl_tree_t *threshold_tree = NULL; -static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER; -/* }}} */ - -/* * Threshold management * ==================== * The following functions add, delete, search, etc. configured thresholds to @@ -171,7 +138,7 @@ static int ut_threshold_add (const threshold_t *th) return (status); } /* }}} int ut_threshold_add */ -/* +/* * threshold_t *threshold_search * * Searches for a threshold configuration using all the possible variations of @@ -862,7 +829,7 @@ static int ut_check_one_threshold (const data_set_t *ds, * * Gets a list of matching thresholds and searches for the worst status by one * of the thresholds. Then reports that status using the ut_report_state - * function above. + * function above. * Returns zero on success and if no threshold has been configured. Returns * less than zero on failure. */ @@ -990,7 +957,7 @@ int ut_config (oconfig_item_t *ci) th.hits = 0; th.hysteresis = 0; th.flags = UT_FLAG_INTERESTING; /* interesting by default */ - + for (i = 0; i < ci->children_num; i++) { oconfig_item_t *option = ci->children + i; diff --git a/src/utils_threshold.c b/src/utils_threshold.c new file mode 100644 index 00000000..005c49c3 --- /dev/null +++ b/src/utils_threshold.c @@ -0,0 +1,34 @@ +/** + * collectd - src/utils_threshold.c + * Copyright (C) 2014 Pierre-Yves Ritschard + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Pierre-Yves Ritschard + **/ + +#include "collectd.h" +#include "common.h" +#include "utils_avltree.h" +#include "utils_threshold.h" + +#include + +/* + * Exported symbols + * {{{ */ +c_avl_tree_t *threshold_tree = NULL; +pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER; +/* }}} */ diff --git a/src/utils_threshold.h b/src/utils_threshold.h new file mode 100644 index 00000000..d1abf563 --- /dev/null +++ b/src/utils_threshold.h @@ -0,0 +1,53 @@ +/** + * collectd - src/utils_threshold.h + * Copyright (C) 2014 Pierre-Yves Ritschard + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Pierre-Yves Ritschard + **/ + +#ifndef UTILS_THRESHOLD_H +#define UTILS_THRESHOLD_H 1 + +#define UT_FLAG_INVERT 0x01 +#define UT_FLAG_PERSIST 0x02 +#define UT_FLAG_PERCENTAGE 0x04 +#define UT_FLAG_INTERESTING 0x08 +#define UT_FLAG_PERSIST_OK 0x10 +typedef struct threshold_s +{ + char host[DATA_MAX_NAME_LEN]; + char plugin[DATA_MAX_NAME_LEN]; + char plugin_instance[DATA_MAX_NAME_LEN]; + char type[DATA_MAX_NAME_LEN]; + char type_instance[DATA_MAX_NAME_LEN]; + char data_source[DATA_MAX_NAME_LEN]; + gauge_t warning_min; + gauge_t warning_max; + gauge_t failure_min; + gauge_t failure_max; + gauge_t hysteresis; + unsigned int flags; + int hits; + struct threshold_s *next; +} threshold_t; + +extern c_avl_tree_t *threshold_tree; +extern pthread_mutex_t threshold_lock; + +#endif /* UTILS_THRESHOLD_H */ + +/* vim: set sw=2 sts=2 ts=8 : */ diff --git a/src/write_riemann.c b/src/write_riemann.c index e85e9436..2875a017 100644 --- a/src/write_riemann.c +++ b/src/write_riemann.c @@ -39,11 +39,14 @@ #define RIEMANN_PORT "5555" #define RIEMANN_TTL_FACTOR 2.0 +int write_riemann_threshold_check(const data_set_t *, const value_list_t *, int *); + struct riemann_host { char *name; #define F_CONNECT 0x01 uint8_t flags; pthread_mutex_t lock; + _Bool notifications; _Bool store_rates; _Bool always_append_ds; char *node; @@ -453,7 +456,8 @@ static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */ data_set_t const *ds, value_list_t const *vl, size_t index, - gauge_t const *rates) + gauge_t const *rates, + int status) { Event *event; char name_buffer[5 * DATA_MAX_NAME_LEN]; @@ -474,6 +478,21 @@ static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ event->time = CDTIME_T_TO_TIME_T (vl->time); event->has_time = 1; + switch (status) { + case STATE_OKAY: + event->state = strdup("ok"); + break; + case STATE_ERROR: + event->state = strdup("critical"); + break; + case STATE_WARNING: + event->state = strdup("warning"); + break; + case STATE_MISSING: + event->state = strdup("unknown"); + break; + } + ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor; event->ttl = (float) ttl; event->has_ttl = 1; @@ -557,8 +576,9 @@ static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ } /* }}} Event *riemann_value_to_protobuf */ static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */ - data_set_t const *ds, - value_list_t const *vl) + data_set_t const *ds, + value_list_t const *vl, + int *statuses) { Msg *msg; size_t i; @@ -598,7 +618,7 @@ static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* for (i = 0; i < msg->n_events; i++) { msg->events[i] = riemann_value_to_protobuf (host, ds, vl, - (int) i, rates); + (int) i, rates, statuses[i]); if (msg->events[i] == NULL) { riemann_msg_protobuf_free (msg); @@ -617,6 +637,9 @@ static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ struct riemann_host *host = ud->data; Msg *msg; + if (!host->notifications) + return 0; + msg = riemann_notification_to_protobuf (host, n); if (msg == NULL) return (-1); @@ -635,10 +658,12 @@ static int riemann_write(const data_set_t *ds, /* {{{ */ user_data_t *ud) { int status; + int statuses[vl->values_len]; struct riemann_host *host = ud->data; Msg *msg; - msg = riemann_value_list_to_protobuf (host, ds, vl); + write_riemann_threshold_check(ds, vl, statuses); + msg = riemann_value_list_to_protobuf (host, ds, vl, statuses); if (msg == NULL) return (-1); @@ -691,6 +716,7 @@ static int riemann_config_node(oconfig_item_t *ci) /* {{{ */ host->reference_count = 1; host->node = NULL; host->service = NULL; + host->notifications = 1; host->store_rates = 1; host->always_append_ds = 0; host->use_tcp = 0; @@ -715,6 +741,10 @@ static int riemann_config_node(oconfig_item_t *ci) /* {{{ */ status = cf_util_get_string (child, &host->node); if (status != 0) break; + } else if (strcasecmp ("Notifications", child->key) == 0) { + status = cf_util_get_boolean(child, &host->notifications); + if (status != 0) + break; } else if (strcasecmp ("Port", child->key) == 0) { status = cf_util_get_service (child, &host->service); if (status != 0) { @@ -884,7 +914,7 @@ static int riemann_config(oconfig_item_t *ci) /* {{{ */ child->key); } } - return (0); + return 0; } /* }}} int riemann_config */ void module_register(void) diff --git a/src/write_riemann_threshold.c b/src/write_riemann_threshold.c new file mode 100644 index 00000000..9f497741 --- /dev/null +++ b/src/write_riemann_threshold.c @@ -0,0 +1,321 @@ +/** + * collectd - src/threshold.c + * Copyright (C) 2007-2010 Florian Forster + * Copyright (C) 2008-2009 Sebastian Harl + * Copyright (C) 2009 Andrés J. Díaz + * Copyright (C) 2014 Pierre-Yves Ritschard + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Pierre-Yves Ritschard + * Florian octo Forster + * Sebastian Harl + * Andrés J. Díaz + **/ + +#include "collectd.h" +#include "common.h" +#include "plugin.h" +#include "utils_avltree.h" +#include "utils_cache.h" +#include "utils_threshold.h" + +#include +#include +#include + +/* + * Threshold management + * ==================== + * The following functions add, delete, search, etc. configured thresholds to + * the underlying AVL trees. + */ +/* + * threshold_t *threshold_get + * + * Retrieve one specific threshold configuration. For looking up a threshold + * matching a value_list_t, see "threshold_search" below. Returns NULL if the + * specified threshold doesn't exist. + */ +static threshold_t *threshold_get (const char *hostname, + const char *plugin, const char *plugin_instance, + const char *type, const char *type_instance) +{ /* {{{ */ + char name[6 * DATA_MAX_NAME_LEN]; + threshold_t *th = NULL; + + format_name (name, sizeof (name), + (hostname == NULL) ? "" : hostname, + (plugin == NULL) ? "" : plugin, plugin_instance, + (type == NULL) ? "" : type, type_instance); + name[sizeof (name) - 1] = '\0'; + + if (c_avl_get (threshold_tree, name, (void *) &th) == 0) + return (th); + else + return (NULL); +} /* }}} threshold_t *threshold_get */ + +/* + * threshold_t *threshold_search + * + * Searches for a threshold configuration using all the possible variations of + * "Host", "Plugin" and "Type" blocks. Returns NULL if no threshold could be + * found. + * XXX: This is likely the least efficient function in collectd. + */ +static threshold_t *threshold_search (const value_list_t *vl) +{ /* {{{ */ + threshold_t *th; + + if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance, + vl->type, vl->type_instance)) != NULL) + return (th); + else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance, + vl->type, NULL)) != NULL) + return (th); + else if ((th = threshold_get (vl->host, vl->plugin, NULL, + vl->type, vl->type_instance)) != NULL) + return (th); + else if ((th = threshold_get (vl->host, vl->plugin, NULL, + vl->type, NULL)) != NULL) + return (th); + else if ((th = threshold_get (vl->host, "", NULL, + vl->type, vl->type_instance)) != NULL) + return (th); + else if ((th = threshold_get (vl->host, "", NULL, + vl->type, NULL)) != NULL) + return (th); + else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance, + vl->type, vl->type_instance)) != NULL) + return (th); + else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance, + vl->type, NULL)) != NULL) + return (th); + else if ((th = threshold_get ("", vl->plugin, NULL, + vl->type, vl->type_instance)) != NULL) + return (th); + else if ((th = threshold_get ("", vl->plugin, NULL, + vl->type, NULL)) != NULL) + return (th); + else if ((th = threshold_get ("", "", NULL, + vl->type, vl->type_instance)) != NULL) + return (th); + else if ((th = threshold_get ("", "", NULL, + vl->type, NULL)) != NULL) + return (th); + + return (NULL); +} /* }}} threshold_t *threshold_search */ + +/* + * int ut_check_one_data_source + * + * Checks one data source against the given threshold configuration. If the + * `DataSource' option is set in the threshold, and the name does NOT match, + * `okay' is returned. If the threshold does match, its failure and warning + * min and max values are checked and `failure' or `warning' is returned if + * appropriate. + * Does not fail. + */ +static int ut_check_one_data_source (const data_set_t *ds, + const value_list_t __attribute__((unused)) *vl, + const threshold_t *th, + const gauge_t *values, + int ds_index) +{ /* {{{ */ + const char *ds_name; + int is_warning = 0; + int is_failure = 0; + int prev_state = STATE_OKAY; + + /* check if this threshold applies to this data source */ + if (ds != NULL) + { + ds_name = ds->ds[ds_index].name; + if ((th->data_source[0] != 0) + && (strcmp (ds_name, th->data_source) != 0)) + return (STATE_OKAY); + } + + if ((th->flags & UT_FLAG_INVERT) != 0) + { + is_warning--; + is_failure--; + } + + /* XXX: This is an experimental code, not optimized, not fast, not reliable, + * and probably, do not work as you expect. Enjoy! :D */ + if ( (th->hysteresis > 0) && ((prev_state = uc_get_state(ds,vl)) != STATE_OKAY) ) + { + switch(prev_state) + { + case STATE_ERROR: + if ( (!isnan (th->failure_min) && ((th->failure_min + th->hysteresis) < values[ds_index])) || + (!isnan (th->failure_max) && ((th->failure_max - th->hysteresis) > values[ds_index])) ) + return (STATE_OKAY); + else + is_failure++; + case STATE_WARNING: + if ( (!isnan (th->warning_min) && ((th->warning_min + th->hysteresis) < values[ds_index])) || + (!isnan (th->warning_max) && ((th->warning_max - th->hysteresis) > values[ds_index])) ) + return (STATE_OKAY); + else + is_warning++; + } + } + else { /* no hysteresis */ + if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index])) + || (!isnan (th->failure_max) && (th->failure_max < values[ds_index]))) + is_failure++; + + if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index])) + || (!isnan (th->warning_max) && (th->warning_max < values[ds_index]))) + is_warning++; + } + + if (is_failure != 0) + return (STATE_ERROR); + + if (is_warning != 0) + return (STATE_WARNING); + + return (STATE_OKAY); +} /* }}} int ut_check_one_data_source */ + +/* + * int ut_check_one_threshold + * + * Checks all data sources of a value list against the given threshold, using + * the ut_check_one_data_source function above. Returns the worst status, + * which is `okay' if nothing has failed. + * Returns less than zero if the data set doesn't have any data sources. + */ +static int ut_check_one_threshold (const data_set_t *ds, + const value_list_t *vl, + const threshold_t *th, + const gauge_t *values, + int *statuses) +{ /* {{{ */ + int ret = -1; + int i; + int status; + gauge_t values_copy[ds->ds_num]; + + memcpy (values_copy, values, sizeof (values_copy)); + + if ((th->flags & UT_FLAG_PERCENTAGE) != 0) + { + int num = 0; + gauge_t sum=0.0; + + if (ds->ds_num == 1) + { + WARNING ("ut_check_one_threshold: The %s type has only one data " + "source, but you have configured to check this as a percentage. " + "That doesn't make much sense, because the percentage will always " + "be 100%%!", ds->type); + } + + /* Prepare `sum' and `num'. */ + for (i = 0; i < ds->ds_num; i++) + if (!isnan (values[i])) + { + num++; + sum += values[i]; + } + + if ((num == 0) /* All data sources are undefined. */ + || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */ + { + for (i = 0; i < ds->ds_num; i++) + values_copy[i] = NAN; + } + else /* We can actually calculate the percentage. */ + { + for (i = 0; i < ds->ds_num; i++) + values_copy[i] = 100.0 * values[i] / sum; + } + } /* if (UT_FLAG_PERCENTAGE) */ + + for (i = 0; i < ds->ds_num; i++) + { + status = ut_check_one_data_source (ds, vl, th, values_copy, i); + if (status != -1) { + ret = 0; + if (statuses[i] < status) + statuses[i] = status; + } + } /* for (ds->ds_num) */ + + return (ret); +} /* }}} int ut_check_one_threshold */ + +/* + * int ut_check_threshold + * + * Gets a list of matching thresholds and searches for the worst status by one + * of the thresholds. Then reports that status using the ut_report_state + * function above. + * Returns zero on success and if no threshold has been configured. Returns + * less than zero on failure. + */ +int write_riemann_threshold_check (const data_set_t *ds, const value_list_t *vl, + int *statuses) +{ /* {{{ */ + threshold_t *th; + gauge_t *values; + int status; + + memset(statuses, 0, vl->values_len * sizeof(*statuses)); + + + if (threshold_tree == NULL) + return 0; + + /* Is this lock really necessary? So far, thresholds are only inserted at + * startup. -octo */ + pthread_mutex_lock (&threshold_lock); + th = threshold_search (vl); + pthread_mutex_unlock (&threshold_lock); + if (th == NULL) + return (0); + + DEBUG ("ut_check_threshold: Found matching threshold(s)"); + + values = uc_get_rate (ds, vl); + if (values == NULL) + return (0); + + while (th != NULL) + { + status = ut_check_one_threshold (ds, vl, th, values, statuses); + if (status < 0) + { + ERROR ("ut_check_threshold: ut_check_one_threshold failed."); + sfree (values); + return (-1); + } + + th = th->next; + } /* while (th) */ + + sfree (values); + + return (0); +} /* }}} int ut_check_threshold */ + + +/* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */