2 * collectd - src/http.c
3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2009 Doug MacEachern
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * Doug MacEachern <dougm@hyperic.com>
27 #include "utils_cache.h"
28 #include "utils_parse_option.h"
30 #include <curl/curl.h>
35 static const char *config_keys[] =
37 "Location", "User", "Password"
39 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
41 static char *location = NULL;
47 static int http_init_curl(CURL *curl, char curl_errbuf[])
49 struct curl_slist *headers=NULL;
51 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
53 headers = curl_slist_append(headers, "Accept: text/csv");
54 headers = curl_slist_append(headers, "Content-Type: text/csv");
55 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
57 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
58 curl_easy_setopt (curl, CURLOPT_URL, location);
62 size_t credentials_size;
64 credentials_size = strlen (user) + 2;
66 credentials_size += strlen (pass);
68 credentials = (char *) malloc (credentials_size);
69 if (credentials == NULL)
71 ERROR ("curl plugin: malloc failed.");
75 ssnprintf (credentials, credentials_size, "%s:%s",
76 user, (pass == NULL) ? "" : pass);
77 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
78 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
84 static int http_init(void)
89 static int value_list_to_string (char *buffer, int buffer_len,
90 const data_set_t *ds, const value_list_t *vl, int index)
94 gauge_t *rates = NULL;
96 assert (0 == strcmp (ds->type, vl->type));
98 memset (buffer, '\0', buffer_len);
100 if ((ds->ds[index].type != DS_TYPE_COUNTER)
101 && (ds->ds[index].type != DS_TYPE_GAUGE))
104 if (ds->ds[index].type == DS_TYPE_COUNTER)
107 rates = uc_get_rate (ds, vl);
110 WARNING ("http plugin: "
111 "uc_get_rate failed.");
114 if (isnan(rates[index]))
119 status = ssnprintf (buffer + offset,
121 "%lf", rates[index]);
123 else /* if (ds->ds[index].type == DS_TYPE_GAUGE) */
125 status = ssnprintf (buffer + offset, buffer_len - offset,
126 "%lf", vl->values[index].gauge);
129 if ((status < 1) || (status >= (buffer_len - offset)))
139 } /* int value_list_to_string */
141 static int value_list_to_timestamp (char *buffer, int buffer_len,
142 const data_set_t *ds, const value_list_t *vl)
147 assert (0 == strcmp (ds->type, vl->type));
149 memset (buffer, '\0', buffer_len);
151 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
152 if ((status < 1) || (status >= buffer_len))
157 } /* int value_list_to_timestamp */
159 static int value_list_to_metric_name (char *buffer, int buffer_len,
160 const data_set_t *ds, const value_list_t *vl)
165 assert (0 == strcmp (ds->type, vl->type));
168 status = ssnprintf (buffer + offset, buffer_len - offset,
170 if ((status < 1) || (status >= buffer_len - offset))
175 status = ssnprintf (buffer + offset, buffer_len - offset,
177 if ((status < 1) || (status >= buffer_len - offset))
181 /* plugin_instance */
182 if (strlen (vl->plugin_instance) > 0)
184 status = ssnprintf (buffer + offset, buffer_len - offset,
185 ",%s", vl->plugin_instance);
186 if ((status < 1) || (status >= buffer_len - offset))
191 /* type (if its the same as plugin, don't bother repeating it */
192 if (0 != strcmp (vl->type, vl->plugin))
194 status = ssnprintf (buffer + offset, buffer_len - offset,
196 if ((status < 1) || (status >= buffer_len - offset))
202 if (strlen (vl->type_instance) > 0)
204 status = ssnprintf (buffer + offset, buffer_len - offset,
205 ",%s", vl->type_instance);
206 if ((status < 1) || (status >= buffer_len - offset))
212 } /* int value_list_to_metric_name */
214 static int http_config (const char *key, const char *value)
216 if (strcasecmp ("Location", key) == 0)
218 if (location != NULL)
220 location = strdup (value);
221 if (location != NULL)
223 int len = strlen (location);
224 while ((len > 0) && (location[len - 1] == '/'))
227 location[len] = '\0';
236 else if (strcasecmp ("User", key) == 0)
240 user = strdup (value);
243 int len = strlen (user);
244 while ((len > 0) && (user[len - 1] == '/'))
256 else if (strcasecmp ("Password", key) == 0)
260 pass = strdup (value);
263 int len = strlen (pass);
264 while ((len > 0) && (pass[len - 1] == '/'))
281 } /* int http_config */
283 static int http_write (const data_set_t *ds, const value_list_t *vl,
284 user_data_t __attribute__((unused)) *user_data)
287 char curl_errbuf[CURL_ERROR_SIZE];
289 char metric_name[512];
290 int metric_prefix_len;
294 char csv_buffer[10240];
300 if (0 != strcmp (ds->type, vl->type)) {
301 ERROR ("http plugin: DS type does not match value list type");
305 curl = curl_easy_init ();
308 ERROR ("curl plugin: curl_easy_init failed.");
312 http_init_curl(curl, curl_errbuf);
314 metric_prefix_len = value_list_to_metric_name (metric_name,
315 sizeof (metric_name), ds, vl);
317 if (metric_prefix_len == -1)
320 DEBUG ("http plugin: http_write: metric_name = %s;", metric_name);
322 if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
325 for (i = 0; i < ds->ds_num; i++)
328 if (value_list_to_string (value, sizeof (value), ds, vl, i) != 0)
331 ssnprintf(metric_name + metric_prefix_len, sizeof (metric_name) - metric_prefix_len,
332 ",%s", ds->ds[i].name);
334 escape_string (metric_name, sizeof (metric_name));
336 status = ssnprintf (csv_buffer + offset, sizeof (csv_buffer) - offset,
338 metric_name, timestamp, value);
345 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, csv_buffer);
346 status = curl_easy_perform (curl);
349 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
350 status, curl_errbuf);
356 } /* int http_write */
358 void module_register (void)
360 plugin_register_init("http", http_init);
361 plugin_register_config ("http", http_config,
362 config_keys, config_keys_num);
363 plugin_register_write ("http", http_write, /* user_data = */ NULL);
364 } /* void module_register */