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