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"
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_t;
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 /* Fixed point counter value. n is the decimal part multiplied by 10^9. */
81 struct float_counter_s
84 uint64_t n; /* nanos */
86 typedef struct float_counter_s float_counter_t;
88 struct pinba_statnode_s
100 float_counter_t req_time;
101 float_counter_t ru_utime;
102 float_counter_t ru_stime;
107 typedef struct pinba_statnode_s pinba_statnode_t;
110 * Module global variables
112 static pinba_statnode_t *stat_nodes = NULL;
113 static unsigned int stat_nodes_num = 0;
114 static pthread_mutex_t stat_nodes_lock;
116 static char *conf_node = NULL;
117 static char *conf_service = NULL;
119 static _Bool collector_thread_running = 0;
120 static _Bool collector_thread_do_shutdown = 0;
121 static pthread_t collector_thread_id;
126 static void float_counter_add (float_counter_t *fc, float val) /* {{{ */
133 tmp = (uint64_t) val;
137 fc->n += (uint64_t) ((val * 1000000000.0) + .5);
139 if (fc->n >= 1000000000)
143 assert (fc->n < 1000000000);
145 } /* }}} void float_counter_add */
147 static derive_t float_counter_get (const float_counter_t *fc) /* {{{ */
149 if (fc->n >= 500000000)
150 return ((derive_t) (fc->i + 1));
152 return ((derive_t) fc->i);
153 } /* }}} derive_t float_counter_get */
155 static void strset (char **str, const char *new) /* {{{ */
168 } /* }}} void strset */
170 static void service_statnode_add(const char *name, /* {{{ */
175 pinba_statnode_t *node;
177 node = realloc (stat_nodes,
178 sizeof (*stat_nodes) * (stat_nodes_num + 1));
181 ERROR ("pinba plugin: realloc failed");
186 node = stat_nodes + stat_nodes_num;
187 memset (node, 0, sizeof (*node));
195 node->mem_peak = NAN;
197 /* fill query data */
198 strset(&node->name, name);
199 strset(&node->host, host);
200 strset(&node->server, server);
201 strset(&node->script, script);
203 /* increment counter */
205 } /* }}} void service_statnode_add */
207 /* Copy the data from the global "stat_nodes" list into the buffer pointed to
208 * by "res", doing the derivation in the process. Returns the next index or
209 * zero if the end of the list has been reached. */
210 static unsigned int service_statnode_collect (pinba_statnode_t *res, /* {{{ */
213 pinba_statnode_t *node;
215 if (stat_nodes_num == 0)
218 /* begin collecting */
220 pthread_mutex_lock (&stat_nodes_lock);
223 if (index >= stat_nodes_num)
225 pthread_mutex_unlock (&stat_nodes_lock);
229 node = stat_nodes + index;
230 memcpy (res, node, sizeof (*res));
233 node->mem_peak = NAN;
236 } /* }}} unsigned int service_statnode_collect */
238 static void service_statnode_process (pinba_statnode_t *node, /* {{{ */
239 Pinba__Request* request)
243 float_counter_add (&node->req_time, request->request_time);
244 float_counter_add (&node->ru_utime, request->ru_utime);
245 float_counter_add (&node->ru_stime, request->ru_stime);
247 node->doc_size += request->document_size;
249 if (isnan (node->mem_peak)
250 || (node->mem_peak < ((gauge_t) request->memory_peak)))
251 node->mem_peak = (gauge_t) request->memory_peak;
253 } /* }}} void service_statnode_process */
255 static void service_process_request (Pinba__Request *request) /* {{{ */
259 pthread_mutex_lock (&stat_nodes_lock);
261 for (i = 0; i < stat_nodes_num; i++)
263 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
265 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
267 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
270 service_statnode_process(&stat_nodes[i], request);
273 pthread_mutex_unlock(&stat_nodes_lock);
274 } /* }}} void service_process_request */
276 static int pb_del_socket (pinba_socket_t *s, /* {{{ */
279 if (index >= s->fd_num)
282 close (s->fd[index].fd);
283 s->fd[index].fd = -1;
285 /* When deleting the last element in the list, no memmove is necessary. */
286 if (index < (s->fd_num - 1))
288 memmove (&s->fd[index], &s->fd[index + 1],
289 sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
294 } /* }}} int pb_del_socket */
296 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
297 const struct addrinfo *ai)
303 if (s->fd_num == PINBA_MAX_SOCKETS)
305 WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
306 "%i sockets. Please complain to the collectd developers so we can "
307 "raise the limit.", PINBA_MAX_SOCKETS);
311 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
315 ERROR ("pinba plugin: socket(2) failed: %s",
316 sstrerror (errno, errbuf, sizeof (errbuf)));
321 status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
325 WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
326 sstrerror (errno, errbuf, sizeof (errbuf)));
329 status = bind (fd, ai->ai_addr, ai->ai_addrlen);
333 ERROR ("pinba plugin: bind(2) failed: %s",
334 sstrerror (errno, errbuf, sizeof (errbuf)));
338 s->fd[s->fd_num].fd = fd;
339 s->fd[s->fd_num].events = POLLIN | POLLPRI;
340 s->fd[s->fd_num].revents = 0;
344 } /* }}} int pb_add_socket */
346 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
350 struct addrinfo *ai_list;
351 struct addrinfo *ai_ptr;
352 struct addrinfo ai_hints;
355 memset (&ai_hints, 0, sizeof (ai_hints));
356 ai_hints.ai_flags = AI_PASSIVE;
357 ai_hints.ai_family = AF_UNSPEC;
358 ai_hints.ai_socktype = SOCK_DGRAM;
359 ai_hints.ai_addr = NULL;
360 ai_hints.ai_canonname = NULL;
361 ai_hints.ai_next = NULL;
364 node = PINBA_DEFAULT_NODE;
367 service = PINBA_DEFAULT_SERVICE;
370 status = getaddrinfo (node, service,
371 &ai_hints, &ai_list);
374 ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
375 gai_strerror (status));
378 assert (ai_list != NULL);
380 s = malloc (sizeof (*s));
383 freeaddrinfo (ai_list);
384 ERROR ("pinba plugin: malloc failed.");
387 memset (s, 0, sizeof (*s));
389 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
391 status = pb_add_socket (s, ai_ptr);
394 } /* for (ai_list) */
396 freeaddrinfo (ai_list);
400 WARNING ("pinba plugin: Unable to open socket for address %s.", node);
406 } /* }}} pinba_socket_open */
408 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
415 for (i = 0; i < socket->fd_num; i++)
417 if (socket->fd[i].fd < 0)
419 close (socket->fd[i].fd);
420 socket->fd[i].fd = -1;
424 } /* }}} void pinba_socket_free */
426 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
429 Pinba__Request *request;
431 request = pinba__request__unpack (NULL, buffer_size, buffer);
436 service_process_request(request);
437 pinba__request__free_unpacked (request, NULL);
440 } /* }}} int pinba_process_stats_packet */
442 static int pinba_udp_read_callback_fn (int sock) /* {{{ */
444 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
450 buffer_size = sizeof (buffer);
451 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
458 || (errno == EWOULDBLOCK)
460 || (errno == EAGAIN))
465 WARNING("pinba plugin: recvfrom(2) failed: %s",
466 sstrerror (errno, errbuf, sizeof (errbuf)));
469 else if (status == 0)
471 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
474 else /* if (status > 0) */
476 assert (((size_t) status) < buffer_size);
477 buffer_size = (size_t) status;
478 buffer[buffer_size] = 0;
480 status = pinba_process_stats_packet (buffer, buffer_size);
482 DEBUG("pinba plugin: Parsing packet failed.");
490 } /* }}} void pinba_udp_read_callback_fn */
492 static int receive_loop (void) /* {{{ */
496 s = pinba_socket_open (conf_node, conf_service);
499 ERROR ("pinba plugin: Collector thread is exiting prematurely.");
503 while (!collector_thread_do_shutdown)
511 status = poll (s->fd, s->fd_num, /* timeout = */ 1000);
512 if (status == 0) /* timeout */
520 if ((errno == EINTR) || (errno == EAGAIN))
523 ERROR ("pinba plugin: poll(2) failed: %s",
524 sstrerror (errno, errbuf, sizeof (errbuf)));
525 pinba_socket_free (s);
529 for (i = 0; i < s->fd_num; i++)
531 if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
533 pb_del_socket (s, i);
536 else if (s->fd[i].revents & (POLLIN | POLLPRI))
538 pinba_udp_read_callback_fn (s->fd[i].fd);
541 } /* while (!collector_thread_do_shutdown) */
543 pinba_socket_free (s);
547 } /* }}} int receive_loop */
549 static void *collector_thread (void *arg) /* {{{ */
553 memset (&collector_thread_id, 0, sizeof (collector_thread_id));
554 collector_thread_running = 0;
557 } /* }}} void *collector_thread */
560 * Plugin declaration section
563 static int config_set (char **var, const char *value) /* {{{ */
565 /* code from nginx plugin for collectd */
571 if ((*var = strdup (value)) == NULL) return (1);
573 } /* }}} int config_set */
575 static int plugin_config (oconfig_item_t *ci) /* {{{ */
579 pthread_mutex_lock (&stat_nodes_lock);
580 /* FIXME XXX: Remove all those return statements that don't free the lock. */
582 if (stat_nodes == NULL)
584 /* Collect the "total" data by default. */
585 service_statnode_add ("total",
588 /* script = */ NULL);
591 for (i = 0; i < ci->children_num; i++) {
592 oconfig_item_t *child = ci->children + i;
593 if (strcasecmp ("Address", child->key) == 0) {
594 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
595 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
598 config_set(&conf_node, child->values[0].value.string);
599 } else if (strcasecmp ("Port", child->key) == 0) {
600 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
601 WARNING ("pinba plugin: `Port' needs exactly one string argument.");
604 config_set(&conf_service, child->values[0].value.string);
605 } else if (strcasecmp ("View", child->key) == 0) {
606 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
607 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
608 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
611 name = child->values[0].value.string;
612 for(o=0; o<child->children_num; o++){
613 oconfig_item_t *node = child->children + o;
614 if (strcasecmp ("Host", node->key) == 0) {
615 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
616 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
619 host = node->values[0].value.string;
620 } else if (strcasecmp ("Server", node->key) == 0) {
621 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
622 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
625 server = node->values[0].value.string;
626 } else if (strcasecmp ("Script", node->key) == 0) {
627 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
628 WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
631 script = node->values[0].value.string;
633 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
637 /* add new statnode */
638 service_statnode_add(name, host, server, script);
640 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
645 pthread_mutex_unlock(&stat_nodes_lock);
648 } /* int pinba_config */
650 static int plugin_init (void) /* {{{ */
654 if (collector_thread_running)
657 status = pthread_create (&collector_thread_id,
664 ERROR ("pinba plugin: pthread_create(3) failed: %s",
665 sstrerror (errno, errbuf, sizeof (errbuf)));
668 collector_thread_running = 1;
673 static int plugin_shutdown (void) /* {{{ */
675 if (collector_thread_running)
679 DEBUG ("pinba plugin: Shutting down collector thread.");
680 collector_thread_do_shutdown = 1;
682 status = pthread_join (collector_thread_id, /* retval = */ NULL);
686 ERROR ("pinba plugin: pthread_join(3) failed: %s",
687 sstrerror (status, errbuf, sizeof (errbuf)));
690 collector_thread_running = 0;
691 collector_thread_do_shutdown = 0;
692 } /* if (collector_thread_running) */
695 } /* }}} int plugin_shutdown */
697 static int plugin_submit (const pinba_statnode_t *res) /* {{{ */
700 value_list_t vl = VALUE_LIST_INIT;
704 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
705 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
706 sstrncpy (vl.plugin_instance, res->name, sizeof (vl.plugin_instance));
708 value.derive = res->req_count;
709 sstrncpy (vl.type, "requests", sizeof (vl.type));
710 plugin_dispatch_values (&vl);
712 value.derive = float_counter_get (&res->req_time);
713 sstrncpy (vl.type, "total_time", sizeof (vl.type));
714 plugin_dispatch_values (&vl);
716 value.derive = float_counter_get (&res->ru_utime);
717 sstrncpy (vl.type, "cpu", sizeof (vl.type));
718 sstrncpy (vl.type_instance, "user", sizeof (vl.type_instance));
719 plugin_dispatch_values (&vl);
721 value.derive = float_counter_get (&res->ru_stime);
722 sstrncpy (vl.type, "cpu", sizeof (vl.type));
723 sstrncpy (vl.type_instance, "system", sizeof (vl.type_instance));
724 plugin_dispatch_values (&vl);
726 value.gauge = res->mem_peak;
727 sstrncpy (vl.type, "memory", sizeof (vl.type));
728 sstrncpy (vl.type_instance, "peak", sizeof (vl.type_instance));
729 plugin_dispatch_values (&vl);
732 } /* }}} int plugin_submit */
734 static int plugin_read (void) /* {{{ */
737 pinba_statnode_t data;
739 while ((i = service_statnode_collect (&data, i)) != 0)
741 plugin_submit (&data);
745 } /* }}} int plugin_read */
747 void module_register (void) /* {{{ */
749 plugin_register_complex_config ("pinba", plugin_config);
750 plugin_register_init ("pinba", plugin_init);
751 plugin_register_read ("pinba", plugin_read);
752 plugin_register_shutdown ("pinba", plugin_shutdown);
753 } /* }}} void module_register */
755 /* vim: set sw=2 sts=2 et fdm=marker : */