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;
64 status = FORMAT_VL (ident, sizeof (ident), vl);
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 status = format_values (value_ptr, value_size, ds, vl, /* store rates = */ 0);
74 pthread_mutex_lock (&node->lock);
78 if (node->conn == NULL)
80 node->conn = credis_connect (node->host, node->port, node->timeout);
81 if (node->conn == NULL)
83 ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed.",
84 (node->host != NULL) ? node->host : "localhost",
85 (node->port != 0) ? node->port : 6379);
86 pthread_mutex_unlock (&node->lock);
91 /* "credis_zadd" doesn't handle a NULL pointer gracefully, so I'd rather
92 * have a meaningful assertion message than a normal segmentation fault. */
93 assert (node->conn != NULL);
94 status = credis_zadd (node->conn, key, (double) vl->time, value);
96 credis_sadd (node->conn, "collectd/values", ident);
98 pthread_mutex_unlock (&node->lock);
101 } /* }}} int wr_write */
103 static void wr_config_free (void *ptr) /* {{{ */
105 wr_node_t *node = ptr;
110 if (node->conn != NULL)
112 credis_close (node->conn);
118 } /* }}} void wr_config_free */
120 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
126 node = malloc (sizeof (*node));
129 memset (node, 0, sizeof (*node));
132 node->timeout = 1000;
134 pthread_mutex_init (&node->lock, /* attr = */ NULL);
136 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
143 for (i = 0; i < ci->children_num; i++)
145 oconfig_item_t *child = ci->children + i;
147 if (strcasecmp ("Host", child->key) == 0)
148 status = cf_util_get_string (child, &node->host);
149 else if (strcasecmp ("Port", child->key) == 0)
151 status = cf_util_get_port_number (child);
158 else if (strcasecmp ("Timeout", child->key) == 0)
159 status = cf_util_get_int (child, &node->timeout);
161 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
166 } /* for (i = 0; i < ci->children_num; i++) */
170 char cb_name[DATA_MAX_NAME_LEN];
173 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
176 ud.free_func = wr_config_free;
178 status = plugin_register_write (cb_name, wr_write, &ud);
182 wr_config_free (node);
185 } /* }}} int wr_config_node */
187 static int wr_config (oconfig_item_t *ci) /* {{{ */
191 for (i = 0; i < ci->children_num; i++)
193 oconfig_item_t *child = ci->children + i;
195 if (strcasecmp ("Node", child->key) == 0)
196 wr_config_node (child);
198 WARNING ("write_redis plugin: Ignoring unknown "
199 "configuration option \"%s\" at top level.", child->key);
203 } /* }}} int wr_config */
205 void module_register (void)
207 plugin_register_complex_config ("write_redis", wr_config);
210 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */