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;
63 status = FORMAT_VL (ident, sizeof (ident), vl);
64 if (status != 0)
65 return (status);
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 if (status != 0)
74 return (status);
76 pthread_mutex_lock (&node->lock);
78 if (node->conn == NULL)
79 {
80 node->conn = credis_connect (node->host, node->port, node->timeout);
81 if (node->conn == NULL)
82 {
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);
87 return (-1);
88 }
89 }
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);
100 return (0);
101 } /* }}} int wr_write */
103 static void wr_config_free (void *ptr) /* {{{ */
104 {
105 wr_node_t *node = ptr;
107 if (node == NULL)
108 return;
110 if (node->conn != NULL)
111 {
112 credis_close (node->conn);
113 node->conn = NULL;
114 }
116 sfree (node->host);
117 sfree (node);
118 } /* }}} void wr_config_free */
120 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
121 {
122 wr_node_t *node;
123 int status;
124 int i;
126 node = malloc (sizeof (*node));
127 if (node == NULL)
128 return (ENOMEM);
129 memset (node, 0, sizeof (*node));
130 node->host = NULL;
131 node->port = 0;
132 node->timeout = 1000;
133 node->conn = NULL;
134 pthread_mutex_init (&node->lock, /* attr = */ NULL);
136 status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
137 if (status != 0)
138 {
139 sfree (node);
140 return (status);
141 }
143 for (i = 0; i < ci->children_num; i++)
144 {
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)
150 {
151 status = cf_util_get_port_number (child);
152 if (status > 0)
153 {
154 node->port = status;
155 status = 0;
156 }
157 }
158 else if (strcasecmp ("Timeout", child->key) == 0)
159 status = cf_util_get_int (child, &node->timeout);
160 else
161 WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
162 child->key);
164 if (status != 0)
165 break;
166 } /* for (i = 0; i < ci->children_num; i++) */
168 if (status == 0)
169 {
170 char cb_name[DATA_MAX_NAME_LEN];
171 user_data_t ud;
173 ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
175 ud.data = node;
176 ud.free_func = wr_config_free;
178 status = plugin_register_write (cb_name, wr_write, &ud);
179 }
181 if (status != 0)
182 wr_config_free (node);
184 return (status);
185 } /* }}} int wr_config_node */
187 static int wr_config (oconfig_item_t *ci) /* {{{ */
188 {
189 int i;
191 for (i = 0; i < ci->children_num; i++)
192 {
193 oconfig_item_t *child = ci->children + i;
195 if (strcasecmp ("Node", child->key) == 0)
196 wr_config_node (child);
197 else
198 WARNING ("write_redis plugin: Ignoring unknown "
199 "configuration option \"%s\" at top level.", child->key);
200 }
202 return (0);
203 } /* }}} int wr_config */
205 void module_register (void)
206 {
207 plugin_register_complex_config ("write_redis", wr_config);
208 }
210 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */