Merge branch 'collectd-4.7' into collectd-4.8
[collectd.git] / src / tokyotyrant.c
1 /**
2  * collectd - src/tokyotyrant.c
3  * Copyright (C) 2009 Paul Sadauskas
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Paul Sadauskas <psadauskas@gmail.com>
20  **/
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_cache.h"
26 #include "utils_parse_option.h"
27
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netdb.h>
31
32 #include <tcrdb.h>
33
34 #define DEFAULT_HOST "127.0.0.1"
35 #define DEFAULT_PORT 1978
36
37 static const char *config_keys[] =
38 {
39         "Host",
40         "Port"
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43
44 static char *config_host = NULL;
45 static char *config_port = NULL;
46
47 static TCRDB *rdb = NULL;
48
49 static int parse_service_name (const char *service_name)
50 {
51         struct addrinfo *ai_list;
52         struct addrinfo *ai_ptr;
53         struct addrinfo ai_hints;
54         int status;
55         int service_number;
56
57         ai_list = NULL;
58         memset (&ai_hints, 0, sizeof (ai_hints));
59         ai_hints.ai_family = AF_UNSPEC;
60
61         status = getaddrinfo (/* node = */ NULL, service_name,
62                         &ai_hints, &ai_list);
63         if (status != 0)
64         {
65                 ERROR ("tokyotyrant plugin: getaddrinfo failed: %s",
66                                 gai_strerror (status));
67                 return (-1);
68         }
69
70         service_number = -1;
71         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
72         {
73                 if (ai_ptr->ai_family == AF_INET)
74                 {
75                         struct sockaddr_in *sa;
76
77                         sa = (void *) ai_ptr->ai_addr;
78                         service_number = (int) ntohs (sa->sin_port);
79                 }
80                 else if (ai_ptr->ai_family == AF_INET6)
81                 {
82                         struct sockaddr_in6 *sa;
83
84                         sa = (void *) ai_ptr->ai_addr;
85                         service_number = (int) ntohs (sa->sin6_port);
86                 }
87
88                 if ((service_number > 0) && (service_number <= 65535))
89                         break;
90         }
91
92         freeaddrinfo (ai_list);
93
94         if ((service_number > 0) && (service_number <= 65535))
95                 return (service_number);
96         return (-1);
97 } /* int parse_service_name */
98
99 static int tt_config (const char *key, const char *value)
100 {
101         if (strcasecmp ("Host", key) == 0)
102         {
103                 char *temp;
104
105                 temp = strdup (value);
106                 if (temp == NULL)
107                 {
108                         ERROR("tokyotyrant plugin: Host strdup failed.");
109                         return (1);
110                 }
111                 sfree (config_host);
112                 config_host = temp;
113         }
114         else if (strcasecmp ("Port", key) == 0)
115         {
116                 char *temp;
117
118                 temp = strdup (value);
119                 if (temp == NULL)
120                 {
121                         ERROR("tokyotyrant plugin: Port strdup failed.");
122                         return (1);
123                 }
124                 sfree (config_port);
125                 config_port = temp;
126         }
127         else
128         {
129                 ERROR ("tokyotyrant plugin: error: unrecognized configuration key %s", key);
130                 return (-1);
131         }
132
133         return (0);
134 }
135
136 static void printerr()
137 {
138         int ecode = tcrdbecode(rdb);
139         ERROR ("tokyotyrant plugin: error: %d, %s",
140                         ecode, tcrdberrmsg(ecode));
141 }
142
143 static void tt_submit (gauge_t val, const char* type)
144 {
145         value_t values[1];
146         value_list_t vl = VALUE_LIST_INIT;
147
148         values[0].gauge = val;
149
150         vl.values = values;
151         vl.values_len = STATIC_ARRAY_SIZE (values);
152
153         sstrncpy (vl.host, config_host, sizeof (vl.host));
154         sstrncpy (vl.plugin, "tokyotyrant", sizeof (vl.plugin));
155         sstrncpy (vl.plugin_instance, config_port,
156                         sizeof (vl.plugin_instance));
157         sstrncpy (vl.type, type, sizeof (vl.type));
158
159         plugin_dispatch_values (&vl);
160 }
161
162 static void tt_open_db (void)
163 {
164         char* host = NULL;
165         int   port = DEFAULT_PORT;
166
167         if (rdb != NULL)
168                 return;
169
170         host = ((config_host != NULL) ? config_host : DEFAULT_HOST);
171
172         if (config_port != NULL)
173         {
174                 port = parse_service_name (config_port);
175                 if (port <= 0)
176                         return;
177         }
178
179         rdb = tcrdbnew ();
180         if (rdb == NULL)
181                 return;
182         else if (!tcrdbopen(rdb, host, port))
183         {
184                 printerr ();
185                 tcrdbdel (rdb);
186                 rdb = NULL;
187         }
188 } /* void tt_open_db */
189
190 static int tt_read (void) {
191         gauge_t rnum, size;
192
193         tt_open_db ();
194         if (rdb == NULL)
195                 return (-1);
196
197         rnum = tcrdbrnum(rdb);
198         tt_submit (rnum, "records");
199
200         size = tcrdbsize(rdb);
201         tt_submit (size, "file_size");
202
203         return (0);
204 }
205
206 static int tt_shutdown(void)
207 {
208         sfree(config_host);
209         sfree(config_port);
210
211         if (rdb != NULL)
212         {
213                 if (!tcrdbclose(rdb))
214                 {
215                         printerr ();
216                         tcrdbdel (rdb);
217                         return (1);
218                 }
219                 tcrdbdel (rdb);
220                 rdb = NULL;
221         }
222
223         return(0);
224 }
225
226 void module_register (void)
227 {
228         plugin_register_config("tokyotyrant", tt_config,
229                         config_keys, config_keys_num);
230         plugin_register_read("tokyotyrant", tt_read);
231         plugin_register_shutdown("tokyotyrant", tt_shutdown);
232 }
233
234 /* vim: set sw=8 ts=8 tw=78 : */