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