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"
34 #include <hiredis/hiredis.h>
36 #ifndef REDIS_DEFAULT_PREFIX
37 # define REDIS_DEFAULT_PREFIX "collectd/"
42 char name[DATA_MAX_NAME_LEN];
46 struct timeval timeout;
52 typedef struct wr_node_s wr_node_t;
57 static int wr_write (const data_set_t *ds, /* {{{ */
58 const value_list_t *vl,
61 wr_node_t *node = ud->data;
72 status = FORMAT_VL (ident, sizeof (ident), vl);
75 ssnprintf (key, sizeof (key), "%s%s",
76 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
78 ssnprintf (time, sizeof (time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time));
80 memset (value, 0, sizeof (value));
81 value_size = sizeof (value);
82 value_ptr = &value[0];
84 #define APPEND(...) do { \
85 status = snprintf (value_ptr, value_size, __VA_ARGS__); \
86 if (((size_t) status) > value_size) \
88 value_ptr += value_size; \
93 value_ptr += status; \
94 value_size -= status; \
100 for (i = 0; i < ds->ds_num; i++)
102 if (ds->ds[i].type == DS_TYPE_COUNTER)
103 APPEND ("%llu", vl->values[i].counter);
104 else if (ds->ds[i].type == DS_TYPE_GAUGE)
105 APPEND (GAUGE_FORMAT, vl->values[i].gauge);
106 else if (ds->ds[i].type == DS_TYPE_DERIVE)
107 APPEND ("%"PRIi64, vl->values[i].derive);
108 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
109 APPEND ("%"PRIu64, vl->values[i].absolute);
116 status = format_values (value_ptr, value_size, ds, vl, /* store rates = */ 0);
117 pthread_mutex_lock (&node->lock);
121 if (node->conn == NULL)
123 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
124 if (node->conn == NULL)
126 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason",
127 (node->host != NULL) ? node->host : "localhost",
128 (node->port != 0) ? node->port : 6379);
129 pthread_mutex_unlock (&node->lock);
132 else if (node->conn->err)
134 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
135 (node->host != NULL) ? node->host : "localhost",
136 (node->port != 0) ? node->port : 6379,
138 pthread_mutex_unlock (&node->lock);
143 rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
145 WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
147 freeReplyObject (rr);
149 /* TODO(octo): This is more overhead than necessary. Use the cache and
150 * metadata to determine if it is a new metric and call SADD only once for
152 rr = redisCommand (node->conn, "SADD %svalues %s",
153 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
156 WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
158 freeReplyObject (rr);
160 pthread_mutex_unlock (&node->lock);
163 } /* }}} int wr_write */
165 static void wr_config_free (void *ptr) /* {{{ */
167 wr_node_t *node = ptr;
172 if (node->conn != NULL)
174 redisFree (node->conn);
180 } /* }}} void wr_config_free */
182 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
189 node = malloc (sizeof (*node));
192 memset (node, 0, sizeof (*node));
195 node->timeout.tv_sec = 0;
196 node->timeout.tv_usec = 1000;
199 pthread_mutex_init (&node->lock, /* attr = */ NULL);
201 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
208 for (i = 0; i < ci->children_num; i++)
210 oconfig_item_t *child = ci->children + i;
212 if (strcasecmp ("Host", child->key) == 0)
213 status = cf_util_get_string (child, &node->host);
214 else if (strcasecmp ("Port", child->key) == 0)
216 status = cf_util_get_port_number (child);
223 else if (strcasecmp ("Timeout", child->key) == 0) {
224 status = cf_util_get_int (child, &timeout);
225 if (status == 0) node->timeout.tv_usec = timeout;
227 else if (strcasecmp ("Prefix", child->key) == 0) {
228 status = cf_util_get_string (child, &node->prefix);
231 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
236 } /* for (i = 0; i < ci->children_num; i++) */
240 char cb_name[DATA_MAX_NAME_LEN];
243 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
246 ud.free_func = wr_config_free;
248 status = plugin_register_write (cb_name, wr_write, &ud);
252 wr_config_free (node);
255 } /* }}} int wr_config_node */
257 static int wr_config (oconfig_item_t *ci) /* {{{ */
261 for (i = 0; i < ci->children_num; i++)
263 oconfig_item_t *child = ci->children + i;
265 if (strcasecmp ("Node", child->key) == 0)
266 wr_config_node (child);
268 WARNING ("write_redis plugin: Ignoring unknown "
269 "configuration option \"%s\" at top level.", child->key);
273 } /* }}} int wr_config */
275 void module_register (void)
277 plugin_register_complex_config ("write_redis", wr_config);
280 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */