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"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
32 #include <pthread.h>
33 #include <sys/time.h>
34 #include <hiredis/hiredis.h>
36 #ifndef REDIS_DEFAULT_PREFIX
37 # define REDIS_DEFAULT_PREFIX "collectd/"
38 #endif
40 struct wr_node_s
41 {
42 char name[DATA_MAX_NAME_LEN];
44 char *host;
45 int port;
46 struct timeval timeout;
47 char *prefix;
48 int database;
49 int max_set_size;
50 _Bool store_rates;
52 redisContext *conn;
53 pthread_mutex_t lock;
54 };
55 typedef struct wr_node_s wr_node_t;
57 /*
58 * Functions
59 */
60 static int wr_write (const data_set_t *ds, /* {{{ */
61 const value_list_t *vl,
62 user_data_t *ud)
63 {
64 wr_node_t *node = ud->data;
65 char ident[512];
66 char key[512];
67 char value[512];
68 char time[24];
69 size_t value_size;
70 char *value_ptr;
71 int status;
72 redisReply *rr;
74 status = FORMAT_VL (ident, sizeof (ident), vl);
75 if (status != 0)
76 return (status);
77 ssnprintf (key, sizeof (key), "%s%s",
78 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
79 ident);
80 ssnprintf (time, sizeof (time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time));
82 memset (value, 0, sizeof (value));
83 value_size = sizeof (value);
84 value_ptr = &value[0];
85 status = format_values (value_ptr, value_size, ds, vl, node->store_rates);
86 if (status != 0)
87 return (status);
89 pthread_mutex_lock (&node->lock);
91 if (node->conn == NULL)
92 {
93 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
94 if (node->conn == NULL)
95 {
96 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason",
97 (node->host != NULL) ? node->host : "localhost",
98 (node->port != 0) ? node->port : 6379);
99 pthread_mutex_unlock (&node->lock);
100 return (-1);
101 }
102 else if (node->conn->err)
103 {
104 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
105 (node->host != NULL) ? node->host : "localhost",
106 (node->port != 0) ? node->port : 6379,
107 node->conn->errstr);
108 pthread_mutex_unlock (&node->lock);
109 return (-1);
110 }
112 rr = redisCommand(node->conn, "SELECT %d", node->database);
113 if (rr == NULL)
114 WARNING("SELECT command error. database:%d message:%s", node->database, node->conn->errstr);
115 else
116 freeReplyObject (rr);
117 }
119 rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
120 if (rr == NULL)
121 WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
122 else
123 freeReplyObject (rr);
125 if (node->max_set_size >= 0)
126 {
127 rr = redisCommand (node->conn, "ZREMRANGEBYRANK %s %d %d", key, 0, (-1 * node->max_set_size) - 1);
128 if (rr == NULL)
129 WARNING("ZREMRANGEBYRANK command error. key:%s message:%s", key, node->conn->errstr);
130 else
131 freeReplyObject (rr);
132 }
134 /* TODO(octo): This is more overhead than necessary. Use the cache and
135 * metadata to determine if it is a new metric and call SADD only once for
136 * each metric. */
137 rr = redisCommand (node->conn, "SADD %svalues %s",
138 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
139 ident);
140 if (rr==NULL)
141 WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
142 else
143 freeReplyObject (rr);
145 pthread_mutex_unlock (&node->lock);
147 return (0);
148 } /* }}} int wr_write */
150 static void wr_config_free (void *ptr) /* {{{ */
151 {
152 wr_node_t *node = ptr;
154 if (node == NULL)
155 return;
157 if (node->conn != NULL)
158 {
159 redisFree (node->conn);
160 node->conn = NULL;
161 }
163 sfree (node->host);
164 sfree (node);
165 } /* }}} void wr_config_free */
167 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
168 {
169 wr_node_t *node;
170 int timeout;
171 int status;
172 int i;
174 node = malloc (sizeof (*node));
175 if (node == NULL)
176 return (ENOMEM);
177 memset (node, 0, sizeof (*node));
178 node->host = NULL;
179 node->port = 0;
180 node->timeout.tv_sec = 0;
181 node->timeout.tv_usec = 1000;
182 node->conn = NULL;
183 node->prefix = NULL;
184 node->database = 0;
185 node->max_set_size = -1;
186 node->store_rates = 1;
187 pthread_mutex_init (&node->lock, /* attr = */ NULL);
189 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
190 if (status != 0)
191 {
192 sfree (node);
193 return (status);
194 }
196 for (i = 0; i < ci->children_num; i++)
197 {
198 oconfig_item_t *child = ci->children + i;
200 if (strcasecmp ("Host", child->key) == 0)
201 status = cf_util_get_string (child, &node->host);
202 else if (strcasecmp ("Port", child->key) == 0)
203 {
204 status = cf_util_get_port_number (child);
205 if (status > 0)
206 {
207 node->port = status;
208 status = 0;
209 }
210 }
211 else if (strcasecmp ("Timeout", child->key) == 0) {
212 status = cf_util_get_int (child, &timeout);
213 if (status == 0) node->timeout.tv_usec = timeout;
214 }
215 else if (strcasecmp ("Prefix", child->key) == 0) {
216 status = cf_util_get_string (child, &node->prefix);
217 }
218 else if (strcasecmp ("Database", child->key) == 0) {
219 status = cf_util_get_int (child, &node->database);
220 }
221 else if (strcasecmp ("MaxSetSize", child->key) == 0) {
222 status = cf_util_get_int (child, &node->max_set_size);
223 }
224 else if (strcasecmp ("StoreRates", child->key) == 0) {
225 status = cf_util_get_boolean (child, &node->store_rates);
226 }
227 else
228 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
229 child->key);
231 if (status != 0)
232 break;
233 } /* for (i = 0; i < ci->children_num; i++) */
235 if (status == 0)
236 {
237 char cb_name[DATA_MAX_NAME_LEN];
238 user_data_t ud;
240 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
242 ud.data = node;
243 ud.free_func = wr_config_free;
245 status = plugin_register_write (cb_name, wr_write, &ud);
246 }
248 if (status != 0)
249 wr_config_free (node);
251 return (status);
252 } /* }}} int wr_config_node */
254 static int wr_config (oconfig_item_t *ci) /* {{{ */
255 {
256 int i;
258 for (i = 0; i < ci->children_num; i++)
259 {
260 oconfig_item_t *child = ci->children + i;
262 if (strcasecmp ("Node", child->key) == 0)
263 wr_config_node (child);
264 else
265 WARNING ("write_redis plugin: Ignoring unknown "
266 "configuration option \"%s\" at top level.", child->key);
267 }
269 return (0);
270 } /* }}} int wr_config */
272 void module_register (void)
273 {
274 plugin_register_complex_config ("write_redis", wr_config);
275 }
277 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */