#include "common.h"
#include "utils_cache.h"
#include "utils_parse_option.h"
+
#include <tcrdb.h>
#define DEFAULT_HOST "127.0.0.1"
static char *config_host = NULL;
static char *config_port = NULL;
+static TCRDB *rdb = NULL;
+
static int tt_config (const char *key, const char *value)
{
if (strcasecmp ("Host", key) == 0)
return (0);
}
-static void printerr(TCRDB *rdb)
+static void printerr()
{
int ecode = tcrdbecode(rdb);
ERROR ("tokyotyrant plugin: error: %d, %s",
plugin_dispatch_values (&vl);
}
-static int tt_read (void) {
- gauge_t rnum, size;
+static void tt_open_db (void)
+{
+ char* host = NULL;
+ int port = DEFAULT_PORT;
- char* host = NULL;
- int port;
+ if (rdb != NULL)
+ return;
- host = ((config_host != NULL) ? config_host : DEFAULT_HOST);
- port = ((config_port != NULL) ? atoi(config_port) : DEFAULT_PORT);
+ host = ((config_host != NULL) ? config_host : DEFAULT_HOST);
- TCRDB *rdb = tcrdbnew();
+ if (config_port != NULL)
+ {
+ port = service_name_to_port_number (config_port);
+ if (port <= 0)
+ return;
+ }
- if (!tcrdbopen(rdb, host, port))
+ rdb = tcrdbnew ();
+ if (rdb == NULL)
+ return;
+ else if (!tcrdbopen(rdb, host, port))
{
- printerr (rdb);
+ printerr ();
tcrdbdel (rdb);
- return (1);
+ rdb = NULL;
}
+} /* void tt_open_db */
+
+static int tt_read (void) {
+ gauge_t rnum, size;
+
+ tt_open_db ();
+ if (rdb == NULL)
+ return (-1);
rnum = tcrdbrnum(rdb);
- size = tcrdbsize(rdb);
tt_submit (rnum, "records");
- tt_submit (size, "file_size");
- if (!tcrdbclose(rdb))
- {
- printerr (rdb);
- tcrdbdel (rdb);
- return (1);
- }
+ size = tcrdbsize(rdb);
+ tt_submit (size, "file_size");
- tcrdbdel (rdb);
return (0);
}
static int tt_shutdown(void)
{
- sfree(config_host);
- sfree(config_port);
+ sfree(config_host);
+ sfree(config_port);
+
+ if (rdb != NULL)
+ {
+ if (!tcrdbclose(rdb))
+ {
+ printerr ();
+ tcrdbdel (rdb);
+ return (1);
+ }
+ tcrdbdel (rdb);
+ rdb = NULL;
+ }
- return(0);
+ return(0);
}
void module_register (void)
plugin_register_config("tokyotyrant", tt_config,
config_keys, config_keys_num);
plugin_register_read("tokyotyrant", tt_read);
- plugin_register_shutdown("tokyotyrant", tt_shutdown);
+ plugin_register_shutdown("tokyotyrant", tt_shutdown);
}
/* vim: set sw=8 ts=8 tw=78 : */