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 collectd.org>
29 #include "configfile.h"
34 #include "pinba.pb-c.h"
36 /* AIX doesn't have MSG_DONTWAIT */
38 # define MSG_DONTWAIT MSG_NONBLOCK
44 #ifndef PINBA_UDP_BUFFER_SIZE
45 # define PINBA_UDP_BUFFER_SIZE 65536
48 #ifndef PINBA_DEFAULT_NODE
49 # define PINBA_DEFAULT_NODE "::0"
52 #ifndef PINBA_DEFAULT_SERVICE
53 # define PINBA_DEFAULT_SERVICE "30002"
56 #ifndef PINBA_MAX_SOCKETS
57 # define PINBA_MAX_SOCKETS 16
61 * Private data structures
66 struct pollfd fd[PINBA_MAX_SOCKETS];
69 typedef struct pinba_socket_s pinba_socket_t;
71 /* Fixed point counter value. n is the decimal part multiplied by 10^9. */
72 struct float_counter_s
75 uint64_t n; /* nanos */
77 typedef struct float_counter_s float_counter_t;
79 struct pinba_statnode_s
81 /* collector name, used as plugin instance */
91 float_counter_t req_time;
92 float_counter_t ru_utime;
93 float_counter_t ru_stime;
98 typedef struct pinba_statnode_s pinba_statnode_t;
102 * Module global variables
105 static pinba_statnode_t *stat_nodes = NULL;
106 static unsigned int stat_nodes_num = 0;
107 static pthread_mutex_t stat_nodes_lock;
109 static char *conf_node = NULL;
110 static char *conf_service = NULL;
112 static _Bool collector_thread_running = 0;
113 static _Bool collector_thread_do_shutdown = 0;
114 static pthread_t collector_thread_id;
120 static void float_counter_add (float_counter_t *fc, float val) /* {{{ */
127 tmp = (uint64_t) val;
131 fc->n += (uint64_t) ((val * 1000000000.0) + .5);
133 if (fc->n >= 1000000000)
137 assert (fc->n < 1000000000);
139 } /* }}} void float_counter_add */
141 static derive_t float_counter_get (const float_counter_t *fc, /* {{{ */
146 ret = (derive_t) (fc->i * factor);
147 ret += (derive_t) (fc->n / (1000000000 / factor));
150 } /* }}} derive_t float_counter_get */
152 static void strset (char **str, const char *new) /* {{{ */
165 } /* }}} void strset */
167 static void service_statnode_add(const char *name, /* {{{ */
172 pinba_statnode_t *node;
174 node = realloc (stat_nodes,
175 sizeof (*stat_nodes) * (stat_nodes_num + 1));
178 ERROR ("pinba plugin: realloc failed");
183 node = stat_nodes + stat_nodes_num;
184 memset (node, 0, sizeof (*node));
192 node->mem_peak = NAN;
194 /* fill query data */
195 strset (&node->name, name);
196 strset (&node->host, host);
197 strset (&node->server, server);
198 strset (&node->script, script);
200 /* increment counter */
202 } /* }}} void service_statnode_add */
204 /* Copy the data from the global "stat_nodes" list into the buffer pointed to
205 * by "res", doing the derivation in the process. Returns the next index or
206 * zero if the end of the list has been reached. */
207 static unsigned int service_statnode_collect (pinba_statnode_t *res, /* {{{ */
210 pinba_statnode_t *node;
212 if (stat_nodes_num == 0)
215 /* begin collecting */
217 pthread_mutex_lock (&stat_nodes_lock);
220 if (index >= stat_nodes_num)
222 pthread_mutex_unlock (&stat_nodes_lock);
226 node = stat_nodes + index;
227 memcpy (res, node, sizeof (*res));
230 node->mem_peak = NAN;
233 } /* }}} unsigned int service_statnode_collect */
235 static void service_statnode_process (pinba_statnode_t *node, /* {{{ */
236 Pinba__Request* request)
240 float_counter_add (&node->req_time, request->request_time);
241 float_counter_add (&node->ru_utime, request->ru_utime);
242 float_counter_add (&node->ru_stime, request->ru_stime);
244 node->doc_size += request->document_size;
246 if (isnan (node->mem_peak)
247 || (node->mem_peak < ((gauge_t) request->memory_peak)))
248 node->mem_peak = (gauge_t) request->memory_peak;
250 } /* }}} void service_statnode_process */
252 static void service_process_request (Pinba__Request *request) /* {{{ */
256 pthread_mutex_lock (&stat_nodes_lock);
258 for (i = 0; i < stat_nodes_num; i++)
260 if ((stat_nodes[i].host != NULL)
261 && (strcmp (request->hostname, stat_nodes[i].host) != 0))
264 if ((stat_nodes[i].server != NULL)
265 && (strcmp (request->server_name, stat_nodes[i].server) != 0))
268 if ((stat_nodes[i].script != NULL)
269 && (strcmp (request->script_name, stat_nodes[i].script) != 0))
272 service_statnode_process(&stat_nodes[i], request);
275 pthread_mutex_unlock(&stat_nodes_lock);
276 } /* }}} void service_process_request */
278 static int pb_del_socket (pinba_socket_t *s, /* {{{ */
281 if (index >= s->fd_num)
284 close (s->fd[index].fd);
285 s->fd[index].fd = -1;
287 /* When deleting the last element in the list, no memmove is necessary. */
288 if (index < (s->fd_num - 1))
290 memmove (&s->fd[index], &s->fd[index + 1],
291 sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
296 } /* }}} int pb_del_socket */
298 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
299 const struct addrinfo *ai)
305 if (s->fd_num == PINBA_MAX_SOCKETS)
307 WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
308 "%i sockets. Please complain to the collectd developers so we can "
309 "raise the limit.", PINBA_MAX_SOCKETS);
313 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
317 ERROR ("pinba plugin: socket(2) failed: %s",
318 sstrerror (errno, errbuf, sizeof (errbuf)));
323 status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
327 WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
328 sstrerror (errno, errbuf, sizeof (errbuf)));
331 status = bind (fd, ai->ai_addr, ai->ai_addrlen);
335 ERROR ("pinba plugin: bind(2) failed: %s",
336 sstrerror (errno, errbuf, sizeof (errbuf)));
341 s->fd[s->fd_num].fd = fd;
342 s->fd[s->fd_num].events = POLLIN | POLLPRI;
343 s->fd[s->fd_num].revents = 0;
347 } /* }}} int pb_add_socket */
349 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
353 struct addrinfo *ai_list;
354 struct addrinfo *ai_ptr;
355 struct addrinfo ai_hints = { 0 };
358 ai_hints.ai_flags = AI_PASSIVE;
359 ai_hints.ai_family = AF_UNSPEC;
360 ai_hints.ai_socktype = SOCK_DGRAM;
361 ai_hints.ai_addr = NULL;
362 ai_hints.ai_canonname = NULL;
363 ai_hints.ai_next = NULL;
366 node = PINBA_DEFAULT_NODE;
369 service = PINBA_DEFAULT_SERVICE;
372 status = getaddrinfo (node, service,
373 &ai_hints, &ai_list);
376 ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
377 gai_strerror (status));
380 assert (ai_list != NULL);
382 s = calloc (1, sizeof (*s));
385 freeaddrinfo (ai_list);
386 ERROR ("pinba plugin: calloc failed.");
390 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
392 status = pb_add_socket (s, ai_ptr);
395 } /* for (ai_list) */
397 freeaddrinfo (ai_list);
401 WARNING ("pinba plugin: Unable to open socket for address %s.", node);
407 } /* }}} pinba_socket_open */
409 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
416 for (i = 0; i < socket->fd_num; i++)
418 if (socket->fd[i].fd < 0)
420 close (socket->fd[i].fd);
421 socket->fd[i].fd = -1;
425 } /* }}} void pinba_socket_free */
427 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
430 Pinba__Request *request;
432 request = pinba__request__unpack (NULL, buffer_size, buffer);
437 service_process_request(request);
438 pinba__request__free_unpacked (request, NULL);
441 } /* }}} int pinba_process_stats_packet */
443 static int pinba_udp_read_callback_fn (int sock) /* {{{ */
445 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
451 buffer_size = sizeof (buffer);
452 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
459 || (errno == EWOULDBLOCK)
461 || (errno == EAGAIN))
466 WARNING("pinba plugin: recvfrom(2) failed: %s",
467 sstrerror (errno, errbuf, sizeof (errbuf)));
470 else if (status == 0)
472 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
475 else /* if (status > 0) */
477 assert (((size_t) status) < buffer_size);
478 buffer_size = (size_t) status;
479 buffer[buffer_size] = 0;
481 status = pinba_process_stats_packet (buffer, buffer_size);
483 DEBUG("pinba plugin: Parsing packet failed.");
491 } /* }}} void pinba_udp_read_callback_fn */
493 static int receive_loop (void) /* {{{ */
497 s = pinba_socket_open (conf_node, conf_service);
500 ERROR ("pinba plugin: Collector thread is exiting prematurely.");
504 while (!collector_thread_do_shutdown)
512 status = poll (s->fd, s->fd_num, /* timeout = */ 1000);
513 if (status == 0) /* timeout */
521 if ((errno == EINTR) || (errno == EAGAIN))
524 ERROR ("pinba plugin: poll(2) failed: %s",
525 sstrerror (errno, errbuf, sizeof (errbuf)));
526 pinba_socket_free (s);
530 for (i = 0; i < s->fd_num; i++)
532 if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
534 pb_del_socket (s, i);
537 else if (s->fd[i].revents & (POLLIN | POLLPRI))
539 pinba_udp_read_callback_fn (s->fd[i].fd);
542 } /* while (!collector_thread_do_shutdown) */
544 pinba_socket_free (s);
548 } /* }}} int receive_loop */
550 static void *collector_thread (void *arg) /* {{{ */
554 memset (&collector_thread_id, 0, sizeof (collector_thread_id));
555 collector_thread_running = 0;
558 } /* }}} void *collector_thread */
561 * Plugin declaration section
563 static int pinba_config_view (const oconfig_item_t *ci) /* {{{ */
572 status = cf_util_get_string (ci, &name);
576 for (i = 0; i < ci->children_num; i++)
578 oconfig_item_t *child = ci->children + i;
580 if (strcasecmp ("Host", child->key) == 0)
581 status = cf_util_get_string (child, &host);
582 else if (strcasecmp ("Server", child->key) == 0)
583 status = cf_util_get_string (child, &server);
584 else if (strcasecmp ("Script", child->key) == 0)
585 status = cf_util_get_string (child, &script);
588 WARNING ("pinba plugin: Unknown config option: %s", child->key);
597 service_statnode_add (name, host, server, script);
605 } /* }}} int pinba_config_view */
607 static int plugin_config (oconfig_item_t *ci) /* {{{ */
611 /* The lock should not be necessary in the config callback, but let's be
613 pthread_mutex_lock (&stat_nodes_lock);
615 for (i = 0; i < ci->children_num; i++)
617 oconfig_item_t *child = ci->children + i;
619 if (strcasecmp ("Address", child->key) == 0)
620 cf_util_get_string (child, &conf_node);
621 else if (strcasecmp ("Port", child->key) == 0)
622 cf_util_get_service (child, &conf_service);
623 else if (strcasecmp ("View", child->key) == 0)
624 pinba_config_view (child);
626 WARNING ("pinba plugin: Unknown config option: %s", child->key);
629 pthread_mutex_unlock(&stat_nodes_lock);
632 } /* }}} int pinba_config */
634 static int plugin_init (void) /* {{{ */
638 if (stat_nodes == NULL)
640 /* Collect the "total" data by default. */
641 service_statnode_add ("total",
644 /* script = */ NULL);
647 if (collector_thread_running)
650 status = plugin_thread_create (&collector_thread_id,
657 ERROR ("pinba plugin: pthread_create(3) failed: %s",
658 sstrerror (errno, errbuf, sizeof (errbuf)));
661 collector_thread_running = 1;
666 static int plugin_shutdown (void) /* {{{ */
668 if (collector_thread_running)
672 DEBUG ("pinba plugin: Shutting down collector thread.");
673 collector_thread_do_shutdown = 1;
675 status = pthread_join (collector_thread_id, /* retval = */ NULL);
679 ERROR ("pinba plugin: pthread_join(3) failed: %s",
680 sstrerror (status, errbuf, sizeof (errbuf)));
683 collector_thread_running = 0;
684 collector_thread_do_shutdown = 0;
685 } /* if (collector_thread_running) */
688 } /* }}} int plugin_shutdown */
690 static int plugin_submit (const pinba_statnode_t *res) /* {{{ */
693 value_list_t vl = VALUE_LIST_INIT;
697 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
698 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
699 sstrncpy (vl.plugin_instance, res->name, sizeof (vl.plugin_instance));
701 value.derive = res->req_count;
702 sstrncpy (vl.type, "total_requests", sizeof (vl.type));
703 plugin_dispatch_values (&vl);
705 value.derive = float_counter_get (&res->req_time, /* factor = */ 1000);
706 sstrncpy (vl.type, "total_time_in_ms", sizeof (vl.type));
707 plugin_dispatch_values (&vl);
709 value.derive = res->doc_size;
710 sstrncpy (vl.type, "total_bytes", sizeof (vl.type));
711 plugin_dispatch_values (&vl);
713 value.derive = float_counter_get (&res->ru_utime, /* factor = */ 100);
714 sstrncpy (vl.type, "cpu", sizeof (vl.type));
715 sstrncpy (vl.type_instance, "user", sizeof (vl.type_instance));
716 plugin_dispatch_values (&vl);
718 value.derive = float_counter_get (&res->ru_stime, /* factor = */ 100);
719 sstrncpy (vl.type, "cpu", sizeof (vl.type));
720 sstrncpy (vl.type_instance, "system", sizeof (vl.type_instance));
721 plugin_dispatch_values (&vl);
723 value.gauge = res->mem_peak;
724 sstrncpy (vl.type, "memory", sizeof (vl.type));
725 sstrncpy (vl.type_instance, "peak", sizeof (vl.type_instance));
726 plugin_dispatch_values (&vl);
729 } /* }}} int plugin_submit */
731 static int plugin_read (void) /* {{{ */
734 pinba_statnode_t data;
736 while ((i = service_statnode_collect (&data, i)) != 0)
738 plugin_submit (&data);
742 } /* }}} int plugin_read */
744 void module_register (void) /* {{{ */
746 plugin_register_complex_config ("pinba", plugin_config);
747 plugin_register_init ("pinba", plugin_init);
748 plugin_register_read ("pinba", plugin_read);
749 plugin_register_shutdown ("pinba", plugin_shutdown);
750 } /* }}} void module_register */
752 /* vim: set sw=2 sts=2 et fdm=marker : */