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)
99 gauge_t *rates = NULL;
101 assert (0 == strcmp (ds->type, vl->type));
103 memset (buffer, '\0', buffer_len);
105 for (i = 0; i < ds->ds_num; i++)
109 status = ssnprintf (buffer + offset,
114 if ((ds->ds[i].type != DS_TYPE_COUNTER)
115 && (ds->ds[i].type != DS_TYPE_GAUGE))
118 if (ds->ds[i].type == DS_TYPE_COUNTER)
121 rates = uc_get_rate (ds, vl);
124 WARNING ("http plugin: "
125 "uc_get_rate failed.");
133 status = ssnprintf (buffer + offset,
137 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
139 status = ssnprintf (buffer + offset, buffer_len - offset,
140 "%lf", vl->values[i].gauge);
143 if ((status < 1) || (status >= (buffer_len - offset)))
150 } /* for ds->ds_num */
154 } /* int value_list_to_string */
156 static int value_list_to_timestamp (char *buffer, int buffer_len,
157 const data_set_t *ds, const value_list_t *vl)
162 assert (0 == strcmp (ds->type, vl->type));
164 memset (buffer, '\0', buffer_len);
166 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
167 if ((status < 1) || (status >= buffer_len))
172 } /* int value_list_to_timestamp */
174 static int value_list_to_metric_name (char *buffer, int buffer_len,
175 const data_set_t *ds, const value_list_t *vl)
180 assert (0 == strcmp (ds->type, vl->type));
183 status = ssnprintf (buffer + offset, buffer_len - offset,
185 if ((status < 1) || (status >= buffer_len - offset))
190 status = ssnprintf (buffer + offset, buffer_len - offset,
192 if ((status < 1) || (status >= buffer_len - offset))
196 /* plugin_instance */
197 if (strlen (vl->plugin_instance) > 0)
199 status = ssnprintf (buffer + offset, buffer_len - offset,
200 ",%s", vl->plugin_instance);
201 if ((status < 1) || (status >= buffer_len - offset))
206 /* type (if its the same as plugin, don't bother repeating it */
207 if (0 != strcmp (vl->type, vl->plugin))
209 status = ssnprintf (buffer + offset, buffer_len - offset,
211 if ((status < 1) || (status >= buffer_len - offset))
217 if (strlen (vl->type_instance) > 0)
219 status = ssnprintf (buffer + offset, buffer_len - offset,
220 ",%s", vl->type_instance);
221 if ((status < 1) || (status >= buffer_len - offset))
227 } /* int value_list_to_metric_name */
229 static int http_config (const char *key, const char *value)
231 if (strcasecmp ("Location", key) == 0)
233 if (location != NULL)
235 location = strdup (value);
236 if (location != NULL)
238 int len = strlen (location);
239 while ((len > 0) && (location[len - 1] == '/'))
242 location[len] = '\0';
251 else if (strcasecmp ("User", key) == 0)
255 user = strdup (value);
258 int len = strlen (user);
259 while ((len > 0) && (user[len - 1] == '/'))
271 else if (strcasecmp ("Password", key) == 0)
275 pass = strdup (value);
278 int len = strlen (pass);
279 while ((len > 0) && (pass[len - 1] == '/'))
296 } /* int http_config */
298 static int http_write (const data_set_t *ds, const value_list_t *vl,
299 user_data_t __attribute__((unused)) *user_data)
301 char metric_name[512];
305 char csv_buffer[1536];
309 if (0 != strcmp (ds->type, vl->type)) {
310 ERROR ("http plugin: DS type does not match value list type");
314 if (value_list_to_metric_name (metric_name, sizeof (metric_name), ds, vl) != 0)
317 DEBUG ("http plugin: http_write: metric_name = %s;", metric_name);
319 if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
322 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
325 escape_string (metric_name, sizeof (metric_name));
327 status = ssnprintf (csv_buffer, 1536,
329 metric_name, timestamp, values);
331 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, csv_buffer);
332 status = curl_easy_perform (curl);
335 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
336 status, curl_errbuf);
342 } /* int http_write */
344 void module_register (void)
346 plugin_register_init("http", http_init);
347 plugin_register_config ("http", http_config,
348 config_keys, config_keys_num);
349 plugin_register_write ("http", http_write, /* user_data = */ NULL);
350 } /* void module_register */