2 * collectd - src/write_redis.c
3 * Copyright (C) 2010 Florian Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian Forster <ff at octo.it>
30 #include "configfile.h"
37 char name[DATA_MAX_NAME_LEN];
46 typedef struct wr_node_s wr_node_t;
51 static int wr_write (const data_set_t *ds, /* {{{ */
52 const value_list_t *vl,
55 wr_node_t *node = ud->data;
63 status = FORMAT_VL (ident, sizeof (ident), vl);
66 ssnprintf (key, sizeof (key), "collectd/%s", ident);
68 memset (value, 0, sizeof (value));
69 value_size = sizeof (value);
70 value_ptr = &value[0];
72 status = format_values (value_ptr, value_size, ds, vl, /* store rates = */ 0);
73 pthread_mutex_lock (&node->lock);
77 if (node->conn == NULL)
79 node->conn = credis_connect (node->host, node->port, node->timeout);
80 if (node->conn == NULL)
82 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed.",
83 (node->host != NULL) ? node->host : "localhost",
84 (node->port != 0) ? node->port : 6379);
85 pthread_mutex_unlock (&node->lock);
90 /* "credis_zadd" doesn't handle a NULL pointer gracefully, so I'd rather
91 * have a meaningful assertion message than a normal segmentation fault. */
92 assert (node->conn != NULL);
93 status = credis_zadd (node->conn, key, (double) vl->time, value);
95 credis_sadd (node->conn, "collectd/values", ident);
97 pthread_mutex_unlock (&node->lock);
100 } /* }}} int wr_write */
102 static void wr_config_free (void *ptr) /* {{{ */
104 wr_node_t *node = ptr;
109 if (node->conn != NULL)
111 credis_close (node->conn);
117 } /* }}} void wr_config_free */
119 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
125 node = malloc (sizeof (*node));
128 memset (node, 0, sizeof (*node));
131 node->timeout = 1000;
133 pthread_mutex_init (&node->lock, /* attr = */ NULL);
135 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
142 for (i = 0; i < ci->children_num; i++)
144 oconfig_item_t *child = ci->children + i;
146 if (strcasecmp ("Host", child->key) == 0)
147 status = cf_util_get_string (child, &node->host);
148 else if (strcasecmp ("Port", child->key) == 0)
150 status = cf_util_get_port_number (child);
157 else if (strcasecmp ("Timeout", child->key) == 0)
158 status = cf_util_get_int (child, &node->timeout);
160 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
165 } /* for (i = 0; i < ci->children_num; i++) */
169 char cb_name[DATA_MAX_NAME_LEN];
172 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
175 ud.free_func = wr_config_free;
177 status = plugin_register_write (cb_name, wr_write, &ud);
181 wr_config_free (node);
184 } /* }}} int wr_config_node */
186 static int wr_config (oconfig_item_t *ci) /* {{{ */
190 for (i = 0; i < ci->children_num; i++)
192 oconfig_item_t *child = ci->children + i;
194 if (strcasecmp ("Node", child->key) == 0)
195 wr_config_node (child);
197 WARNING ("write_redis plugin: Ignoring unknown "
198 "configuration option \"%s\" at top level.", child->key);
202 } /* }}} int wr_config */
204 void module_register (void)
206 plugin_register_complex_config ("write_redis", wr_config);
209 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */