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