http plugin: Use the `FORMAT_VL' macro.
[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 #if HAVE_PTHREAD_H
31 # include <pthread.h>
32 #endif
33
34 #include <curl/curl.h>
35
36 /*
37  * Private variables
38  */
39 static const char *config_keys[] =
40 {
41         "URL", "User", "Password"
42 };
43 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
44
45 static char *location   = NULL;
46
47 char *user;
48 char *pass;
49 char *credentials;
50
51 CURL *curl;
52 char curl_errbuf[CURL_ERROR_SIZE];
53
54 #define SEND_BUFFER_SIZE 4096
55 static char   send_buffer[SEND_BUFFER_SIZE];
56 static int    send_buffer_fill;
57
58 static pthread_mutex_t  send_lock = PTHREAD_MUTEX_INITIALIZER;
59
60 static int http_init(void) /* {{{ */
61 {
62
63         curl = curl_easy_init ();
64
65         if (curl == NULL)
66         {
67                 ERROR ("curl plugin: curl_easy_init failed.");
68                 return (-1);
69         }
70
71         struct curl_slist *headers=NULL;
72
73         curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
74
75         headers = curl_slist_append(headers, "Accept:  */*");
76         headers = curl_slist_append(headers, "Content-Type: text/plain");
77         curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
78
79         curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
80         curl_easy_setopt (curl, CURLOPT_URL, location);
81
82         if (user != NULL)
83         {
84                 size_t credentials_size;
85
86                 credentials_size = strlen (user) + 2;
87                 if (pass != NULL)
88                         credentials_size += strlen (pass);
89
90                 credentials = (char *) malloc (credentials_size);
91                 if (credentials == NULL)
92                 {
93                         ERROR ("curl plugin: malloc failed.");
94                         return (-1);
95                 }
96
97                 ssnprintf (credentials, credentials_size, "%s:%s",
98                                 user, (pass == NULL) ? "" : pass);
99                 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
100                 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
101         }
102
103         return (0);
104 } /* }}} */
105
106 static int http_value_list_to_string (char *buffer, int buffer_len, /* {{{ */
107                 const data_set_t *ds, const value_list_t *vl)
108 {
109         int offset = 0;
110         int status;
111         int i;
112
113         assert (0 == strcmp (ds->type, vl->type));
114
115         memset (buffer, '\0', buffer_len);
116
117         for (i = 0; i < ds->ds_num; i++)
118         {
119                 if ((ds->ds[i].type != DS_TYPE_COUNTER)
120                                 && (ds->ds[i].type != DS_TYPE_GAUGE)
121                                 && (ds->ds[i].type != DS_TYPE_DERIVE)
122                                 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
123                         return (-1);
124
125                 status = -1;
126                 if (ds->ds[i].type == DS_TYPE_GAUGE)
127                 {
128                         status = ssnprintf (buffer + offset, buffer_len - offset,
129                                         ":%lf", vl->values[i].gauge);
130                 }
131                 else if ((ds->ds[i].type == DS_TYPE_COUNTER)
132                                 || (ds->ds[i].type == DS_TYPE_ABSOLUTE))
133                 {
134                         status = ssnprintf (buffer + offset,
135                                         buffer_len - offset,
136                                         ":%"PRIu64,
137                                         (ds->ds[i].type == DS_TYPE_COUNTER)
138                                         ? vl->values[i].counter
139                                         : vl->values[i].absolute);
140                 }
141                 else if (ds->ds[i].type == DS_TYPE_DERIVE)
142                 {
143                         status = ssnprintf (buffer + offset,
144                                         buffer_len - offset,
145                                         ":%"PRIi64,
146                                         vl->values[i].derive);
147                 }
148
149                 if ((status < 1) || (status >= (buffer_len - offset)))
150                         return (-1);
151
152                 offset += status;
153         } /* for ds->ds_num */
154
155         return (0);
156 } /* }}} int http_value_list_to_string */
157
158 static int http_config (const char *key, const char *value) /* {{{ */
159 {
160         if (strcasecmp ("URL", key) == 0)
161         {
162                 if (location != NULL)
163                         free (location);
164                 location = strdup (value);
165                 if (location != NULL)
166                 {
167                         int len = strlen (location);
168                         while ((len > 0) && (location[len - 1] == '/'))
169                         {
170                                 len--;
171                                 location[len] = '\0';
172                         }
173                         if (len <= 0)
174                         {
175                                 free (location);
176                                 location = NULL;
177                         }
178                 }
179         }
180         else if (strcasecmp ("User", key) == 0)
181         {
182                 if (user != NULL)
183                         free (user);
184                 user = strdup (value);
185                 if (user != NULL)
186                 {
187                         int len = strlen (user);
188                         while ((len > 0) && (user[len - 1] == '/'))
189                         {
190                                 len--;
191                                 user[len] = '\0';
192                         }
193                         if (len <= 0)
194                         {
195                                 free (user);
196                                 user = NULL;
197                         }
198                 }
199         }
200         else if (strcasecmp ("Password", key) == 0)
201         {
202                 if (pass != NULL)
203                         free (pass);
204                 pass = strdup (value);
205                 if (pass != NULL)
206                 {
207                         int len = strlen (pass);
208                         while ((len > 0) && (pass[len - 1] == '/'))
209                         {
210                                 len--;
211                                 pass[len] = '\0';
212                         }
213                         if (len <= 0)
214                         {
215                                 free (pass);
216                                 pass = NULL;
217                         }
218                 }
219         }
220         else
221         {
222                 return (-1);
223         }
224         return (0);
225 } /* }}} int http_config */
226
227 static void http_init_buffer (void)  /* {{{ */
228 {
229         memset (send_buffer, 0, sizeof (send_buffer));
230         send_buffer_fill = 0;
231 } /* }}} http_init_buffer */
232
233 static int http_send_buffer (char *buffer) /* {{{ */
234 {
235         int status = 0;
236         curl_easy_setopt (curl, CURLOPT_POSTFIELDS, buffer);
237         //status = curl_easy_perform (curl);
238         if (status != 0)
239         {
240                 ERROR ("http plugin: curl_easy_perform failed with staus %i: %s",
241                                 status, curl_errbuf);
242         }
243         return (status);
244 } /* }}} http_send_buffer */
245
246 static int http_flush_buffer (void) /* {{{ */
247 {
248         int status = 0;
249         DEBUG ("http plugin: flushing buffer:\n%s", send_buffer);
250
251         status = http_send_buffer (send_buffer);
252         http_init_buffer ();
253
254         return (status);
255 } /* }}} http_flush_buffer */
256
257 static int http_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
258                 user_data_t __attribute__((unused)) *user_data)
259 {
260         char key[1024];
261         char values[512];
262
263         int status;
264
265         if (0 != strcmp (ds->type, vl->type)) {
266                 ERROR ("http plugin: DS type does not match value list type");
267                 return -1;
268         }
269
270         status = FORMAT_VL (key, sizeof (key), vl);
271         if (status != 0) {
272                 ERROR ("http plugin: error with format_name");
273                 return (status);
274         }
275
276         status = http_value_list_to_string (values, sizeof (values), ds, vl);
277         if (status != 0) {
278                 ERROR ("http plugin: error with http_value_list_to_string");
279                 return (status);
280         }
281
282
283         pthread_mutex_lock (&send_lock);
284
285         /* `values' has a leading `:'. */
286         status = ssnprintf (send_buffer + send_buffer_fill,
287                         sizeof (send_buffer) - send_buffer_fill,
288                         "PUTVAL %s interval=%i %lu%s\n",
289                         key, interval_g, (unsigned long) vl->time, values);
290         send_buffer_fill += status;
291
292         if ((sizeof (send_buffer) - send_buffer_fill) < (sizeof(key) + sizeof(values)))
293         {
294                 status = http_flush_buffer();
295                 if (status != 0)
296                         return status;
297
298         }
299
300         pthread_mutex_unlock (&send_lock);
301
302
303         return (0);
304
305 } /* }}} int http_write */
306
307 static int http_shutdown (void) /* {{{ */
308 {
309         http_flush_buffer();
310         curl_easy_cleanup(curl);
311         return (0);
312 }
313
314 void module_register (void) /* {{{ */
315 {
316         plugin_register_init("http", http_init);
317         plugin_register_config ("http", http_config,
318                         config_keys, config_keys_num);
319         plugin_register_write ("http", http_write, /* user_data = */ NULL);
320         plugin_register_shutdown("http", http_shutdown);
321 } /* }}} void module_register */
322
323 /* vim: set fdm=marker sw=8 ts=8 tw=78 : */