2 * collectd - src/tokyotyrant.c
3 * Copyright (C) 2009 Paul Sadauskas
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.
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.
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
19 * Paul Sadauskas <psadauskas@gmail.com>
25 #include "utils_cache.h"
29 #define DEFAULT_HOST "127.0.0.1"
30 #define DEFAULT_PORT 1978
32 static const char *config_keys[] =
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
39 static char *config_host = NULL;
40 static char *config_port = NULL;
42 static TCRDB *rdb = NULL;
44 static int tt_config (const char *key, const char *value)
46 if (strcasecmp ("Host", key) == 0)
50 temp = strdup (value);
53 ERROR("tokyotyrant plugin: Host strdup failed.");
59 else if (strcasecmp ("Port", key) == 0)
63 temp = strdup (value);
66 ERROR("tokyotyrant plugin: Port strdup failed.");
74 ERROR ("tokyotyrant plugin: error: unrecognized configuration key %s", key);
81 static void printerr()
83 int ecode = tcrdbecode(rdb);
84 ERROR ("tokyotyrant plugin: error: %d, %s",
85 ecode, tcrdberrmsg(ecode));
88 static void tt_submit (gauge_t val, const char* type)
91 value_list_t vl = VALUE_LIST_INIT;
93 values[0].gauge = val;
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);
107 static void tt_open_db (void)
110 int port = DEFAULT_PORT;
115 host = ((config_host != NULL) ? config_host : DEFAULT_HOST);
117 if (config_port != NULL)
119 port = service_name_to_port_number (config_port);
127 else if (!tcrdbopen(rdb, host, port))
133 } /* void tt_open_db */
135 static int tt_read (void) {
142 rnum = tcrdbrnum(rdb);
143 tt_submit (rnum, "records");
145 size = tcrdbsize(rdb);
146 tt_submit (size, "file_size");
151 static int tt_shutdown(void)
158 if (!tcrdbclose(rdb))
171 void module_register (void)
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);
179 /* vim: set sw=8 ts=8 tw=78 : */