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