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;
49 redisContext *conn;
50 pthread_mutex_t lock;
51 };
52 typedef struct wr_node_s wr_node_t;
54 /*
55 * Functions
56 */
57 static int wr_write (const data_set_t *ds, /* {{{ */
58 const value_list_t *vl,
59 user_data_t *ud)
60 {
61 wr_node_t *node = ud->data;
62 char ident[512];
63 char key[512];
64 char value[512];
65 char time[24];
66 size_t value_size;
67 char *value_ptr;
68 int status;
69 redisReply *rr;
70 int i;
72 status = FORMAT_VL (ident, sizeof (ident), vl);
73 if (status != 0)
74 return (status);
75 ssnprintf (key, sizeof (key), "%s%s",
76 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
77 ident);
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) \
87 { \
88 value_ptr += value_size; \
89 value_size = 0; \
90 } \
91 else \
92 { \
93 value_ptr += status; \
94 value_size -= status; \
95 } \
96 } while (0)
98 APPEND ("%s:", time);
100 for (i = 0; i < ds->ds_num; i++)
101 {
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);
110 else
111 assert (23 == 42);
112 }
114 #undef APPEND
116 status = format_values (value_ptr, value_size, ds, vl, /* store rates = */ 0);
117 pthread_mutex_lock (&node->lock);
118 if (status != 0)
119 return (status);
121 if (node->conn == NULL)
122 {
123 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
124 if (node->conn == NULL)
125 {
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);
130 return (-1);
131 }
132 else if (node->conn->err)
133 {
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,
137 node->conn->errstr);
138 pthread_mutex_unlock (&node->lock);
139 return (-1);
140 }
141 }
143 rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
144 if (rr == NULL)
145 WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
146 else
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
151 * each metric. */
152 rr = redisCommand (node->conn, "SADD %svalues %s",
153 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
154 ident);
155 if (rr==NULL)
156 WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
157 else
158 freeReplyObject (rr);
160 pthread_mutex_unlock (&node->lock);
162 return (0);
163 } /* }}} int wr_write */
165 static void wr_config_free (void *ptr) /* {{{ */
166 {
167 wr_node_t *node = ptr;
169 if (node == NULL)
170 return;
172 if (node->conn != NULL)
173 {
174 redisFree (node->conn);
175 node->conn = NULL;
176 }
178 sfree (node->host);
179 sfree (node);
180 } /* }}} void wr_config_free */
182 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
183 {
184 wr_node_t *node;
185 int timeout;
186 int status;
187 int i;
189 node = malloc (sizeof (*node));
190 if (node == NULL)
191 return (ENOMEM);
192 memset (node, 0, sizeof (*node));
193 node->host = NULL;
194 node->port = 0;
195 node->timeout.tv_sec = 0;
196 node->timeout.tv_usec = 1000;
197 node->conn = NULL;
198 node->prefix = NULL;
199 pthread_mutex_init (&node->lock, /* attr = */ NULL);
201 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
202 if (status != 0)
203 {
204 sfree (node);
205 return (status);
206 }
208 for (i = 0; i < ci->children_num; i++)
209 {
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)
215 {
216 status = cf_util_get_port_number (child);
217 if (status > 0)
218 {
219 node->port = status;
220 status = 0;
221 }
222 }
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;
226 }
227 else if (strcasecmp ("Prefix", child->key) == 0) {
228 status = cf_util_get_string (child, &node->prefix);
229 }
230 else
231 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
232 child->key);
234 if (status != 0)
235 break;
236 } /* for (i = 0; i < ci->children_num; i++) */
238 if (status == 0)
239 {
240 char cb_name[DATA_MAX_NAME_LEN];
241 user_data_t ud;
243 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
245 ud.data = node;
246 ud.free_func = wr_config_free;
248 status = plugin_register_write (cb_name, wr_write, &ud);
249 }
251 if (status != 0)
252 wr_config_free (node);
254 return (status);
255 } /* }}} int wr_config_node */
257 static int wr_config (oconfig_item_t *ci) /* {{{ */
258 {
259 int i;
261 for (i = 0; i < ci->children_num; i++)
262 {
263 oconfig_item_t *child = ci->children + i;
265 if (strcasecmp ("Node", child->key) == 0)
266 wr_config_node (child);
267 else
268 WARNING ("write_redis plugin: Ignoring unknown "
269 "configuration option \"%s\" at top level.", child->key);
270 }
272 return (0);
273 } /* }}} int wr_config */
275 void module_register (void)
276 {
277 plugin_register_complex_config ("write_redis", wr_config);
278 }
280 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */