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>
27 #include "configfile.h"
30 #include <sys/socket.h>
34 #include "pinba.pb-c.h"
36 typedef uint8_t u_char;
41 * Service declaration section
43 #ifndef PINBA_UDP_BUFFER_SIZE
44 # define PINBA_UDP_BUFFER_SIZE 65536
47 #ifndef PINBA_DEFAULT_ADDRESS
48 # define PINBA_DEFAULT_ADDRESS "127.0.0.1" /* FIXME */
51 #ifndef PINBA_DEFAULT_PORT
52 # define PINBA_DEFAULT_PORT 12345 /* FIXME */
55 #ifndef PINBA_MAX_SOCKETS
56 # define PINBA_MAX_SOCKETS 16
60 # define NI_MAXSERV 32
64 * Private data structures
66 typedef struct _pinba_statres_ pinba_statres;
67 struct _pinba_statres_ {
77 struct pinba_socket_s {
79 struct event *accept_event;
81 struct pollfd fd[PINBA_MAX_SOCKETS];
84 typedef struct pinba_socket_s pinba_socket_t;
86 typedef double pinba_time_t;
87 typedef uint32_t pinba_size_t;
89 static struct event_base *temp_base = NULL;
91 static pinba_socket_t *temp_sock = NULL;
93 static pthread_t temp_thrd;
95 typedef struct pinba_statnode_s pinba_statnode_t;
96 struct pinba_statnode_s
105 pinba_time_t last_coll;
106 pinba_size_t req_count;
107 pinba_time_t req_time;
108 pinba_time_t ru_utime;
109 pinba_time_t ru_stime;
110 pinba_size_t doc_size;
111 pinba_size_t mem_peak;
114 static pinba_statnode_t *stat_nodes = NULL;
115 static unsigned int stat_nodes_num = 0;
116 static pthread_mutex_t stat_nodes_lock;
118 char service_status=0;
119 char *service_address = PINBA_DEFAULT_ADDRESS;
120 unsigned int service_port=PINBA_DEFAULT_PORT;
122 static pinba_time_t now (void) /* {{{ */
124 static struct timeval tv;
126 gettimeofday (&tv, /* tz = */ NULL);
128 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
131 static void service_statnode_reset (pinba_statnode_t *node) /* {{{ */
133 node->last_coll=now();
140 } /* }}} void service_statnode_reset */
142 static void strset (char **str, const char *new) /* {{{ */
155 } /* }}} void strset */
157 static void service_statnode_add(const char *name, /* {{{ */
162 pinba_statnode_t *node;
163 DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
165 stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode_t)*(stat_nodes_num+1));
167 ERROR("Realloc failed!");
171 node=&stat_nodes[stat_nodes_num];
173 /* reset stat data */
174 service_statnode_reset(node);
182 /* fill query data */
183 strset(&node->name, name);
184 strset(&node->host, host);
185 strset(&node->server, server);
186 strset(&node->script, script);
188 /* increment counter */
190 } /* }}} void service_statnode_add */
192 static void service_statnode_free (void)
196 if(stat_nodes_num < 1)
199 for (i = 0; i < stat_nodes_num; i++)
201 sfree (stat_nodes[i].name);
202 sfree (stat_nodes[i].host);
203 sfree (stat_nodes[i].server);
204 sfree (stat_nodes[i].script);
210 pthread_mutex_destroy (&stat_nodes_lock);
213 static void service_statnode_init (void)
215 /* only total info collect by default */
216 service_statnode_free();
218 DEBUG("initializing collector..");
219 pthread_mutex_init(&stat_nodes_lock, 0);
222 static void service_statnode_begin (void)
224 service_statnode_init();
225 pthread_mutex_lock(&stat_nodes_lock);
227 service_statnode_add("total", NULL, NULL, NULL);
230 static void service_statnode_end (void)
232 pthread_mutex_unlock(&stat_nodes_lock);
235 static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
238 pinba_statnode_t* node;
241 if (stat_nodes_num == 0)
244 /* begin collecting */
246 pthread_mutex_lock (&stat_nodes_lock);
249 if (index >= stat_nodes_num)
251 pthread_mutex_unlock (&stat_nodes_lock);
255 node = stat_nodes + index;
256 delta = now() - node->last_coll;
258 res->name = node->name;
259 res->req_per_sec = node->req_count / delta;
261 if (node->req_count == 0)
264 res->req_time = node->req_time / node->req_count;
265 res->ru_utime = node->ru_utime / node->req_count;
266 res->ru_stime = node->ru_stime / node->req_count;
267 res->ru_stime = node->ru_stime / node->req_count;
268 res->doc_size = node->doc_size / node->req_count;
269 res->mem_peak = node->mem_peak / node->req_count;
271 service_statnode_reset (node);
273 } /* }}} unsigned int service_statnode_collect */
275 static void service_statnode_process (pinba_statnode_t *node,
276 Pinba__Request* request)
279 node->req_time+=request->request_time;
280 node->ru_utime+=request->ru_utime;
281 node->ru_stime+=request->ru_stime;
282 node->doc_size+=request->document_size;
283 node->mem_peak+=request->memory_peak;
286 static void service_process_request (Pinba__Request *request)
290 pthread_mutex_lock (&stat_nodes_lock);
292 for (i = 0; i < stat_nodes_num; i++)
294 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
296 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
298 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
301 service_statnode_process(&stat_nodes[i], request);
304 pthread_mutex_unlock(&stat_nodes_lock);
307 static void *pinba_main (void *arg)
309 DEBUG("entering listen-loop..");
312 event_base_dispatch(temp_base);
318 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
323 if (socket->listen_sock >= 0)
325 close(socket->listen_sock);
326 socket->listen_sock = -1;
329 if (socket->accept_event)
331 event_del(socket->accept_event);
332 free(socket->accept_event);
333 socket->accept_event = NULL;
337 } /* }}} void pinba_socket_free */
339 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
342 Pinba__Request *request;
344 request = pinba__request__unpack (NULL, buffer_size, buffer);
349 service_process_request(request);
350 pinba__request__free_unpacked (request, NULL);
353 } /* }}} int pinba_process_stats_packet */
355 static void pinba_udp_read_callback_fn (int sock, short event, void *arg) /* {{{ */
357 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
361 if ((event & EV_READ) == 0)
366 buffer_size = sizeof (buffer);
367 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
374 || (errno == EWOULDBLOCK)
376 || (errno == EAGAIN))
381 WARNING("pinba plugin: recvfrom(2) failed: %s",
382 sstrerror (errno, errbuf, sizeof (errbuf)));
385 else if (status == 0)
387 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
390 else /* if (status > 0) */
392 assert (((size_t) status) < buffer_size);
393 buffer_size = (size_t) status;
394 buffer[buffer_size] = 0;
396 status = pinba_process_stats_packet (buffer, buffer_size);
398 DEBUG("pinba plugin: Parsing packet failed.");
405 } /* }}} void pinba_udp_read_callback_fn */
407 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
408 const struct addrinfo *ai)
414 if (s->fd_num == PINBA_MAX_SOCKETS)
416 WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
417 "%i sockets. Please complain to the collectd developers so we can "
418 "raise the limit.", PINBA_MAX_SOCKETS);
422 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
426 ERROR ("pinba plugin: socket(2) failed: %s",
427 sstrerror (errno, errbuf, sizeof (errbuf)));
432 status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
436 WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
437 sstrerror (errno, errbuf, sizeof (errbuf)));
440 status = bind (fd, ai->ai_addr, ai->ai_addrlen);
444 ERROR ("pinba plugin: bind(2) failed: %s",
445 sstrerror (errno, errbuf, sizeof (errbuf)));
449 s->fd[s->fd_num].fd = fd;
450 s->fd[s->fd_num].events = POLLIN | POLLPRI;
451 s->fd[s->fd_num].revents = 0;
455 } /* }}} int pb_add_socket */
457 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
461 struct addrinfo *ai_list;
462 struct addrinfo *ai_ptr;
463 struct addrinfo ai_hints;
466 char service[NI_MAXSERV]; /* FIXME */
467 snprintf (service, sizeof (service), "%i", listen_port);
469 memset (&ai_hints, 0, sizeof (ai_hints));
470 ai_hints.ai_flags = AI_PASSIVE;
471 ai_hints.ai_family = AF_UNSPEC;
472 ai_hints.ai_socktype = SOCK_DGRAM;
473 ai_hints.ai_addr = NULL;
474 ai_hints.ai_canonname = NULL;
475 ai_hints.ai_next = NULL;
478 status = getaddrinfo (node, service,
479 &ai_hints, &ai_list);
482 ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
483 gai_strerror (status));
486 assert (ai_list != NULL);
488 s = malloc (sizeof (*s));
491 freeaddrinfo (ai_list);
492 ERROR ("pinba plugin: malloc failed.");
495 memset (s, 0, sizeof (*s));
497 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
499 status = pb_add_socket (s, ai_ptr);
502 } /* for (ai_list) */
504 freeaddrinfo (ai_list);
508 WARNING ("pinba plugin: Unable to open socket for address %s.", node);
514 } /* }}} pinba_socket_open */
516 static int service_cleanup (void)
518 DEBUG("closing socket..");
520 pthread_mutex_lock(&stat_nodes_lock);
521 pinba_socket_free(temp_sock);
522 pthread_mutex_unlock(&stat_nodes_lock);
525 DEBUG("shutdowning event..");
526 event_base_free(temp_base);
528 DEBUG("shutting down..");
533 static int service_start(void)
535 DEBUG("starting up..");
537 DEBUG("initializing event..");
538 temp_base = event_base_new();
540 DEBUG("opening socket..");
542 temp_sock = pinba_socket_open(service_address, service_port);
549 if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
557 static int service_stop (void)
559 pthread_cancel(temp_thrd);
560 pthread_join(temp_thrd, NULL);
562 DEBUG("terminating listen-loop..");
569 static void service_config (const char *address, unsigned int port) /* {{{ */
571 int need_restart = 0;
573 if (address && service_address && (strcmp(service_address, address) != 0))
575 strset (&service_address, address);
579 if ((port > 0) && (port < 65536) && (service_port != port))
585 if(service_status && need_restart)
590 } /* }}} void service_config */
593 * Plugin declaration section
596 static int config_set (char **var, const char *value)
598 /* code from nginx plugin for collectd */
604 if ((*var = strdup (value)) == NULL) return (1);
608 static int plugin_config (oconfig_item_t *ci)
612 char *pinba_address = NULL;
614 INFO("Pinba Configure..");
616 service_statnode_begin();
618 /* Set default values */
619 config_set(&pinba_address, PINBA_DEFAULT_ADDRESS);
620 pinba_port = PINBA_DEFAULT_PORT;
622 for (i = 0; i < ci->children_num; i++) {
623 oconfig_item_t *child = ci->children + i;
624 if (strcasecmp ("Address", child->key) == 0) {
625 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
626 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
629 config_set(&pinba_address, child->values[0].value.string);
630 } else if (strcasecmp ("Port", child->key) == 0) {
631 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_NUMBER)){
632 WARNING ("pinba plugin: `Port' needs exactly one number argument.");
635 pinba_port=child->values[0].value.number;
636 } else if (strcasecmp ("View", child->key) == 0) {
637 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
638 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
639 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
642 name = child->values[0].value.string;
643 for(o=0; o<child->children_num; o++){
644 oconfig_item_t *node = child->children + o;
645 if (strcasecmp ("Host", node->key) == 0) {
646 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
647 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
650 host = node->values[0].value.string;
651 } else if (strcasecmp ("Server", node->key) == 0) {
652 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
653 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
656 server = node->values[0].value.string;
657 } else if (strcasecmp ("Script", node->key) == 0) {
658 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
659 WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
662 script = node->values[0].value.string;
664 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
668 /* add new statnode */
669 service_statnode_add(name, host, server, script);
671 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
676 service_statnode_end();
678 service_config(pinba_address, pinba_port);
680 } /* int pinba_config */
682 static int plugin_init (void)
684 INFO("Pinba Starting..");
689 static int plugin_shutdown (void)
691 INFO("Pinba Stopping..");
693 service_statnode_free();
697 static int plugin_submit (const char *plugin_instance,
699 const pinba_statres *res) {
701 value_list_t vl = VALUE_LIST_INIT;
703 values[0].gauge = res->req_per_sec;
704 values[1].gauge = res->req_time;
705 values[2].gauge = res->ru_utime;
706 values[3].gauge = res->ru_stime;
707 values[4].gauge = res->doc_size;
708 values[5].gauge = res->mem_peak;
712 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
713 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
714 sstrncpy (vl.plugin_instance, plugin_instance,
715 sizeof(vl.plugin_instance));
716 sstrncpy (vl.type, type, sizeof (vl.type));
717 INFO("Pinba Dispatch");
718 plugin_dispatch_values (&vl);
723 static int plugin_read (void)
726 static pinba_statres res;
728 while ((i = service_statnode_collect (&res, i)) != 0)
730 plugin_submit(res.name, "pinba_view", &res);
736 void module_register (void)
738 plugin_register_complex_config ("pinba", plugin_config);
739 plugin_register_init ("pinba", plugin_init);
740 plugin_register_read ("pinba", plugin_read);
741 plugin_register_shutdown ("pinba", plugin_shutdown);
742 } /* void module_register */
744 /* vim: set sw=2 sts=2 et fdm=marker : */