write_redis: Increase parsability of multi-valued keys by insterting delimiting chara...
[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 <credis.h>
34
35 struct wr_node_s
36 {
37   char name[DATA_MAX_NAME_LEN];
38
39   char *host;
40   int port;
41   int timeout;
42
43   REDIS conn;
44   pthread_mutex_t lock;
45 };
46 typedef struct wr_node_s wr_node_t;
47
48 /*
49  * Functions
50  */
51 static int wr_write (const data_set_t *ds, /* {{{ */
52     const value_list_t *vl,
53     user_data_t *ud)
54 {
55   wr_node_t *node = ud->data;
56   char ident[512];
57   char key[512];
58   char value[512];
59   size_t value_size;
60   char *value_ptr;
61   int status;
62   int i;
63
64   status = FORMAT_VL (ident, sizeof (ident), vl);
65   if (status != 0)
66     return (status);
67   ssnprintf (key, sizeof (key), "collectd/%s", ident);
68
69   memset (value, 0, sizeof (value));
70   value_size = sizeof (value);
71   value_ptr = &value[0];
72
73 #define APPEND(...) do {                                             \
74   status = snprintf (value_ptr, value_size, __VA_ARGS__);            \
75   if (((size_t) status) > value_size)                                \
76   {                                                                  \
77     value_ptr += value_size;                                         \
78     value_size = 0;                                                  \
79   }                                                                  \
80   else                                                               \
81   {                                                                  \
82     value_ptr += status;                                             \
83     value_size -= status;                                            \
84   }                                                                  \
85 } while (0)
86
87   APPEND ("%.3f:", CDTIME_T_TO_DOUBLE (vl->time));
88   for (i = 0; i < ds->ds_num; i++)
89   {
90     // Increase parsability by delimiting the individual values
91     if (ds->ds_num > 1 && i > 0)
92       APPEND ("%s", "|");
93
94     if (ds->ds[i].type == DS_TYPE_COUNTER)
95       APPEND ("%llu", vl->values[i].counter);
96     else if (ds->ds[i].type == DS_TYPE_GAUGE)
97       APPEND (GAUGE_FORMAT, vl->values[i].gauge);
98     else if (ds->ds[i].type == DS_TYPE_DERIVE)
99       APPEND ("%"PRIi64, vl->values[i].derive);
100     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
101       APPEND ("%"PRIu64, vl->values[i].absolute);
102     else
103       assert (23 == 42);
104   }
105
106 #undef APPEND
107
108   pthread_mutex_lock (&node->lock);
109
110   if (node->conn == NULL)
111   {
112     node->conn = credis_connect (node->host, node->port, node->timeout);
113     if (node->conn == NULL)
114     {
115       ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed.",
116           (node->host != NULL) ? node->host : "localhost",
117           (node->port != 0) ? node->port : 6379);
118       pthread_mutex_unlock (&node->lock);
119       return (-1);
120     }
121   }
122
123   /* "credis_zadd" doesn't handle a NULL pointer gracefully, so I'd rather
124    * have a meaningful assertion message than a normal segmentation fault. */
125   assert (node->conn != NULL);
126   status = credis_zadd (node->conn, key, (double) vl->time, value);
127
128   credis_sadd (node->conn, "collectd/values", ident);
129
130   pthread_mutex_unlock (&node->lock);
131
132   return (0);
133 } /* }}} int wr_write */
134
135 static void wr_config_free (void *ptr) /* {{{ */
136 {
137   wr_node_t *node = ptr;
138
139   if (node == NULL)
140     return;
141
142   if (node->conn != NULL)
143   {
144     credis_close (node->conn);
145     node->conn = NULL;
146   }
147
148   sfree (node->host);
149   sfree (node);
150 } /* }}} void wr_config_free */
151
152 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
153 {
154   wr_node_t *node;
155   int status;
156   int i;
157
158   node = malloc (sizeof (*node));
159   if (node == NULL)
160     return (ENOMEM);
161   memset (node, 0, sizeof (*node));
162   node->host = NULL;
163   node->port = 0;
164   node->timeout = 1000;
165   node->conn = NULL;
166   pthread_mutex_init (&node->lock, /* attr = */ NULL);
167
168   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
169   if (status != 0)
170   {
171     sfree (node);
172     return (status);
173   }
174
175   for (i = 0; i < ci->children_num; i++)
176   {
177     oconfig_item_t *child = ci->children + i;
178
179     if (strcasecmp ("Host", child->key) == 0)
180       status = cf_util_get_string (child, &node->host);
181     else if (strcasecmp ("Port", child->key) == 0)
182     {
183       status = cf_util_get_port_number (child);
184       if (status > 0)
185       {
186         node->port = status;
187         status = 0;
188       }
189     }
190     else if (strcasecmp ("Timeout", child->key) == 0)
191       status = cf_util_get_int (child, &node->timeout);
192     else
193       WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
194           child->key);
195
196     if (status != 0)
197       break;
198   } /* for (i = 0; i < ci->children_num; i++) */
199
200   if (status == 0)
201   {
202     char cb_name[DATA_MAX_NAME_LEN];
203     user_data_t ud;
204
205     ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
206
207     ud.data = node;
208     ud.free_func = wr_config_free;
209
210     status = plugin_register_write (cb_name, wr_write, &ud);
211   }
212
213   if (status != 0)
214     wr_config_free (node);
215
216   return (status);
217 } /* }}} int wr_config_node */
218
219 static int wr_config (oconfig_item_t *ci) /* {{{ */
220 {
221   int i;
222
223   for (i = 0; i < ci->children_num; i++)
224   {
225     oconfig_item_t *child = ci->children + i;
226
227     if (strcasecmp ("Node", child->key) == 0)
228       wr_config_node (child);
229     else
230       WARNING ("write_redis plugin: Ignoring unknown "
231           "configuration option \"%s\" at top level.", child->key);
232   }
233
234   return (0);
235 } /* }}} int wr_config */
236
237 void module_register (void)
238 {
239   plugin_register_complex_config ("write_redis", wr_config);
240 }
241
242 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */