pinba plugin: pinba_udp_read_callback_fn: Streamlined the function a bit.
[collectd.git] / src / pinba.c
index 6b46ee0..be8ed57 100644 (file)
@@ -1,5 +1,6 @@
 /**
- * collectd - src/pinba.c
+ * collectd - src/pinba.c (based on code from pinba_engine 0.0.5)
+ * Copyright (c) 2007-2009  Antony Dovgal
  * Copyright (C) 2010       Phoenix Kayo
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -16,6 +17,7 @@
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  *
  * Authors:
+ *   Antony Dovgal <tony at daylessday.org>
  *   Phoenix Kayo <kayo.k11.4 at gmail.com>
  **/
 
@@ -33,6 +35,8 @@
 
 #include "pinba.pb-c.h"
 
+typedef uint8_t u_char;
+
 #include <event.h>
 
 /*
 # define PINBA_UDP_BUFFER_SIZE 65536
 #endif
 
+#ifndef PINBA_DEFAULT_ADDRESS
+# define PINBA_DEFAULT_ADDRESS "127.0.0.1" /* FIXME */
+#endif
+
+#ifndef PINBA_DEFAULT_PORT
+# define PINBA_DEFAULT_PORT 12345 /* FIXME */
+#endif
+
+/*
+ * Private data structures
+ */
 typedef struct _pinba_statres_ pinba_statres;
 struct _pinba_statres_ {
   const char *name;
@@ -62,7 +77,8 @@ struct _pinba_socket_ {
 typedef double pinba_time;
 typedef uint32_t pinba_size;
 
-static pinba_time now(){
+static pinba_time now (void)
+{
   static struct timeval tv;
   
   gettimeofday (&tv, /* tz = */ NULL);
@@ -104,8 +120,8 @@ char service_status=0;
 char *service_address = PINBA_DEFAULT_ADDRESS;
 unsigned int service_port=PINBA_DEFAULT_PORT;
 
-static void
-service_statnode_reset(pinba_statnode *node){
+static void service_statnode_reset (pinba_statnode *node) /* {{{ */
+{
   node->last_coll=now();
   node->req_count=0;
   node->req_time=0.0;
@@ -113,9 +129,9 @@ service_statnode_reset(pinba_statnode *node){
   node->ru_stime=0.0;
   node->doc_size=0;
   node->mem_peak=0;
-}
+} /* }}} void service_statnode_reset */
 
-static void strset (char **str, const char *new)
+static void strset (char **str, const char *new) /* {{{ */
 {
   char *tmp;
 
@@ -128,13 +144,13 @@ static void strset (char **str, const char *new)
 
   sfree (*str);
   *str = tmp;
-} /* void strset */
+} /* }}} void strset */
 
-static void
-service_statnode_add(const char *name,
-                    const char *host,
-                    const char *server,
-                    const char *script){
+static void service_statnode_add(const char *name, /* {{{ */
+    const char *host,
+    const char *server,
+    const char *script)
+{
   pinba_statnode *node;
   DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
   
@@ -163,7 +179,7 @@ service_statnode_add(const char *name,
   
   /* increment counter */
   stat_nodes_count++;
-}
+} /* }}} void service_statnode_add */
 
 static void service_statnode_free (void)
 {
@@ -186,8 +202,8 @@ static void service_statnode_free (void)
   pthread_rwlock_destroy (&temp_lock);
 }
 
-static void
-service_statnode_init(){
+static void service_statnode_init (void)
+{
   /* only total info collect by default */
   service_statnode_free();
   
@@ -195,62 +211,62 @@ service_statnode_init(){
   pthread_rwlock_init(&temp_lock, 0);
 }
 
-static void
-service_statnode_begin(){
+static void service_statnode_begin (void)
+{
   service_statnode_init();
   pthread_rwlock_wrlock(&temp_lock);
   
   service_statnode_add("total", NULL, NULL, NULL);
 }
 
-static void
-service_statnode_end(){
+static void service_statnode_end (void)
+{
   pthread_rwlock_unlock(&temp_lock);
 }
 
-static unsigned int
-service_statnode_collect(pinba_statres *res, unsigned int i){
+static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
+    unsigned int index)
+{
   pinba_statnode* node;
+  pinba_time delta;
   
-  if(stat_nodes_count==0) return 0;
+  if (stat_nodes_count == 0)
+    return 0;
   
   /* begin collecting */
-  if(i==0){
-    pthread_rwlock_wrlock(&temp_lock);
-  }
-  
-  /* find non-empty node */
-  //for(node=stat_nodes+i; node->req_count==0 && ++i<stat_nodes_count; node=stat_nodes+i);
+  if (index == 0)
+    pthread_rwlock_wrlock (&temp_lock);
   
   /* end collecting */
-  if(i>=stat_nodes_count){
-    pthread_rwlock_unlock(&temp_lock);
+  if (index >= stat_nodes_count)
+  {
+    pthread_rwlock_unlock (&temp_lock);
     return 0;
   }
   
-  node=stat_nodes+i;
+  node = stat_nodes + index;
+  delta = now() - node->last_coll;
   
-  pinba_time delta=now()-node->last_coll;
+  res->name = node->name;
+  res->req_per_sec = node->req_count / delta;
   
-  res->name=node->name;
-  
-  res->req_per_sec=node->req_count/delta;
-  
-  if(node->req_count==0)node->req_count=1;
-  res->req_time=node->req_time/node->req_count;
-  res->ru_utime=node->ru_utime/node->req_count;
-  res->ru_stime=node->ru_stime/node->req_count;
-  res->ru_stime=node->ru_stime/node->req_count;
-  res->doc_size=node->doc_size/node->req_count;
-  res->mem_peak=node->mem_peak/node->req_count;
+  if (node->req_count == 0)
+    node->req_count = 1;
+
+  res->req_time = node->req_time / node->req_count;
+  res->ru_utime = node->ru_utime / node->req_count;
+  res->ru_stime = node->ru_stime / node->req_count;
+  res->ru_stime = node->ru_stime / node->req_count;
+  res->doc_size = node->doc_size / node->req_count;
+  res->mem_peak = node->mem_peak / node->req_count;
   
-  service_statnode_reset(node);
-  return ++i;
-}
+  service_statnode_reset (node);
+  return (index + 1);
+} /* }}} unsigned int service_statnode_collect */
 
-static void
-service_statnode_process(pinba_statnode *node,
-                        Pinba__Request* request){
+static void service_statnode_process (pinba_statnode *node,
+    Pinba__Request* request)
+{
   node->req_count++;
   node->req_time+=request->request_time;
   node->ru_utime+=request->ru_utime;
@@ -259,7 +275,7 @@ service_statnode_process(pinba_statnode *node,
   node->mem_peak+=request->memory_peak;
 }
 
-static void service_process_request(Pinba__Request *request)
+static void service_process_request (Pinba__Request *request)
 {
   unsigned int i;
 
@@ -280,36 +296,8 @@ static void service_process_request(Pinba__Request *request)
   pthread_rwlock_unlock(&temp_lock);
 }
 
-static void
-service_config(const char* address,
-              unsigned int port){
-  char need_restart=0;
-  
-  if(address && (service_address && strcmp(service_address, address))){
-    strset(&service_address, address);
-    need_restart++;
-  }
-  
-  if(port>0 && port<65536 && service_port!=port){
-    service_port=port;
-    need_restart++;
-  }
-  
-  if(service_status && need_restart){
-    service_stop();
-    service_start();
-  }
-}
-
-static void
-pinba_socket_free (pinba_socket *socket);
-
-static pinba_socket *
-pinba_socket_open (const char *ip,
-                  int listen_port);
-
-static void *
-pinba_main(void *arg){
+static void *pinba_main (void *arg)
+{
   DEBUG("entering listen-loop..");
   
   service_status=1;
@@ -319,122 +307,98 @@ pinba_main(void *arg){
   return NULL;
 }
 
-static int
-service_cleanup(){
-  DEBUG("closing socket..");
-  if(temp_sock){
-    pthread_rwlock_wrlock(&temp_lock);
-    pinba_socket_free(temp_sock);
-    pthread_rwlock_unlock(&temp_lock);
-  }
-  
-  DEBUG("shutdowning event..");
-  event_base_free(temp_base);
-  
-  DEBUG("shutting down..");
-}
-
-static int
-service_start(){
-  DEBUG("starting up..");
-  
-  DEBUG("initializing event..");
-  temp_base = event_base_new();
-  
-  DEBUG("opening socket..");
-  
-  temp_sock = pinba_socket_open(service_address, service_port);
+static void pinba_socket_free (pinba_socket *socket) /* {{{ */
+{
+  if (!socket)
+    return;
   
-  if (!temp_sock) {
-    service_cleanup();
-    return 1;
+  if (socket->listen_sock >= 0)
+  {
+    close(socket->listen_sock);
+    socket->listen_sock = -1;
   }
   
-  if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
-    service_cleanup();
-    return 1;
+  if (socket->accept_event)
+  {
+    event_del(socket->accept_event);
+    free(socket->accept_event);
+    socket->accept_event = NULL;
   }
   
-  return 0;
-}
-
-static int
-service_stop(){
-  pthread_cancel(temp_thrd);
-  pthread_join(temp_thrd, NULL);
-  service_status=0;
-  DEBUG("terminating listen-loop..");
-  
-  service_cleanup();
-  
-  return 0;
-}
+  free(socket);
+} /* }}} void pinba_socket_free */
 
-static int
-pinba_process_stats_packet (const unsigned char *buf,
-                           int buf_len) {
+static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
+    size_t buffer_size)
+{
   Pinba__Request *request;  
   
-  request = pinba__request__unpack(NULL, buf_len, buf);
+  request = pinba__request__unpack (NULL, buffer_size, buffer);
   
-  if (!request) {
-    return P_FAILURE;
-  } else {
-    service_process_request(request);
-    
-    pinba__request__free_unpacked(request, NULL);
-    
-    return P_SUCCESS;
-  }
-}
+  if (!request)
+    return (-1);
 
-static void
-pinba_udp_read_callback_fn (int sock,
-                           short event,
-                           void *arg) {
-  if (event & EV_READ) {
-    int ret;
-    unsigned char buf[PINBA_UDP_BUFFER_SIZE];
-    struct sockaddr_in from;
-    socklen_t fromlen = sizeof(struct sockaddr_in);
+  service_process_request(request);
+  pinba__request__free_unpacked (request, NULL);
     
-    ret = recvfrom(sock, buf, PINBA_UDP_BUFFER_SIZE-1, MSG_DONTWAIT, (struct sockaddr *)&from, &fromlen);
-    if (ret > 0) {
-      if (pinba_process_stats_packet(buf, ret) != P_SUCCESS) {
-       DEBUG("failed to parse data received from %s", inet_ntoa(from.sin_addr));
-      }
-    } else if (ret < 0) {
-      if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
-       return;
+  return (0);
+} /* }}} int pinba_process_stats_packet */
+
+static void pinba_udp_read_callback_fn (int sock, short event, void *arg) /* {{{ */
+{
+  uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
+  size_t buffer_size;
+  int status;
+
+  if ((event & EV_READ) == 0)
+    return;
+
+  while (42)
+  {
+    buffer_size = sizeof (buffer);
+    status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
+    if (status < 0)
+    {
+      char errbuf[1024];
+
+      if ((errno == EINTR)
+#ifdef EWOULDBLOCK
+          || (errno == EWOULDBLOCK)
+#endif
+          || (errno == EAGAIN))
+      {
+        continue;
       }
-      WARNING("recv() failed: %s (%d)", strerror(errno), errno);
-    } else {
-      WARNING("recv() returned 0");
+
+      WARNING("pinba plugin: recvfrom(2) failed: %s",
+          sstrerror (errno, errbuf, sizeof (errbuf)));
+      return;
+    }
+    else if (status == 0)
+    {
+      DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
+      return;
+    }
+    else /* if (status > 0) */
+    {
+      assert (((size_t) status) < buffer_size);
+      buffer_size = (size_t) status;
+      buffer[buffer_size] = 0;
+
+      status = pinba_process_stats_packet (buffer, buffer_size);
+      if (status != 0)
+        DEBUG("pinba plugin: Parsing packet failed.");
+      return;
     }
-  }
-}
 
-static void
-pinba_socket_free (pinba_socket *socket) {
-  if (!socket) return;
-  
-  if (socket->listen_sock >= 0) {
-    close(socket->listen_sock);
-    socket->listen_sock = -1;
-  }
-  
-  if (socket->accept_event) {
-    event_del(socket->accept_event);
-    free(socket->accept_event);
-    socket->accept_event = NULL;
-  }
-  
-  free(socket);
-}
+    /* not reached */
+    assert (23 == 42);
+  } /* while (42) */
+} /* }}} void pinba_udp_read_callback_fn */
 
-static pinba_socket *
-pinba_socket_open (const char *ip,
-                  int listen_port) {
+static pinba_socket *pinba_socket_open (const char *ip, /* {{{ */
+    int listen_port)
+{
   struct sockaddr_in addr;
   pinba_socket *s;
   int sfd, flags, yes = 1;
@@ -494,16 +458,90 @@ pinba_socket_open (const char *ip,
   event_add(s->accept_event, NULL);
   
   return s;
+} /* }}} pinba_socket_open */
+
+static int service_cleanup (void)
+{
+  DEBUG("closing socket..");
+  if(temp_sock){
+    pthread_rwlock_wrlock(&temp_lock);
+    pinba_socket_free(temp_sock);
+    pthread_rwlock_unlock(&temp_lock);
+  }
+  
+  DEBUG("shutdowning event..");
+  event_base_free(temp_base);
+  
+  DEBUG("shutting down..");
+
+  return (0);
+}
+
+static int service_start(void)
+{
+  DEBUG("starting up..");
+  
+  DEBUG("initializing event..");
+  temp_base = event_base_new();
+  
+  DEBUG("opening socket..");
+  
+  temp_sock = pinba_socket_open(service_address, service_port);
+  
+  if (!temp_sock) {
+    service_cleanup();
+    return 1;
+  }
+  
+  if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
+    service_cleanup();
+    return 1;
+  }
+  
+  return 0;
 }
 
+static int service_stop (void)
+{
+  pthread_cancel(temp_thrd);
+  pthread_join(temp_thrd, NULL);
+  service_status=0;
+  DEBUG("terminating listen-loop..");
+  
+  service_cleanup();
+  
+  return 0;
+}
+
+static void service_config (const char *address, unsigned int port) /* {{{ */
+{
+  int need_restart = 0;
+
+  if (address && service_address && (strcmp(service_address, address) != 0))
+  {
+    strset (&service_address, address);
+    need_restart++;
+  }
+
+  if ((port > 0) && (port < 65536) && (service_port != port))
+  {
+    service_port=port;
+    need_restart++;
+  }
+
+  if(service_status && need_restart)
+  {
+    service_stop();
+    service_start();
+  }
+} /* }}} void service_config */
+
 /*
  * Plugin declaration section
  */
 
-#include<stdint.h>
-
-static int
-config_set (char **var, const char *value) {
+static int config_set (char **var, const char *value)
+{
   /* code from nginx plugin for collectd */
   if (*var != NULL) {
     free (*var);
@@ -514,8 +552,8 @@ config_set (char **var, const char *value) {
   else return (0);
 }
 
-static int
-plugin_config (oconfig_item_t *ci) {
+static int plugin_config (oconfig_item_t *ci)
+{
   unsigned int i, o;
   int pinba_port = 0;
   char *pinba_address = NULL;
@@ -587,23 +625,22 @@ plugin_config (oconfig_item_t *ci) {
   service_config(pinba_address, pinba_port);
 } /* int pinba_config */
 
-static int
-plugin_init (void) {
+static int plugin_init (void)
+{
   INFO("Pinba Starting..");
   service_start();
   return 0;
 }
 
-static int
-plugin_shutdown (void) {
+static int plugin_shutdown (void)
+{
   INFO("Pinba Stopping..");
   service_stop();
   service_statnode_free();
   return 0;
 }
 
-static int
-plugin_submit (const char *plugin_instance,
+static int plugin_submit (const char *plugin_instance,
               const char *type,
               const pinba_statres *res) {
   value_t values[6];
@@ -642,11 +679,12 @@ static int plugin_read (void)
   return 0;
 }
 
-void module_register (void) {
+void module_register (void)
+{
   plugin_register_complex_config ("pinba", plugin_config);
   plugin_register_init ("pinba", plugin_init);
   plugin_register_read ("pinba", plugin_read);
   plugin_register_shutdown ("pinba", plugin_shutdown);
 } /* void module_register */
 
-/* vim: set sw=2 sts=2 et : */
+/* vim: set sw=2 sts=2 et fdm=marker : */