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_FORBID_REUSE, 1);
59 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
60 curl_easy_setopt (curl, CURLOPT_URL, location);
64 size_t credentials_size;
66 credentials_size = strlen (user) + 2;
68 credentials_size += strlen (pass);
70 credentials = (char *) malloc (credentials_size);
71 if (credentials == NULL)
73 ERROR ("curl plugin: malloc failed.");
77 ssnprintf (credentials, credentials_size, "%s:%s",
78 user, (pass == NULL) ? "" : pass);
79 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
80 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
86 static int http_init(void)
91 static int value_list_to_string (char *buffer, int buffer_len,
92 const data_set_t *ds, const value_list_t *vl, int index)
96 gauge_t *rates = NULL;
98 assert (0 == strcmp (ds->type, vl->type));
100 memset (buffer, '\0', buffer_len);
102 if ((ds->ds[index].type != DS_TYPE_COUNTER)
103 && (ds->ds[index].type != DS_TYPE_GAUGE))
106 if (ds->ds[index].type == DS_TYPE_COUNTER)
109 rates = uc_get_rate (ds, vl);
112 WARNING ("http plugin: "
113 "uc_get_rate failed.");
116 if (isnan(rates[index]))
121 status = ssnprintf (buffer + offset,
123 "%lf", rates[index]);
125 else /* if (ds->ds[index].type == DS_TYPE_GAUGE) */
127 status = ssnprintf (buffer + offset, buffer_len - offset,
128 "%lf", vl->values[index].gauge);
131 if ((status < 1) || (status >= (buffer_len - offset)))
141 } /* int value_list_to_string */
143 static int value_list_to_timestamp (char *buffer, int buffer_len,
144 const data_set_t *ds, const value_list_t *vl)
149 assert (0 == strcmp (ds->type, vl->type));
151 memset (buffer, '\0', buffer_len);
153 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
154 if ((status < 1) || (status >= buffer_len))
159 } /* int value_list_to_timestamp */
161 static int value_list_to_metric_name (char *buffer, int buffer_len,
162 const data_set_t *ds, const value_list_t *vl)
167 assert (0 == strcmp (ds->type, vl->type));
170 status = ssnprintf (buffer + offset, buffer_len - offset,
172 if ((status < 1) || (status >= buffer_len - offset))
177 status = ssnprintf (buffer + offset, buffer_len - offset,
179 if ((status < 1) || (status >= buffer_len - offset))
183 /* plugin_instance */
184 if (strlen (vl->plugin_instance) > 0)
186 status = ssnprintf (buffer + offset, buffer_len - offset,
187 ",%s", vl->plugin_instance);
188 if ((status < 1) || (status >= buffer_len - offset))
193 /* type (if its the same as plugin, don't bother repeating it */
194 if (0 != strcmp (vl->type, vl->plugin))
196 status = ssnprintf (buffer + offset, buffer_len - offset,
198 if ((status < 1) || (status >= buffer_len - offset))
204 if (strlen (vl->type_instance) > 0)
206 status = ssnprintf (buffer + offset, buffer_len - offset,
207 ",%s", vl->type_instance);
208 if ((status < 1) || (status >= buffer_len - offset))
214 } /* int value_list_to_metric_name */
216 static int http_config (const char *key, const char *value)
218 if (strcasecmp ("Location", key) == 0)
220 if (location != NULL)
222 location = strdup (value);
223 if (location != NULL)
225 int len = strlen (location);
226 while ((len > 0) && (location[len - 1] == '/'))
229 location[len] = '\0';
238 else if (strcasecmp ("User", key) == 0)
242 user = strdup (value);
245 int len = strlen (user);
246 while ((len > 0) && (user[len - 1] == '/'))
258 else if (strcasecmp ("Password", key) == 0)
262 pass = strdup (value);
265 int len = strlen (pass);
266 while ((len > 0) && (pass[len - 1] == '/'))
283 } /* int http_config */
285 static int http_write (const data_set_t *ds, const value_list_t *vl,
286 user_data_t __attribute__((unused)) *user_data)
289 char curl_errbuf[CURL_ERROR_SIZE];
291 char metric_name[512];
292 int metric_prefix_len;
296 char csv_buffer[10240];
302 if (0 != strcmp (ds->type, vl->type)) {
303 ERROR ("http plugin: DS type does not match value list type");
307 curl = curl_easy_init ();
310 ERROR ("curl plugin: curl_easy_init failed.");
314 http_init_curl(curl, curl_errbuf);
316 metric_prefix_len = value_list_to_metric_name (metric_name,
317 sizeof (metric_name), ds, vl);
319 if (metric_prefix_len == -1)
322 DEBUG ("http plugin: http_write: metric_name = %s;", metric_name);
324 if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
327 for (i = 0; i < ds->ds_num; i++)
330 if (value_list_to_string (value, sizeof (value), ds, vl, i) != 0)
333 ssnprintf(metric_name + metric_prefix_len, sizeof (metric_name) - metric_prefix_len,
334 ",%s", ds->ds[i].name);
336 escape_string (metric_name, sizeof (metric_name));
338 status = ssnprintf (csv_buffer + offset, sizeof (csv_buffer) - offset,
340 metric_name, timestamp, value);
347 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, csv_buffer);
348 status = curl_easy_perform (curl);
351 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
352 status, curl_errbuf);
356 curl_easy_cleanup(curl);
360 } /* int http_write */
362 void module_register (void)
364 plugin_register_init("http", http_init);
365 plugin_register_config ("http", http_config,
366 config_keys, config_keys_num);
367 plugin_register_write ("http", http_write, /* user_data = */ NULL);
368 } /* void module_register */