5560b4d1fcfcc72fbaf01ec7947bb81314d991c9
[collectd.git] / src / write_redis.c
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  **/
26
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31
32 #include <pthread.h>
33 #include <sys/time.h>
34 #include <hiredis/hiredis.h>
35
36 #ifndef REDIS_DEFAULT_PREFIX
37 # define REDIS_DEFAULT_PREFIX "collectd/"
38 #endif
39
40 struct wr_node_s
41 {
42   char name[DATA_MAX_NAME_LEN];
43
44   char *host;
45   int port;
46   struct timeval timeout;
47   char *prefix;
48   int database;
49
50   redisContext *conn;
51   pthread_mutex_t lock;
52 };
53 typedef struct wr_node_s wr_node_t;
54
55 /*
56  * Functions
57  */
58 static int wr_write (const data_set_t *ds, /* {{{ */
59     const value_list_t *vl,
60     user_data_t *ud)
61 {
62   wr_node_t *node = ud->data;
63   char ident[512];
64   char key[512];
65   char value[512];
66   char time[24];
67   size_t value_size;
68   char *value_ptr;
69   int status;
70   redisReply   *rr;
71
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));
79
80   memset (value, 0, sizeof (value));
81   value_size = sizeof (value);
82   value_ptr = &value[0];
83   status = format_values (value_ptr, value_size, ds, vl, /* store rates = */ 0);
84   pthread_mutex_lock (&node->lock);
85   if (status != 0)
86     return (status);
87
88   if (node->conn == NULL)
89   {
90     node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
91     if (node->conn == NULL)
92     {
93       ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason",
94           (node->host != NULL) ? node->host : "localhost",
95           (node->port != 0) ? node->port : 6379);
96       pthread_mutex_unlock (&node->lock);
97       return (-1);
98     }
99     else if (node->conn->err)
100     {
101       ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
102           (node->host != NULL) ? node->host : "localhost",
103           (node->port != 0) ? node->port : 6379,
104           node->conn->errstr);
105       pthread_mutex_unlock (&node->lock);
106       return (-1);
107     }
108    
109     rr = redisCommand(node->conn, "SELECT %d", node->database);
110     if (rr == NULL)
111       WARNING("SELECT command error. database:%d message:%s", node->database, node->conn->errstr);
112     else
113       freeReplyObject (rr);
114   }
115
116   rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
117   if (rr == NULL)
118     WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
119   else
120     freeReplyObject (rr);
121
122   /* TODO(octo): This is more overhead than necessary. Use the cache and
123    * metadata to determine if it is a new metric and call SADD only once for
124    * each metric. */
125   rr = redisCommand (node->conn, "SADD %svalues %s",
126       (node->prefix != NULL) ? node->prefix : REDIS_DEFAULT_PREFIX,
127       ident);
128   if (rr==NULL)
129     WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
130   else
131     freeReplyObject (rr);
132
133   pthread_mutex_unlock (&node->lock);
134
135   return (0);
136 } /* }}} int wr_write */
137
138 static void wr_config_free (void *ptr) /* {{{ */
139 {
140   wr_node_t *node = ptr;
141
142   if (node == NULL)
143     return;
144
145   if (node->conn != NULL)
146   {
147     redisFree (node->conn);
148     node->conn = NULL;
149   }
150
151   sfree (node->host);
152   sfree (node);
153 } /* }}} void wr_config_free */
154
155 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
156 {
157   wr_node_t *node;
158   int timeout;
159   int status;
160   int i;
161
162   node = malloc (sizeof (*node));
163   if (node == NULL)
164     return (ENOMEM);
165   memset (node, 0, sizeof (*node));
166   node->host = NULL;
167   node->port = 0;
168   node->timeout.tv_sec = 0;
169   node->timeout.tv_usec = 1000;
170   node->conn = NULL;
171   node->prefix = NULL;
172   node->database = 0;
173   pthread_mutex_init (&node->lock, /* attr = */ NULL);
174
175   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
176   if (status != 0)
177   {
178     sfree (node);
179     return (status);
180   }
181
182   for (i = 0; i < ci->children_num; i++)
183   {
184     oconfig_item_t *child = ci->children + i;
185
186     if (strcasecmp ("Host", child->key) == 0)
187       status = cf_util_get_string (child, &node->host);
188     else if (strcasecmp ("Port", child->key) == 0)
189     {
190       status = cf_util_get_port_number (child);
191       if (status > 0)
192       {
193         node->port = status;
194         status = 0;
195       }
196     }
197     else if (strcasecmp ("Timeout", child->key) == 0) {
198       status = cf_util_get_int (child, &timeout);
199       if (status == 0) node->timeout.tv_usec = timeout;
200     }
201     else if (strcasecmp ("Prefix", child->key) == 0) {
202       status = cf_util_get_string (child, &node->prefix);
203     }
204     else if (strcasecmp ("Database", child->key) == 0) {
205       status = cf_util_get_int (child, &node->database);
206     }
207     else
208       WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
209           child->key);
210
211     if (status != 0)
212       break;
213   } /* for (i = 0; i < ci->children_num; i++) */
214
215   if (status == 0)
216   {
217     char cb_name[DATA_MAX_NAME_LEN];
218     user_data_t ud;
219
220     ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
221
222     ud.data = node;
223     ud.free_func = wr_config_free;
224
225     status = plugin_register_write (cb_name, wr_write, &ud);
226   }
227
228   if (status != 0)
229     wr_config_free (node);
230
231   return (status);
232 } /* }}} int wr_config_node */
233
234 static int wr_config (oconfig_item_t *ci) /* {{{ */
235 {
236   int i;
237
238   for (i = 0; i < ci->children_num; i++)
239   {
240     oconfig_item_t *child = ci->children + i;
241
242     if (strcasecmp ("Node", child->key) == 0)
243       wr_config_node (child);
244     else
245       WARNING ("write_redis plugin: Ignoring unknown "
246           "configuration option \"%s\" at top level.", child->key);
247   }
248
249   return (0);
250 } /* }}} int wr_config */
251
252 void module_register (void)
253 {
254   plugin_register_complex_config ("write_redis", wr_config);
255 }
256
257 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */