Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / pinba.c
index da54e9d..e94ad8f 100644 (file)
@@ -20,7 +20,7 @@
  * Authors:
  *   Antony Dovgal <tony at daylessday.org>
  *   Phoenix Kayo <kayo.k11.4 at gmail.com>
- *   Florian Forster <octo at verplant.org>
+ *   Florian Forster <octo at collectd.org>
  **/
 
 #include "collectd.h"
 
 #include "pinba.pb-c.h"
 
+/* AIX doesn't have MSG_DONTWAIT */
+#ifndef MSG_DONTWAIT
+#  define MSG_DONTWAIT MSG_NONBLOCK
+#endif
+
 /*
  * Defines
  */
 #endif
 
 #ifndef PINBA_DEFAULT_NODE
-# define PINBA_DEFAULT_NODE "127.0.0.1" /* FIXME */
+# define PINBA_DEFAULT_NODE "::0"
 #endif
 
 #ifndef PINBA_DEFAULT_SERVICE
-# define PINBA_DEFAULT_SERVICE "12345" /* FIXME */
+# define PINBA_DEFAULT_SERVICE "30002"
 #endif
 
 #ifndef PINBA_MAX_SOCKETS
@@ -135,12 +140,15 @@ static void float_counter_add (float_counter_t *fc, float val) /* {{{ */
   }
 } /* }}} void float_counter_add */
 
-static derive_t float_counter_get (const float_counter_t *fc) /* {{{ */
+static derive_t float_counter_get (const float_counter_t *fc, /* {{{ */
+    uint64_t factor)
 {
-  if (fc->n >= 500000000)
-    return ((derive_t) (fc->i + 1));
-  else
-    return ((derive_t) fc->i);
+  derive_t ret;
+
+  ret = (derive_t) (fc->i * factor);
+  ret += (derive_t) (fc->n / (1000000000 / factor));
+
+  return (ret);
 } /* }}} derive_t float_counter_get */
 
 static void strset (char **str, const char *new) /* {{{ */
@@ -186,10 +194,10 @@ static void service_statnode_add(const char *name, /* {{{ */
   node->mem_peak = NAN;
   
   /* fill query data */
-  strset(&node->name, name);
-  strset(&node->host, host);
-  strset(&node->server, server);
-  strset(&node->script, script);
+  strset (&node->name, name);
+  strset (&node->host, host);
+  strset (&node->server, server);
+  strset (&node->script, script);
   
   /* increment counter */
   stat_nodes_num++;
@@ -251,11 +259,16 @@ static void service_process_request (Pinba__Request *request) /* {{{ */
   
   for (i = 0; i < stat_nodes_num; i++)
   {
-    if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
+    if ((stat_nodes[i].host != NULL)
+        && (strcmp (request->hostname, stat_nodes[i].host) != 0))
       continue;
-    if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
+
+    if ((stat_nodes[i].server != NULL)
+      && (strcmp (request->server_name, stat_nodes[i].server) != 0))
       continue;
-    if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
+
+    if ((stat_nodes[i].script != NULL)
+      && (strcmp (request->script_name, stat_nodes[i].script) != 0))
       continue;
 
     service_statnode_process(&stat_nodes[i], request);
@@ -323,6 +336,7 @@ static int pb_add_socket (pinba_socket_t *s, /* {{{ */
     char errbuf[1024];
     ERROR ("pinba plugin: bind(2) failed: %s",
         sstrerror (errno, errbuf, sizeof (errbuf)));
+    close (fd);
     return (0);
   }
 
@@ -369,7 +383,7 @@ static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
   assert (ai_list != NULL);
 
   s = malloc (sizeof (*s));
-  if (s != NULL)
+  if (s == NULL)
   {
     freeaddrinfo (ai_list);
     ERROR ("pinba plugin: malloc failed.");
@@ -609,7 +623,7 @@ static int plugin_config (oconfig_item_t *ci) /* {{{ */
     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);
+      cf_util_get_service (child, &conf_service);
     else if (strcasecmp ("View", child->key) == 0)
       pinba_config_view (child);
     else
@@ -637,7 +651,7 @@ static int plugin_init (void) /* {{{ */
   if (collector_thread_running)
     return (0);
 
-  status = pthread_create (&collector_thread_id,
+  status = plugin_thread_create (&collector_thread_id,
       /* attrs = */ NULL,
       collector_thread,
       /* args = */ NULL);
@@ -689,19 +703,23 @@ static int plugin_submit (const pinba_statnode_t *res) /* {{{ */
   sstrncpy (vl.plugin_instance, res->name, sizeof (vl.plugin_instance));
 
   value.derive = res->req_count;
-  sstrncpy (vl.type, "requests", sizeof (vl.type)); 
+  sstrncpy (vl.type, "total_requests", sizeof (vl.type)); 
+  plugin_dispatch_values (&vl);
+
+  value.derive = float_counter_get (&res->req_time, /* factor = */ 1000);
+  sstrncpy (vl.type, "total_time_in_ms", sizeof (vl.type)); 
   plugin_dispatch_values (&vl);
 
-  value.derive = float_counter_get (&res->req_time);
-  sstrncpy (vl.type, "total_time", sizeof (vl.type)); 
+  value.derive = res->doc_size;
+  sstrncpy (vl.type, "total_bytes", sizeof (vl.type)); 
   plugin_dispatch_values (&vl);
 
-  value.derive = float_counter_get (&res->ru_utime);
+  value.derive = float_counter_get (&res->ru_utime, /* factor = */ 100);
   sstrncpy (vl.type, "cpu", sizeof (vl.type));
   sstrncpy (vl.type_instance, "user", sizeof (vl.type_instance));
   plugin_dispatch_values (&vl);
 
-  value.derive = float_counter_get (&res->ru_stime);
+  value.derive = float_counter_get (&res->ru_stime, /* factor = */ 100);
   sstrncpy (vl.type, "cpu", sizeof (vl.type));
   sstrncpy (vl.type_instance, "system", sizeof (vl.type_instance));
   plugin_dispatch_values (&vl);