write_redis: Increase parsability by using format_values from common.h
[collectd.git] / src / write_redis.c
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  **/
26
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31
32 #include <pthread.h>
33 #include <credis.h>
34
35 struct wr_node_s
36 {
37   char name[DATA_MAX_NAME_LEN];
38
39   char *host;
40   int port;
41   int timeout;
42
43   REDIS conn;
44   pthread_mutex_t lock;
45 };
46 typedef struct wr_node_s wr_node_t;
47
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;
63
64   status = FORMAT_VL (ident, sizeof (ident), vl);
65   if (status != 0)
66     return (status);
67   ssnprintf (key, sizeof (key), "collectd/%s", ident);
68
69   memset (value, 0, sizeof (value));
70   value_size = sizeof (value);
71   value_ptr = &value[0];
72   format_values(value_ptr, value_size, ds, vl, 0);
73
74   pthread_mutex_lock (&node->lock);
75
76   if (node->conn == NULL)
77   {
78     node->conn = credis_connect (node->host, node->port, node->timeout);
79     if (node->conn == NULL)
80     {
81       ERROR ("write_redis plugin: Connecting to host \"%s\" (port %i) failed.",
82           (node->host != NULL) ? node->host : "localhost",
83           (node->port != 0) ? node->port : 6379);
84       pthread_mutex_unlock (&node->lock);
85       return (-1);
86     }
87   }
88
89   /* "credis_zadd" doesn't handle a NULL pointer gracefully, so I'd rather
90    * have a meaningful assertion message than a normal segmentation fault. */
91   assert (node->conn != NULL);
92   status = credis_zadd (node->conn, key, (double) vl->time, value);
93
94   credis_sadd (node->conn, "collectd/values", ident);
95
96   pthread_mutex_unlock (&node->lock);
97
98   return (0);
99 } /* }}} int wr_write */
100
101 static void wr_config_free (void *ptr) /* {{{ */
102 {
103   wr_node_t *node = ptr;
104
105   if (node == NULL)
106     return;
107
108   if (node->conn != NULL)
109   {
110     credis_close (node->conn);
111     node->conn = NULL;
112   }
113
114   sfree (node->host);
115   sfree (node);
116 } /* }}} void wr_config_free */
117
118 static int wr_config_node (oconfig_item_t *ci) /* {{{ */
119 {
120   wr_node_t *node;
121   int status;
122   int i;
123
124   node = malloc (sizeof (*node));
125   if (node == NULL)
126     return (ENOMEM);
127   memset (node, 0, sizeof (*node));
128   node->host = NULL;
129   node->port = 0;
130   node->timeout = 1000;
131   node->conn = NULL;
132   pthread_mutex_init (&node->lock, /* attr = */ NULL);
133
134   status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
135   if (status != 0)
136   {
137     sfree (node);
138     return (status);
139   }
140
141   for (i = 0; i < ci->children_num; i++)
142   {
143     oconfig_item_t *child = ci->children + i;
144
145     if (strcasecmp ("Host", child->key) == 0)
146       status = cf_util_get_string (child, &node->host);
147     else if (strcasecmp ("Port", child->key) == 0)
148     {
149       status = cf_util_get_port_number (child);
150       if (status > 0)
151       {
152         node->port = status;
153         status = 0;
154       }
155     }
156     else if (strcasecmp ("Timeout", child->key) == 0)
157       status = cf_util_get_int (child, &node->timeout);
158     else
159       WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".",
160           child->key);
161
162     if (status != 0)
163       break;
164   } /* for (i = 0; i < ci->children_num; i++) */
165
166   if (status == 0)
167   {
168     char cb_name[DATA_MAX_NAME_LEN];
169     user_data_t ud;
170
171     ssnprintf (cb_name, sizeof (cb_name), "write_redis/%s", node->name);
172
173     ud.data = node;
174     ud.free_func = wr_config_free;
175
176     status = plugin_register_write (cb_name, wr_write, &ud);
177   }
178
179   if (status != 0)
180     wr_config_free (node);
181
182   return (status);
183 } /* }}} int wr_config_node */
184
185 static int wr_config (oconfig_item_t *ci) /* {{{ */
186 {
187   int i;
188
189   for (i = 0; i < ci->children_num; i++)
190   {
191     oconfig_item_t *child = ci->children + i;
192
193     if (strcasecmp ("Node", child->key) == 0)
194       wr_config_node (child);
195     else
196       WARNING ("write_redis plugin: Ignoring unknown "
197           "configuration option \"%s\" at top level.", child->key);
198   }
199
200   return (0);
201 } /* }}} int wr_config */
202
203 void module_register (void)
204 {
205   plugin_register_complex_config ("write_redis", wr_config);
206 }
207
208 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */