X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Ftokyotyrant.c;h=678a341c1ceeaf15f24a708d12567c1b8ed92f15;hb=fadd1df67243af6d0d4f58b10b21755ee1f433d6;hp=87001a05eb1107361f1bb95e97fe78cbac7b69b5;hpb=159ed49f25849438b4106c71dff1755f9d7d5ed1;p=collectd.git diff --git a/src/tokyotyrant.c b/src/tokyotyrant.c index 87001a05..678a341c 100644 --- a/src/tokyotyrant.c +++ b/src/tokyotyrant.c @@ -24,6 +24,7 @@ #include "common.h" #include "utils_cache.h" #include "utils_parse_option.h" + #include #define DEFAULT_HOST "127.0.0.1" @@ -39,7 +40,7 @@ static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); static char *config_host = NULL; static char *config_port = NULL; -TCRDB *rdb = NULL; +static TCRDB *rdb = NULL; static int tt_config (const char *key, const char *value) { @@ -78,7 +79,7 @@ static int tt_config (const char *key, const char *value) return (0); } -static void printerr(TCRDB *rdb) +static void printerr() { int ecode = tcrdbecode(rdb); ERROR ("tokyotyrant plugin: error: %d, %s", @@ -104,52 +105,68 @@ static void tt_submit (gauge_t val, const char* type) plugin_dispatch_values (&vl); } -static int tt_read (void) { - gauge_t rnum, size; - - rnum = tcrdbrnum(rdb); - size = tcrdbsize(rdb); - tt_submit (rnum, "records"); - tt_submit (size, "file_size"); - - return (0); -} - -static int tt_init(void) +static void tt_open_db (void) { - char* host = NULL; - int port; + char* host = NULL; + int port = DEFAULT_PORT; - host = ((config_host != NULL) ? config_host : DEFAULT_HOST); - port = ((config_port != NULL) ? atoi(config_port) : DEFAULT_PORT); + if (rdb != NULL) + return; - rdb = tcrdbnew(); + host = ((config_host != NULL) ? config_host : DEFAULT_HOST); - if (!tcrdbopen(rdb, host, port)) + if (config_port != NULL) { - printerr (rdb); + port = service_name_to_port_number (config_port); + if (port <= 0) + return; + } + + rdb = tcrdbnew (); + if (rdb == NULL) + return; + else if (!tcrdbopen(rdb, host, port)) + { + printerr (); tcrdbdel (rdb); - return (1); + rdb = NULL; } +} /* void tt_open_db */ - return(0); +static int tt_read (void) { + gauge_t rnum, size; + + tt_open_db (); + if (rdb == NULL) + return (-1); + + rnum = tcrdbrnum(rdb); + tt_submit (rnum, "records"); + + size = tcrdbsize(rdb); + tt_submit (size, "file_size"); + + return (0); } static int tt_shutdown(void) { - sfree(config_host); - sfree(config_port); + sfree(config_host); + sfree(config_port); - if (!tcrdbclose(rdb)) + if (rdb != NULL) { - printerr (rdb); + if (!tcrdbclose(rdb)) + { + printerr (); + tcrdbdel (rdb); + return (1); + } tcrdbdel (rdb); - return (1); + rdb = NULL; } - tcrdbdel (rdb); - - return(0); + return(0); } void module_register (void) @@ -157,8 +174,7 @@ void module_register (void) plugin_register_config("tokyotyrant", tt_config, config_keys, config_keys_num); plugin_register_read("tokyotyrant", tt_read); - plugin_register_init("tokyotyrant", tt_init); - plugin_register_shutdown("tokyotyrant", tt_shutdown); + plugin_register_shutdown("tokyotyrant", tt_shutdown); } /* vim: set sw=8 ts=8 tw=78 : */