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