1 /**
2 * collectd - src/write_redis.c
3 * Copyright (C) 2010-2015 Florian Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian Forster <ff at octo.it>
25 **/
27 #include "collectd.h"
29 #include "plugin.h"
30 #include "common.h"
32 #include <sys/time.h>
33 #include <hiredis/hiredis.h>
35 #ifndef REDIS_DEFAULT_PREFIX
36 # define REDIS_DEFAULT_PREFIX "collectd/"
37 #endif
39 struct wr_node_s
40 {
41 char name[DATA_MAX_NAME_LEN];
43 char *host;
44 int port;
45 struct timeval timeout;
46 char *prefix;
47 int database;
48 int max_set_size;
49 _Bool store_rates;
51 redisContext *conn;
52 pthread_mutex_t lock;
53 };
54 typedef struct wr_node_s wr_node_t;
56 /*
57 * Functions
58 */
59 static int wr_write (const data_set_t *ds, /* {{{ */
60 const value_list_t *vl,
61 user_data_t *ud)
62 {
63 wr_node_t *node = ud->data;
64 char ident[512];
65 char key[512];
66 char value[512] = { 0 };
67 char time[24];
68 size_t value_size;
69 char *value_ptr;
70 int status;
71 redisReply *rr;
73 status = FORMAT_VL (ident, sizeof (ident), vl);
74 if (status != 0)
75 return (status);
76 ssnprintf (key, sizeof (key), "%s%s",
77 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
78 ident);
79 ssnprintf (time, sizeof (time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time));
81 value_size = sizeof (value);
82 value_ptr = &value[0];
83 status = format_values (value_ptr, value_size, ds, vl, node->store_rates);
84 if (status != 0)
85 return (status);
87 pthread_mutex_lock (&node->lock);
89 if (node->conn == NULL)
90 {
91 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
92 if (node->conn == NULL)
93 {
94 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason",
95 (node->host != NULL) ? node->host : "localhost",
96 (node->port != 0) ? node->port : 6379);
97 pthread_mutex_unlock (&node->lock);
98 return (-1);
99 }
100 else if (node->conn->err)
101 {
102 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
103 (node->host != NULL) ? node->host : "localhost",
104 (node->port != 0) ? node->port : 6379,
105 node->conn->errstr);
106 pthread_mutex_unlock (&node->lock);
107 return (-1);
108 }
110 rr = redisCommand(node->conn, "SELECT %d", node->database);
111 if (rr == NULL)
112 WARNING("SELECT command error. database:%d message:%s", node->database, node->conn->errstr);
113 else
114 freeReplyObject (rr);
115 }
117 rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
118 if (rr == NULL)
119 WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
120 else
121 freeReplyObject (rr);
123 if (node->max_set_size >= 0)
124 {
125 rr = redisCommand (node->conn, "ZREMRANGEBYRANK %s %d %d", key, 0, (-1 * node->max_set_size) - 1);
126 if (rr == NULL)
127 WARNING("ZREMRANGEBYRANK command error. key:%s message:%s", key, node->conn->errstr);
128 else
129 freeReplyObject (rr);
130 }
132 /* TODO(octo): This is more overhead than necessary. Use the cache and
133 * metadata to determine if it is a new metric and call SADD only once for
134 * each metric. */
135 rr = redisCommand (node->conn, "SADD %svalues %s",
136 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
137 ident);
138 if (rr==NULL)
139 WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
140 else
141 freeReplyObject (rr);
143 pthread_mutex_unlock (&node->lock);
145 return (0);
146 } /* }}} int wr_write */
148 static void wr_config_free (void *ptr) /* {{{ */
149 {
150 wr_node_t *node = ptr;
152 if (node == NULL)
153 return;
155 if (node->conn != NULL)
156 {
157 redisFree (node->conn);
158 node->conn = NULL;
159 }
161 sfree (node->host);
162 sfree (node);
163 } /* }}} void wr_config_free */
165 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
166 {
167 wr_node_t *node;
168 int timeout;
169 int status;
171 node = calloc (1, sizeof (*node));
172 if (node == NULL)
173 return (ENOMEM);
174 node->host = NULL;
175 node->port = 0;
176 node->timeout.tv_sec = 0;
177 node->timeout.tv_usec = 1000;
178 node->conn = NULL;
179 node->prefix = NULL;
180 node->database = 0;
181 node->max_set_size = -1;
182 node->store_rates = 1;
183 pthread_mutex_init (&node->lock, /* attr = */ NULL);
185 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
186 if (status != 0)
187 {
188 sfree (node);
189 return (status);
190 }
192 for (int i = 0; i < ci->children_num; i++)
193 {
194 oconfig_item_t *child = ci->children + i;
196 if (strcasecmp ("Host", child->key) == 0)
197 status = cf_util_get_string (child, &node->host);
198 else if (strcasecmp ("Port", child->key) == 0)
199 {
200 status = cf_util_get_port_number (child);
201 if (status > 0)
202 {
203 node->port = status;
204 status = 0;
205 }
206 }
207 else if (strcasecmp ("Timeout", child->key) == 0) {
208 status = cf_util_get_int (child, &timeout);
209 if (status == 0) node->timeout.tv_usec = timeout;
210 }
211 else if (strcasecmp ("Prefix", child->key) == 0) {
212 status = cf_util_get_string (child, &node->prefix);
213 }
214 else if (strcasecmp ("Database", child->key) == 0) {
215 status = cf_util_get_int (child, &node->database);
216 }
217 else if (strcasecmp ("MaxSetSize", child->key) == 0) {
218 status = cf_util_get_int (child, &node->max_set_size);
219 }
220 else if (strcasecmp ("StoreRates", child->key) == 0) {
221 status = cf_util_get_boolean (child, &node->store_rates);
222 }
223 else
224 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
225 child->key);
227 if (status != 0)
228 break;
229 } /* for (i = 0; i < ci->children_num; i++) */
231 if (status == 0)
232 {
233 char cb_name[DATA_MAX_NAME_LEN];
235 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
237 user_data_t ud = {
238 .data = node,
239 .free_func = wr_config_free
240 };
242 status = plugin_register_write (cb_name, wr_write, &ud);
243 }
245 if (status != 0)
246 wr_config_free (node);
248 return (status);
249 } /* }}} int wr_config_node */
251 static int wr_config (oconfig_item_t *ci) /* {{{ */
252 {
253 for (int i = 0; i < ci->children_num; i++)
254 {
255 oconfig_item_t *child = ci->children + i;
257 if (strcasecmp ("Node", child->key) == 0)
258 wr_config_node (child);
259 else
260 WARNING ("write_redis plugin: Ignoring unknown "
261 "configuration option \"%s\" at top level.", child->key);
262 }
264 return (0);
265 } /* }}} int wr_config */
267 void module_register (void)
268 {
269 plugin_register_complex_config ("write_redis", wr_config);
270 }
272 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */