tokyotyrant plugin: Don't need to pass the db handle around, its global.
[collectd.git] / src / tokyotyrant.c
index 5260619..160bf66 100644 (file)
 #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"
+       "Host",
+       "Port"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
-static char *host = NULL;
+static char *config_host = NULL;
+static char *config_port = NULL;
 
-static int tt_config (const char *key, const char *value);
-static int tt_read (void);
-static void tt_submit(gauge_t rnum, const char *type);
+TCRDB *rdb = NULL;
 
-void module_register (void)
+static int tt_config (const char *key, const char *value)
 {
-  plugin_register_config("tokyotyrant", tt_config, config_keys, config_keys_num);
-  plugin_register_read("tokyotyrant", tt_read);
+       if (strcasecmp ("Host", key) == 0)
+       {
+               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)
+       {
+               char *temp;
+
+               temp = strdup (value);
+               if (temp == NULL)
+               {
+                       ERROR("tokyotyrant plugin: Port strdup failed.");
+                       return (1);
+               }
+               sfree (config_port);
+               config_port = temp;
+       }
+       else
+       {
+               ERROR ("tokyotyrant plugin: error: unrecognized configuration key %s", key);
+               return (-1);
+       }
+
+       return (0);
 }
 
-static int tt_config (const char *key, const char *value)
+static void printerr()
 {
-
-  if (strcasecmp ("Host", key) == 0)
-  {
-    if (host != NULL)
-      free (host);
-    host = strdup(value);
-  }
-  return (0);
+       int ecode = tcrdbecode(rdb);
+       ERROR ("tokyotyrant plugin: error: %d, %s",
+                       ecode, tcrdberrmsg(ecode));
 }
 
-static void printerr(TCRDB *rdb)
+static void tt_submit (gauge_t val, const char* type)
 {
-  int ecode = tcrdbecode(rdb);
-  ERROR ("tokyotyrant plugin: error: %d, %s", ecode, tcrdberrmsg(ecode));
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
+
+       values[0].gauge = val;
+
+       vl.values = values;
+       vl.values_len = STATIC_ARRAY_SIZE (values);
+
+       sstrncpy (vl.host, config_host, sizeof (vl.host));
+       sstrncpy (vl.plugin, "tokyotyrant", sizeof (vl.plugin));
+       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;
-
-  TCRDB *rdb = tcrdbnew();
-
-  if (!tcrdbopen2(rdb, host))
-  {
-    printerr (rdb);
-    tcrdbdel (rdb);
-    return (1);
-  }
-
-  rnum = tcrdbrnum(rdb);
-  size = tcrdbsize(rdb);
-
-  if (!tcrdbclose(rdb))
-  {
-    printerr (rdb);
-    tcrdbdel (rdb);
-    return (1);
-  }
-  tt_submit (rnum, "records");
-  tt_submit (size, "file_size");
-
-  return (0);
+       gauge_t rnum, size;
+
+       rnum = tcrdbrnum(rdb);
+       tt_submit (rnum, "records");
+
+       size = tcrdbsize(rdb);
+       tt_submit (size, "file_size");
+
+       return (0);
 }
 
-static void tt_submit (gauge_t val, const char* type)
+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)
 {
-  value_t values[1];
-  value_list_t vl = VALUE_LIST_INIT;
+        sfree(config_host);
+        sfree(config_port);
 
-  values[0].gauge = val;
+       if (!tcrdbclose(rdb))
+       {
+               printerr ();
+               tcrdbdel (rdb);
+               return (1);
+       }
 
-  vl.values = values;
-  vl.values_len = STATIC_ARRAY_SIZE (values);
+        tcrdbdel (rdb);
 
-  sstrncpy (vl.host, hostname_g, sizeof (vl.host));
-  sstrncpy (vl.plugin, "tokyotyrant", sizeof (vl.plugin));
-  sstrncpy (vl.plugin_instance, host, sizeof (vl.plugin_instance));
-  sstrncpy (vl.type, type, sizeof (vl.type));
+        return(0);
+}
 
-  plugin_dispatch_values (&vl);
+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);
 }
+
+/* vim: set sw=8 ts=8 tw=78 : */