From c699053580b3a23d5919de101b273e4388ecac23 Mon Sep 17 00:00:00 2001 From: =?utf8?q?M=C3=A4rt=20Bakhoff?= Date: Tue, 3 Mar 2020 21:28:08 +0200 Subject: [PATCH] write_sensu: add the IncludeSource option --- src/collectd.conf.in | 1 + src/collectd.conf.pod | 14 ++++++++++---- src/write_sensu.c | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/collectd.conf.in b/src/collectd.conf.in index c09a0c1f5..6292ddc23 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1969,6 +1969,7 @@ # Port 3030 # StoreRates true # AlwaysAppendDS false +# IncludeSource false # Notifications true # Metrics true # EventServicePrefix "" diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 09ea15fc2..7fa48ee59 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -11316,10 +11316,6 @@ The I will send values to I, a powerful stream aggregation and monitoring system. The plugin sends I encoded data to a local I client using a TCP socket. -At the moment, the I does not send over a collectd_host -parameter so it is not possible to use one collectd instance as a gateway for -others. Each collectd host must pair with one I client. - Synopsis: @@ -11328,6 +11324,7 @@ Synopsis: Port "3030" StoreRates true AlwaysAppendDS false + IncludeSource false MetricHandler "influx" MetricHandler "default" NotificationHandler "flapjack" @@ -11418,6 +11415,15 @@ I. Consider the two given strings to be the key and value of an additional attribute for each metric being sent out to I. +=item B B|B + +If set to B, then the source host of the metrics/notification is passed +on to sensu using the source attribute. This may register the host as a proxy +client in sensu. + +If set to B (the default), then the hostname is discarded, making it appear +as if the event originated from the connected sensu agent. + =back =head2 Plugin C diff --git a/src/write_sensu.c b/src/write_sensu.c index 7d08fb5f6..03e633be4 100644 --- a/src/write_sensu.c +++ b/src/write_sensu.c @@ -111,6 +111,7 @@ struct sensu_host { bool metrics; bool store_rates; bool always_append_ds; + bool include_source; char *separator; char *node; char *service; @@ -371,6 +372,16 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ } } + if (host->include_source) { + res = my_asprintf(&temp_str, "%s, \"source\": \"%s\"", ret_str, vl->host); + free(ret_str); + if (res == -1) { + ERROR("write_sensu plugin: Unable to alloc memory"); + return NULL; + } + ret_str = temp_str; + } + // incorporate the plugin name information res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin); @@ -697,6 +708,16 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ } ret_str = temp_str; + if (host->include_source) { + res = my_asprintf(&temp_str, "%s, \"source\": \"%s\"", ret_str, n->host); + free(ret_str); + if (res == -1) { + ERROR("write_sensu plugin: Unable to alloc memory"); + return NULL; + } + ret_str = temp_str; + } + char *handlers_str = build_json_str_list("handlers", &(host->notification_handlers)); if (handlers_str == NULL) { @@ -1022,6 +1043,7 @@ static int sensu_config_node(oconfig_item_t *ci) /* {{{ */ host->metrics = false; host->store_rates = true; host->always_append_ds = false; + host->include_source = false; host->metric_handlers.nb_strs = 0; host->metric_handlers.strs = NULL; host->notification_handlers.nb_strs = 0; @@ -1094,6 +1116,10 @@ static int sensu_config_node(oconfig_item_t *ci) /* {{{ */ status = cf_util_get_boolean(child, &host->always_append_ds); if (status != 0) break; + } else if (strcasecmp("IncludeSource", child->key) == 0) { + status = cf_util_get_boolean(child, &host->include_source); + if (status != 0) + break; } else { WARNING("write_sensu plugin: ignoring unknown config " "option: \"%s\"", -- 2.39.5