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"
34 #include <curl/curl.h>
39 static const char *config_keys[] =
41 "Location", "User", "Password"
43 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
45 static char *location = NULL;
52 char curl_errbuf[CURL_ERROR_SIZE];
54 static pthread_mutex_t send_lock = PTHREAD_MUTEX_INITIALIZER;
56 static int http_init(void) /* {{{ */
59 curl = curl_easy_init ();
63 ERROR ("curl plugin: curl_easy_init failed.");
67 struct curl_slist *headers=NULL;
69 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
71 headers = curl_slist_append(headers, "Accept: text/csv;q=0.8, */*;q=0.2");
72 headers = curl_slist_append(headers, "Content-Type: text/csv");
73 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
75 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
76 curl_easy_setopt (curl, CURLOPT_URL, location);
80 size_t credentials_size;
82 credentials_size = strlen (user) + 2;
84 credentials_size += strlen (pass);
86 credentials = (char *) malloc (credentials_size);
87 if (credentials == NULL)
89 ERROR ("curl plugin: malloc failed.");
93 ssnprintf (credentials, credentials_size, "%s:%s",
94 user, (pass == NULL) ? "" : pass);
95 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
96 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
102 static int http_value_list_to_string (char *buffer, int buffer_len, /* {{{ */
103 const data_set_t *ds, const value_list_t *vl, int index)
107 gauge_t *rates = NULL;
109 assert (0 == strcmp (ds->type, vl->type));
111 memset (buffer, '\0', buffer_len);
113 if ((ds->ds[index].type != DS_TYPE_COUNTER)
114 && (ds->ds[index].type != DS_TYPE_GAUGE))
117 if (ds->ds[index].type == DS_TYPE_COUNTER)
120 rates = uc_get_rate (ds, vl);
123 WARNING ("http plugin: "
124 "uc_get_rate failed.");
127 if (isnan(rates[index]))
132 status = ssnprintf (buffer + offset,
134 "%lf", rates[index]);
136 else /* if (ds->ds[index].type == DS_TYPE_GAUGE) */
138 status = ssnprintf (buffer + offset, buffer_len - offset,
139 "%lf", vl->values[index].gauge);
142 if ((status < 1) || (status >= (buffer_len - offset)))
152 } /* }}} int http_value_list_to_string */
154 static int http_value_list_to_timestamp (char *buffer, int buffer_len, /* {{{ */
155 const data_set_t *ds, const value_list_t *vl)
160 assert (0 == strcmp (ds->type, vl->type));
162 memset (buffer, '\0', buffer_len);
164 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
165 if ((status < 1) || (status >= buffer_len))
170 } /* }}} int http_value_list_to_timestamp */
172 static int http_value_list_to_metric_name (char *buffer, int buffer_len, /* {{{ */
173 const data_set_t *ds, const value_list_t *vl)
178 assert (0 == strcmp (ds->type, vl->type));
181 status = ssnprintf (buffer + offset, buffer_len - offset,
183 if ((status < 1) || (status >= buffer_len - offset))
188 status = ssnprintf (buffer + offset, buffer_len - offset,
190 if ((status < 1) || (status >= buffer_len - offset))
194 /* plugin_instance */
195 if (strlen (vl->plugin_instance) > 0)
197 status = ssnprintf (buffer + offset, buffer_len - offset,
198 ",%s", vl->plugin_instance);
199 if ((status < 1) || (status >= buffer_len - offset))
204 /* type (if its the same as plugin, don't bother repeating it */
205 if (0 != strcmp (vl->type, vl->plugin))
207 status = ssnprintf (buffer + offset, buffer_len - offset,
209 if ((status < 1) || (status >= buffer_len - offset))
215 if (strlen (vl->type_instance) > 0)
217 status = ssnprintf (buffer + offset, buffer_len - offset,
218 ",%s", vl->type_instance);
219 if ((status < 1) || (status >= buffer_len - offset))
225 } /* }}} int http_value_list_to_metric_name */
227 static int http_config (const char *key, const char *value) /* {{{ */
229 if (strcasecmp ("Location", key) == 0)
231 if (location != NULL)
233 location = strdup (value);
234 if (location != NULL)
236 int len = strlen (location);
237 while ((len > 0) && (location[len - 1] == '/'))
240 location[len] = '\0';
249 else if (strcasecmp ("User", key) == 0)
253 user = strdup (value);
256 int len = strlen (user);
257 while ((len > 0) && (user[len - 1] == '/'))
269 else if (strcasecmp ("Password", key) == 0)
273 pass = strdup (value);
276 int len = strlen (pass);
277 while ((len > 0) && (pass[len - 1] == '/'))
294 } /* }}} int http_config */
296 static int http_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
297 user_data_t __attribute__((unused)) *user_data)
300 char metric_name[512];
301 int metric_prefix_len;
305 char csv_buffer[1024];
311 if (0 != strcmp (ds->type, vl->type)) {
312 ERROR ("http plugin: DS type does not match value list type");
316 metric_prefix_len = http_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 (http_value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
327 for (i = 0; i < ds->ds_num; i++)
330 if (http_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 pthread_mutex_lock (&send_lock);
349 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, csv_buffer);
350 status = curl_easy_perform (curl);
353 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
354 status, curl_errbuf);
358 pthread_mutex_unlock (&send_lock);
362 } /* }}} int http_write */
364 static int http_shutdown (void) /* {{{ */
366 curl_easy_cleanup(curl);
370 void module_register (void) /* {{{ */
372 plugin_register_init("http", http_init);
373 plugin_register_config ("http", http_config,
374 config_keys, config_keys_num);
375 plugin_register_write ("http", http_write, /* user_data = */ NULL);
376 plugin_register_shutdown("http", http_shutdown);
377 } /* }}} void module_register */
379 /* vim: set fdm=marker sw=8 ts=8 tw=78 : */