From: Florian Forster Date: Fri, 20 Nov 2015 09:55:09 +0000 (+0100) Subject: write_http plugin: Implement writing of notifications. X-Git-Tag: collectd-5.6.0~96^2~2 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=adb1603b1fc09b3f19924cc9e5c8949603dae2d4;p=collectd.git write_http plugin: Implement writing of notifications. --- diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 1e8994ee..244fd679 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1400,6 +1400,8 @@ # Header "X-Custom-Header: custom_value" # SSLVersion "TLSv1" # Format "Command" +# Metrics true +# Notifications false # StoreRates false # BufferSize 4096 # LowSpeedLimit 0 diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 03cf8f0c..1e564cf4 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -7843,6 +7843,14 @@ create output in the I (JSON). When set to KAIROSDB Defaults to B. +=item B B|B + +Controls whether I are POSTed to this location. Defaults to B. + +=item B B|B + +Controls whether I are POSTed to this location. Defaults to B. + =item B B If set to B, convert counter values to rates. If set to B (the diff --git a/src/write_http.c b/src/write_http.c index a12a7f56..d8d8c34e 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -65,6 +65,8 @@ struct wh_callback_s #define WH_FORMAT_JSON 1 #define WH_FORMAT_KAIROSDB 2 int format; + _Bool send_metrics; + _Bool send_notifications; CURL *curl; struct curl_slist *headers; @@ -111,11 +113,12 @@ static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */ } } /* }}} wh_reset_buffer */ -static int wh_send_buffer (wh_callback_t *cb) /* {{{ */ +/* must hold cb->send_lock when calling */ +static int wh_post_nolock (wh_callback_t *cb, char const *data) /* {{{ */ { int status = 0; - curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer); + curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, data); status = curl_easy_perform (cb->curl); wh_log_http_error (cb); @@ -127,7 +130,7 @@ static int wh_send_buffer (wh_callback_t *cb) /* {{{ */ status, cb->curl_errbuf); } return (status); -} /* }}} wh_send_buffer */ +} /* }}} wh_post_nolock */ static int wh_callback_init (wh_callback_t *cb) /* {{{ */ { @@ -247,7 +250,7 @@ static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */ return (0); } - status = wh_send_buffer (cb); + status = wh_post_nolock (cb, cb->send_buffer); wh_reset_buffer (cb); } else if (cb->format == WH_FORMAT_JSON || cb->format == WH_FORMAT_KAIROSDB) @@ -269,7 +272,7 @@ static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */ return (status); } - status = wh_send_buffer (cb); + status = wh_post_nolock (cb, cb->send_buffer); wh_reset_buffer (cb); } else @@ -297,15 +300,11 @@ static int wh_flush (cdtime_t timeout, /* {{{ */ pthread_mutex_lock (&cb->send_lock); - if (cb->curl == NULL) + if (wh_callback_init (cb) != 0) { - status = wh_callback_init (cb); - if (status != 0) - { - ERROR ("write_http plugin: wh_callback_init failed."); - pthread_mutex_unlock (&cb->send_lock); - return (-1); - } + ERROR ("write_http plugin: wh_callback_init failed."); + pthread_mutex_unlock (&cb->send_lock); + return (-1); } status = wh_flush_nolock (timeout, cb); @@ -398,16 +397,11 @@ static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{ } pthread_mutex_lock (&cb->send_lock); - - if (cb->curl == NULL) + if (wh_callback_init (cb) != 0) { - status = wh_callback_init (cb); - if (status != 0) - { - ERROR ("write_http plugin: wh_callback_init failed."); - pthread_mutex_unlock (&cb->send_lock); - return (-1); - } + ERROR ("write_http plugin: wh_callback_init failed."); + pthread_mutex_unlock (&cb->send_lock); + return (-1); } if (command_len >= cb->send_buffer_free) @@ -446,16 +440,11 @@ static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ * int status; pthread_mutex_lock (&cb->send_lock); - - if (cb->curl == NULL) + if (wh_callback_init (cb) != 0) { - status = wh_callback_init (cb); - if (status != 0) - { - ERROR ("write_http plugin: wh_callback_init failed."); - pthread_mutex_unlock (&cb->send_lock); - return (-1); - } + ERROR ("write_http plugin: wh_callback_init failed."); + pthread_mutex_unlock (&cb->send_lock); + return (-1); } status = format_json_value_list (cb->send_buffer, @@ -558,6 +547,7 @@ static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */ return (-EINVAL); cb = user_data->data; + assert (cb->send_metrics); switch(cb->format) { case WH_FORMAT_JSON: @@ -573,6 +563,39 @@ static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */ return (status); } /* }}} int wh_write */ +static int wh_notify (notification_t const *n, user_data_t *ud) /* {{{ */ +{ + wh_callback_t *cb; + char alert[4096]; + int status; + + if ((ud == NULL) || (ud->data == NULL)) + return (EINVAL); + + cb = ud->data; + assert (cb->send_notifications); + + status = format_json_notification (alert, sizeof (alert), n); + if (status != 0) + { + ERROR ("write_http plugin: formatting notification failed"); + return status; + } + + pthread_mutex_lock (&cb->send_lock); + if (wh_callback_init (cb) != 0) + { + ERROR ("write_http plugin: wh_callback_init failed."); + pthread_mutex_unlock (&cb->send_lock); + return (-1); + } + + status = wh_post_nolock (cb, alert); + pthread_mutex_unlock (&cb->send_lock); + + return (status); +} /* }}} int wh_notify */ + static int config_set_format (wh_callback_t *cb, /* {{{ */ oconfig_item_t *ci) { @@ -644,7 +667,8 @@ static int wh_config_node (oconfig_item_t *ci) /* {{{ */ cb->timeout = 0; cb->log_http_error = 0; cb->headers = NULL; - + cb->send_metrics = 1; + cb->send_notifications = 0; pthread_mutex_init (&cb->send_lock, /* attr = */ NULL); @@ -713,6 +737,10 @@ static int wh_config_node (oconfig_item_t *ci) /* {{{ */ } else if (strcasecmp ("Format", child->key) == 0) status = config_set_format (cb, child); + else if (strcasecmp ("Metrics", child->key) == 0) + cf_util_get_boolean (child, &cb->send_metrics); + else if (strcasecmp ("Notifications", child->key) == 0) + cf_util_get_boolean (child, &cb->send_notifications); else if (strcasecmp ("StoreRates", child->key) == 0) status = cf_util_get_boolean (child, &cb->store_rates); else if (strcasecmp ("BufferSize", child->key) == 0) @@ -750,6 +778,14 @@ static int wh_config_node (oconfig_item_t *ci) /* {{{ */ return (-1); } + if (!cb->send_metrics && !cb->send_notifications) + { + ERROR ("write_http plugin: Neither metrics nor notifications " + "are enabled for \"%s\".", cb->name); + wh_callback_free (cb); + return (-1); + } + if (cb->low_speed_limit > 0) cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval()); @@ -781,7 +817,20 @@ static int wh_config_node (oconfig_item_t *ci) /* {{{ */ plugin_register_flush (callback_name, wh_flush, &user_data); user_data.free_func = wh_callback_free; - plugin_register_write (callback_name, wh_write, &user_data); + + if (cb->send_metrics) + { + plugin_register_write (callback_name, wh_write, &user_data); + user_data.free_func = NULL; + + plugin_register_flush (callback_name, wh_flush, &user_data); + } + + if (cb->send_notifications) + { + plugin_register_notification (callback_name, wh_notify, &user_data); + user_data.free_func = NULL; + } return (0); } /* }}} int wh_config_node */