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