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);
147 rr = redisCommand (node->conn, "SADD %svalues %s",
148 (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
149 ident);
150 if (rr==NULL)
151 WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
153 pthread_mutex_unlock (&node->lock);
155 return (0);
156 } /* }}} int wr_write */
158 static void wr_config_free (void *ptr) /* {{{ */
159 {
160 wr_node_t *node = ptr;
162 if (node == NULL)
163 return;
165 if (node->conn != NULL)
166 {
167 redisFree (node->conn);
168 node->conn = NULL;
169 }
171 sfree (node->host);
172 sfree (node);
173 } /* }}} void wr_config_free */
175 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
176 {
177 wr_node_t *node;
178 int timeout;
179 int status;
180 int i;
182 node = malloc (sizeof (*node));
183 if (node == NULL)
184 return (ENOMEM);
185 memset (node, 0, sizeof (*node));
186 node->host = NULL;
187 node->port = 0;
188 node->timeout.tv_sec = 0;
189 node->timeout.tv_usec = 1000;
190 node->conn = NULL;
191 node->prefix = NULL;
192 pthread_mutex_init (&node->lock, /* attr = */ NULL);
194 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
195 if (status != 0)
196 {
197 sfree (node);
198 return (status);
199 }
201 for (i = 0; i < ci->children_num; i++)
202 {
203 oconfig_item_t *child = ci->children + i;
205 if (strcasecmp ("Host", child->key) == 0)
206 status = cf_util_get_string (child, &node->host);
207 else if (strcasecmp ("Port", child->key) == 0)
208 {
209 status = cf_util_get_port_number (child);
210 if (status > 0)
211 {
212 node->port = status;
213 status = 0;
214 }
215 }
216 else if (strcasecmp ("Timeout", child->key) == 0) {
217 status = cf_util_get_int (child, &timeout);
218 if (status == 0) node->timeout.tv_usec = timeout;
219 }
220 else if (strcasecmp ("Prefix", child->key) == 0) {
221 status = cf_util_get_string (child, &node->prefix);
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];
234 user_data_t ud;
236 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
238 ud.data = node;
239 ud.free_func = wr_config_free;
241 status = plugin_register_write (cb_name, wr_write, &ud);
242 }
244 if (status != 0)
245 wr_config_free (node);
247 return (status);
248 } /* }}} int wr_config_node */
250 static int wr_config (oconfig_item_t *ci) /* {{{ */
251 {
252 int i;
254 for (i = 0; i < ci->children_num; i++)
255 {
256 oconfig_item_t *child = ci->children + i;
258 if (strcasecmp ("Node", child->key) == 0)
259 wr_config_node (child);
260 else
261 WARNING ("write_redis plugin: Ignoring unknown "
262 "configuration option \"%s\" at top level.", child->key);
263 }
265 return (0);
266 } /* }}} int wr_config */
268 void module_register (void)
269 {
270 plugin_register_complex_config ("write_redis", wr_config);
271 }
273 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */