X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Ftokyotyrant.c;h=678a341c1ceeaf15f24a708d12567c1b8ed92f15;hb=2794e703be245165757b0bec50a6ba09ae5417f0;hp=160bf66760503601b8c7552b6d6aec78460e2420;hpb=31a153c2bdf8e2ff270b58238cde64a15d2dfe6a;p=collectd.git diff --git a/src/tokyotyrant.c b/src/tokyotyrant.c index 160bf667..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) { @@ -104,9 +105,41 @@ static void tt_submit (gauge_t val, const char* type) plugin_dispatch_values (&vl); } +static void tt_open_db (void) +{ + char* host = NULL; + int port = DEFAULT_PORT; + + if (rdb != NULL) + return; + + host = ((config_host != NULL) ? config_host : DEFAULT_HOST); + + if (config_port != NULL) + { + 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); + 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); tt_submit (rnum, "records"); @@ -116,41 +149,24 @@ static int tt_read (void) { return (0); } -static int tt_init(void) -{ - char* host = NULL; - int port; - - host = ((config_host != NULL) ? config_host : DEFAULT_HOST); - port = ((config_port != NULL) ? atoi(config_port) : DEFAULT_PORT); - - rdb = tcrdbnew(); - - if (!tcrdbopen(rdb, host, port)) - { - printerr (); - tcrdbdel (rdb); - return (1); - } - - 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 (); + if (!tcrdbclose(rdb)) + { + printerr (); + tcrdbdel (rdb); + return (1); + } tcrdbdel (rdb); - return (1); + rdb = NULL; } - tcrdbdel (rdb); - - return(0); + return(0); } void module_register (void) @@ -158,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 : */