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_ADDRESS
46 # define PINBA_DEFAULT_ADDRESS "127.0.0.1" /* FIXME */
49 #ifndef PINBA_DEFAULT_PORT
50 # define PINBA_DEFAULT_PORT 12345 /* FIXME */
53 #ifndef PINBA_MAX_SOCKETS
54 # define PINBA_MAX_SOCKETS 16
58 # define NI_MAXSERV 32
62 * Private data structures
64 typedef struct _pinba_statres_ pinba_statres;
65 struct _pinba_statres_ {
75 struct pinba_socket_s {
76 struct pollfd fd[PINBA_MAX_SOCKETS];
79 typedef struct pinba_socket_s pinba_socket_t;
81 typedef double pinba_time_t;
82 typedef uint32_t pinba_size_t;
84 typedef struct pinba_statnode_s pinba_statnode_t;
85 struct pinba_statnode_s
94 pinba_time_t last_coll;
95 pinba_size_t req_count;
96 pinba_time_t req_time;
97 pinba_time_t ru_utime;
98 pinba_time_t ru_stime;
99 pinba_size_t doc_size;
100 pinba_size_t mem_peak;
103 static pinba_statnode_t *stat_nodes = NULL;
104 static unsigned int stat_nodes_num = 0;
105 static pthread_mutex_t stat_nodes_lock;
107 char service_status=0;
108 char *service_address = PINBA_DEFAULT_ADDRESS;
109 unsigned int service_port=PINBA_DEFAULT_PORT;
111 static _Bool collector_thread_running = 0;
112 static _Bool collector_thread_do_shutdown = 0;
113 static pthread_t collector_thread_id;
115 static pinba_time_t now (void) /* {{{ */
117 static struct timeval tv;
119 gettimeofday (&tv, /* tz = */ NULL);
121 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
122 } /* }}} pinba_time_t now */
124 static void service_statnode_reset (pinba_statnode_t *node) /* {{{ */
126 node->last_coll=now();
133 } /* }}} void service_statnode_reset */
135 static void strset (char **str, const char *new) /* {{{ */
148 } /* }}} void strset */
150 static void service_statnode_add(const char *name, /* {{{ */
155 pinba_statnode_t *node;
156 DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
158 stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode_t)*(stat_nodes_num+1));
160 ERROR("Realloc failed!");
164 node=&stat_nodes[stat_nodes_num];
166 /* reset stat data */
167 service_statnode_reset(node);
175 /* fill query data */
176 strset(&node->name, name);
177 strset(&node->host, host);
178 strset(&node->server, server);
179 strset(&node->script, script);
181 /* increment counter */
183 } /* }}} void service_statnode_add */
185 static void service_statnode_free (void) /* {{{ */
189 if(stat_nodes_num < 1)
192 for (i = 0; i < stat_nodes_num; i++)
194 sfree (stat_nodes[i].name);
195 sfree (stat_nodes[i].host);
196 sfree (stat_nodes[i].server);
197 sfree (stat_nodes[i].script);
203 pthread_mutex_destroy (&stat_nodes_lock);
204 } /* }}} void service_statnode_free */
206 static void service_statnode_init (void) /* {{{ */
208 /* only total info collect by default */
209 service_statnode_free();
211 DEBUG("initializing collector..");
212 pthread_mutex_init(&stat_nodes_lock, 0);
213 } /* }}} void service_statnode_init */
215 static void service_statnode_begin (void) /* {{{ */
217 service_statnode_init();
218 pthread_mutex_lock(&stat_nodes_lock);
220 service_statnode_add("total", NULL, NULL, NULL);
221 } /* }}} void service_statnode_begin */
223 static void service_statnode_end (void) /* {{{ */
225 pthread_mutex_unlock(&stat_nodes_lock);
226 } /* }}} void service_statnode_end */
228 static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
231 pinba_statnode_t* node;
234 if (stat_nodes_num == 0)
237 /* begin collecting */
239 pthread_mutex_lock (&stat_nodes_lock);
242 if (index >= stat_nodes_num)
244 pthread_mutex_unlock (&stat_nodes_lock);
248 node = stat_nodes + index;
249 delta = now() - node->last_coll;
251 res->name = node->name;
252 res->req_per_sec = node->req_count / delta;
254 if (node->req_count == 0)
257 res->req_time = node->req_time / node->req_count;
258 res->ru_utime = node->ru_utime / node->req_count;
259 res->ru_stime = node->ru_stime / node->req_count;
260 res->ru_stime = node->ru_stime / node->req_count;
261 res->doc_size = node->doc_size / node->req_count;
262 res->mem_peak = node->mem_peak / node->req_count;
264 service_statnode_reset (node);
266 } /* }}} unsigned int service_statnode_collect */
268 static void service_statnode_process (pinba_statnode_t *node, /* {{{ */
269 Pinba__Request* request)
272 node->req_time+=request->request_time;
273 node->ru_utime+=request->ru_utime;
274 node->ru_stime+=request->ru_stime;
275 node->doc_size+=request->document_size;
276 node->mem_peak+=request->memory_peak;
277 } /* }}} void service_statnode_process */
279 static void service_process_request (Pinba__Request *request) /* {{{ */
283 pthread_mutex_lock (&stat_nodes_lock);
285 for (i = 0; i < stat_nodes_num; i++)
287 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
289 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
291 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
294 service_statnode_process(&stat_nodes[i], request);
297 pthread_mutex_unlock(&stat_nodes_lock);
298 } /* }}} void service_process_request */
300 static int pb_del_socket (pinba_socket_t *s, /* {{{ */
303 if (index >= s->fd_num)
306 close (s->fd[index].fd);
307 s->fd[index].fd = -1;
309 /* When deleting the last element in the list, no memmove is necessary. */
310 if (index < (s->fd_num - 1))
312 memmove (&s->fd[index], &s->fd[index + 1],
313 sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
318 } /* }}} int pb_del_socket */
320 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
321 const struct addrinfo *ai)
327 if (s->fd_num == PINBA_MAX_SOCKETS)
329 WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
330 "%i sockets. Please complain to the collectd developers so we can "
331 "raise the limit.", PINBA_MAX_SOCKETS);
335 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
339 ERROR ("pinba plugin: socket(2) failed: %s",
340 sstrerror (errno, errbuf, sizeof (errbuf)));
345 status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
349 WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
350 sstrerror (errno, errbuf, sizeof (errbuf)));
353 status = bind (fd, ai->ai_addr, ai->ai_addrlen);
357 ERROR ("pinba plugin: bind(2) failed: %s",
358 sstrerror (errno, errbuf, sizeof (errbuf)));
362 s->fd[s->fd_num].fd = fd;
363 s->fd[s->fd_num].events = POLLIN | POLLPRI;
364 s->fd[s->fd_num].revents = 0;
368 } /* }}} int pb_add_socket */
370 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
374 struct addrinfo *ai_list;
375 struct addrinfo *ai_ptr;
376 struct addrinfo ai_hints;
379 char service[NI_MAXSERV]; /* FIXME */
380 snprintf (service, sizeof (service), "%i", listen_port);
382 memset (&ai_hints, 0, sizeof (ai_hints));
383 ai_hints.ai_flags = AI_PASSIVE;
384 ai_hints.ai_family = AF_UNSPEC;
385 ai_hints.ai_socktype = SOCK_DGRAM;
386 ai_hints.ai_addr = NULL;
387 ai_hints.ai_canonname = NULL;
388 ai_hints.ai_next = NULL;
391 status = getaddrinfo (node, service,
392 &ai_hints, &ai_list);
395 ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
396 gai_strerror (status));
399 assert (ai_list != NULL);
401 s = malloc (sizeof (*s));
404 freeaddrinfo (ai_list);
405 ERROR ("pinba plugin: malloc failed.");
408 memset (s, 0, sizeof (*s));
410 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
412 status = pb_add_socket (s, ai_ptr);
415 } /* for (ai_list) */
417 freeaddrinfo (ai_list);
421 WARNING ("pinba plugin: Unable to open socket for address %s.", node);
427 } /* }}} pinba_socket_open */
429 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
436 for (i = 0; i < socket->fd_num; i++)
438 if (socket->fd[i].fd < 0)
440 close (socket->fd[i].fd);
441 socket->fd[i].fd = -1;
445 } /* }}} void pinba_socket_free */
447 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
450 Pinba__Request *request;
452 request = pinba__request__unpack (NULL, buffer_size, buffer);
457 service_process_request(request);
458 pinba__request__free_unpacked (request, NULL);
461 } /* }}} int pinba_process_stats_packet */
463 static int pinba_udp_read_callback_fn (int sock) /* {{{ */
465 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
471 buffer_size = sizeof (buffer);
472 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
479 || (errno == EWOULDBLOCK)
481 || (errno == EAGAIN))
486 WARNING("pinba plugin: recvfrom(2) failed: %s",
487 sstrerror (errno, errbuf, sizeof (errbuf)));
490 else if (status == 0)
492 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
495 else /* if (status > 0) */
497 assert (((size_t) status) < buffer_size);
498 buffer_size = (size_t) status;
499 buffer[buffer_size] = 0;
501 status = pinba_process_stats_packet (buffer, buffer_size);
503 DEBUG("pinba plugin: Parsing packet failed.");
511 } /* }}} void pinba_udp_read_callback_fn */
513 static int receive_loop (void) /* {{{ */
517 s = pinba_socket_open (service_address, service_port);
520 ERROR ("pinba plugin: Collector thread is exiting prematurely.");
524 while (!collector_thread_do_shutdown)
532 status = poll (s->fd, s->fd_num, /* timeout = */ 1000);
533 if (status == 0) /* timeout */
541 if ((errno == EINTR) || (errno == EAGAIN))
544 ERROR ("pinba plugin: poll(2) failed: %s",
545 sstrerror (errno, errbuf, sizeof (errbuf)));
546 pinba_socket_free (s);
550 for (i = 0; i < s->fd_num; i++)
552 if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
554 pb_del_socket (s, i);
557 else if (s->fd[i].revents & (POLLIN | POLLPRI))
559 pinba_udp_read_callback_fn (s->fd[i].fd);
562 } /* while (!collector_thread_do_shutdown) */
564 pinba_socket_free (s);
568 } /* }}} int receive_loop */
570 static void *collector_thread (void *arg) /* {{{ */
574 memset (&collector_thread_id, 0, sizeof (collector_thread_id));
575 collector_thread_running = 0;
578 } /* }}} void *collector_thread */
581 * Plugin declaration section
584 static int config_set (char **var, const char *value) /* {{{ */
586 /* code from nginx plugin for collectd */
592 if ((*var = strdup (value)) == NULL) return (1);
594 } /* }}} int config_set */
596 static int plugin_config (oconfig_item_t *ci) /* {{{ */
600 char *pinba_address = NULL;
602 INFO("Pinba Configure..");
604 service_statnode_begin();
606 /* Set default values */
607 config_set(&pinba_address, PINBA_DEFAULT_ADDRESS);
608 pinba_port = PINBA_DEFAULT_PORT;
610 for (i = 0; i < ci->children_num; i++) {
611 oconfig_item_t *child = ci->children + i;
612 if (strcasecmp ("Address", child->key) == 0) {
613 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
614 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
617 config_set(&pinba_address, child->values[0].value.string);
618 } else if (strcasecmp ("Port", child->key) == 0) {
619 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_NUMBER)){
620 WARNING ("pinba plugin: `Port' needs exactly one number argument.");
623 pinba_port=child->values[0].value.number;
624 } else if (strcasecmp ("View", child->key) == 0) {
625 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
626 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
627 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
630 name = child->values[0].value.string;
631 for(o=0; o<child->children_num; o++){
632 oconfig_item_t *node = child->children + o;
633 if (strcasecmp ("Host", node->key) == 0) {
634 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
635 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
638 host = node->values[0].value.string;
639 } else if (strcasecmp ("Server", node->key) == 0) {
640 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
641 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
644 server = node->values[0].value.string;
645 } else if (strcasecmp ("Script", 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->Script' needs exactly one non-empty string argument.");
650 script = node->values[0].value.string;
652 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
656 /* add new statnode */
657 service_statnode_add(name, host, server, script);
659 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
664 service_statnode_end();
667 } /* int pinba_config */
669 static int plugin_init (void) /* {{{ */
673 if (collector_thread_running)
676 status = pthread_create (&collector_thread_id,
683 ERROR ("pinba plugin: pthread_create(3) failed: %s",
684 sstrerror (errno, errbuf, sizeof (errbuf)));
687 collector_thread_running = 1;
692 static int plugin_shutdown (void) /* {{{ */
694 if (collector_thread_running)
698 DEBUG ("pinba plugin: Shutting down collector thread.");
699 collector_thread_do_shutdown = 1;
701 status = pthread_join (collector_thread_id, /* retval = */ NULL);
705 ERROR ("pinba plugin: pthread_join(3) failed: %s",
706 sstrerror (status, errbuf, sizeof (errbuf)));
709 collector_thread_running = 0;
710 collector_thread_do_shutdown = 0;
711 } /* if (collector_thread_running) */
714 } /* }}} int plugin_shutdown */
716 static int plugin_submit (const char *plugin_instance, /* {{{ */
718 const pinba_statres *res) {
720 value_list_t vl = VALUE_LIST_INIT;
722 values[0].gauge = res->req_per_sec;
723 values[1].gauge = res->req_time;
724 values[2].gauge = res->ru_utime;
725 values[3].gauge = res->ru_stime;
726 values[4].gauge = res->doc_size;
727 values[5].gauge = res->mem_peak;
731 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
732 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
733 sstrncpy (vl.plugin_instance, plugin_instance,
734 sizeof(vl.plugin_instance));
735 sstrncpy (vl.type, type, sizeof (vl.type));
736 INFO("Pinba Dispatch");
737 plugin_dispatch_values (&vl);
740 } /* }}} int plugin_submit */
742 static int plugin_read (void) /* {{{ */
745 static pinba_statres res;
747 while ((i = service_statnode_collect (&res, i)) != 0)
749 plugin_submit(res.name, "pinba_view", &res);
753 } /* }}} int plugin_read */
755 void module_register (void) /* {{{ */
757 plugin_register_complex_config ("pinba", plugin_config);
758 plugin_register_init ("pinba", plugin_init);
759 plugin_register_read ("pinba", plugin_read);
760 plugin_register_shutdown ("pinba", plugin_shutdown);
761 } /* }}} void module_register */
763 /* vim: set sw=2 sts=2 et fdm=marker : */