2 * collectd - src/http.c
3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2009 Doug MacEachern
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
21 * Doug MacEachern <dougm@hyperic.com>
27 #include "utils_cache.h"
28 #include "utils_parse_option.h"
33 static const char *config_keys[] =
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
39 static char *location = NULL;
41 static int value_list_to_string (char *buffer, int buffer_len,
42 const data_set_t *ds, const value_list_t *vl)
47 gauge_t *rates = NULL;
49 assert (0 == strcmp (ds->type, vl->type));
51 memset (buffer, '\0', buffer_len);
53 for (i = 0; i < ds->ds_num; i++)
57 status = ssnprintf (buffer + offset,
62 if ((ds->ds[i].type != DS_TYPE_COUNTER)
63 && (ds->ds[i].type != DS_TYPE_GAUGE))
66 if (ds->ds[i].type == DS_TYPE_COUNTER)
69 rates = uc_get_rate (ds, vl);
72 WARNING ("http plugin: "
73 "uc_get_rate failed.");
81 status = ssnprintf (buffer + offset,
85 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
87 status = ssnprintf (buffer + offset, buffer_len - offset,
88 "%lf", vl->values[i].gauge);
91 if ((status < 1) || (status >= (buffer_len - offset)))
98 } /* for ds->ds_num */
102 } /* int value_list_to_string */
104 static int value_list_to_timestamp (char *buffer, int buffer_len,
105 const data_set_t *ds, const value_list_t *vl)
110 assert (0 == strcmp (ds->type, vl->type));
112 memset (buffer, '\0', buffer_len);
114 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
115 if ((status < 1) || (status >= buffer_len))
120 } /* int value_list_to_timestamp */
122 static int value_list_to_metric_name (char *buffer, int buffer_len,
123 const data_set_t *ds, const value_list_t *vl)
128 assert (0 == strcmp (ds->type, vl->type));
131 status = ssnprintf (buffer + offset, buffer_len - offset,
133 if ((status < 1) || (status >= buffer_len - offset))
138 status = ssnprintf (buffer + offset, buffer_len - offset,
140 if ((status < 1) || (status >= buffer_len - offset))
144 /* plugin_instance */
145 if (strlen (vl->plugin_instance) > 0)
147 status = ssnprintf (buffer + offset, buffer_len - offset,
148 ",%s", vl->plugin_instance);
149 if ((status < 1) || (status >= buffer_len - offset))
154 /* type (if its the same as plugin, don't bother repeating it */
155 if (0 != strcmp (vl->type, vl->plugin))
157 status = ssnprintf (buffer + offset, buffer_len - offset,
159 if ((status < 1) || (status >= buffer_len - offset))
165 if (strlen (vl->type_instance) > 0)
167 status = ssnprintf (buffer + offset, buffer_len - offset,
168 ",%s", vl->type_instance);
169 if ((status < 1) || (status >= buffer_len - offset))
175 } /* int value_list_to_metric_name */
177 static int http_config (const char *key, const char *value)
179 if (strcasecmp ("Location", key) == 0)
181 if (location != NULL)
183 location = strdup (value);
184 if (location != NULL)
186 int len = strlen (location);
187 while ((len > 0) && (location[len - 1] == '/'))
190 location[len] = '\0';
204 } /* int http_config */
206 static int http_write (const data_set_t *ds, const value_list_t *vl,
207 user_data_t __attribute__((unused)) *user_data)
209 char metric_name[512];
213 if (0 != strcmp (ds->type, vl->type)) {
214 ERROR ("http plugin: DS type does not match value list type");
218 if (value_list_to_metric_name (metric_name, sizeof (metric_name), ds, vl) != 0)
221 DEBUG ("http plugin: http_write: metric_name = %s;", metric_name);
223 if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
226 if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
229 escape_string (metric_name, sizeof (metric_name));
233 metric_name, timestamp, values);
236 } /* int http_write */
238 void module_register (void)
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 */