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
5 * Copyright (C) 2010 Florian Forster
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Antony Dovgal <tony at daylessday.org>
22 * Phoenix Kayo <kayo.k11.4 at gmail.com>
23 * Florian Forster <octo at verplant.org>
29 #include "configfile.h"
32 #include <sys/socket.h>
36 #include "pinba.pb-c.h"
39 * Service declaration section
41 #ifndef PINBA_UDP_BUFFER_SIZE
42 # define PINBA_UDP_BUFFER_SIZE 65536
45 #ifndef PINBA_DEFAULT_NODE
46 # define PINBA_DEFAULT_NODE "127.0.0.1" /* FIXME */
49 #ifndef PINBA_DEFAULT_SERVICE
50 # define PINBA_DEFAULT_SERVICE "12345" /* FIXME */
53 #ifndef PINBA_MAX_SOCKETS
54 # define PINBA_MAX_SOCKETS 16
58 * Private data structures
60 typedef struct _pinba_statres_ pinba_statres;
61 struct _pinba_statres_ {
71 struct pinba_socket_s {
72 struct pollfd fd[PINBA_MAX_SOCKETS];
75 typedef struct pinba_socket_s pinba_socket_t;
77 typedef double pinba_time_t;
78 typedef uint32_t pinba_size_t;
80 typedef struct pinba_statnode_s pinba_statnode_t;
81 struct pinba_statnode_s
90 pinba_time_t last_coll;
91 pinba_size_t req_count;
92 pinba_time_t req_time;
93 pinba_time_t ru_utime;
94 pinba_time_t ru_stime;
95 pinba_size_t doc_size;
96 pinba_size_t mem_peak;
99 static pinba_statnode_t *stat_nodes = NULL;
100 static unsigned int stat_nodes_num = 0;
101 static pthread_mutex_t stat_nodes_lock;
103 static char *conf_node = NULL;
104 static char *conf_service = NULL;
106 static _Bool collector_thread_running = 0;
107 static _Bool collector_thread_do_shutdown = 0;
108 static pthread_t collector_thread_id;
110 static pinba_time_t now (void) /* {{{ */
112 static struct timeval tv;
114 gettimeofday (&tv, /* tz = */ NULL);
116 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
117 } /* }}} pinba_time_t now */
119 static void service_statnode_reset (pinba_statnode_t *node) /* {{{ */
121 node->last_coll=now();
128 } /* }}} void service_statnode_reset */
130 static void strset (char **str, const char *new) /* {{{ */
143 } /* }}} void strset */
145 static void service_statnode_add(const char *name, /* {{{ */
150 pinba_statnode_t *node;
151 DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
153 stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode_t)*(stat_nodes_num+1));
155 ERROR("Realloc failed!");
159 node=&stat_nodes[stat_nodes_num];
161 /* reset stat data */
162 service_statnode_reset(node);
170 /* fill query data */
171 strset(&node->name, name);
172 strset(&node->host, host);
173 strset(&node->server, server);
174 strset(&node->script, script);
176 /* increment counter */
178 } /* }}} void service_statnode_add */
180 static void service_statnode_free (void) /* {{{ */
184 if(stat_nodes_num < 1)
187 for (i = 0; i < stat_nodes_num; i++)
189 sfree (stat_nodes[i].name);
190 sfree (stat_nodes[i].host);
191 sfree (stat_nodes[i].server);
192 sfree (stat_nodes[i].script);
198 pthread_mutex_destroy (&stat_nodes_lock);
199 } /* }}} void service_statnode_free */
201 static void service_statnode_init (void) /* {{{ */
203 /* only total info collect by default */
204 service_statnode_free();
206 DEBUG("initializing collector..");
207 pthread_mutex_init(&stat_nodes_lock, 0);
208 } /* }}} void service_statnode_init */
210 static void service_statnode_begin (void) /* {{{ */
212 service_statnode_init();
213 pthread_mutex_lock(&stat_nodes_lock);
215 service_statnode_add("total", NULL, NULL, NULL);
216 } /* }}} void service_statnode_begin */
218 static void service_statnode_end (void) /* {{{ */
220 pthread_mutex_unlock(&stat_nodes_lock);
221 } /* }}} void service_statnode_end */
223 static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
226 pinba_statnode_t* node;
229 if (stat_nodes_num == 0)
232 /* begin collecting */
234 pthread_mutex_lock (&stat_nodes_lock);
237 if (index >= stat_nodes_num)
239 pthread_mutex_unlock (&stat_nodes_lock);
243 node = stat_nodes + index;
244 delta = now() - node->last_coll;
246 res->name = node->name;
247 res->req_per_sec = node->req_count / delta;
249 if (node->req_count == 0)
252 res->req_time = node->req_time / node->req_count;
253 res->ru_utime = node->ru_utime / node->req_count;
254 res->ru_stime = node->ru_stime / node->req_count;
255 res->ru_stime = node->ru_stime / node->req_count;
256 res->doc_size = node->doc_size / node->req_count;
257 res->mem_peak = node->mem_peak / node->req_count;
259 service_statnode_reset (node);
261 } /* }}} unsigned int service_statnode_collect */
263 static void service_statnode_process (pinba_statnode_t *node, /* {{{ */
264 Pinba__Request* request)
267 node->req_time+=request->request_time;
268 node->ru_utime+=request->ru_utime;
269 node->ru_stime+=request->ru_stime;
270 node->doc_size+=request->document_size;
271 node->mem_peak+=request->memory_peak;
272 } /* }}} void service_statnode_process */
274 static void service_process_request (Pinba__Request *request) /* {{{ */
278 pthread_mutex_lock (&stat_nodes_lock);
280 for (i = 0; i < stat_nodes_num; i++)
282 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
284 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
286 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
289 service_statnode_process(&stat_nodes[i], request);
292 pthread_mutex_unlock(&stat_nodes_lock);
293 } /* }}} void service_process_request */
295 static int pb_del_socket (pinba_socket_t *s, /* {{{ */
298 if (index >= s->fd_num)
301 close (s->fd[index].fd);
302 s->fd[index].fd = -1;
304 /* When deleting the last element in the list, no memmove is necessary. */
305 if (index < (s->fd_num - 1))
307 memmove (&s->fd[index], &s->fd[index + 1],
308 sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
313 } /* }}} int pb_del_socket */
315 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
316 const struct addrinfo *ai)
322 if (s->fd_num == PINBA_MAX_SOCKETS)
324 WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
325 "%i sockets. Please complain to the collectd developers so we can "
326 "raise the limit.", PINBA_MAX_SOCKETS);
330 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
334 ERROR ("pinba plugin: socket(2) failed: %s",
335 sstrerror (errno, errbuf, sizeof (errbuf)));
340 status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
344 WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
345 sstrerror (errno, errbuf, sizeof (errbuf)));
348 status = bind (fd, ai->ai_addr, ai->ai_addrlen);
352 ERROR ("pinba plugin: bind(2) failed: %s",
353 sstrerror (errno, errbuf, sizeof (errbuf)));
357 s->fd[s->fd_num].fd = fd;
358 s->fd[s->fd_num].events = POLLIN | POLLPRI;
359 s->fd[s->fd_num].revents = 0;
363 } /* }}} int pb_add_socket */
365 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
369 struct addrinfo *ai_list;
370 struct addrinfo *ai_ptr;
371 struct addrinfo ai_hints;
374 memset (&ai_hints, 0, sizeof (ai_hints));
375 ai_hints.ai_flags = AI_PASSIVE;
376 ai_hints.ai_family = AF_UNSPEC;
377 ai_hints.ai_socktype = SOCK_DGRAM;
378 ai_hints.ai_addr = NULL;
379 ai_hints.ai_canonname = NULL;
380 ai_hints.ai_next = NULL;
383 node = PINBA_DEFAULT_NODE;
386 service = PINBA_DEFAULT_SERVICE;
389 status = getaddrinfo (node, service,
390 &ai_hints, &ai_list);
393 ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
394 gai_strerror (status));
397 assert (ai_list != NULL);
399 s = malloc (sizeof (*s));
402 freeaddrinfo (ai_list);
403 ERROR ("pinba plugin: malloc failed.");
406 memset (s, 0, sizeof (*s));
408 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
410 status = pb_add_socket (s, ai_ptr);
413 } /* for (ai_list) */
415 freeaddrinfo (ai_list);
419 WARNING ("pinba plugin: Unable to open socket for address %s.", node);
425 } /* }}} pinba_socket_open */
427 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
434 for (i = 0; i < socket->fd_num; i++)
436 if (socket->fd[i].fd < 0)
438 close (socket->fd[i].fd);
439 socket->fd[i].fd = -1;
443 } /* }}} void pinba_socket_free */
445 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
448 Pinba__Request *request;
450 request = pinba__request__unpack (NULL, buffer_size, buffer);
455 service_process_request(request);
456 pinba__request__free_unpacked (request, NULL);
459 } /* }}} int pinba_process_stats_packet */
461 static int pinba_udp_read_callback_fn (int sock) /* {{{ */
463 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
469 buffer_size = sizeof (buffer);
470 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
477 || (errno == EWOULDBLOCK)
479 || (errno == EAGAIN))
484 WARNING("pinba plugin: recvfrom(2) failed: %s",
485 sstrerror (errno, errbuf, sizeof (errbuf)));
488 else if (status == 0)
490 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
493 else /* if (status > 0) */
495 assert (((size_t) status) < buffer_size);
496 buffer_size = (size_t) status;
497 buffer[buffer_size] = 0;
499 status = pinba_process_stats_packet (buffer, buffer_size);
501 DEBUG("pinba plugin: Parsing packet failed.");
509 } /* }}} void pinba_udp_read_callback_fn */
511 static int receive_loop (void) /* {{{ */
515 s = pinba_socket_open (conf_node, conf_service);
518 ERROR ("pinba plugin: Collector thread is exiting prematurely.");
522 while (!collector_thread_do_shutdown)
530 status = poll (s->fd, s->fd_num, /* timeout = */ 1000);
531 if (status == 0) /* timeout */
539 if ((errno == EINTR) || (errno == EAGAIN))
542 ERROR ("pinba plugin: poll(2) failed: %s",
543 sstrerror (errno, errbuf, sizeof (errbuf)));
544 pinba_socket_free (s);
548 for (i = 0; i < s->fd_num; i++)
550 if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
552 pb_del_socket (s, i);
555 else if (s->fd[i].revents & (POLLIN | POLLPRI))
557 pinba_udp_read_callback_fn (s->fd[i].fd);
560 } /* while (!collector_thread_do_shutdown) */
562 pinba_socket_free (s);
566 } /* }}} int receive_loop */
568 static void *collector_thread (void *arg) /* {{{ */
572 memset (&collector_thread_id, 0, sizeof (collector_thread_id));
573 collector_thread_running = 0;
576 } /* }}} void *collector_thread */
579 * Plugin declaration section
582 static int config_set (char **var, const char *value) /* {{{ */
584 /* code from nginx plugin for collectd */
590 if ((*var = strdup (value)) == NULL) return (1);
592 } /* }}} int config_set */
594 static int plugin_config (oconfig_item_t *ci) /* {{{ */
598 service_statnode_begin();
600 for (i = 0; i < ci->children_num; i++) {
601 oconfig_item_t *child = ci->children + i;
602 if (strcasecmp ("Address", child->key) == 0) {
603 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
604 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
607 config_set(&conf_node, child->values[0].value.string);
608 } else if (strcasecmp ("Port", child->key) == 0) {
609 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
610 WARNING ("pinba plugin: `Port' needs exactly one string argument.");
613 config_set(&conf_service, child->values[0].value.string);
614 } else if (strcasecmp ("View", child->key) == 0) {
615 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
616 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
617 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
620 name = child->values[0].value.string;
621 for(o=0; o<child->children_num; o++){
622 oconfig_item_t *node = child->children + o;
623 if (strcasecmp ("Host", node->key) == 0) {
624 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
625 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
628 host = node->values[0].value.string;
629 } else if (strcasecmp ("Server", node->key) == 0) {
630 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
631 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
634 server = node->values[0].value.string;
635 } else if (strcasecmp ("Script", node->key) == 0) {
636 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
637 WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
640 script = node->values[0].value.string;
642 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
646 /* add new statnode */
647 service_statnode_add(name, host, server, script);
649 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
654 service_statnode_end();
657 } /* int pinba_config */
659 static int plugin_init (void) /* {{{ */
663 if (collector_thread_running)
666 status = pthread_create (&collector_thread_id,
673 ERROR ("pinba plugin: pthread_create(3) failed: %s",
674 sstrerror (errno, errbuf, sizeof (errbuf)));
677 collector_thread_running = 1;
682 static int plugin_shutdown (void) /* {{{ */
684 if (collector_thread_running)
688 DEBUG ("pinba plugin: Shutting down collector thread.");
689 collector_thread_do_shutdown = 1;
691 status = pthread_join (collector_thread_id, /* retval = */ NULL);
695 ERROR ("pinba plugin: pthread_join(3) failed: %s",
696 sstrerror (status, errbuf, sizeof (errbuf)));
699 collector_thread_running = 0;
700 collector_thread_do_shutdown = 0;
701 } /* if (collector_thread_running) */
704 } /* }}} int plugin_shutdown */
706 static int plugin_submit (const char *plugin_instance, /* {{{ */
708 const pinba_statres *res) {
710 value_list_t vl = VALUE_LIST_INIT;
712 values[0].gauge = res->req_per_sec;
713 values[1].gauge = res->req_time;
714 values[2].gauge = res->ru_utime;
715 values[3].gauge = res->ru_stime;
716 values[4].gauge = res->doc_size;
717 values[5].gauge = res->mem_peak;
721 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
722 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
723 sstrncpy (vl.plugin_instance, plugin_instance,
724 sizeof(vl.plugin_instance));
725 sstrncpy (vl.type, type, sizeof (vl.type));
726 INFO("Pinba Dispatch");
727 plugin_dispatch_values (&vl);
730 } /* }}} int plugin_submit */
732 static int plugin_read (void) /* {{{ */
735 static pinba_statres res;
737 while ((i = service_statnode_collect (&res, i)) != 0)
739 plugin_submit(res.name, "pinba_view", &res);
743 } /* }}} int plugin_read */
745 void module_register (void) /* {{{ */
747 plugin_register_complex_config ("pinba", plugin_config);
748 plugin_register_init ("pinba", plugin_init);
749 plugin_register_read ("pinba", plugin_read);
750 plugin_register_shutdown ("pinba", plugin_shutdown);
751 } /* }}} void module_register */
753 /* vim: set sw=2 sts=2 et fdm=marker : */