2 * collectd - src/write_redis.c
3 * Copyright (C) 2010 Florian Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian Forster <ff at octo.it>
30 #include "configfile.h"
34 #include <hiredis/hiredis.h>
38 char name[DATA_MAX_NAME_LEN];
42 struct timeval timeout;
47 typedef struct wr_node_s wr_node_t;
52 static int wr_write (const data_set_t *ds, /* {{{ */
53 const value_list_t *vl,
56 wr_node_t *node = ud->data;
66 status = FORMAT_VL (ident, sizeof (ident), vl);
69 ssnprintf (key, sizeof (key), "collectd/%s", ident);
71 memset (value, 0, sizeof (value));
72 value_size = sizeof (value);
73 value_ptr = &value[0];
75 #define APPEND(...) do { \
76 status = snprintf (value_ptr, value_size, __VA_ARGS__); \
77 if (((size_t) status) > value_size) \
79 value_ptr += value_size; \
84 value_ptr += status; \
85 value_size -= status; \
89 APPEND ("%lu", (unsigned long) vl->time);
90 for (i = 0; i < ds->ds_num; i++)
92 if (ds->ds[i].type == DS_TYPE_COUNTER)
93 APPEND ("%llu", vl->values[i].counter);
94 else if (ds->ds[i].type == DS_TYPE_GAUGE)
95 APPEND ("%g", vl->values[i].gauge);
96 else if (ds->ds[i].type == DS_TYPE_DERIVE)
97 APPEND ("%"PRIi64, vl->values[i].derive);
98 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
99 APPEND ("%"PRIu64, vl->values[i].absolute);
106 pthread_mutex_lock (&node->lock);
108 if (node->conn == NULL)
110 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
111 if (node->conn == NULL)
113 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed.",
114 (node->host != NULL) ? node->host : "localhost",
115 (node->port != 0) ? node->port : 6379);
116 pthread_mutex_unlock (&node->lock);
121 assert (node->conn != NULL);
122 rr = redisCommand (node->conn, "ZADD %b %f %b", key, sizeof (key),
123 (double) vl->time, value, sizeof (value));
125 WARNING("ZADD command error. key:%s", key);
127 rr = redisCommand (node->conn, "SADD collectd/values %b", ident, sizeof(ident));
129 WARNING("SADD command error. ident:%s", ident);
131 pthread_mutex_unlock (&node->lock);
134 } /* }}} int wr_write */
136 static void wr_config_free (void *ptr) /* {{{ */
138 wr_node_t *node = ptr;
143 if (node->conn != NULL)
145 redisFree (node->conn);
151 } /* }}} void wr_config_free */
153 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
160 node = malloc (sizeof (*node));
163 memset (node, 0, sizeof (*node));
166 node->timeout.tv_sec = 0;
167 node->timeout.tv_usec = 1000;
169 pthread_mutex_init (&node->lock, /* attr = */ NULL);
171 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
178 for (i = 0; i < ci->children_num; i++)
180 oconfig_item_t *child = ci->children + i;
182 if (strcasecmp ("Host", child->key) == 0)
183 status = cf_util_get_string (child, &node->host);
184 else if (strcasecmp ("Port", child->key) == 0)
186 status = cf_util_get_port_number (child);
193 else if (strcasecmp ("Timeout", child->key) == 0) {
194 status = cf_util_get_int (child, &timeout);
195 if (status == 0) node->timeout.tv_usec = timeout;
198 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
203 } /* for (i = 0; i < ci->children_num; i++) */
207 char cb_name[DATA_MAX_NAME_LEN];
210 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
213 ud.free_func = wr_config_free;
215 status = plugin_register_write (cb_name, wr_write, &ud);
219 wr_config_free (node);
222 } /* }}} int wr_config_node */
224 static int wr_config (oconfig_item_t *ci) /* {{{ */
228 for (i = 0; i < ci->children_num; i++)
230 oconfig_item_t *child = ci->children + i;
232 if (strcasecmp ("Node", child->key) == 0)
233 wr_config_node (child);
235 WARNING ("write_redis plugin: Ignoring unknown "
236 "configuration option \"%s\" at top level.", child->key);
240 } /* }}} int wr_config */
242 void module_register (void)
244 plugin_register_complex_config ("write_redis", wr_config);
247 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */