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