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;
53 typedef struct wr_node_s wr_node_t;
58 static int wr_write(const data_set_t *ds, /* {{{ */
59 const value_list_t *vl, user_data_t *ud) {
60 wr_node_t *node = ud->data;
63 char value[512] = {0};
70 status = FORMAT_VL(ident, sizeof(ident), vl);
73 ssnprintf(key, sizeof(key), "%s%s",
74 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
76 ssnprintf(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 /* TODO(octo): This is more overhead than necessary. Use the cache and
130 * metadata to determine if it is a new metric and call SADD only once for
133 node->conn, "SADD %svalues %s",
134 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX, ident);
136 WARNING("SADD command error. ident:%s message:%s", ident,
141 pthread_mutex_unlock(&node->lock);
144 } /* }}} int wr_write */
146 static void wr_config_free(void *ptr) /* {{{ */
148 wr_node_t *node = ptr;
153 if (node->conn != NULL) {
154 redisFree(node->conn);
160 } /* }}} void wr_config_free */
162 static int wr_config_node(oconfig_item_t *ci) /* {{{ */
168 node = calloc(1, sizeof(*node));
173 node->timeout.tv_sec = 0;
174 node->timeout.tv_usec = 1000;
178 node->max_set_size = -1;
179 node->store_rates = 1;
180 pthread_mutex_init(&node->lock, /* attr = */ NULL);
182 status = cf_util_get_string_buffer(ci, node->name, sizeof(node->name));
188 for (int i = 0; i < ci->children_num; i++) {
189 oconfig_item_t *child = ci->children + i;
191 if (strcasecmp("Host", child->key) == 0)
192 status = cf_util_get_string(child, &node->host);
193 else if (strcasecmp("Port", child->key) == 0) {
194 status = cf_util_get_port_number(child);
199 } else if (strcasecmp("Timeout", child->key) == 0) {
200 status = cf_util_get_int(child, &timeout);
202 node->timeout.tv_usec = timeout;
203 } else if (strcasecmp("Prefix", child->key) == 0) {
204 status = cf_util_get_string(child, &node->prefix);
205 } else if (strcasecmp("Database", child->key) == 0) {
206 status = cf_util_get_int(child, &node->database);
207 } else if (strcasecmp("MaxSetSize", child->key) == 0) {
208 status = cf_util_get_int(child, &node->max_set_size);
209 } else if (strcasecmp("StoreRates", child->key) == 0) {
210 status = cf_util_get_boolean(child, &node->store_rates);
212 WARNING("write_redis plugin: Ignoring unknown config option \"%s\".",
217 } /* for (i = 0; i < ci->children_num; i++) */
220 char cb_name[DATA_MAX_NAME_LEN];
222 ssnprintf(cb_name, sizeof(cb_name), "write_redis/%s", node->name);
224 status = plugin_register_write(
225 cb_name, wr_write, &(user_data_t){
226 .data = node, .free_func = wr_config_free,
231 wr_config_free(node);
234 } /* }}} int wr_config_node */
236 static int wr_config(oconfig_item_t *ci) /* {{{ */
238 for (int i = 0; i < ci->children_num; i++) {
239 oconfig_item_t *child = ci->children + i;
241 if (strcasecmp("Node", child->key) == 0)
242 wr_config_node(child);
244 WARNING("write_redis plugin: Ignoring unknown "
245 "configuration option \"%s\" at top level.",
250 } /* }}} int wr_config */
252 void module_register(void) {
253 plugin_register_complex_config("write_redis", wr_config);