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>
32 #include <hiredis/hiredis.h>
35 #ifndef REDIS_DEFAULT_PREFIX
36 #define REDIS_DEFAULT_PREFIX "collectd/"
40 char name[DATA_MAX_NAME_LEN];
44 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, user_data_t *ud) {
61 wr_node_t *node = ud->data;
64 char value[512] = {0};
71 status = FORMAT_VL(ident, sizeof(ident), vl);
74 snprintf(key, sizeof(key), "%s%s",
75 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX, ident);
76 snprintf(time, sizeof(time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time));
78 value_size = sizeof(value);
79 value_ptr = &value[0];
80 status = format_values(value_ptr, value_size, ds, vl, node->store_rates);
84 pthread_mutex_lock(&node->lock);
86 if (node->conn == NULL) {
88 redisConnectWithTimeout((char *)node->host, node->port, node->timeout);
89 if (node->conn == NULL) {
90 ERROR("write_redis plugin: Connecting to host \"%s\" (port %i) failed: "
92 (node->host != NULL) ? node->host : "localhost",
93 (node->port != 0) ? node->port : 6379);
94 pthread_mutex_unlock(&node->lock);
96 } else if (node->conn->err) {
98 "write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
99 (node->host != NULL) ? node->host : "localhost",
100 (node->port != 0) ? node->port : 6379, node->conn->errstr);
101 pthread_mutex_unlock(&node->lock);
105 rr = redisCommand(node->conn, "SELECT %d", node->database);
107 WARNING("SELECT command error. database:%d message:%s", node->database,
113 rr = redisCommand(node->conn, "ZADD %s %s %s", key, time, value);
115 WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
119 if (node->max_set_size >= 0) {
120 rr = redisCommand(node->conn, "ZREMRANGEBYRANK %s %d %d", key, 0,
121 (-1 * node->max_set_size) - 1);
123 WARNING("ZREMRANGEBYRANK command error. key:%s message:%s", key,
129 if (node->max_set_duration > 0) {
131 * remove element, scored less than 'current-max_set_duration'
132 * '(...' indicates 'less than' in redis CLI.
134 rr = redisCommand(node->conn, "ZREMRANGEBYSCORE %s -1 (%.9f", key,
135 (CDTIME_T_TO_DOUBLE(vl->time) - node->max_set_duration));
137 WARNING("ZREMRANGEBYSCORE command error. key:%s message:%s", key,
143 /* TODO(octo): This is more overhead than necessary. Use the cache and
144 * metadata to determine if it is a new metric and call SADD only once for
147 node->conn, "SADD %svalues %s",
148 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX, ident);
150 WARNING("SADD command error. ident:%s message:%s", ident,
155 pthread_mutex_unlock(&node->lock);
158 } /* }}} int wr_write */
160 static void wr_config_free(void *ptr) /* {{{ */
162 wr_node_t *node = ptr;
167 if (node->conn != NULL) {
168 redisFree(node->conn);
174 } /* }}} void wr_config_free */
176 static int wr_config_node(oconfig_item_t *ci) /* {{{ */
182 node = calloc(1, sizeof(*node));
187 node->timeout.tv_sec = 0;
188 node->timeout.tv_usec = 1000;
192 node->max_set_size = -1;
193 node->max_set_duration = -1;
194 node->store_rates = true;
195 pthread_mutex_init(&node->lock, /* attr = */ NULL);
197 status = cf_util_get_string_buffer(ci, node->name, sizeof(node->name));
203 for (int i = 0; i < ci->children_num; i++) {
204 oconfig_item_t *child = ci->children + i;
206 if (strcasecmp("Host", child->key) == 0)
207 status = cf_util_get_string(child, &node->host);
208 else if (strcasecmp("Port", child->key) == 0) {
209 status = cf_util_get_port_number(child);
214 } else if (strcasecmp("Timeout", child->key) == 0) {
215 status = cf_util_get_int(child, &timeout);
217 node->timeout.tv_usec = timeout;
218 } else if (strcasecmp("Prefix", child->key) == 0) {
219 status = cf_util_get_string(child, &node->prefix);
220 } else if (strcasecmp("Database", child->key) == 0) {
221 status = cf_util_get_int(child, &node->database);
222 } else if (strcasecmp("MaxSetSize", child->key) == 0) {
223 status = cf_util_get_int(child, &node->max_set_size);
224 } else if (strcasecmp("MaxSetDuration", child->key) == 0) {
225 status = cf_util_get_int(child, &node->max_set_duration);
226 } else if (strcasecmp("StoreRates", child->key) == 0) {
227 status = cf_util_get_boolean(child, &node->store_rates);
229 WARNING("write_redis plugin: Ignoring unknown config option \"%s\".",
234 } /* for (i = 0; i < ci->children_num; i++) */
237 char cb_name[sizeof("write_redis/") + DATA_MAX_NAME_LEN];
239 snprintf(cb_name, sizeof(cb_name), "write_redis/%s", node->name);
242 plugin_register_write(cb_name, wr_write,
244 .data = node, .free_func = wr_config_free,
249 wr_config_free(node);
252 } /* }}} int wr_config_node */
254 static int wr_config(oconfig_item_t *ci) /* {{{ */
256 for (int i = 0; i < ci->children_num; i++) {
257 oconfig_item_t *child = ci->children + i;
259 if (strcasecmp("Node", child->key) == 0)
260 wr_config_node(child);
262 WARNING("write_redis plugin: Ignoring unknown "
263 "configuration option \"%s\" at top level.",
268 } /* }}} int wr_config */
270 void module_register(void) {
271 plugin_register_complex_config("write_redis", wr_config);