Code

d6f7fee36c0389778089aa95e6155a1d7f568171
[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  **/
24 #include "collectd.h"
25 #include "plugin.h"
26 #include "common.h"
27 #include "utils_cache.h"
28 #include "utils_parse_option.h"
30 #if HAVE_PTHREAD_H
31 # include <pthread.h>
32 #endif
34 #include <curl/curl.h>
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);
45 static char *location   = NULL;
47 char *user;
48 char *pass;
49 char *credentials;
51 CURL *curl;
52 char curl_errbuf[CURL_ERROR_SIZE];
54 #define SEND_BUFFER_SIZE 4096
55 static char   send_buffer[SEND_BUFFER_SIZE];
56 static int    send_buffer_fill;
58 static pthread_mutex_t  send_lock = PTHREAD_MUTEX_INITIALIZER;
60 static int http_init(void) /* {{{ */
61 {
63         curl = curl_easy_init ();
65         if (curl == NULL)
66         {
67                 ERROR ("curl plugin: curl_easy_init failed.");
68                 return (-1);
69         }
71         struct curl_slist *headers=NULL;
73         curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
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);
79         curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
80         curl_easy_setopt (curl, CURLOPT_URL, location);
82         if (user != NULL)
83         {
84                 size_t credentials_size;
86                 credentials_size = strlen (user) + 2;
87                 if (pass != NULL)
88                         credentials_size += strlen (pass);
90                 credentials = (char *) malloc (credentials_size);
91                 if (credentials == NULL)
92                 {
93                         ERROR ("curl plugin: malloc failed.");
94                         return (-1);
95                 }
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         }
103         return (0);
104 } /* }}} */
106 static int http_value_list_to_string (char *buffer, int buffer_len, /* {{{ */
107                 const data_set_t *ds, const value_list_t *vl)
109         int offset = 0;
110         int status;
111         int i;
112         gauge_t *rates = NULL;
114         assert (0 == strcmp (ds->type, vl->type));
116         memset (buffer, '\0', buffer_len);
118         for (i = 0; i < ds->ds_num; i++)
119         {
120                 if ((ds->ds[i].type != DS_TYPE_COUNTER)
121                                 && (ds->ds[i].type != DS_TYPE_GAUGE)
122                                 && (ds->ds[i].type != DS_TYPE_DERIVE)
123                                 && (ds->ds[i].type != DS_TYPE_ABSOLUTE))
124                         return (-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                 {
133                         if (rates == NULL)
134                                 rates = uc_get_rate (ds, vl);
135                         if (rates == NULL)
136                         {
137                                 WARNING ("csv plugin: "
138                                                 "uc_get_rate failed.");
139                                 return (-1);
140                         }
141                         status = ssnprintf (buffer + offset,
142                                         buffer_len - offset,
143                                         ":%lf", rates[i]);
144                 }
145                 else if (ds->ds[i].type == DS_TYPE_DERIVE)
146                 {
147                         status = ssnprintf (buffer + offset,
148                                         buffer_len - offset,
149                                         ":%"PRIi64,
150                                         vl->values[i].derive);
151                 }
152                 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
153                 {
154                         status = ssnprintf (buffer + offset,
155                                         buffer_len - offset,
156                                         ":%"PRIu64,
157                                         vl->values[i].absolute);
158                 }
160                 if ((status < 1) || (status >= (buffer_len - offset)))
161                 {
162                         sfree (rates);
163                         return (-1);
164                 }
166                 offset += status;
167         } /* for ds->ds_num */
169         sfree (rates);
170         return (0);
171 } /* }}} int http_value_list_to_string */
173 static int http_config (const char *key, const char *value) /* {{{ */
175         if (strcasecmp ("URL", key) == 0)
176         {
177                 if (location != NULL)
178                         free (location);
179                 location = strdup (value);
180                 if (location != NULL)
181                 {
182                         int len = strlen (location);
183                         while ((len > 0) && (location[len - 1] == '/'))
184                         {
185                                 len--;
186                                 location[len] = '\0';
187                         }
188                         if (len <= 0)
189                         {
190                                 free (location);
191                                 location = NULL;
192                         }
193                 }
194         }
195         else if (strcasecmp ("User", key) == 0)
196         {
197                 if (user != NULL)
198                         free (user);
199                 user = strdup (value);
200                 if (user != NULL)
201                 {
202                         int len = strlen (user);
203                         while ((len > 0) && (user[len - 1] == '/'))
204                         {
205                                 len--;
206                                 user[len] = '\0';
207                         }
208                         if (len <= 0)
209                         {
210                                 free (user);
211                                 user = NULL;
212                         }
213                 }
214         }
215         else if (strcasecmp ("Password", key) == 0)
216         {
217                 if (pass != NULL)
218                         free (pass);
219                 pass = strdup (value);
220                 if (pass != NULL)
221                 {
222                         int len = strlen (pass);
223                         while ((len > 0) && (pass[len - 1] == '/'))
224                         {
225                                 len--;
226                                 pass[len] = '\0';
227                         }
228                         if (len <= 0)
229                         {
230                                 free (pass);
231                                 pass = NULL;
232                         }
233                 }
234         }
235         else
236         {
237                 return (-1);
238         }
239         return (0);
240 } /* }}} int http_config */
242 static void http_init_buffer (void)  /* {{{ */
244         memset (send_buffer, 0, sizeof (send_buffer));
245         send_buffer_fill = 0;
246 } /* }}} http_init_buffer */
248 static int http_send_buffer (char *buffer) /* {{{ */
250         int status = 0;
251         curl_easy_setopt (curl, CURLOPT_POSTFIELDS, buffer);
252         //status = curl_easy_perform (curl);
253         if (status != 0)
254         {
255                 ERROR ("http plugin: curl_easy_perform failed with staus %i: %s",
256                                 status, curl_errbuf);
257         }
258         return (status);
259 } /* }}} http_send_buffer */
261 static int http_flush_buffer (void) /* {{{ */
263         int status = 0;
264         DEBUG ("http plugin: flushing buffer:\n%s", send_buffer);
266         status = http_send_buffer (send_buffer);
267         http_init_buffer ();
269         return (status);
270 } /* }}} http_flush_buffer */
272 static int http_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
273                 user_data_t __attribute__((unused)) *user_data)
276         char key[1024];
277         char values[512];
279         int status;
281         if (0 != strcmp (ds->type, vl->type)) {
282                 ERROR ("http plugin: DS type does not match value list type");
283                 return -1;
284         }
286         status = format_name( key, sizeof(key), vl->host, vl->plugin,
287                         vl->plugin_instance, vl->type, vl->type_instance );
289         if (status != 0) {
290                 ERROR ("http plugin: error with format_name");
291                 return (status);
292         }
294         status = http_value_list_to_string (values, sizeof (values), ds, vl);
296         if (status != 0) {
297                 ERROR ("http plugin: error with http_value_list_to_string");
298                 return (status);
299         }
302         pthread_mutex_lock (&send_lock);
304         status = ssnprintf (send_buffer + send_buffer_fill, sizeof (send_buffer) - send_buffer_fill,
305                         "PUTVAL %s interval=%i %u%s\n",
306                         key, interval_g, vl->time, values);
307         send_buffer_fill += status;
309         if ((sizeof (send_buffer) - send_buffer_fill) < (sizeof(key) + sizeof(values)))
310         {
311                 status = http_flush_buffer();
312                 if (status != 0)
313                         return status;
315         }
317         pthread_mutex_unlock (&send_lock);
320         return (0);
322 } /* }}} int http_write */
324 static int http_shutdown (void) /* {{{ */
326         http_flush_buffer();
327         curl_easy_cleanup(curl);
328         return (0);
331 void module_register (void) /* {{{ */
333         plugin_register_init("http", http_init);
334         plugin_register_config ("http", http_config,
335                         config_keys, config_keys_num);
336         plugin_register_write ("http", http_write, /* user_data = */ NULL);
337         plugin_register_shutdown("http", http_shutdown);
338 } /* }}} void module_register */
340 /* vim: set fdm=marker sw=8 ts=8 tw=78 : */