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;
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,
62 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];
85 #define APPEND(...) do { \
86 status = snprintf (value_ptr, value_size, __VA_ARGS__); \
87 if (((size_t) status) > value_size) \
89 value_ptr += value_size; \
94 value_ptr += status; \
95 value_size -= status; \
101 for (i = 0; i < ds->ds_num; i++)
103 if (ds->ds[i].type == DS_TYPE_COUNTER)
104 APPEND ("%llu", vl->values[i].counter);
105 else if (ds->ds[i].type == DS_TYPE_GAUGE)
106 APPEND (GAUGE_FORMAT, vl->values[i].gauge);
107 else if (ds->ds[i].type == DS_TYPE_DERIVE)
108 APPEND ("%"PRIi64, vl->values[i].derive);
109 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
110 APPEND ("%"PRIu64, vl->values[i].absolute);
117 status = format_values (value_ptr, value_size, ds, vl, /* store rates = */ 0);
118 pthread_mutex_lock (&node->lock);
122 if (node->conn == NULL)
124 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
125 if (node->conn == NULL)
127 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason",
128 (node->host != NULL) ? node->host : "localhost",
129 (node->port != 0) ? node->port : 6379);
130 pthread_mutex_unlock (&node->lock);
133 else if (node->conn->err)
135 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
136 (node->host != NULL) ? node->host : "localhost",
137 (node->port != 0) ? node->port : 6379,
139 pthread_mutex_unlock (&node->lock);
143 rr = redisCommand(node->conn, "SELECT %d", node->database);
145 WARNING("SELECT command error. database:%d message:%s", node->database, node->conn->errstr);
147 freeReplyObject (rr);
150 rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
152 WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
154 freeReplyObject (rr);
156 /* TODO(octo): This is more overhead than necessary. Use the cache and
157 * metadata to determine if it is a new metric and call SADD only once for
159 rr = redisCommand (node->conn, "SADD %svalues %s",
160 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
163 WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
165 freeReplyObject (rr);
167 pthread_mutex_unlock (&node->lock);
170 } /* }}} int wr_write */
172 static void wr_config_free (void *ptr) /* {{{ */
174 wr_node_t *node = ptr;
179 if (node->conn != NULL)
181 redisFree (node->conn);
187 } /* }}} void wr_config_free */
189 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
196 node = malloc (sizeof (*node));
199 memset (node, 0, sizeof (*node));
202 node->timeout.tv_sec = 0;
203 node->timeout.tv_usec = 1000;
207 pthread_mutex_init (&node->lock, /* attr = */ NULL);
209 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
216 for (i = 0; i < ci->children_num; i++)
218 oconfig_item_t *child = ci->children + i;
220 if (strcasecmp ("Host", child->key) == 0)
221 status = cf_util_get_string (child, &node->host);
222 else if (strcasecmp ("Port", child->key) == 0)
224 status = cf_util_get_port_number (child);
231 else if (strcasecmp ("Timeout", child->key) == 0) {
232 status = cf_util_get_int (child, &timeout);
233 if (status == 0) node->timeout.tv_usec = timeout;
235 else if (strcasecmp ("Prefix", child->key) == 0) {
236 status = cf_util_get_string (child, &node->prefix);
238 else if (strcasecmp ("Database", child->key) == 0) {
239 status = cf_util_get_int (child, &node->database);
242 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
247 } /* for (i = 0; i < ci->children_num; i++) */
251 char cb_name[DATA_MAX_NAME_LEN];
254 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
257 ud.free_func = wr_config_free;
259 status = plugin_register_write (cb_name, wr_write, &ud);
263 wr_config_free (node);
266 } /* }}} int wr_config_node */
268 static int wr_config (oconfig_item_t *ci) /* {{{ */
272 for (i = 0; i < ci->children_num; i++)
274 oconfig_item_t *child = ci->children + i;
276 if (strcasecmp ("Node", child->key) == 0)
277 wr_config_node (child);
279 WARNING ("write_redis plugin: Ignoring unknown "
280 "configuration option \"%s\" at top level.", child->key);
284 } /* }}} int wr_config */
286 void module_register (void)
288 plugin_register_complex_config ("write_redis", wr_config);
291 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */