/**
- * 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
* 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>
**/
+#define _XOPEN_SOURCE 500
+
#include "collectd.h"
#include "common.h"
#include "plugin.h"
#include "configfile.h"
+#include <pthread.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "pinba.pb-c.h"
+
+typedef uint8_t u_char;
+
+#include <event.h>
+
/*
* Service declaration section
*/
-
#ifndef PINBA_UDP_BUFFER_SIZE
-#define PINBA_UDP_BUFFER_SIZE 65536
+# define PINBA_UDP_BUFFER_SIZE 65536
#endif
-typedef struct _pinba_statres_ pinba_statres;
-struct _pinba_statres_ {
- const char *name;
- float req_per_sec;
- float req_time;
- float ru_utime;
- float ru_stime;
- float doc_size;
- float mem_peak;
-};
+#ifndef PINBA_DEFAULT_ADDRESS
+# define PINBA_DEFAULT_ADDRESS "127.0.0.1" /* FIXME */
+#endif
-/*
- * Fetch collection datas
- *
- * Return value: id of next data, zero if no data.
- */
-static unsigned int
-service_statnode_collect(pinba_statres *res,
- unsigned int id);
-static void
-service_statnode_begin();
-static void
-service_statnode_end();
-static void
-service_statnode_free();
-static void
-service_statnode_init();
-static void
-service_statnode_add(const char *name,
- const char *host,
- const char *server,
- const char *script);
-
-static void
-service_config(const char* address,
- unsigned int port);
-static int
-service_start();
-static int
-service_stop();
+#ifndef PINBA_DEFAULT_PORT
+# define PINBA_DEFAULT_PORT 12345 /* FIXME */
+#endif
/*
- * Service implementation section
+ * Private data structures
*/
-
-#include"pinba.pb-c.h"
-
-#include<stdarg.h>
-#include<stdlib.h>
-#include<stdio.h>
-#include<string.h>
-#include<unistd.h>
-#include<errno.h>
-#include<fcntl.h>
-
-#include<sys/types.h>
-#include<math.h>
-#include<pthread.h>
-
-#include<sys/time.h>
-#include<event.h>
-
-#include<netinet/in.h>
-#include<arpa/inet.h>
-#include<sys/socket.h>
+typedef struct _pinba_statres_ pinba_statres;
+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;
+};
typedef struct _pinba_socket_ pinba_socket;
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;
- static struct timezone tz;
- gettimeofday(&tv, &tz);
+ gettimeofday (&tv, /* tz = */ NULL);
return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
}
-static pthread_rwlock_t
-temp_lock;
+static pthread_rwlock_t temp_lock;
-static struct event_base *
-temp_base=NULL;
+static struct event_base *temp_base = NULL;
-static pinba_socket *
-temp_sock=NULL;
+static pinba_socket *temp_sock = NULL;
-static pthread_t
-temp_thrd;
+static pthread_t temp_thrd;
typedef struct _pinba_statnode_ pinba_statnode;
struct _pinba_statnode_{
pinba_size mem_peak;
};
-static unsigned int
-stat_nodes_count=0;
+static unsigned int stat_nodes_count=0;
-static pinba_statnode *
-stat_nodes=NULL;
+static pinba_statnode *stat_nodes = NULL;
-static void
-service_statnode_reset(pinba_statnode *node){
+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) /* {{{ */
+{
node->last_coll=now();
node->req_count=0;
node->req_time=0.0;
node->ru_stime=0.0;
node->doc_size=0;
node->mem_peak=0;
-}
+} /* }}} void service_statnode_reset */
-static inline void
-strdel(char **str){
- if(*str)free(*str);
- *str=NULL;
-}
+static void strset (char **str, const char *new) /* {{{ */
+{
+ char *tmp;
-static inline void
-strset(char **str, const char *new){
- strdel(str);
- new && (*str=strdup(new));
-}
+ if (!str || !new)
+ return;
+
+ tmp = strdup (new);
+ if (tmp == NULL)
+ return;
+
+ sfree (*str);
+ *str = tmp;
+} /* }}} 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:"");
/* increment counter */
stat_nodes_count++;
-}
+} /* }}} void service_statnode_add */
-static void
-service_statnode_free(){
- if(stat_nodes_count){
- DEBUG("shutdowning collector..");
- unsigned int i;
-
- for(i=0;i<stat_nodes_count;i++){
- /* free strings */
- strdel(&stat_nodes[i].name);
- strdel(&stat_nodes[i].host);
- strdel(&stat_nodes[i].server);
- strdel(&stat_nodes[i].script);
- }
-
- free(stat_nodes);
- stat_nodes_count=0;
-
- pthread_rwlock_destroy(&temp_lock);
+static void service_statnode_free (void)
+{
+ unsigned int i;
+
+ if(stat_nodes_count < 1)
+ return;
+
+ for (i = 0; i < stat_nodes_count; i++)
+ {
+ sfree (stat_nodes[i].name);
+ sfree (stat_nodes[i].host);
+ sfree (stat_nodes[i].server);
+ sfree (stat_nodes[i].script);
}
+
+ sfree (stat_nodes);
+ stat_nodes_count = 0;
+
+ 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();
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;
-
- pinba_time delta=now()-node->last_coll;
-
- res->name=node->name;
+ node = stat_nodes + index;
+ delta = now() - node->last_coll;
- 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;
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;
- pthread_rwlock_wrlock(&temp_lock);
+
+ pthread_rwlock_wrlock (&temp_lock);
- DEBUG("Process request..");
- for(i=0;i<stat_nodes_count;i++){
- if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))continue;
- if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))continue;
- if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))continue;
+ for (i = 0; i < stat_nodes_count; i++)
+ {
+ if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
+ continue;
+ if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
+ continue;
+ if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
+ continue;
+
service_statnode_process(&stat_nodes[i], request);
}
pthread_rwlock_unlock(&temp_lock);
}
-char
-service_status=0;
-
-char *
-service_address=PINBA_DEFAULT_ADDRESS;
-
-unsigned int
-service_port=PINBA_DEFAULT_PORT;
-
-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;
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);
- }
+static void pinba_socket_free (pinba_socket *socket) /* {{{ */
+{
+ if (!socket)
+ return;
- 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);
-
- 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 unsigned char *buf,
+ int buf_len)
+{
Pinba__Request *request;
request = pinba__request__unpack(NULL, buf_len, buf);
}
}
-static void
-pinba_udp_read_callback_fn (int sock,
- short event,
- void *arg) {
+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];
}
}
-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);
-}
-
-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;
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);
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;
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];
sstrncpy (vl.type, type, sizeof (vl.type));
INFO("Pinba Dispatch");
plugin_dispatch_values (&vl);
+
+ return (0);
}
-static int
-plugin_read (void) {
- DEBUG("Pinba Read..");
+static int plugin_read (void)
+{
unsigned int i=0;
static pinba_statres res;
- for(; i=service_statnode_collect(&res, i); ){
+ while ((i = service_statnode_collect (&res, i)) != 0)
+ {
plugin_submit(res.name, "pinba_view", &res);
}
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 fdm=marker : */