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;
44 redisContext *conn;
45 pthread_mutex_t lock;
46 };
47 typedef struct wr_node_s wr_node_t;
49 /*
50 * Functions
51 */
52 static int wr_write (const data_set_t *ds, /* {{{ */
53 const value_list_t *vl,
54 user_data_t *ud)
55 {
56 wr_node_t *node = ud->data;
57 char ident[512];
58 char key[512];
59 char value[512];
60 char time[24];
61 size_t value_size;
62 char *value_ptr;
63 int status;
64 redisReply *rr;
65 int i;
67 status = FORMAT_VL (ident, sizeof (ident), vl);
68 if (status != 0)
69 return (status);
70 ssnprintf (key, sizeof (key), "collectd/%s", ident);
71 ssnprintf (time, sizeof (time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time));
73 memset (value, 0, sizeof (value));
74 value_size = sizeof (value);
75 value_ptr = &value[0];
77 #define APPEND(...) do { \
78 status = snprintf (value_ptr, value_size, __VA_ARGS__); \
79 if (((size_t) status) > value_size) \
80 { \
81 value_ptr += value_size; \
82 value_size = 0; \
83 } \
84 else \
85 { \
86 value_ptr += status; \
87 value_size -= status; \
88 } \
89 } while (0)
91 APPEND (time);
92 APPEND (":");
94 for (i = 0; i < ds->ds_num; i++)
95 {
96 if (ds->ds[i].type == DS_TYPE_COUNTER)
97 APPEND ("%llu", vl->values[i].counter);
98 else if (ds->ds[i].type == DS_TYPE_GAUGE)
99 APPEND ("%g", vl->values[i].gauge);
100 else if (ds->ds[i].type == DS_TYPE_DERIVE)
101 APPEND ("%"PRIi64, vl->values[i].derive);
102 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
103 APPEND ("%"PRIu64, vl->values[i].absolute);
104 else
105 assert (23 == 42);
106 }
108 #undef APPEND
110 pthread_mutex_lock (&node->lock);
112 if (node->conn == NULL)
113 {
114 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
115 if (node->conn == NULL)
116 {
117 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed.",
118 (node->host != NULL) ? node->host : "localhost",
119 (node->port != 0) ? node->port : 6379);
120 pthread_mutex_unlock (&node->lock);
121 return (-1);
122 }
123 }
125 assert (node->conn != NULL);
126 rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
127 if (rr==NULL)
128 WARNING("ZADD command error. key:%s", key);
130 rr = redisCommand (node->conn, "SADD collectd/values %s", ident);
131 if (rr==NULL)
132 WARNING("SADD command error. ident:%s", ident);
134 pthread_mutex_unlock (&node->lock);
136 return (0);
137 } /* }}} int wr_write */
139 static void wr_config_free (void *ptr) /* {{{ */
140 {
141 wr_node_t *node = ptr;
143 if (node == NULL)
144 return;
146 if (node->conn != NULL)
147 {
148 redisFree (node->conn);
149 node->conn = NULL;
150 }
152 sfree (node->host);
153 sfree (node);
154 } /* }}} void wr_config_free */
156 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
157 {
158 wr_node_t *node;
159 int timeout;
160 int status;
161 int i;
163 node = malloc (sizeof (*node));
164 if (node == NULL)
165 return (ENOMEM);
166 memset (node, 0, sizeof (*node));
167 node->host = NULL;
168 node->port = 0;
169 node->timeout.tv_sec = 0;
170 node->timeout.tv_usec = 1000;
171 node->conn = NULL;
172 pthread_mutex_init (&node->lock, /* attr = */ NULL);
174 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
175 if (status != 0)
176 {
177 sfree (node);
178 return (status);
179 }
181 for (i = 0; i < ci->children_num; i++)
182 {
183 oconfig_item_t *child = ci->children + i;
185 if (strcasecmp ("Host", child->key) == 0)
186 status = cf_util_get_string (child, &node->host);
187 else if (strcasecmp ("Port", child->key) == 0)
188 {
189 status = cf_util_get_port_number (child);
190 if (status > 0)
191 {
192 node->port = status;
193 status = 0;
194 }
195 }
196 else if (strcasecmp ("Timeout", child->key) == 0) {
197 status = cf_util_get_int (child, &timeout);
198 if (status == 0) node->timeout.tv_usec = timeout;
199 }
200 else
201 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
202 child->key);
204 if (status != 0)
205 break;
206 } /* for (i = 0; i < ci->children_num; i++) */
208 if (status == 0)
209 {
210 char cb_name[DATA_MAX_NAME_LEN];
211 user_data_t ud;
213 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
215 ud.data = node;
216 ud.free_func = wr_config_free;
218 status = plugin_register_write (cb_name, wr_write, &ud);
219 }
221 if (status != 0)
222 wr_config_free (node);
224 return (status);
225 } /* }}} int wr_config_node */
227 static int wr_config (oconfig_item_t *ci) /* {{{ */
228 {
229 int i;
231 for (i = 0; i < ci->children_num; i++)
232 {
233 oconfig_item_t *child = ci->children + i;
235 if (strcasecmp ("Node", child->key) == 0)
236 wr_config_node (child);
237 else
238 WARNING ("write_redis plugin: Ignoring unknown "
239 "configuration option \"%s\" at top level.", child->key);
240 }
242 return (0);
243 } /* }}} int wr_config */
245 void module_register (void)
246 {
247 plugin_register_complex_config ("write_redis", wr_config);
248 }
250 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */