1 /**
2 * collectd - src/http.c
3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2009 Doug MacEachern
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 * Doug MacEachern <dougm@hyperic.com>
22 **/
24 #include "collectd.h"
25 #include "plugin.h"
26 #include "common.h"
27 #include "utils_cache.h"
28 #include "utils_parse_option.h"
30 #include <curl/curl.h>
32 /*
33 * Private variables
34 */
35 static const char *config_keys[] =
36 {
37 "Location", "User", "Password"
38 };
39 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
41 static char *location = NULL;
43 char *user;
44 char *pass;
45 char *credentials;
47 static int http_init_curl(CURL *curl, char curl_errbuf[])
48 {
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);
62 if (user != NULL)
63 {
64 size_t credentials_size;
66 credentials_size = strlen (user) + 2;
67 if (pass != NULL)
68 credentials_size += strlen (pass);
70 credentials = (char *) malloc (credentials_size);
71 if (credentials == NULL)
72 {
73 ERROR ("curl plugin: malloc failed.");
74 return (-1);
75 }
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);
81 }
83 return (0);
84 }
86 static int http_init(void)
87 {
88 return (0);
89 }
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)
93 {
94 int offset = 0;
95 int status;
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))
104 return (-1);
106 if (ds->ds[index].type == DS_TYPE_COUNTER)
107 {
108 if (rates == NULL)
109 rates = uc_get_rate (ds, vl);
110 if (rates == NULL)
111 {
112 WARNING ("http plugin: "
113 "uc_get_rate failed.");
114 return (-1);
115 }
116 if (isnan(rates[index]))
117 {
118 /* dont output */
119 return (-1);
120 }
121 status = ssnprintf (buffer + offset,
122 buffer_len - offset,
123 "%lf", rates[index]);
124 }
125 else /* if (ds->ds[index].type == DS_TYPE_GAUGE) */
126 {
127 status = ssnprintf (buffer + offset, buffer_len - offset,
128 "%lf", vl->values[index].gauge);
129 }
131 if ((status < 1) || (status >= (buffer_len - offset)))
132 {
133 sfree (rates);
134 return (-1);
135 }
137 offset += status;
139 sfree (rates);
140 return (0);
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)
145 {
146 int offset = 0;
147 int status;
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))
155 return (-1);
156 offset = status;
158 return (0);
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)
163 {
164 int offset = 0;
165 int status;
167 assert (0 == strcmp (ds->type, vl->type));
169 /* hostname */
170 status = ssnprintf (buffer + offset, buffer_len - offset,
171 "%s", vl->host);
172 if ((status < 1) || (status >= buffer_len - offset))
173 return (-1);
174 offset += status;
176 /* plugin */
177 status = ssnprintf (buffer + offset, buffer_len - offset,
178 ",%s", vl->plugin);
179 if ((status < 1) || (status >= buffer_len - offset))
180 return (-1);
181 offset += status;
183 /* plugin_instance */
184 if (strlen (vl->plugin_instance) > 0)
185 {
186 status = ssnprintf (buffer + offset, buffer_len - offset,
187 ",%s", vl->plugin_instance);
188 if ((status < 1) || (status >= buffer_len - offset))
189 return (-1);
190 offset += status;
191 }
193 /* type (if its the same as plugin, don't bother repeating it */
194 if (0 != strcmp (vl->type, vl->plugin))
195 {
196 status = ssnprintf (buffer + offset, buffer_len - offset,
197 ",%s", vl->type);
198 if ((status < 1) || (status >= buffer_len - offset))
199 return (-1);
200 offset += status;
201 }
203 /* type_instance */
204 if (strlen (vl->type_instance) > 0)
205 {
206 status = ssnprintf (buffer + offset, buffer_len - offset,
207 ",%s", vl->type_instance);
208 if ((status < 1) || (status >= buffer_len - offset))
209 return (-1);
210 offset += status;
211 }
213 return (offset);
214 } /* int value_list_to_metric_name */
216 static int http_config (const char *key, const char *value)
217 {
218 if (strcasecmp ("Location", key) == 0)
219 {
220 if (location != NULL)
221 free (location);
222 location = strdup (value);
223 if (location != NULL)
224 {
225 int len = strlen (location);
226 while ((len > 0) && (location[len - 1] == '/'))
227 {
228 len--;
229 location[len] = '\0';
230 }
231 if (len <= 0)
232 {
233 free (location);
234 location = NULL;
235 }
236 }
237 }
238 else if (strcasecmp ("User", key) == 0)
239 {
240 if (user != NULL)
241 free (user);
242 user = strdup (value);
243 if (user != NULL)
244 {
245 int len = strlen (user);
246 while ((len > 0) && (user[len - 1] == '/'))
247 {
248 len--;
249 user[len] = '\0';
250 }
251 if (len <= 0)
252 {
253 free (user);
254 user = NULL;
255 }
256 }
257 }
258 else if (strcasecmp ("Password", key) == 0)
259 {
260 if (pass != NULL)
261 free (pass);
262 pass = strdup (value);
263 if (pass != NULL)
264 {
265 int len = strlen (pass);
266 while ((len > 0) && (pass[len - 1] == '/'))
267 {
268 len--;
269 pass[len] = '\0';
270 }
271 if (len <= 0)
272 {
273 free (pass);
274 pass = NULL;
275 }
276 }
277 }
278 else
279 {
280 return (-1);
281 }
282 return (0);
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)
287 {
288 CURL *curl;
289 char curl_errbuf[CURL_ERROR_SIZE];
291 char metric_name[512];
292 int metric_prefix_len;
293 char value[512];
294 char timestamp[512];
296 char csv_buffer[10240];
298 int status;
299 int offset = 0;
300 int i;
302 if (0 != strcmp (ds->type, vl->type)) {
303 ERROR ("http plugin: DS type does not match value list type");
304 return -1;
305 }
307 curl = curl_easy_init ();
308 if (curl == NULL)
309 {
310 ERROR ("curl plugin: curl_easy_init failed.");
311 return (-1);
312 }
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)
320 return (-1);
322 DEBUG ("http plugin: http_write: metric_name = %s;", metric_name);
324 if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
325 return (-1);
327 for (i = 0; i < ds->ds_num; i++)
328 {
330 if (value_list_to_string (value, sizeof (value), ds, vl, i) != 0)
331 return (-1);
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,
339 "\"%s\",%s,%s\n",
340 metric_name, timestamp, value);
341 offset += status;
343 } /* for */
345 printf(csv_buffer);
347 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, csv_buffer);
348 status = curl_easy_perform (curl);
349 if (status != 0)
350 {
351 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
352 status, curl_errbuf);
353 return (-1);
354 }
356 return (0);
358 } /* int http_write */
360 void module_register (void)
361 {
362 plugin_register_init("http", http_init);
363 plugin_register_config ("http", http_config,
364 config_keys, config_keys_num);
365 plugin_register_write ("http", http_write, /* user_data = */ NULL);
366 } /* void module_register */