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 # 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 static char *conf_node = NULL;
108 static char *conf_service = NULL;
110 static _Bool collector_thread_running = 0;
111 static _Bool collector_thread_do_shutdown = 0;
112 static pthread_t collector_thread_id;
114 static pinba_time_t now (void) /* {{{ */
116 static struct timeval tv;
118 gettimeofday (&tv, /* tz = */ NULL);
120 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
121 } /* }}} pinba_time_t now */
123 static void service_statnode_reset (pinba_statnode_t *node) /* {{{ */
125 node->last_coll=now();
132 } /* }}} void service_statnode_reset */
134 static void strset (char **str, const char *new) /* {{{ */
147 } /* }}} void strset */
149 static void service_statnode_add(const char *name, /* {{{ */
154 pinba_statnode_t *node;
155 DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
157 stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode_t)*(stat_nodes_num+1));
159 ERROR("Realloc failed!");
163 node=&stat_nodes[stat_nodes_num];
165 /* reset stat data */
166 service_statnode_reset(node);
174 /* fill query data */
175 strset(&node->name, name);
176 strset(&node->host, host);
177 strset(&node->server, server);
178 strset(&node->script, script);
180 /* increment counter */
182 } /* }}} void service_statnode_add */
184 static void service_statnode_free (void) /* {{{ */
188 if(stat_nodes_num < 1)
191 for (i = 0; i < stat_nodes_num; i++)
193 sfree (stat_nodes[i].name);
194 sfree (stat_nodes[i].host);
195 sfree (stat_nodes[i].server);
196 sfree (stat_nodes[i].script);
202 pthread_mutex_destroy (&stat_nodes_lock);
203 } /* }}} void service_statnode_free */
205 static void service_statnode_init (void) /* {{{ */
207 /* only total info collect by default */
208 service_statnode_free();
210 DEBUG("initializing collector..");
211 pthread_mutex_init(&stat_nodes_lock, 0);
212 } /* }}} void service_statnode_init */
214 static void service_statnode_begin (void) /* {{{ */
216 service_statnode_init();
217 pthread_mutex_lock(&stat_nodes_lock);
219 service_statnode_add("total", NULL, NULL, NULL);
220 } /* }}} void service_statnode_begin */
222 static void service_statnode_end (void) /* {{{ */
224 pthread_mutex_unlock(&stat_nodes_lock);
225 } /* }}} void service_statnode_end */
227 static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
230 pinba_statnode_t* node;
233 if (stat_nodes_num == 0)
236 /* begin collecting */
238 pthread_mutex_lock (&stat_nodes_lock);
241 if (index >= stat_nodes_num)
243 pthread_mutex_unlock (&stat_nodes_lock);
247 node = stat_nodes + index;
248 delta = now() - node->last_coll;
250 res->name = node->name;
251 res->req_per_sec = node->req_count / delta;
253 if (node->req_count == 0)
256 res->req_time = node->req_time / node->req_count;
257 res->ru_utime = node->ru_utime / node->req_count;
258 res->ru_stime = node->ru_stime / node->req_count;
259 res->ru_stime = node->ru_stime / node->req_count;
260 res->doc_size = node->doc_size / node->req_count;
261 res->mem_peak = node->mem_peak / node->req_count;
263 service_statnode_reset (node);
265 } /* }}} unsigned int service_statnode_collect */
267 static void service_statnode_process (pinba_statnode_t *node, /* {{{ */
268 Pinba__Request* request)
271 node->req_time+=request->request_time;
272 node->ru_utime+=request->ru_utime;
273 node->ru_stime+=request->ru_stime;
274 node->doc_size+=request->document_size;
275 node->mem_peak+=request->memory_peak;
276 } /* }}} void service_statnode_process */
278 static void service_process_request (Pinba__Request *request) /* {{{ */
282 pthread_mutex_lock (&stat_nodes_lock);
284 for (i = 0; i < stat_nodes_num; i++)
286 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
288 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
290 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
293 service_statnode_process(&stat_nodes[i], request);
296 pthread_mutex_unlock(&stat_nodes_lock);
297 } /* }}} void service_process_request */
299 static int pb_del_socket (pinba_socket_t *s, /* {{{ */
302 if (index >= s->fd_num)
305 close (s->fd[index].fd);
306 s->fd[index].fd = -1;
308 /* When deleting the last element in the list, no memmove is necessary. */
309 if (index < (s->fd_num - 1))
311 memmove (&s->fd[index], &s->fd[index + 1],
312 sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
317 } /* }}} int pb_del_socket */
319 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
320 const struct addrinfo *ai)
326 if (s->fd_num == PINBA_MAX_SOCKETS)
328 WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
329 "%i sockets. Please complain to the collectd developers so we can "
330 "raise the limit.", PINBA_MAX_SOCKETS);
334 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
338 ERROR ("pinba plugin: socket(2) failed: %s",
339 sstrerror (errno, errbuf, sizeof (errbuf)));
344 status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
348 WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
349 sstrerror (errno, errbuf, sizeof (errbuf)));
352 status = bind (fd, ai->ai_addr, ai->ai_addrlen);
356 ERROR ("pinba plugin: bind(2) failed: %s",
357 sstrerror (errno, errbuf, sizeof (errbuf)));
361 s->fd[s->fd_num].fd = fd;
362 s->fd[s->fd_num].events = POLLIN | POLLPRI;
363 s->fd[s->fd_num].revents = 0;
367 } /* }}} int pb_add_socket */
369 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
373 struct addrinfo *ai_list;
374 struct addrinfo *ai_ptr;
375 struct addrinfo ai_hints;
378 memset (&ai_hints, 0, sizeof (ai_hints));
379 ai_hints.ai_flags = AI_PASSIVE;
380 ai_hints.ai_family = AF_UNSPEC;
381 ai_hints.ai_socktype = SOCK_DGRAM;
382 ai_hints.ai_addr = NULL;
383 ai_hints.ai_canonname = NULL;
384 ai_hints.ai_next = NULL;
387 node = PINBA_DEFAULT_NODE;
390 service = PINBA_DEFAULT_SERVICE;
393 status = getaddrinfo (node, service,
394 &ai_hints, &ai_list);
397 ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
398 gai_strerror (status));
401 assert (ai_list != NULL);
403 s = malloc (sizeof (*s));
406 freeaddrinfo (ai_list);
407 ERROR ("pinba plugin: malloc failed.");
410 memset (s, 0, sizeof (*s));
412 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
414 status = pb_add_socket (s, ai_ptr);
417 } /* for (ai_list) */
419 freeaddrinfo (ai_list);
423 WARNING ("pinba plugin: Unable to open socket for address %s.", node);
429 } /* }}} pinba_socket_open */
431 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
438 for (i = 0; i < socket->fd_num; i++)
440 if (socket->fd[i].fd < 0)
442 close (socket->fd[i].fd);
443 socket->fd[i].fd = -1;
447 } /* }}} void pinba_socket_free */
449 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
452 Pinba__Request *request;
454 request = pinba__request__unpack (NULL, buffer_size, buffer);
459 service_process_request(request);
460 pinba__request__free_unpacked (request, NULL);
463 } /* }}} int pinba_process_stats_packet */
465 static int pinba_udp_read_callback_fn (int sock) /* {{{ */
467 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
473 buffer_size = sizeof (buffer);
474 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
481 || (errno == EWOULDBLOCK)
483 || (errno == EAGAIN))
488 WARNING("pinba plugin: recvfrom(2) failed: %s",
489 sstrerror (errno, errbuf, sizeof (errbuf)));
492 else if (status == 0)
494 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
497 else /* if (status > 0) */
499 assert (((size_t) status) < buffer_size);
500 buffer_size = (size_t) status;
501 buffer[buffer_size] = 0;
503 status = pinba_process_stats_packet (buffer, buffer_size);
505 DEBUG("pinba plugin: Parsing packet failed.");
513 } /* }}} void pinba_udp_read_callback_fn */
515 static int receive_loop (void) /* {{{ */
519 s = pinba_socket_open (conf_node, conf_service);
522 ERROR ("pinba plugin: Collector thread is exiting prematurely.");
526 while (!collector_thread_do_shutdown)
534 status = poll (s->fd, s->fd_num, /* timeout = */ 1000);
535 if (status == 0) /* timeout */
543 if ((errno == EINTR) || (errno == EAGAIN))
546 ERROR ("pinba plugin: poll(2) failed: %s",
547 sstrerror (errno, errbuf, sizeof (errbuf)));
548 pinba_socket_free (s);
552 for (i = 0; i < s->fd_num; i++)
554 if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
556 pb_del_socket (s, i);
559 else if (s->fd[i].revents & (POLLIN | POLLPRI))
561 pinba_udp_read_callback_fn (s->fd[i].fd);
564 } /* while (!collector_thread_do_shutdown) */
566 pinba_socket_free (s);
570 } /* }}} int receive_loop */
572 static void *collector_thread (void *arg) /* {{{ */
576 memset (&collector_thread_id, 0, sizeof (collector_thread_id));
577 collector_thread_running = 0;
580 } /* }}} void *collector_thread */
583 * Plugin declaration section
586 static int config_set (char **var, const char *value) /* {{{ */
588 /* code from nginx plugin for collectd */
594 if ((*var = strdup (value)) == NULL) return (1);
596 } /* }}} int config_set */
598 static int plugin_config (oconfig_item_t *ci) /* {{{ */
602 service_statnode_begin();
604 for (i = 0; i < ci->children_num; i++) {
605 oconfig_item_t *child = ci->children + i;
606 if (strcasecmp ("Address", child->key) == 0) {
607 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
608 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
611 config_set(&conf_node, child->values[0].value.string);
612 } else if (strcasecmp ("Port", child->key) == 0) {
613 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
614 WARNING ("pinba plugin: `Port' needs exactly one string argument.");
617 config_set(&conf_service, child->values[0].value.string);
618 } else if (strcasecmp ("View", child->key) == 0) {
619 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
620 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
621 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
624 name = child->values[0].value.string;
625 for(o=0; o<child->children_num; o++){
626 oconfig_item_t *node = child->children + o;
627 if (strcasecmp ("Host", node->key) == 0) {
628 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
629 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
632 host = node->values[0].value.string;
633 } else if (strcasecmp ("Server", 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->Server' needs exactly one non-empty string argument.");
638 server = node->values[0].value.string;
639 } else if (strcasecmp ("Script", 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->Script' needs exactly one non-empty string argument.");
644 script = node->values[0].value.string;
646 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
650 /* add new statnode */
651 service_statnode_add(name, host, server, script);
653 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
658 service_statnode_end();
661 } /* int pinba_config */
663 static int plugin_init (void) /* {{{ */
667 if (collector_thread_running)
670 status = pthread_create (&collector_thread_id,
677 ERROR ("pinba plugin: pthread_create(3) failed: %s",
678 sstrerror (errno, errbuf, sizeof (errbuf)));
681 collector_thread_running = 1;
686 static int plugin_shutdown (void) /* {{{ */
688 if (collector_thread_running)
692 DEBUG ("pinba plugin: Shutting down collector thread.");
693 collector_thread_do_shutdown = 1;
695 status = pthread_join (collector_thread_id, /* retval = */ NULL);
699 ERROR ("pinba plugin: pthread_join(3) failed: %s",
700 sstrerror (status, errbuf, sizeof (errbuf)));
703 collector_thread_running = 0;
704 collector_thread_do_shutdown = 0;
705 } /* if (collector_thread_running) */
708 } /* }}} int plugin_shutdown */
710 static int plugin_submit (const char *plugin_instance, /* {{{ */
712 const pinba_statres *res) {
714 value_list_t vl = VALUE_LIST_INIT;
716 values[0].gauge = res->req_per_sec;
717 values[1].gauge = res->req_time;
718 values[2].gauge = res->ru_utime;
719 values[3].gauge = res->ru_stime;
720 values[4].gauge = res->doc_size;
721 values[5].gauge = res->mem_peak;
725 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
726 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
727 sstrncpy (vl.plugin_instance, plugin_instance,
728 sizeof(vl.plugin_instance));
729 sstrncpy (vl.type, type, sizeof (vl.type));
730 INFO("Pinba Dispatch");
731 plugin_dispatch_values (&vl);
734 } /* }}} int plugin_submit */
736 static int plugin_read (void) /* {{{ */
739 static pinba_statres res;
741 while ((i = service_statnode_collect (&res, i)) != 0)
743 plugin_submit(res.name, "pinba_view", &res);
747 } /* }}} int plugin_read */
749 void module_register (void) /* {{{ */
751 plugin_register_complex_config ("pinba", plugin_config);
752 plugin_register_init ("pinba", plugin_init);
753 plugin_register_read ("pinba", plugin_read);
754 plugin_register_shutdown ("pinba", plugin_shutdown);
755 } /* }}} void module_register */
757 /* vim: set sw=2 sts=2 et fdm=marker : */