pinba plugin: Remove unused type definitions.
[collectd.git] / src / pinba.c
index 46a2ca3..da54e9d 100644 (file)
 /*
  * Private data structures
  */
-typedef struct _pinba_statres_ pinba_statres_t;
-struct _pinba_statres_ {
-  const char *name;
-  double req_per_sec;
-  double req_time;
-  double ru_utime;
-  double ru_stime;
-  double doc_size;
-  double mem_peak;
-};
-
-struct pinba_socket_s {
+/* {{{ */
+struct pinba_socket_s
+{
   struct pollfd fd[PINBA_MAX_SOCKETS];
   nfds_t fd_num;
 };
 typedef struct pinba_socket_s pinba_socket_t;
 
-typedef double pinba_time_t;
-typedef uint32_t pinba_size_t;
-
 /* Fixed point counter value. n is the decimal part multiplied by 10^9. */
 struct float_counter_s
 {
@@ -87,7 +75,7 @@ typedef struct float_counter_s float_counter_t;
 
 struct pinba_statnode_s
 {
-  /* collector name */
+  /* collector name, used as plugin instance */
   char *name;
 
   /* query data */
@@ -105,10 +93,12 @@ struct pinba_statnode_s
   gauge_t mem_peak;
 };
 typedef struct pinba_statnode_s pinba_statnode_t;
+/* }}} */
 
 /*
  * Module global variables
  */
+/* {{{ */
 static pinba_statnode_t *stat_nodes = NULL;
 static unsigned int stat_nodes_num = 0;
 static pthread_mutex_t stat_nodes_lock;
@@ -119,6 +109,7 @@ static char *conf_service = NULL;
 static _Bool collector_thread_running = 0;
 static _Bool collector_thread_do_shutdown = 0;
 static pthread_t collector_thread_id;
+/* }}} */
 
 /*
  * Functions
@@ -559,98 +550,90 @@ static void *collector_thread (void *arg) /* {{{ */
 /*
  * Plugin declaration section
  */
-
-static int config_set (char **var, const char *value) /* {{{ */
+static int pinba_config_view (const oconfig_item_t *ci) /* {{{ */
 {
-  /* code from nginx plugin for collectd */
-  if (*var != NULL) {
-    free (*var);
-    *var = NULL;
+  char *name   = NULL;
+  char *host   = NULL;
+  char *server = NULL;
+  char *script = NULL;
+  int status;
+  int i;
+
+  status = cf_util_get_string (ci, &name);
+  if (status != 0)
+    return (status);
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("Host", child->key) == 0)
+      status = cf_util_get_string (child, &host);
+    else if (strcasecmp ("Server", child->key) == 0)
+      status = cf_util_get_string (child, &server);
+    else if (strcasecmp ("Script", child->key) == 0)
+      status = cf_util_get_string (child, &script);
+    else
+    {
+      WARNING ("pinba plugin: Unknown config option: %s", child->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
   }
-  
-  if ((*var = strdup (value)) == NULL) return (1);
-  else return (0);
-} /* }}} int config_set */
+
+  if (status == 0)
+    service_statnode_add (name, host, server, script);
+
+  sfree (name);
+  sfree (host);
+  sfree (server);
+  sfree (script);
+
+  return (status);
+} /* }}} int pinba_config_view */
 
 static int plugin_config (oconfig_item_t *ci) /* {{{ */
 {
-  unsigned int i, o;
+  int i;
   
+  /* The lock should not be necessary in the config callback, but let's be
+   * sure.. */
   pthread_mutex_lock (&stat_nodes_lock);
-  /* FIXME XXX: Remove all those return statements that don't free the lock. */
 
-  if (stat_nodes == NULL)
+  for (i = 0; i < ci->children_num; i++)
   {
-    /* Collect the "total" data by default. */
-    service_statnode_add ("total",
-        /* host = */ NULL,
-        /* server = */ NULL,
-        /* script = */ NULL);
-  }
-
-  for (i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
-    if (strcasecmp ("Address", child->key) == 0) {
-      if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
-       WARNING ("pinba plugin: `Address' needs exactly one string argument.");
-       return (-1);
-      }
-      config_set(&conf_node, child->values[0].value.string);
-    } else if (strcasecmp ("Port", child->key) == 0) {
-      if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
-       WARNING ("pinba plugin: `Port' needs exactly one string argument.");
-       return (-1);
-      }
-      config_set(&conf_service, child->values[0].value.string);
-    } else if (strcasecmp ("View", child->key) == 0) {
-      const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
-      if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
-       WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
-       return (-1);
-      }
-      name = child->values[0].value.string;
-      for(o=0; o<child->children_num; o++){
-       oconfig_item_t *node = child->children + o;
-       if (strcasecmp ("Host", node->key) == 0) {
-         if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
-           WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
-           return (-1);
-         }
-         host = node->values[0].value.string;
-       } else if (strcasecmp ("Server", node->key) == 0) {
-         if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
-           WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
-           return (-1);
-         }
-         server = node->values[0].value.string;
-       } else if (strcasecmp ("Script", node->key) == 0) {
-         if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
-           WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
-           return (-1);
-         }
-         script = node->values[0].value.string;
-       } else {
-         WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
-         return (-1);
-       }
-      }
-      /* add new statnode */
-      service_statnode_add(name, host, server, script);
-    } else {
-      WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
-      return (-1);
-    }
+
+    if (strcasecmp ("Address", child->key) == 0)
+      cf_util_get_string (child, &conf_node);
+    else if (strcasecmp ("Port", child->key) == 0)
+      cf_util_get_string (child, &conf_service);
+    else if (strcasecmp ("View", child->key) == 0)
+      pinba_config_view (child);
+    else
+      WARNING ("pinba plugin: Unknown config option: %s", child->key);
   }
-  
+
   pthread_mutex_unlock(&stat_nodes_lock);
   
   return (0);
-} /* int pinba_config */
+} /* }}} int pinba_config */
 
 static int plugin_init (void) /* {{{ */
 {
   int status;
 
+  if (stat_nodes == NULL)
+  {
+    /* Collect the "total" data by default. */
+    service_statnode_add ("total",
+        /* host   = */ NULL,
+        /* server = */ NULL,
+        /* script = */ NULL);
+  }
+
   if (collector_thread_running)
     return (0);