27d1842c1bfcbe5833a5d34dc7b18099236a6f98
1 /**
2 * collectd - src/write_http.c
3 * Copyright (C) 2009 Paul Sadauskas
4 * Copyright (C) 2009 Doug MacEachern
5 * Copyright (C) 2007-2009 Florian octo Forster
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at verplant.org>
22 * Doug MacEachern <dougm@hyperic.com>
23 * Paul Sadauskas <psadauskas@gmail.com>
24 **/
26 #include "collectd.h"
27 #include "plugin.h"
28 #include "common.h"
29 #include "utils_cache.h"
30 #include "utils_parse_option.h"
32 #if HAVE_PTHREAD_H
33 # include <pthread.h>
34 #endif
36 #include <curl/curl.h>
38 /*
39 * Private variables
40 */
41 static const char *config_keys[] =
42 {
43 "URL", "User", "Password"
44 };
45 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
47 static char *location = NULL;
49 char *user;
50 char *pass;
51 char *credentials;
53 CURL *curl;
54 char curl_errbuf[CURL_ERROR_SIZE];
56 #define SEND_BUFFER_SIZE 4096
57 static char send_buffer[SEND_BUFFER_SIZE];
58 static size_t send_buffer_free;
59 static size_t send_buffer_fill;
60 static time_t send_buffer_init_time;
62 static pthread_mutex_t send_lock = PTHREAD_MUTEX_INITIALIZER;
64 static void wh_init_buffer (void) /* {{{ */
65 {
66 memset (send_buffer, 0, sizeof (send_buffer));
67 send_buffer_free = sizeof (send_buffer);
68 send_buffer_fill = 0;
69 send_buffer_init_time = time (NULL);
70 } /* }}} wh_init_buffer */
72 static int wh_init(void) /* {{{ */
73 {
75 curl = curl_easy_init ();
77 if (curl == NULL)
78 {
79 ERROR ("curl plugin: curl_easy_init failed.");
80 return (-1);
81 }
83 struct curl_slist *headers=NULL;
85 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
87 headers = curl_slist_append(headers, "Accept: */*");
88 headers = curl_slist_append(headers, "Content-Type: text/plain");
89 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
91 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
92 curl_easy_setopt (curl, CURLOPT_URL, location);
94 if (user != NULL)
95 {
96 size_t credentials_size;
98 credentials_size = strlen (user) + 2;
99 if (pass != NULL)
100 credentials_size += strlen (pass);
102 credentials = (char *) malloc (credentials_size);
103 if (credentials == NULL)
104 {
105 ERROR ("curl plugin: malloc failed.");
106 return (-1);
107 }
109 ssnprintf (credentials, credentials_size, "%s:%s",
110 user, (pass == NULL) ? "" : pass);
111 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
112 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
113 }
115 wh_init_buffer ();
117 return (0);
118 } /* }}} */
120 static int wh_value_list_to_string (char *buffer, /* {{{ */
121 size_t buffer_size,
122 const data_set_t *ds, const value_list_t *vl)
123 {
124 size_t offset = 0;
125 int status;
126 int i;
128 assert (0 == strcmp (ds->type, vl->type));
130 memset (buffer, 0, buffer_size);
132 #define BUFFER_ADD(...) do { \
133 status = ssnprintf (buffer + offset, buffer_size - offset, \
134 __VA_ARGS__); \
135 if (status < 1) \
136 return (-1); \
137 else if (((size_t) status) >= (buffer_size - offset)) \
138 return (-1); \
139 else \
140 offset += ((size_t) status); \
141 } while (0)
143 BUFFER_ADD ("%lu", (unsigned long) vl->time);
145 for (i = 0; i < ds->ds_num; i++)
146 {
147 if (ds->ds[i].type == DS_TYPE_GAUGE)
148 BUFFER_ADD (":%f", vl->values[i].gauge);
149 else if (ds->ds[i].type == DS_TYPE_COUNTER)
150 BUFFER_ADD (":%llu", vl->values[i].counter);
151 else if (ds->ds[i].type == DS_TYPE_DERIVE)
152 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
153 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
154 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
155 else
156 {
157 ERROR ("write_http plugin: Unknown data source type: %i",
158 ds->ds[i].type);
159 return (-1);
160 }
161 } /* for ds->ds_num */
163 #undef BUFFER_ADD
165 return (0);
166 } /* }}} int wh_value_list_to_string */
168 static int wh_config (const char *key, const char *value) /* {{{ */
169 {
170 if (strcasecmp ("URL", key) == 0)
171 {
172 if (location != NULL)
173 free (location);
174 location = strdup (value);
175 if (location != NULL)
176 {
177 int len = strlen (location);
178 while ((len > 0) && (location[len - 1] == '/'))
179 {
180 len--;
181 location[len] = '\0';
182 }
183 if (len <= 0)
184 {
185 free (location);
186 location = NULL;
187 }
188 }
189 }
190 else if (strcasecmp ("User", key) == 0)
191 {
192 if (user != NULL)
193 free (user);
194 user = strdup (value);
195 if (user != NULL)
196 {
197 int len = strlen (user);
198 while ((len > 0) && (user[len - 1] == '/'))
199 {
200 len--;
201 user[len] = '\0';
202 }
203 if (len <= 0)
204 {
205 free (user);
206 user = NULL;
207 }
208 }
209 }
210 else if (strcasecmp ("Password", key) == 0)
211 {
212 if (pass != NULL)
213 free (pass);
214 pass = strdup (value);
215 if (pass != NULL)
216 {
217 int len = strlen (pass);
218 while ((len > 0) && (pass[len - 1] == '/'))
219 {
220 len--;
221 pass[len] = '\0';
222 }
223 if (len <= 0)
224 {
225 free (pass);
226 pass = NULL;
227 }
228 }
229 }
230 else
231 {
232 return (-1);
233 }
234 return (0);
235 } /* }}} int wh_config */
237 static int wh_send_buffer (char *buffer) /* {{{ */
238 {
239 int status = 0;
241 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, buffer);
242 status = curl_easy_perform (curl);
243 if (status != 0)
244 {
245 ERROR ("write_http plugin: curl_easy_perform failed with "
246 "staus %i: %s",
247 status, curl_errbuf);
248 }
249 return (status);
250 } /* }}} wh_send_buffer */
252 static int wh_flush_nolock (int timeout) /* {{{ */
253 {
254 int status;
256 DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
257 "send_buffer =\n %s", timeout, send_buffer);
259 if (timeout > 0)
260 {
261 time_t now;
263 now = time (NULL);
264 if ((send_buffer_init_time + timeout) > now)
265 return (0);
266 }
268 if (send_buffer_fill <= 0)
269 {
270 send_buffer_init_time = time (NULL);
271 return (0);
272 }
274 status = wh_send_buffer (send_buffer);
275 wh_init_buffer ();
277 return (status);
278 } /* }}} wh_flush_nolock */
280 static int wh_flush (int timeout, /* {{{ */
281 const char *identifier __attribute__((unused)),
282 user_data_t *user_data __attribute__((unused)))
283 {
284 int status;
286 pthread_mutex_lock (&send_lock);
287 status = wh_flush_nolock (timeout);
288 pthread_mutex_unlock (&send_lock);
290 return (status);
291 } /* }}} int wh_flush */
293 static int wh_write_command (const data_set_t *ds, const value_list_t *vl) /* {{{ */
294 {
295 char key[10*DATA_MAX_NAME_LEN];
296 char values[512];
297 char command[1024];
298 size_t command_len;
300 int status;
302 if (0 != strcmp (ds->type, vl->type)) {
303 ERROR ("write_http plugin: DS type does not match "
304 "value list type");
305 return -1;
306 }
308 /* Copy the identifier to `key' and escape it. */
309 status = FORMAT_VL (key, sizeof (key), vl);
310 if (status != 0) {
311 ERROR ("write_http plugin: error with format_name");
312 return (status);
313 }
314 escape_string (key, sizeof (key));
316 /* Convert the values to an ASCII representation and put that into
317 * `values'. */
318 status = wh_value_list_to_string (values, sizeof (values), ds, vl);
319 if (status != 0) {
320 ERROR ("write_http plugin: error with "
321 "wh_value_list_to_string");
322 return (status);
323 }
325 command_len = (size_t) ssnprintf (command, sizeof (command),
326 "PUTVAL %s interval=%i %s\n",
327 key, vl->interval, values);
328 if (command_len >= sizeof (command)) {
329 ERROR ("write_http plugin: Command buffer too small: "
330 "Need %zu bytes.", command_len + 1);
331 return (-1);
332 }
334 pthread_mutex_lock (&send_lock);
336 /* Check if we have enough space for this command. */
337 if (command_len >= send_buffer_free)
338 {
339 status = wh_flush_nolock (/* timeout = */ -1);
340 if (status != 0)
341 {
342 pthread_mutex_unlock (&send_lock);
343 return status;
344 }
345 }
346 assert (command_len < send_buffer_free);
348 /* `command_len + 1' because `command_len' does not include the
349 * trailing null byte. Neither does `send_buffer_fill'. */
350 memcpy (send_buffer + send_buffer_fill, command, command_len + 1);
351 send_buffer_fill += command_len;
352 send_buffer_free -= command_len;
354 pthread_mutex_unlock (&send_lock);
356 return (0);
357 } /* }}} int wh_write_command */
359 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
360 user_data_t __attribute__((unused)) *user_data)
361 {
362 int status;
364 status = wh_write_command (ds, vl);
366 return (status);
367 } /* }}} int wh_write */
369 static int wh_shutdown (void) /* {{{ */
370 {
371 wh_flush_nolock (/* timeout = */ -1);
372 curl_easy_cleanup(curl);
373 return (0);
374 } /* }}} int wh_shutdown */
376 void module_register (void) /* {{{ */
377 {
378 plugin_register_init("write_http", wh_init);
379 plugin_register_config ("write_http", wh_config,
380 config_keys, config_keys_num);
381 plugin_register_write ("write_http", wh_write, /* user_data = */ NULL);
382 plugin_register_flush ("write_http", wh_flush, /* user_data = */ NULL);
383 plugin_register_shutdown("write_http", wh_shutdown);
384 } /* }}} void module_register */
386 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */