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