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;
66 status = FORMAT_VL (ident, sizeof (ident), vl);
67 if (status != 0)
68 return (status);
69 ssnprintf (key, sizeof (key), "collectd/%s", ident);
70 ssnprintf (time, sizeof (time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time));
72 memset (value, 0, sizeof (value));
73 value_size = sizeof (value);
74 value_ptr = &value[0];
75 status = format_values (value_ptr, value_size, ds, vl, /* store rates = */ 0);
76 pthread_mutex_lock (&node->lock);
77 if (status != 0)
78 return (status);
80 if (node->conn == NULL)
81 {
82 node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
83 if (node->conn == NULL)
84 {
85 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason",
86 (node->host != NULL) ? node->host : "localhost",
87 (node->port != 0) ? node->port : 6379);
88 pthread_mutex_unlock (&node->lock);
89 return (-1);
90 }
91 else if (node->conn->err)
92 {
93 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
94 (node->host != NULL) ? node->host : "localhost",
95 (node->port != 0) ? node->port : 6379,
96 node->conn->errstr);
97 pthread_mutex_unlock (&node->lock);
98 return (-1);
99 }
100 }
102 rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
103 if (rr == NULL)
104 WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
105 else
106 freeReplyObject (rr);
108 /* TODO(octo): This is more overhead than necessary. Use the cache and
109 * metadata to determine if it is a new metric and call SADD only once for
110 * each metric. */
111 rr = redisCommand (node->conn, "SADD collectd/values %s", ident);
112 if (rr==NULL)
113 WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
114 else
115 freeReplyObject (rr);
117 pthread_mutex_unlock (&node->lock);
119 return (0);
120 } /* }}} int wr_write */
122 static void wr_config_free (void *ptr) /* {{{ */
123 {
124 wr_node_t *node = ptr;
126 if (node == NULL)
127 return;
129 if (node->conn != NULL)
130 {
131 redisFree (node->conn);
132 node->conn = NULL;
133 }
135 sfree (node->host);
136 sfree (node);
137 } /* }}} void wr_config_free */
139 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
140 {
141 wr_node_t *node;
142 int timeout;
143 int status;
144 int i;
146 node = malloc (sizeof (*node));
147 if (node == NULL)
148 return (ENOMEM);
149 memset (node, 0, sizeof (*node));
150 node->host = NULL;
151 node->port = 0;
152 node->timeout.tv_sec = 0;
153 node->timeout.tv_usec = 1000;
154 node->conn = NULL;
155 pthread_mutex_init (&node->lock, /* attr = */ NULL);
157 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
158 if (status != 0)
159 {
160 sfree (node);
161 return (status);
162 }
164 for (i = 0; i < ci->children_num; i++)
165 {
166 oconfig_item_t *child = ci->children + i;
168 if (strcasecmp ("Host", child->key) == 0)
169 status = cf_util_get_string (child, &node->host);
170 else if (strcasecmp ("Port", child->key) == 0)
171 {
172 status = cf_util_get_port_number (child);
173 if (status > 0)
174 {
175 node->port = status;
176 status = 0;
177 }
178 }
179 else if (strcasecmp ("Timeout", child->key) == 0) {
180 status = cf_util_get_int (child, &timeout);
181 if (status == 0) node->timeout.tv_usec = timeout;
182 }
183 else
184 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
185 child->key);
187 if (status != 0)
188 break;
189 } /* for (i = 0; i < ci->children_num; i++) */
191 if (status == 0)
192 {
193 char cb_name[DATA_MAX_NAME_LEN];
194 user_data_t ud;
196 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
198 ud.data = node;
199 ud.free_func = wr_config_free;
201 status = plugin_register_write (cb_name, wr_write, &ud);
202 }
204 if (status != 0)
205 wr_config_free (node);
207 return (status);
208 } /* }}} int wr_config_node */
210 static int wr_config (oconfig_item_t *ci) /* {{{ */
211 {
212 int i;
214 for (i = 0; i < ci->children_num; i++)
215 {
216 oconfig_item_t *child = ci->children + i;
218 if (strcasecmp ("Node", child->key) == 0)
219 wr_config_node (child);
220 else
221 WARNING ("write_redis plugin: Ignoring unknown "
222 "configuration option \"%s\" at top level.", child->key);
223 }
225 return (0);
226 } /* }}} int wr_config */
228 void module_register (void)
229 {
230 plugin_register_complex_config ("write_redis", wr_config);
231 }
233 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */