X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_http.c;h=95132cf92f7bcd8d541bd53aa38a34b72de28c9a;hb=361f0013d5a20fa0b229376ddc3804c6e00058c5;hp=a12a7f56f1792cde3baec5166909334c12187cd6;hpb=e6798095bb2f063dd97619399575fef3e0fe501e;p=collectd.git diff --git a/src/write_http.c b/src/write_http.c index a12a7f56..95132cf9 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) { @@ -626,7 +649,6 @@ static int wh_config_node (oconfig_item_t *ci) /* {{{ */ { wh_callback_t *cb; int buffer_size = 0; - user_data_t user_data = { 0 }; char callback_name[DATA_MAX_NAME_LEN]; int status = 0; @@ -644,7 +666,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 +736,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 +777,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()); @@ -777,11 +812,27 @@ static int wh_config_node (oconfig_item_t *ci) /* {{{ */ DEBUG ("write_http: Registering write callback '%s' with URL '%s'", callback_name, cb->location); - user_data.data = cb; + user_data_t user_data = { + .data = cb + }; + 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 */