From 840f4cef30b01a6928832f09daf00890b35f3582 Mon Sep 17 00:00:00 2001 From: Brandon Arp Date: Thu, 24 Mar 2016 16:44:46 -0700 Subject: [PATCH] add http headers functionality to write_http plugin --- src/collectd.conf.in | 1 + src/collectd.conf.pod | 6 ++++++ src/write_http.c | 22 +++++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 20b17b2d..41d10ee3 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1371,6 +1371,7 @@ # ClientKey "/etc/ssl/client.pem" # ClientCert "/etc/ssl/client.crt" # ClientKeyPass "secret" +# Header "X-Custom-Header: custom_value" # SSLVersion "TLSv1" # Format "Command" # StoreRates false diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index cf7ccd01..805fce4b 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -7577,6 +7577,12 @@ authentication. Password required to load the private key in B. +=item B
I
+ +A HTTP header to add to the request. Multiple headers are added if this option is specified more than once. Example: + + Header "X-Custom-Header: custom_value" + =item B B|B|B|B|B|B Define which SSL protocol version must be used. By default C will diff --git a/src/write_http.c b/src/write_http.c index ec3a01ce..27337813 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -156,7 +156,6 @@ static int wh_callback_init (wh_callback_t *cb) /* {{{ */ curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); - cb->headers = NULL; cb->headers = curl_slist_append (cb->headers, "Accept: */*"); if (cb->format == WH_FORMAT_JSON) cb->headers = curl_slist_append (cb->headers, "Content-Type: application/json"); @@ -540,6 +539,25 @@ static int config_set_format (wh_callback_t *cb, /* {{{ */ return (0); } /* }}} int config_set_format */ +static int wh_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */ + oconfig_item_t *ci) +{ + struct curl_slist *temp = NULL; + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) + { + WARNING ("write_http plugin: `%s' needs exactly one string argument.", name); + return (-1); + } + + temp = curl_slist_append(*dest, ci->values[0].value.string); + if (temp == NULL) + return (-1); + + *dest = temp; + + return (0); +} /* }}} int wh_config_append_string */ + static int wh_config_node (oconfig_item_t *ci) /* {{{ */ { wh_callback_t *cb; @@ -643,6 +661,8 @@ static int wh_config_node (oconfig_item_t *ci) /* {{{ */ status = cf_util_get_int (child, &cb->timeout); else if (strcasecmp ("LogHttpError", child->key) == 0) status = cf_util_get_boolean (child, &cb->log_http_error); + else if (strcasecmp ("Header", child->key) == 0) + status = wh_config_append_string ("Header", &cb->headers, child); else { ERROR ("write_http plugin: Invalid configuration " -- 2.11.0