6618dc1a9422d4d3053039c0315f669cef4a65e6
[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 tt_config (const char *key, const char *value)
50 {
51         if (strcasecmp ("Host", key) == 0)
52         {
53                 char *temp;
54
55                 temp = strdup (value);
56                 if (temp == NULL)
57                 {
58                         ERROR("tokyotyrant plugin: Host strdup failed.");
59                         return (1);
60                 }
61                 sfree (config_host);
62                 config_host = temp;
63         }
64         else if (strcasecmp ("Port", key) == 0)
65         {
66                 char *temp;
67
68                 temp = strdup (value);
69                 if (temp == NULL)
70                 {
71                         ERROR("tokyotyrant plugin: Port strdup failed.");
72                         return (1);
73                 }
74                 sfree (config_port);
75                 config_port = temp;
76         }
77         else
78         {
79                 ERROR ("tokyotyrant plugin: error: unrecognized configuration key %s", key);
80                 return (-1);
81         }
82
83         return (0);
84 }
85
86 static void printerr()
87 {
88         int ecode = tcrdbecode(rdb);
89         ERROR ("tokyotyrant plugin: error: %d, %s",
90                         ecode, tcrdberrmsg(ecode));
91 }
92
93 static void tt_submit (gauge_t val, const char* type)
94 {
95         value_t values[1];
96         value_list_t vl = VALUE_LIST_INIT;
97
98         values[0].gauge = val;
99
100         vl.values = values;
101         vl.values_len = STATIC_ARRAY_SIZE (values);
102
103         sstrncpy (vl.host, config_host, sizeof (vl.host));
104         sstrncpy (vl.plugin, "tokyotyrant", sizeof (vl.plugin));
105         sstrncpy (vl.plugin_instance, config_port,
106                         sizeof (vl.plugin_instance));
107         sstrncpy (vl.type, type, sizeof (vl.type));
108
109         plugin_dispatch_values (&vl);
110 }
111
112 static void tt_open_db (void)
113 {
114         char* host = NULL;
115         int   port = DEFAULT_PORT;
116
117         if (rdb != NULL)
118                 return;
119
120         host = ((config_host != NULL) ? config_host : DEFAULT_HOST);
121
122         if (config_port != NULL)
123         {
124                 port = service_name_to_port_number (config_port);
125                 if (port <= 0)
126                         return;
127         }
128
129         rdb = tcrdbnew ();
130         if (rdb == NULL)
131                 return;
132         else if (!tcrdbopen(rdb, host, port))
133         {
134                 printerr ();
135                 tcrdbdel (rdb);
136                 rdb = NULL;
137         }
138 } /* void tt_open_db */
139
140 static int tt_read (void) {
141         gauge_t rnum, size;
142
143         tt_open_db ();
144         if (rdb == NULL)
145                 return (-1);
146
147         rnum = tcrdbrnum(rdb);
148         tt_submit (rnum, "records");
149
150         size = tcrdbsize(rdb);
151         tt_submit (size, "file_size");
152
153         return (0);
154 }
155
156 static int tt_shutdown(void)
157 {
158         sfree(config_host);
159         sfree(config_port);
160
161         if (rdb != NULL)
162         {
163                 if (!tcrdbclose(rdb))
164                 {
165                         printerr ();
166                         tcrdbdel (rdb);
167                         return (1);
168                 }
169                 tcrdbdel (rdb);
170                 rdb = NULL;
171         }
172
173         return(0);
174 }
175
176 void module_register (void)
177 {
178         plugin_register_config("tokyotyrant", tt_config,
179                         config_keys, config_keys_num);
180         plugin_register_read("tokyotyrant", tt_read);
181         plugin_register_shutdown("tokyotyrant", tt_shutdown);
182 }
183
184 /* vim: set sw=8 ts=8 tw=78 : */