Merge pull request #774 from trenkel/master
[collectd.git] / src / tokyotyrant.c
index 73a065e..7fa621b 100644 (file)
 #include "plugin.h"
 #include "common.h"
 #include "utils_cache.h"
-#include "utils_parse_option.h"
+
 #include <tcrdb.h>
 
+#define DEFAULT_HOST "127.0.0.1"
+#define DEFAULT_PORT 1978
+
 static const char *config_keys[] =
 {
        "Host",
@@ -33,33 +36,38 @@ static const char *config_keys[] =
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
-static char *host = NULL;
+static char *config_host = NULL;
+static char *config_port = NULL;
 
-/* int is for opening connection, string is for plugin_instance */
-static char *port_str = NULL;
-static int   port;
+static TCRDB *rdb = NULL;
 
 static int tt_config (const char *key, const char *value)
 {
        if (strcasecmp ("Host", key) == 0)
        {
-               if (host != NULL)
-                       free (host);
-               host = strdup(value);
+               char *temp;
+
+               temp = strdup (value);
+               if (temp == NULL)
+               {
+                       ERROR("tokyotyrant plugin: Host strdup failed.");
+                       return (1);
+               }
+               sfree (config_host);
+               config_host = temp;
        }
        else if (strcasecmp ("Port", key) == 0)
        {
-               if (port_str != NULL)
-                       free (port_str);
-               port_str = strdup(value);
-
-               port = atoi(value);
+               char *temp;
 
-               if ((port < 0) || (port > 65535))
+               temp = strdup (value);
+               if (temp == NULL)
                {
-                       ERROR ("tokyotyrant plugin: error: Port %s out of range", value);
-                       return (-1);
+                       ERROR("tokyotyrant plugin: Port strdup failed.");
+                       return (1);
                }
+               sfree (config_port);
+               config_port = temp;
        }
        else
        {
@@ -70,7 +78,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",
@@ -87,40 +95,77 @@ static void tt_submit (gauge_t val, const char* type)
        vl.values = values;
        vl.values_len = STATIC_ARRAY_SIZE (values);
 
-       sstrncpy (vl.host, host, sizeof (vl.host));
+       sstrncpy (vl.host, config_host, sizeof (vl.host));
        sstrncpy (vl.plugin, "tokyotyrant", sizeof (vl.plugin));
-       sstrncpy (vl.plugin_instance, port_str,
+       sstrncpy (vl.plugin_instance, config_port,
                        sizeof (vl.plugin_instance));
        sstrncpy (vl.type, type, sizeof (vl.type));
 
        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;
+
+       if (rdb != NULL)
+               return;
 
-       TCRDB *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 */
+
+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");
+
+       size = tcrdbsize(rdb);
        tt_submit (size, "file_size");
 
-       if (!tcrdbclose(rdb))
+       return (0);
+}
+
+static int tt_shutdown(void)
+{
+       sfree(config_host);
+       sfree(config_port);
+
+       if (rdb != NULL)
        {
-               printerr (rdb);
+               if (!tcrdbclose(rdb))
+               {
+                       printerr ();
+                       tcrdbdel (rdb);
+                       return (1);
+               }
                tcrdbdel (rdb);
-               return (1);
+               rdb = NULL;
        }
 
-       return (0);
+       return(0);
 }
 
 void module_register (void)
@@ -128,6 +173,7 @@ 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);
 }
 
 /* vim: set sw=8 ts=8 tw=78 : */