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