2 * collectd - src/write_redis.c
3 * Copyright (C) 2010-2015 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"
33 #include <hiredis/hiredis.h>
35 #ifndef REDIS_DEFAULT_PREFIX
36 # define REDIS_DEFAULT_PREFIX "collectd/"
41 char name[DATA_MAX_NAME_LEN];
45 struct timeval timeout;
54 typedef struct wr_node_s wr_node_t;
59 static int wr_write (const data_set_t *ds, /* {{{ */
60 const value_list_t *vl,
63 wr_node_t *node = ud->data;
73 status = FORMAT_VL (ident, sizeof (ident), vl);
76 ssnprintf (key, sizeof (key), "%s%s",
77 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
79 ssnprintf (time, sizeof (time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time));
81 memset (value, 0, sizeof (value));
82 value_size = sizeof (value);
83 value_ptr = &value[0];
84 status = format_values (value_ptr, value_size, ds, vl, node->store_rates);
88 pthread_mutex_lock (&node->lock);
90 if (node->conn == NULL)
92 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
93 if (node->conn == NULL)
95 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason",
96 (node->host != NULL) ? node->host : "localhost",
97 (node->port != 0) ? node->port : 6379);
98 pthread_mutex_unlock (&node->lock);
101 else if (node->conn->err)
103 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
104 (node->host != NULL) ? node->host : "localhost",
105 (node->port != 0) ? node->port : 6379,
107 pthread_mutex_unlock (&node->lock);
111 rr = redisCommand(node->conn, "SELECT %d", node->database);
113 WARNING("SELECT command error. database:%d message:%s", node->database, node->conn->errstr);
115 freeReplyObject (rr);
118 rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
120 WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
122 freeReplyObject (rr);
124 if (node->max_set_size >= 0)
126 rr = redisCommand (node->conn, "ZREMRANGEBYRANK %s %d %d", key, 0, (-1 * node->max_set_size) - 1);
128 WARNING("ZREMRANGEBYRANK command error. key:%s message:%s", key, node->conn->errstr);
130 freeReplyObject (rr);
133 /* TODO(octo): This is more overhead than necessary. Use the cache and
134 * metadata to determine if it is a new metric and call SADD only once for
136 rr = redisCommand (node->conn, "SADD %svalues %s",
137 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
140 WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
142 freeReplyObject (rr);
144 pthread_mutex_unlock (&node->lock);
147 } /* }}} int wr_write */
149 static void wr_config_free (void *ptr) /* {{{ */
151 wr_node_t *node = ptr;
156 if (node->conn != NULL)
158 redisFree (node->conn);
164 } /* }}} void wr_config_free */
166 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
173 node = calloc (1, sizeof (*node));
178 node->timeout.tv_sec = 0;
179 node->timeout.tv_usec = 1000;
183 node->max_set_size = -1;
184 node->store_rates = 1;
185 pthread_mutex_init (&node->lock, /* attr = */ NULL);
187 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
194 for (i = 0; i < ci->children_num; i++)
196 oconfig_item_t *child = ci->children + i;
198 if (strcasecmp ("Host", child->key) == 0)
199 status = cf_util_get_string (child, &node->host);
200 else if (strcasecmp ("Port", child->key) == 0)
202 status = cf_util_get_port_number (child);
209 else if (strcasecmp ("Timeout", child->key) == 0) {
210 status = cf_util_get_int (child, &timeout);
211 if (status == 0) node->timeout.tv_usec = timeout;
213 else if (strcasecmp ("Prefix", child->key) == 0) {
214 status = cf_util_get_string (child, &node->prefix);
216 else if (strcasecmp ("Database", child->key) == 0) {
217 status = cf_util_get_int (child, &node->database);
219 else if (strcasecmp ("MaxSetSize", child->key) == 0) {
220 status = cf_util_get_int (child, &node->max_set_size);
222 else if (strcasecmp ("StoreRates", child->key) == 0) {
223 status = cf_util_get_boolean (child, &node->store_rates);
226 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
231 } /* for (i = 0; i < ci->children_num; i++) */
235 char cb_name[DATA_MAX_NAME_LEN];
238 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
241 ud.free_func = wr_config_free;
243 status = plugin_register_write (cb_name, wr_write, &ud);
247 wr_config_free (node);
250 } /* }}} int wr_config_node */
252 static int wr_config (oconfig_item_t *ci) /* {{{ */
256 for (i = 0; i < ci->children_num; i++)
258 oconfig_item_t *child = ci->children + i;
260 if (strcasecmp ("Node", child->key) == 0)
261 wr_config_node (child);
263 WARNING ("write_redis plugin: Ignoring unknown "
264 "configuration option \"%s\" at top level.", child->key);
268 } /* }}} int wr_config */
270 void module_register (void)
272 plugin_register_complex_config ("write_redis", wr_config);
275 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */