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;
48 char curl_errbuf[CURL_ERROR_SIZE];
49 struct curl_slist *headers=NULL;
51 static int http_init(void)
54 curl = curl_easy_init ();
57 ERROR ("curl plugin: curl_easy_init failed.");
61 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
62 headers = curl_slist_append(headers, "Accept: application/vnd.absperf.ssbe+json");
63 headers = curl_slist_append(headers, "Content-Type: text/csv");
65 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
66 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
67 curl_easy_setopt (curl, CURLOPT_URL, location);
71 size_t credentials_size;
73 credentials_size = strlen (user) + 2;
75 credentials_size += strlen (pass);
77 credentials = (char *) malloc (credentials_size);
78 if (credentials == NULL)
80 ERROR ("curl plugin: malloc failed.");
84 ssnprintf (credentials, credentials_size, "%s:%s",
85 user, (pass == NULL) ? "" : pass);
86 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
87 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
93 static int value_list_to_string (char *buffer, int buffer_len,
94 const data_set_t *ds, const value_list_t *vl, int index)
98 gauge_t *rates = NULL;
100 assert (0 == strcmp (ds->type, vl->type));
102 memset (buffer, '\0', buffer_len);
104 if ((ds->ds[index].type != DS_TYPE_COUNTER)
105 && (ds->ds[index].type != DS_TYPE_GAUGE))
108 if (ds->ds[index].type == DS_TYPE_COUNTER)
111 rates = uc_get_rate (ds, vl);
114 WARNING ("http plugin: "
115 "uc_get_rate failed.");
118 if (isnan(rates[index]))
123 status = ssnprintf (buffer + offset,
125 "%lf", rates[index]);
127 else /* if (ds->ds[index].type == DS_TYPE_GAUGE) */
129 status = ssnprintf (buffer + offset, buffer_len - offset,
130 "%lf", vl->values[index].gauge);
133 if ((status < 1) || (status >= (buffer_len - offset)))
143 } /* int value_list_to_string */
145 static int value_list_to_timestamp (char *buffer, int buffer_len,
146 const data_set_t *ds, const value_list_t *vl)
151 assert (0 == strcmp (ds->type, vl->type));
153 memset (buffer, '\0', buffer_len);
155 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
156 if ((status < 1) || (status >= buffer_len))
161 } /* int value_list_to_timestamp */
163 static int value_list_to_metric_name (char *buffer, int buffer_len,
164 const data_set_t *ds, const value_list_t *vl)
169 assert (0 == strcmp (ds->type, vl->type));
172 status = ssnprintf (buffer + offset, buffer_len - offset,
174 if ((status < 1) || (status >= buffer_len - offset))
179 status = ssnprintf (buffer + offset, buffer_len - offset,
181 if ((status < 1) || (status >= buffer_len - offset))
185 /* plugin_instance */
186 if (strlen (vl->plugin_instance) > 0)
188 status = ssnprintf (buffer + offset, buffer_len - offset,
189 ",%s", vl->plugin_instance);
190 if ((status < 1) || (status >= buffer_len - offset))
195 /* type (if its the same as plugin, don't bother repeating it */
196 if (0 != strcmp (vl->type, vl->plugin))
198 status = ssnprintf (buffer + offset, buffer_len - offset,
200 if ((status < 1) || (status >= buffer_len - offset))
206 if (strlen (vl->type_instance) > 0)
208 status = ssnprintf (buffer + offset, buffer_len - offset,
209 ",%s", vl->type_instance);
210 if ((status < 1) || (status >= buffer_len - offset))
216 } /* int value_list_to_metric_name */
218 static int http_config (const char *key, const char *value)
220 if (strcasecmp ("Location", key) == 0)
222 if (location != NULL)
224 location = strdup (value);
225 if (location != NULL)
227 int len = strlen (location);
228 while ((len > 0) && (location[len - 1] == '/'))
231 location[len] = '\0';
240 else if (strcasecmp ("User", key) == 0)
244 user = strdup (value);
247 int len = strlen (user);
248 while ((len > 0) && (user[len - 1] == '/'))
260 else if (strcasecmp ("Password", key) == 0)
264 pass = strdup (value);
267 int len = strlen (pass);
268 while ((len > 0) && (pass[len - 1] == '/'))
285 } /* int http_config */
287 static int http_write (const data_set_t *ds, const value_list_t *vl,
288 user_data_t __attribute__((unused)) *user_data)
290 char metric_name[512];
291 int metric_prefix_len;
295 char csv_buffer[10240];
301 if (0 != strcmp (ds->type, vl->type)) {
302 ERROR ("http plugin: DS type does not match value list type");
306 metric_prefix_len = value_list_to_metric_name (metric_name,
307 sizeof (metric_name), ds, vl);
309 if (metric_prefix_len == -1)
312 DEBUG ("http plugin: http_write: metric_name = %s;", metric_name);
314 if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
317 for (i = 0; i < ds->ds_num; i++)
320 if (value_list_to_string (value, sizeof (value), ds, vl, i) != 0)
323 ssnprintf(metric_name + metric_prefix_len, sizeof (metric_name) - metric_prefix_len,
324 ",%s", ds->ds[i].name);
326 escape_string (metric_name, sizeof (metric_name));
328 status = ssnprintf (csv_buffer + offset, sizeof (csv_buffer) - offset,
330 metric_name, timestamp, value);
337 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, csv_buffer);
338 status = curl_easy_perform (curl);
341 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
342 status, curl_errbuf);
348 } /* int http_write */
350 void module_register (void)
352 plugin_register_init("http", http_init);
353 plugin_register_config ("http", http_config,
354 config_keys, config_keys_num);
355 plugin_register_write ("http", http_write, /* user_data = */ NULL);
356 } /* void module_register */