http plugin: Start in http plugin that can output csv to stdout
[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 /*
31  * Private variables
32  */
33 static const char *config_keys[] =
34 {
35   "Location",
36 };
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
38
39 static char *location   = NULL;
40
41 static int value_list_to_string (char *buffer, int buffer_len,
42     const data_set_t *ds, const value_list_t *vl)
43 {
44   int offset = 0;
45   int status;
46   int i;
47   gauge_t *rates = NULL;
48
49   assert (0 == strcmp (ds->type, vl->type));
50
51   memset (buffer, '\0', buffer_len);
52
53   for (i = 0; i < ds->ds_num; i++)
54   {
55     if (i > 0) 
56     {
57       status = ssnprintf (buffer + offset,
58           buffer_len - offset,
59           ":");
60       offset += status;
61     }
62     if ((ds->ds[i].type != DS_TYPE_COUNTER)
63         && (ds->ds[i].type != DS_TYPE_GAUGE))
64       return (-1);
65
66     if (ds->ds[i].type == DS_TYPE_COUNTER)
67     {
68       if (rates == NULL)
69         rates = uc_get_rate (ds, vl);
70       if (rates == NULL)
71       {
72         WARNING ("http plugin: "
73             "uc_get_rate failed.");
74         return (-1);
75       }
76       if (isnan(rates[i]))
77       {
78         /* dont output */
79         return (-1);
80       }
81       status = ssnprintf (buffer + offset,
82           buffer_len - offset,
83           "%lf", rates[i]);
84     }
85     else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
86     {
87       status = ssnprintf (buffer + offset, buffer_len - offset,
88           "%lf", vl->values[i].gauge);
89     }
90
91     if ((status < 1) || (status >= (buffer_len - offset)))
92     {
93       sfree (rates);
94       return (-1);
95     }
96
97     offset += status;
98   } /* for ds->ds_num */
99
100   sfree (rates);
101   return (0);
102 } /* int value_list_to_string */
103
104 static int value_list_to_timestamp (char *buffer, int buffer_len,
105     const data_set_t *ds, const value_list_t *vl)
106 {
107   int offset = 0;
108   int status;
109
110   assert (0 == strcmp (ds->type, vl->type));
111
112   memset (buffer, '\0', buffer_len);
113
114   status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
115   if ((status < 1) || (status >= buffer_len))
116     return (-1);
117   offset = status;
118
119   return (0);
120 } /* int value_list_to_timestamp */
121
122 static int value_list_to_metric_name (char *buffer, int buffer_len,
123     const data_set_t *ds, const value_list_t *vl)
124 {
125   int offset = 0;
126   int status;
127
128   assert (0 == strcmp (ds->type, vl->type));
129
130   /* hostname */
131   status = ssnprintf (buffer + offset, buffer_len - offset,
132       "%s", vl->host);
133   if ((status < 1) || (status >= buffer_len - offset))
134     return (-1);
135   offset += status;
136
137   /* plugin */
138   status = ssnprintf (buffer + offset, buffer_len - offset,
139       ",%s", vl->plugin);
140   if ((status < 1) || (status >= buffer_len - offset))
141     return (-1);
142   offset += status;
143
144   /* plugin_instance */
145   if (strlen (vl->plugin_instance) > 0)
146   {
147     status = ssnprintf (buffer + offset, buffer_len - offset,
148         ",%s", vl->plugin_instance);
149     if ((status < 1) || (status >= buffer_len - offset))
150       return (-1);
151     offset += status;
152   }
153
154   /* type (if its the same as plugin, don't bother repeating it */
155   if (0 != strcmp (vl->type, vl->plugin)) 
156   {
157     status = ssnprintf (buffer + offset, buffer_len - offset,
158         ",%s", vl->type);
159     if ((status < 1) || (status >= buffer_len - offset))
160       return (-1);
161     offset += status;
162   }
163
164   /* type_instance */
165   if (strlen (vl->type_instance) > 0)
166   {
167     status = ssnprintf (buffer + offset, buffer_len - offset,
168         ",%s", vl->type_instance);
169     if ((status < 1) || (status >= buffer_len - offset))
170       return (-1);
171     offset += status;
172   }
173
174   return (0);
175 } /* int value_list_to_metric_name */
176
177 static int http_config (const char *key, const char *value)
178 {
179   if (strcasecmp ("Location", key) == 0)
180   {
181     if (location != NULL)
182       free (location);
183     location = strdup (value);
184     if (location != NULL)
185     {
186       int len = strlen (location);
187       while ((len > 0) && (location[len - 1] == '/'))
188       {
189         len--;
190         location[len] = '\0';
191       }
192       if (len <= 0)
193       {
194         free (location);
195         location = NULL;
196       }
197     }
198   }
199   else
200   {
201     return (-1);
202   }
203   return (0);
204 } /* int http_config */
205
206 static int http_write (const data_set_t *ds, const value_list_t *vl,
207     user_data_t __attribute__((unused)) *user_data)
208 {
209   char         metric_name[512];
210   char         values[512];
211   char         timestamp[512];
212
213   if (0 != strcmp (ds->type, vl->type)) {
214     ERROR ("http plugin: DS type does not match value list type");
215     return -1;
216   }
217
218   if (value_list_to_metric_name (metric_name, sizeof (metric_name), ds, vl) != 0)
219     return (-1);
220
221   DEBUG ("http plugin: http_write: metric_name = %s;", metric_name);
222
223   if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
224     return (-1);
225
226   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
227     return (-1);
228
229   escape_string (metric_name, sizeof (metric_name));
230
231   fprintf (stdout,
232       "\"%s\",%s,%s\n",
233       metric_name, timestamp, values);
234   return (0);
235
236 } /* int http_write */
237
238 void module_register (void)
239 {
240   plugin_register_config ("http", http_config,
241       config_keys, config_keys_num);
242   plugin_register_write ("http", http_write, /* user_data = */ NULL);
243 } /* void module_register */