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 **/
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>
29 #define DEFAULT_HOST "127.0.0.1"
30 #define DEFAULT_PORT 1978
32 static const char *config_keys[] =
33 {
34 "Host",
35 "Port"
36 };
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
39 static char *config_host = NULL;
40 static char *config_port = NULL;
42 TCRDB *rdb = NULL;
44 static int tt_config (const char *key, const char *value)
45 {
46 if (strcasecmp ("Host", key) == 0)
47 {
48 char *temp;
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;
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 }
78 return (0);
79 }
81 static void printerr(TCRDB *rdb)
82 {
83 int ecode = tcrdbecode(rdb);
84 ERROR ("tokyotyrant plugin: error: %d, %s",
85 ecode, tcrdberrmsg(ecode));
86 }
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;
93 values[0].gauge = val;
95 vl.values = values;
96 vl.values_len = STATIC_ARRAY_SIZE (values);
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));
104 plugin_dispatch_values (&vl);
105 }
107 static int tt_read (void) {
108 gauge_t rnum, size;
110 rnum = tcrdbrnum(rdb);
111 size = tcrdbsize(rdb);
112 tt_submit (rnum, "records");
113 tt_submit (size, "file_size");
115 return (0);
116 }
118 static int tt_init(void)
119 {
120 char* host = NULL;
121 int port;
123 host = ((config_host != NULL) ? config_host : DEFAULT_HOST);
124 port = ((config_port != NULL) ? atoi(config_port) : DEFAULT_PORT);
126 rdb = tcrdbnew();
128 if (!tcrdbopen(rdb, host, port))
129 {
130 printerr (rdb);
131 tcrdbdel (rdb);
132 return (1);
133 }
135 return(0);
136 }
138 static int tt_shutdown(void)
139 {
140 sfree(config_host);
141 sfree(config_port);
143 if (!tcrdbclose(rdb))
144 {
145 printerr (rdb);
146 tcrdbdel (rdb);
147 return (1);
148 }
150 tcrdbdel (rdb);
152 return(0);
153 }
155 void module_register (void)
156 {
157 plugin_register_config("tokyotyrant", tt_config,
158 config_keys, config_keys_num);
159 plugin_register_read("tokyotyrant", tt_read);
160 plugin_register_init("tokyotyrant", tt_init);
161 plugin_register_shutdown("tokyotyrant", tt_shutdown);
162 }
164 /* vim: set sw=8 ts=8 tw=78 : */