1 /**
2 * collectd - src/write_redis.c
3 * Copyright (C) 2010 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 struct wr_node_s
37 {
38 char name[DATA_MAX_NAME_LEN];
40 char *host;
41 int port;
42 struct timeval timeout;
43 char *prefix;
45 redisContext *conn;
46 pthread_mutex_t lock;
47 };
48 typedef struct wr_node_s wr_node_t;
50 /*
51 * Functions
52 */
53 static int wr_write (const data_set_t *ds, /* {{{ */
54 const value_list_t *vl,
55 user_data_t *ud)
56 {
57 wr_node_t *node = ud->data;
58 char ident[512];
59 char key[512];
60 char value[512];
61 char time[24];
62 size_t value_size;
63 char *value_ptr;
64 int status;
65 redisReply *rr;
66 int i;
68 status = FORMAT_VL (ident, sizeof (ident), vl);
69 if (status != 0)
70 return (status);
71 if (node->prefix == NULL) {
72 ssnprintf (key, sizeof (key), "collectd/%s", ident);
73 }
74 else {
75 ssnprintf (key, sizeof (key), "%s/collectd/%s", node->prefix, ident);
76 }
77 ssnprintf (time, sizeof (time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time));
79 memset (value, 0, sizeof (value));
80 value_size = sizeof (value);
81 value_ptr = &value[0];
83 #define APPEND(...) do { \
84 status = snprintf (value_ptr, value_size, __VA_ARGS__); \
85 if (((size_t) status) > value_size) \
86 { \
87 value_ptr += value_size; \
88 value_size = 0; \
89 } \
90 else \
91 { \
92 value_ptr += status; \
93 value_size -= status; \
94 } \
95 } while (0)
97 APPEND ("%s:", time);
99 for (i = 0; i < ds->ds_num; i++)
100 {
101 if (ds->ds[i].type == DS_TYPE_COUNTER)
102 APPEND ("%llu", vl->values[i].counter);
103 else if (ds->ds[i].type == DS_TYPE_GAUGE)
104 APPEND (GAUGE_FORMAT, vl->values[i].gauge);
105 else if (ds->ds[i].type == DS_TYPE_DERIVE)
106 APPEND ("%"PRIi64, vl->values[i].derive);
107 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
108 APPEND ("%"PRIu64, vl->values[i].absolute);
109 else
110 assert (23 == 42);
111 }
113 #undef APPEND
115 pthread_mutex_lock (&node->lock);
117 if (node->conn == NULL)
118 {
119 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
120 if (node->conn == NULL)
121 {
122 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason",
123 (node->host != NULL) ? node->host : "localhost",
124 (node->port != 0) ? node->port : 6379);
125 pthread_mutex_unlock (&node->lock);
126 return (-1);
127 }
128 else if (node->conn->err)
129 {
130 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
131 (node->host != NULL) ? node->host : "localhost",
132 (node->port != 0) ? node->port : 6379,
133 node->conn->errstr);
134 pthread_mutex_unlock (&node->lock);
135 return (-1);
136 }
137 }
139 rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
140 if (rr==NULL)
141 WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
143 rr = redisCommand (node->conn, "SADD collectd/values %s", ident);
144 if (rr==NULL)
145 WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
147 pthread_mutex_unlock (&node->lock);
149 return (0);
150 } /* }}} int wr_write */
152 static void wr_config_free (void *ptr) /* {{{ */
153 {
154 wr_node_t *node = ptr;
156 if (node == NULL)
157 return;
159 if (node->conn != NULL)
160 {
161 redisFree (node->conn);
162 node->conn = NULL;
163 }
165 sfree (node->host);
166 sfree (node);
167 } /* }}} void wr_config_free */
169 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
170 {
171 wr_node_t *node;
172 int timeout;
173 int status;
174 int i;
176 node = malloc (sizeof (*node));
177 if (node == NULL)
178 return (ENOMEM);
179 memset (node, 0, sizeof (*node));
180 node->host = NULL;
181 node->port = 0;
182 node->timeout.tv_sec = 0;
183 node->timeout.tv_usec = 1000;
184 node->conn = NULL;
185 node->prefix = NULL;
186 pthread_mutex_init (&node->lock, /* attr = */ NULL);
188 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
189 if (status != 0)
190 {
191 sfree (node);
192 return (status);
193 }
195 for (i = 0; i < ci->children_num; i++)
196 {
197 oconfig_item_t *child = ci->children + i;
199 if (strcasecmp ("Host", child->key) == 0)
200 status = cf_util_get_string (child, &node->host);
201 else if (strcasecmp ("Port", child->key) == 0)
202 {
203 status = cf_util_get_port_number (child);
204 if (status > 0)
205 {
206 node->port = status;
207 status = 0;
208 }
209 }
210 else if (strcasecmp ("Timeout", child->key) == 0) {
211 status = cf_util_get_int (child, &timeout);
212 if (status == 0) node->timeout.tv_usec = timeout;
213 }
214 else if (strcasecmp ("Prefix", child->key) == 0) {
215 status = cf_util_get_string (child, &node->prefix);
216 }
217 else
218 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
219 child->key);
221 if (status != 0)
222 break;
223 } /* for (i = 0; i < ci->children_num; i++) */
225 if (status == 0)
226 {
227 char cb_name[DATA_MAX_NAME_LEN];
228 user_data_t ud;
230 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
232 ud.data = node;
233 ud.free_func = wr_config_free;
235 status = plugin_register_write (cb_name, wr_write, &ud);
236 }
238 if (status != 0)
239 wr_config_free (node);
241 return (status);
242 } /* }}} int wr_config_node */
244 static int wr_config (oconfig_item_t *ci) /* {{{ */
245 {
246 int i;
248 for (i = 0; i < ci->children_num; i++)
249 {
250 oconfig_item_t *child = ci->children + i;
252 if (strcasecmp ("Node", child->key) == 0)
253 wr_config_node (child);
254 else
255 WARNING ("write_redis plugin: Ignoring unknown "
256 "configuration option \"%s\" at top level.", child->key);
257 }
259 return (0);
260 } /* }}} int wr_config */
262 void module_register (void)
263 {
264 plugin_register_complex_config ("write_redis", wr_config);
265 }
267 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */