2 * collectd - src/pinba.c (based on code from pinba_engine 0.0.5)
3 * Copyright (c) 2007-2009 Antony Dovgal
4 * Copyright (C) 2010 Phoenix Kayo
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Antony Dovgal <tony at daylessday.org>
21 * Phoenix Kayo <kayo.k11.4 at gmail.com>
24 #define _XOPEN_SOURCE 500
29 #include "configfile.h"
32 #include <sys/socket.h>
36 #include "pinba.pb-c.h"
38 typedef uint8_t u_char;
43 * Service declaration section
45 #ifndef PINBA_UDP_BUFFER_SIZE
46 # define PINBA_UDP_BUFFER_SIZE 65536
49 #ifndef PINBA_DEFAULT_ADDRESS
50 # define PINBA_DEFAULT_ADDRESS "127.0.0.1" /* FIXME */
53 #ifndef PINBA_DEFAULT_PORT
54 # define PINBA_DEFAULT_PORT 12345 /* FIXME */
57 #ifndef PINBA_MAX_SOCKETS
58 # define PINBA_MAX_SOCKETS 16
62 # define NI_MAXSERV 32
66 * Private data structures
68 typedef struct _pinba_statres_ pinba_statres;
69 struct _pinba_statres_ {
79 struct pinba_socket_s {
81 struct event *accept_event;
83 struct pollfd fd[PINBA_MAX_SOCKETS];
86 typedef struct pinba_socket_s pinba_socket_t;
88 typedef double pinba_time_t;
89 typedef uint32_t pinba_size_t;
91 static pinba_time_t now (void)
93 static struct timeval tv;
95 gettimeofday (&tv, /* tz = */ NULL);
97 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
100 static pthread_rwlock_t temp_lock;
102 static struct event_base *temp_base = NULL;
104 static pinba_socket_t *temp_sock = NULL;
106 static pthread_t temp_thrd;
108 typedef struct _pinba_statnode_ pinba_statnode;
109 struct _pinba_statnode_{
117 pinba_time_t last_coll;
118 pinba_size_t req_count;
119 pinba_time_t req_time;
120 pinba_time_t ru_utime;
121 pinba_time_t ru_stime;
122 pinba_size_t doc_size;
123 pinba_size_t mem_peak;
126 static unsigned int stat_nodes_count=0;
128 static pinba_statnode *stat_nodes = NULL;
130 char service_status=0;
131 char *service_address = PINBA_DEFAULT_ADDRESS;
132 unsigned int service_port=PINBA_DEFAULT_PORT;
134 static void service_statnode_reset (pinba_statnode *node) /* {{{ */
136 node->last_coll=now();
143 } /* }}} void service_statnode_reset */
145 static void strset (char **str, const char *new) /* {{{ */
158 } /* }}} void strset */
160 static void service_statnode_add(const char *name, /* {{{ */
165 pinba_statnode *node;
166 DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
168 stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode)*(stat_nodes_count+1));
170 ERROR("Realloc failed!");
174 node=&stat_nodes[stat_nodes_count];
176 /* reset stat data */
177 service_statnode_reset(node);
185 /* fill query data */
186 strset(&node->name, name);
187 strset(&node->host, host);
188 strset(&node->server, server);
189 strset(&node->script, script);
191 /* increment counter */
193 } /* }}} void service_statnode_add */
195 static void service_statnode_free (void)
199 if(stat_nodes_count < 1)
202 for (i = 0; i < stat_nodes_count; i++)
204 sfree (stat_nodes[i].name);
205 sfree (stat_nodes[i].host);
206 sfree (stat_nodes[i].server);
207 sfree (stat_nodes[i].script);
211 stat_nodes_count = 0;
213 pthread_rwlock_destroy (&temp_lock);
216 static void service_statnode_init (void)
218 /* only total info collect by default */
219 service_statnode_free();
221 DEBUG("initializing collector..");
222 pthread_rwlock_init(&temp_lock, 0);
225 static void service_statnode_begin (void)
227 service_statnode_init();
228 pthread_rwlock_wrlock(&temp_lock);
230 service_statnode_add("total", NULL, NULL, NULL);
233 static void service_statnode_end (void)
235 pthread_rwlock_unlock(&temp_lock);
238 static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
241 pinba_statnode* node;
244 if (stat_nodes_count == 0)
247 /* begin collecting */
249 pthread_rwlock_wrlock (&temp_lock);
252 if (index >= stat_nodes_count)
254 pthread_rwlock_unlock (&temp_lock);
258 node = stat_nodes + index;
259 delta = now() - node->last_coll;
261 res->name = node->name;
262 res->req_per_sec = node->req_count / delta;
264 if (node->req_count == 0)
267 res->req_time = node->req_time / node->req_count;
268 res->ru_utime = node->ru_utime / node->req_count;
269 res->ru_stime = node->ru_stime / node->req_count;
270 res->ru_stime = node->ru_stime / node->req_count;
271 res->doc_size = node->doc_size / node->req_count;
272 res->mem_peak = node->mem_peak / node->req_count;
274 service_statnode_reset (node);
276 } /* }}} unsigned int service_statnode_collect */
278 static void service_statnode_process (pinba_statnode *node,
279 Pinba__Request* request)
282 node->req_time+=request->request_time;
283 node->ru_utime+=request->ru_utime;
284 node->ru_stime+=request->ru_stime;
285 node->doc_size+=request->document_size;
286 node->mem_peak+=request->memory_peak;
289 static void service_process_request (Pinba__Request *request)
293 pthread_rwlock_wrlock (&temp_lock);
295 for (i = 0; i < stat_nodes_count; i++)
297 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
299 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
301 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
304 service_statnode_process(&stat_nodes[i], request);
307 pthread_rwlock_unlock(&temp_lock);
310 static void *pinba_main (void *arg)
312 DEBUG("entering listen-loop..");
315 event_base_dispatch(temp_base);
321 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
326 if (socket->listen_sock >= 0)
328 close(socket->listen_sock);
329 socket->listen_sock = -1;
332 if (socket->accept_event)
334 event_del(socket->accept_event);
335 free(socket->accept_event);
336 socket->accept_event = NULL;
340 } /* }}} void pinba_socket_free */
342 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
345 Pinba__Request *request;
347 request = pinba__request__unpack (NULL, buffer_size, buffer);
352 service_process_request(request);
353 pinba__request__free_unpacked (request, NULL);
356 } /* }}} int pinba_process_stats_packet */
358 static void pinba_udp_read_callback_fn (int sock, short event, void *arg) /* {{{ */
360 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
364 if ((event & EV_READ) == 0)
369 buffer_size = sizeof (buffer);
370 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
377 || (errno == EWOULDBLOCK)
379 || (errno == EAGAIN))
384 WARNING("pinba plugin: recvfrom(2) failed: %s",
385 sstrerror (errno, errbuf, sizeof (errbuf)));
388 else if (status == 0)
390 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
393 else /* if (status > 0) */
395 assert (((size_t) status) < buffer_size);
396 buffer_size = (size_t) status;
397 buffer[buffer_size] = 0;
399 status = pinba_process_stats_packet (buffer, buffer_size);
401 DEBUG("pinba plugin: Parsing packet failed.");
408 } /* }}} void pinba_udp_read_callback_fn */
410 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
411 const struct addrinfo *ai)
417 if (s->fd_num == PINBA_MAX_SOCKETS)
419 WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
420 "%i sockets. Please complain to the collectd developers so we can "
421 "raise the limit.", PINBA_MAX_SOCKETS);
425 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
429 ERROR ("pinba plugin: socket(2) failed: %s",
430 sstrerror (errno, errbuf, sizeof (errbuf)));
435 status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
439 WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
440 sstrerror (errno, errbuf, sizeof (errbuf)));
443 status = bind (fd, ai->ai_addr, ai->ai_addrlen);
447 ERROR ("pinba plugin: bind(2) failed: %s",
448 sstrerror (errno, errbuf, sizeof (errbuf)));
452 s->fd[s->fd_num].fd = fd;
453 s->fd[s->fd_num].events = POLLIN | POLLPRI;
454 s->fd[s->fd_num].revents = 0;
458 } /* }}} int pb_add_socket */
460 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
464 struct addrinfo *ai_list;
465 struct addrinfo *ai_ptr;
466 struct addrinfo ai_hints;
469 char service[NI_MAXSERV]; /* FIXME */
470 snprintf (service, sizeof (service), "%i", listen_port);
472 memset (&ai_hints, 0, sizeof (ai_hints));
473 ai_hints.ai_flags = AI_PASSIVE;
474 ai_hints.ai_family = AF_UNSPEC;
475 ai_hints.ai_socktype = SOCK_DGRAM;
476 ai_hints.ai_addr = NULL;
477 ai_hints.ai_canonname = NULL;
478 ai_hints.ai_next = NULL;
481 status = getaddrinfo (node, service,
482 &ai_hints, &ai_list);
485 ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
486 gai_strerror (status));
489 assert (ai_list != NULL);
491 s = malloc (sizeof (*s));
494 freeaddrinfo (ai_list);
495 ERROR ("pinba plugin: malloc failed.");
498 memset (s, 0, sizeof (*s));
500 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
502 status = pb_add_socket (s, ai_ptr);
505 } /* for (ai_list) */
507 freeaddrinfo (ai_list);
511 WARNING ("pinba plugin: Unable to open socket for address %s.", node);
517 } /* }}} pinba_socket_open */
519 static int service_cleanup (void)
521 DEBUG("closing socket..");
523 pthread_rwlock_wrlock(&temp_lock);
524 pinba_socket_free(temp_sock);
525 pthread_rwlock_unlock(&temp_lock);
528 DEBUG("shutdowning event..");
529 event_base_free(temp_base);
531 DEBUG("shutting down..");
536 static int service_start(void)
538 DEBUG("starting up..");
540 DEBUG("initializing event..");
541 temp_base = event_base_new();
543 DEBUG("opening socket..");
545 temp_sock = pinba_socket_open(service_address, service_port);
552 if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
560 static int service_stop (void)
562 pthread_cancel(temp_thrd);
563 pthread_join(temp_thrd, NULL);
565 DEBUG("terminating listen-loop..");
572 static void service_config (const char *address, unsigned int port) /* {{{ */
574 int need_restart = 0;
576 if (address && service_address && (strcmp(service_address, address) != 0))
578 strset (&service_address, address);
582 if ((port > 0) && (port < 65536) && (service_port != port))
588 if(service_status && need_restart)
593 } /* }}} void service_config */
596 * Plugin declaration section
599 static int config_set (char **var, const char *value)
601 /* code from nginx plugin for collectd */
607 if ((*var = strdup (value)) == NULL) return (1);
611 static int plugin_config (oconfig_item_t *ci)
615 char *pinba_address = NULL;
617 INFO("Pinba Configure..");
619 service_statnode_begin();
621 /* Set default values */
622 config_set(&pinba_address, PINBA_DEFAULT_ADDRESS);
623 pinba_port = PINBA_DEFAULT_PORT;
625 for (i = 0; i < ci->children_num; i++) {
626 oconfig_item_t *child = ci->children + i;
627 if (strcasecmp ("Address", child->key) == 0) {
628 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
629 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
632 config_set(&pinba_address, child->values[0].value.string);
633 } else if (strcasecmp ("Port", child->key) == 0) {
634 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_NUMBER)){
635 WARNING ("pinba plugin: `Port' needs exactly one number argument.");
638 pinba_port=child->values[0].value.number;
639 } else if (strcasecmp ("View", child->key) == 0) {
640 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
641 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
642 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
645 name = child->values[0].value.string;
646 for(o=0; o<child->children_num; o++){
647 oconfig_item_t *node = child->children + o;
648 if (strcasecmp ("Host", node->key) == 0) {
649 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
650 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
653 host = node->values[0].value.string;
654 } else if (strcasecmp ("Server", node->key) == 0) {
655 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
656 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
659 server = node->values[0].value.string;
660 } else if (strcasecmp ("Script", node->key) == 0) {
661 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
662 WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
665 script = node->values[0].value.string;
667 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
671 /* add new statnode */
672 service_statnode_add(name, host, server, script);
674 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
679 service_statnode_end();
681 service_config(pinba_address, pinba_port);
682 } /* int pinba_config */
684 static int plugin_init (void)
686 INFO("Pinba Starting..");
691 static int plugin_shutdown (void)
693 INFO("Pinba Stopping..");
695 service_statnode_free();
699 static int plugin_submit (const char *plugin_instance,
701 const pinba_statres *res) {
703 value_list_t vl = VALUE_LIST_INIT;
705 values[0].gauge = res->req_per_sec;
706 values[1].gauge = res->req_time;
707 values[2].gauge = res->ru_utime;
708 values[3].gauge = res->ru_stime;
709 values[4].gauge = res->doc_size;
710 values[5].gauge = res->mem_peak;
714 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
715 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
716 sstrncpy (vl.plugin_instance, plugin_instance,
717 sizeof(vl.plugin_instance));
718 sstrncpy (vl.type, type, sizeof (vl.type));
719 INFO("Pinba Dispatch");
720 plugin_dispatch_values (&vl);
725 static int plugin_read (void)
728 static pinba_statres res;
730 while ((i = service_statnode_collect (&res, i)) != 0)
732 plugin_submit(res.name, "pinba_view", &res);
738 void module_register (void)
740 plugin_register_complex_config ("pinba", plugin_config);
741 plugin_register_init ("pinba", plugin_init);
742 plugin_register_read ("pinba", plugin_read);
743 plugin_register_shutdown ("pinba", plugin_shutdown);
744 } /* void module_register */
746 /* vim: set sw=2 sts=2 et fdm=marker : */