#include <pthread.h>
#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include <netdb.h>
+#include <poll.h>
#include "pinba.pb-c.h"
# define PINBA_DEFAULT_PORT 12345 /* FIXME */
#endif
+#ifndef PINBA_MAX_SOCKETS
+# define PINBA_MAX_SOCKETS 16
+#endif
+
+#ifndef NI_MAXSERV
+# define NI_MAXSERV 32
+#endif
+
/*
* Private data structures
*/
double mem_peak;
};
-typedef struct _pinba_socket_ pinba_socket;
-struct _pinba_socket_ {
+struct pinba_socket_s {
int listen_sock;
struct event *accept_event;
+
+ struct pollfd fd[PINBA_MAX_SOCKETS];
+ nfds_t fd_num;
};
+typedef struct pinba_socket_s pinba_socket_t;
-typedef double pinba_time;
-typedef uint32_t pinba_size;
+typedef double pinba_time_t;
+typedef uint32_t pinba_size_t;
-static pinba_time now (void)
+static pinba_time_t now (void)
{
static struct timeval tv;
static struct event_base *temp_base = NULL;
-static pinba_socket *temp_sock = NULL;
+static pinba_socket_t *temp_sock = NULL;
static pthread_t temp_thrd;
char *server;
char *script;
/* collected data */
- pinba_time last_coll;
- pinba_size req_count;
- pinba_time req_time;
- pinba_time ru_utime;
- pinba_time ru_stime;
- pinba_size doc_size;
- pinba_size mem_peak;
+ pinba_time_t last_coll;
+ pinba_size_t req_count;
+ pinba_time_t req_time;
+ pinba_time_t ru_utime;
+ pinba_time_t ru_stime;
+ pinba_size_t doc_size;
+ pinba_size_t mem_peak;
};
static unsigned int stat_nodes_count=0;
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_t 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;
+ node = stat_nodes + index;
+ delta = now() - node->last_coll;
- res->name=node->name;
+ res->name = node->name;
+ res->req_per_sec = node->req_count / delta;
- 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)
return NULL;
}
-static void pinba_socket_free (pinba_socket *socket) /* {{{ */
+static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
{
if (!socket)
return;
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);
+ if (!request)
+ return (-1);
+
+ service_process_request(request);
+ pinba__request__free_unpacked (request, NULL);
- return P_SUCCESS;
- }
-}
+ return (0);
+} /* }}} int pinba_process_stats_packet */
-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];
- struct sockaddr_in from;
- socklen_t fromlen = sizeof(struct sockaddr_in);
-
- 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;
+ 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 pinba_socket *pinba_socket_open (const char *ip, /* {{{ */
- int listen_port)
+ /* not reached */
+ assert (23 == 42);
+ } /* while (42) */
+} /* }}} void pinba_udp_read_callback_fn */
+
+static int pb_add_socket (pinba_socket_t *s, /* {{{ */
+ const struct addrinfo *ai)
{
- struct sockaddr_in addr;
- pinba_socket *s;
- int sfd, flags, yes = 1;
-
- if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
- ERROR("socket() failed: %s (%d)", strerror(errno), errno);
- return NULL;
+ int fd;
+ int tmp;
+ int status;
+
+ if (s->fd_num == PINBA_MAX_SOCKETS)
+ {
+ WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
+ "%i sockets. Please complain to the collectd developers so we can "
+ "raise the limit.", PINBA_MAX_SOCKETS);
+ return (-1);
}
-
- if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
- close(sfd);
- return NULL;
+
+ fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (fd < 0)
+ {
+ char errbuf[1024];
+ ERROR ("pinba plugin: socket(2) failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (0);
}
-
- if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
- close(sfd);
- return NULL;
+
+ tmp = 1;
+ status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
+ if (status != 0)
+ {
+ char errbuf[1024];
+ WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
}
-
- s = (pinba_socket *)calloc(1, sizeof(pinba_socket));
- if (!s) {
- return NULL;
+
+ status = bind (fd, ai->ai_addr, ai->ai_addrlen);
+ if (status != 0)
+ {
+ char errbuf[1024];
+ ERROR ("pinba plugin: bind(2) failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (0);
}
- s->listen_sock = sfd;
-
- memset(&addr, 0, sizeof(addr));
-
- addr.sin_family = AF_INET;
- addr.sin_port = htons(listen_port);
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
-
- if (ip && *ip) {
- struct in_addr tmp;
-
- if (inet_aton(ip, &tmp)) {
- addr.sin_addr.s_addr = tmp.s_addr;
- } else {
- WARNING("inet_aton(%s) failed, listening on ANY IP-address", ip);
- }
+
+ s->fd[s->fd_num].fd = fd;
+ s->fd[s->fd_num].events = POLLIN | POLLPRI;
+ s->fd[s->fd_num].revents = 0;
+ s->fd_num++;
+
+ return (0);
+} /* }}} int pb_add_socket */
+
+static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
+ int listen_port)
+{
+ pinba_socket_t *s;
+ struct addrinfo *ai_list;
+ struct addrinfo *ai_ptr;
+ struct addrinfo ai_hints;
+ int status;
+
+ char service[NI_MAXSERV]; /* FIXME */
+ snprintf (service, sizeof (service), "%i", listen_port);
+
+ memset (&ai_hints, 0, sizeof (ai_hints));
+ ai_hints.ai_flags = AI_PASSIVE;
+ ai_hints.ai_family = AF_UNSPEC;
+ ai_hints.ai_socktype = SOCK_DGRAM;
+ ai_hints.ai_addr = NULL;
+ ai_hints.ai_canonname = NULL;
+ ai_hints.ai_next = NULL;
+
+ ai_list = NULL;
+ status = getaddrinfo (node, service,
+ &ai_hints, &ai_list);
+ if (status != 0)
+ {
+ ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
+ gai_strerror (status));
+ return (NULL);
}
-
- if (bind(s->listen_sock, (struct sockaddr *)&addr, sizeof(addr))) {
- pinba_socket_free(s);
- ERROR("bind() failed: %s (%d)", strerror(errno), errno);
- return NULL;
+ assert (ai_list != NULL);
+
+ s = malloc (sizeof (*s));
+ if (s != NULL)
+ {
+ freeaddrinfo (ai_list);
+ ERROR ("pinba plugin: malloc failed.");
+ return (NULL);
}
+ memset (s, 0, sizeof (*s));
+
+ for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
+ {
+ status = pb_add_socket (s, ai_ptr);
+ if (status != 0)
+ break;
+ } /* for (ai_list) */
- s->accept_event = (struct event *)calloc(1, sizeof(struct event));
- if (!s->accept_event) {
- ERROR("calloc() failed: %s (%d)", strerror(errno), errno);
- pinba_socket_free(s);
- return NULL;
+ freeaddrinfo (ai_list);
+
+ if (s->fd_num < 1)
+ {
+ WARNING ("pinba plugin: Unable to open socket for address %s.", node);
+ sfree (s);
+ s = NULL;
}
-
- event_set(s->accept_event, s->listen_sock, EV_READ | EV_PERSIST, pinba_udp_read_callback_fn, s);
- event_base_set(temp_base, s->accept_event);
- event_add(s->accept_event, NULL);
-
- return s;
-} /* }}} */
+
+ return (s);
+} /* }}} pinba_socket_open */
static int service_cleanup (void)
{
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];
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 : */