http plugin: it works, i think
[collectd.git] / src / http.c
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  **/
23
24 #include "collectd.h"
25 #include "plugin.h"
26 #include "common.h"
27 #include "utils_cache.h"
28 #include "utils_parse_option.h"
29
30 #include <curl/curl.h>
31
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);
40
41 static char *location   = NULL;
42
43 char *user;
44 char *pass;
45 char *credentials;
46
47 CURL *curl;
48 char curl_errbuf[CURL_ERROR_SIZE];
49 struct curl_slist *headers=NULL;
50
51 static int http_init(void)
52 {
53
54   curl = curl_easy_init ();
55   if (curl == NULL)
56   {
57     ERROR ("curl plugin: curl_easy_init failed.");
58     return (-1);
59   }
60
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");
64
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);
68
69   if (user != NULL)
70   {
71     size_t credentials_size;
72
73     credentials_size = strlen (user) + 2;
74     if (pass != NULL)
75       credentials_size += strlen (pass);
76
77     credentials = (char *) malloc (credentials_size);
78     if (credentials == NULL)
79     {
80       ERROR ("curl plugin: malloc failed.");
81       return (-1);
82     }
83
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);
88   }
89
90   return (0);
91 }
92
93 static int value_list_to_string (char *buffer, int buffer_len,
94     const data_set_t *ds, const value_list_t *vl)
95 {
96   int offset = 0;
97   int status;
98   int i;
99   gauge_t *rates = NULL;
100
101   assert (0 == strcmp (ds->type, vl->type));
102
103   memset (buffer, '\0', buffer_len);
104
105   for (i = 0; i < ds->ds_num; i++)
106   {
107     if (i > 0) 
108     {
109       status = ssnprintf (buffer + offset,
110           buffer_len - offset,
111           ":");
112       offset += status;
113     }
114     if ((ds->ds[i].type != DS_TYPE_COUNTER)
115         && (ds->ds[i].type != DS_TYPE_GAUGE))
116       return (-1);
117
118     if (ds->ds[i].type == DS_TYPE_COUNTER)
119     {
120       if (rates == NULL)
121         rates = uc_get_rate (ds, vl);
122       if (rates == NULL)
123       {
124         WARNING ("http plugin: "
125             "uc_get_rate failed.");
126         return (-1);
127       }
128       if (isnan(rates[i]))
129       {
130         /* dont output */
131         return (-1);
132       }
133       status = ssnprintf (buffer + offset,
134           buffer_len - offset,
135           "%lf", rates[i]);
136     }
137     else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
138     {
139       status = ssnprintf (buffer + offset, buffer_len - offset,
140           "%lf", vl->values[i].gauge);
141     }
142
143     if ((status < 1) || (status >= (buffer_len - offset)))
144     {
145       sfree (rates);
146       return (-1);
147     }
148
149     offset += status;
150   } /* for ds->ds_num */
151
152   sfree (rates);
153   return (0);
154 } /* int value_list_to_string */
155
156 static int value_list_to_timestamp (char *buffer, int buffer_len,
157     const data_set_t *ds, const value_list_t *vl)
158 {
159   int offset = 0;
160   int status;
161
162   assert (0 == strcmp (ds->type, vl->type));
163
164   memset (buffer, '\0', buffer_len);
165
166   status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
167   if ((status < 1) || (status >= buffer_len))
168     return (-1);
169   offset = status;
170
171   return (0);
172 } /* int value_list_to_timestamp */
173
174 static int value_list_to_metric_name (char *buffer, int buffer_len,
175     const data_set_t *ds, const value_list_t *vl)
176 {
177   int offset = 0;
178   int status;
179
180   assert (0 == strcmp (ds->type, vl->type));
181
182   /* hostname */
183   status = ssnprintf (buffer + offset, buffer_len - offset,
184       "%s", vl->host);
185   if ((status < 1) || (status >= buffer_len - offset))
186     return (-1);
187   offset += status;
188
189   /* plugin */
190   status = ssnprintf (buffer + offset, buffer_len - offset,
191       ",%s", vl->plugin);
192   if ((status < 1) || (status >= buffer_len - offset))
193     return (-1);
194   offset += status;
195
196   /* plugin_instance */
197   if (strlen (vl->plugin_instance) > 0)
198   {
199     status = ssnprintf (buffer + offset, buffer_len - offset,
200         ",%s", vl->plugin_instance);
201     if ((status < 1) || (status >= buffer_len - offset))
202       return (-1);
203     offset += status;
204   }
205
206   /* type (if its the same as plugin, don't bother repeating it */
207   if (0 != strcmp (vl->type, vl->plugin)) 
208   {
209     status = ssnprintf (buffer + offset, buffer_len - offset,
210         ",%s", vl->type);
211     if ((status < 1) || (status >= buffer_len - offset))
212       return (-1);
213     offset += status;
214   }
215
216   /* type_instance */
217   if (strlen (vl->type_instance) > 0)
218   {
219     status = ssnprintf (buffer + offset, buffer_len - offset,
220         ",%s", vl->type_instance);
221     if ((status < 1) || (status >= buffer_len - offset))
222       return (-1);
223     offset += status;
224   }
225
226   return (0);
227 } /* int value_list_to_metric_name */
228
229 static int http_config (const char *key, const char *value)
230 {
231   if (strcasecmp ("Location", key) == 0)
232   {
233     if (location != NULL)
234       free (location);
235     location = strdup (value);
236     if (location != NULL)
237     {
238       int len = strlen (location);
239       while ((len > 0) && (location[len - 1] == '/'))
240       {
241         len--;
242         location[len] = '\0';
243       }
244       if (len <= 0)
245       {
246         free (location);
247         location = NULL;
248       }
249     }
250   }
251   else if (strcasecmp ("User", key) == 0)
252   {
253     if (user != NULL)
254       free (user);
255     user = strdup (value);
256     if (user != NULL)
257     {
258       int len = strlen (user);
259       while ((len > 0) && (user[len - 1] == '/'))
260       {
261         len--;
262         user[len] = '\0';
263       }
264       if (len <= 0)
265       {
266         free (user);
267         user = NULL;
268       }
269     }
270   }
271   else if (strcasecmp ("Password", key) == 0)
272   {
273     if (pass != NULL)
274       free (pass);
275     pass = strdup (value);
276     if (pass != NULL)
277     {
278       int len = strlen (pass);
279       while ((len > 0) && (pass[len - 1] == '/'))
280       {
281         len--;
282         pass[len] = '\0';
283       }
284       if (len <= 0)
285       {
286         free (pass);
287         pass = NULL;
288       }
289     }
290   }
291   else
292   {
293     return (-1);
294   }
295   return (0);
296 } /* int http_config */
297
298 static int http_write (const data_set_t *ds, const value_list_t *vl,
299     user_data_t __attribute__((unused)) *user_data)
300 {
301   char         metric_name[512];
302   char         values[512];
303   char         timestamp[512];
304
305   char csv_buffer[1536];
306
307   int status;
308
309   if (0 != strcmp (ds->type, vl->type)) {
310     ERROR ("http plugin: DS type does not match value list type");
311     return -1;
312   }
313
314   if (value_list_to_metric_name (metric_name, sizeof (metric_name), ds, vl) != 0)
315     return (-1);
316
317   DEBUG ("http plugin: http_write: metric_name = %s;", metric_name);
318
319   if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
320     return (-1);
321
322   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
323     return (-1);
324
325   escape_string (metric_name, sizeof (metric_name));
326
327   status = ssnprintf (csv_buffer, 1536,
328       "\"%s\",%s,%s\n",
329       metric_name, timestamp, values);
330
331   curl_easy_setopt (curl, CURLOPT_POSTFIELDS, csv_buffer);
332   status = curl_easy_perform (curl);
333   if (status != 0)
334   {
335     ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
336         status, curl_errbuf);
337     return (-1);
338   }
339
340   return (0);
341
342 } /* int http_write */
343
344 void module_register (void)
345 {
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 */