fix a couple of typos spotted by Debian's lintian tool
[collectd.git] / src / write_redis.c
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  **/
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 struct wr_node_s
37 {
38   char name[DATA_MAX_NAME_LEN];
39
40   char *host;
41   int port;
42   struct timeval timeout;
43
44   redisContext *conn;
45   pthread_mutex_t lock;
46 };
47 typedef struct wr_node_s wr_node_t;
48
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
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));
71
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   if (status != 0)
77     return (status);
78
79   pthread_mutex_lock (&node->lock);
80
81   if (node->conn == NULL)
82   {
83     node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout);
84     if (node->conn == NULL)
85     {
86       ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: Unknown reason",
87           (node->host != NULL) ? node->host : "localhost",
88           (node->port != 0) ? node->port : 6379);
89       pthread_mutex_unlock (&node->lock);
90       return (-1);
91     }
92     else if (node->conn->err)
93     {
94       ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed: %s",
95           (node->host != NULL) ? node->host : "localhost",
96           (node->port != 0) ? node->port : 6379,
97           node->conn->errstr);
98       pthread_mutex_unlock (&node->lock);
99       return (-1);
100     }
101   }
102
103   rr = redisCommand (node->conn, "ZADD %s %s %s", key, time, value);
104   if (rr == NULL)
105     WARNING("ZADD command error. key:%s message:%s", key, node->conn->errstr);
106   else
107     freeReplyObject (rr);
108
109   /* TODO(octo): This is more overhead than necessary. Use the cache and
110    * metadata to determine if it is a new metric and call SADD only once for
111    * each metric. */
112   rr = redisCommand (node->conn, "SADD collectd/values %s", ident);
113   if (rr==NULL)
114     WARNING("SADD command error. ident:%s message:%s", ident, node->conn->errstr);
115   else
116     freeReplyObject (rr);
117
118   pthread_mutex_unlock (&node->lock);
119
120   return (0);
121 } /* }}} int wr_write */
122
123 static void wr_config_free (void *ptr) /* {{{ */
124 {
125   wr_node_t *node = ptr;
126
127   if (node == NULL)
128     return;
129
130   if (node->conn != NULL)
131   {
132     redisFree (node->conn);
133     node->conn = NULL;
134   }
135
136   sfree (node->host);
137   sfree (node);
138 } /* }}} void wr_config_free */
139
140 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
141 {
142   wr_node_t *node;
143   int timeout;
144   int status;
145   int i;
146
147   node = malloc (sizeof (*node));
148   if (node == NULL)
149     return (ENOMEM);
150   memset (node, 0, sizeof (*node));
151   node->host = NULL;
152   node->port = 0;
153   node->timeout.tv_sec = 0;
154   node->timeout.tv_usec = 1000;
155   node->conn = NULL;
156   pthread_mutex_init (&node->lock, /* attr = */ NULL);
157
158   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
159   if (status != 0)
160   {
161     sfree (node);
162     return (status);
163   }
164
165   for (i = 0; i < ci->children_num; i++)
166   {
167     oconfig_item_t *child = ci->children + i;
168
169     if (strcasecmp ("Host", child->key) == 0)
170       status = cf_util_get_string (child, &node->host);
171     else if (strcasecmp ("Port", child->key) == 0)
172     {
173       status = cf_util_get_port_number (child);
174       if (status > 0)
175       {
176         node->port = status;
177         status = 0;
178       }
179     }
180     else if (strcasecmp ("Timeout", child->key) == 0) {
181       status = cf_util_get_int (child, &timeout);
182       if (status == 0) node->timeout.tv_usec = timeout;
183     }
184     else
185       WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
186           child->key);
187
188     if (status != 0)
189       break;
190   } /* for (i = 0; i < ci->children_num; i++) */
191
192   if (status == 0)
193   {
194     char cb_name[DATA_MAX_NAME_LEN];
195     user_data_t ud;
196
197     ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
198
199     ud.data = node;
200     ud.free_func = wr_config_free;
201
202     status = plugin_register_write (cb_name, wr_write, &ud);
203   }
204
205   if (status != 0)
206     wr_config_free (node);
207
208   return (status);
209 } /* }}} int wr_config_node */
210
211 static int wr_config (oconfig_item_t *ci) /* {{{ */
212 {
213   int i;
214
215   for (i = 0; i < ci->children_num; i++)
216   {
217     oconfig_item_t *child = ci->children + i;
218
219     if (strcasecmp ("Node", child->key) == 0)
220       wr_config_node (child);
221     else
222       WARNING ("write_redis plugin: Ignoring unknown "
223           "configuration option \"%s\" at top level.", child->key);
224   }
225
226   return (0);
227 } /* }}} int wr_config */
228
229 void module_register (void)
230 {
231   plugin_register_complex_config ("write_redis", wr_config);
232 }
233
234 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */