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 **/
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
32 #include <pthread.h>
33 #include <credis.h>
35 struct wr_node_s
36 {
37 char name[DATA_MAX_NAME_LEN];
39 char *host;
40 int port;
41 int timeout;
43 REDIS conn;
44 pthread_mutex_t lock;
45 };
46 typedef struct wr_node_s wr_node_t;
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;
64 status = FORMAT_VL (ident, sizeof (ident), vl);
65 if (status != 0)
66 return (status);
67 ssnprintf (key, sizeof (key), "collectd/%s", ident);
69 memset (value, 0, sizeof (value));
70 value_size = sizeof (value);
71 value_ptr = &value[0];
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)
87 APPEND ("%lu", (unsigned long) vl->time);
88 for (i = 0; i < ds->ds_num; i++)
89 {
90 if (ds->ds[i].type == DS_TYPE_COUNTER)
91 APPEND ("%llu", vl->values[i].counter);
92 else if (ds->ds[i].type == DS_TYPE_GAUGE)
93 APPEND ("%g", vl->values[i].gauge);
94 else if (ds->ds[i].type == DS_TYPE_DERIVE)
95 APPEND ("%"PRIi64, vl->values[i].derive);
96 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
97 APPEND ("%"PRIu64, vl->values[i].absolute);
98 else
99 assert (23 == 42);
100 }
102 #undef APPEND
104 pthread_mutex_lock (&node->lock);
106 if (node->conn == NULL)
107 {
108 node->conn = credis_connect (node->host, node->port, node->timeout);
109 if (node->conn == NULL)
110 {
111 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed.",
112 (node->host != NULL) ? node->host : "localhost",
113 (node->port != 0) ? node->port : 6379);
114 pthread_mutex_unlock (&node->lock);
115 return (-1);
116 }
117 }
119 /* "credis_zadd" doesn't handle a NULL pointer gracefully, so I'd rather
120 * have a meaningful assertion message than a normal segmentation fault. */
121 assert (node->conn != NULL);
122 status = credis_zadd (node->conn, key, (double) vl->time, value);
124 credis_sadd (node->conn, "collectd/values", ident);
126 pthread_mutex_unlock (&node->lock);
128 return (0);
129 } /* }}} int wr_write */
131 static void wr_config_free (void *ptr) /* {{{ */
132 {
133 wr_node_t *node = ptr;
135 if (node == NULL)
136 return;
138 if (node->conn != NULL)
139 {
140 credis_close (node->conn);
141 node->conn = NULL;
142 }
144 sfree (node->host);
145 sfree (node);
146 } /* }}} void wr_config_free */
148 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
149 {
150 wr_node_t *node;
151 int status;
152 int i;
154 node = malloc (sizeof (*node));
155 if (node == NULL)
156 return (ENOMEM);
157 memset (node, 0, sizeof (*node));
158 node->host = NULL;
159 node->port = 0;
160 node->timeout = 1000;
161 node->conn = NULL;
162 pthread_mutex_init (&node->lock, /* attr = */ NULL);
164 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
165 if (status != 0)
166 {
167 sfree (node);
168 return (status);
169 }
171 for (i = 0; i < ci->children_num; i++)
172 {
173 oconfig_item_t *child = ci->children + i;
175 if (strcasecmp ("Host", child->key) == 0)
176 status = cf_util_get_string (child, &node->host);
177 else if (strcasecmp ("Port", child->key) == 0)
178 {
179 status = cf_util_get_port_number (child);
180 if (status > 0)
181 {
182 node->port = status;
183 status = 0;
184 }
185 }
186 else if (strcasecmp ("Timeout", child->key) == 0)
187 status = cf_util_get_int (child, &node->timeout);
188 else
189 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
190 child->key);
192 if (status != 0)
193 break;
194 } /* for (i = 0; i < ci->children_num; i++) */
196 if (status == 0)
197 {
198 char cb_name[DATA_MAX_NAME_LEN];
199 user_data_t ud;
201 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
203 ud.data = node;
204 ud.free_func = wr_config_free;
206 status = plugin_register_write (cb_name, wr_write, &ud);
207 }
209 if (status != 0)
210 wr_config_free (node);
212 return (status);
213 } /* }}} int wr_config_node */
215 static int wr_config (oconfig_item_t *ci) /* {{{ */
216 {
217 int i;
219 for (i = 0; i < ci->children_num; i++)
220 {
221 oconfig_item_t *child = ci->children + i;
223 if (strcasecmp ("Node", child->key) == 0)
224 wr_config_node (child);
225 else
226 WARNING ("write_redis plugin: Ignoring unknown "
227 "configuration option \"%s\" at top level.", child->key);
228 }
230 return (0);
231 } /* }}} int wr_config */
233 void module_register (void)
234 {
235 plugin_register_complex_config ("write_redis", wr_config);
236 }
238 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */