1 /**
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
6 *
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.
10 *
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.
15 *
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
19 *
20 * Authors:
21 * Antony Dovgal <tony at daylessday.org>
22 * Phoenix Kayo <kayo.k11.4 at gmail.com>
23 * Florian Forster <octo at verplant.org>
24 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
31 #include <pthread.h>
32 #include <sys/socket.h>
33 #include <netdb.h>
34 #include <poll.h>
36 #include "pinba.pb-c.h"
38 /*
39 * Defines
40 */
41 #ifndef PINBA_UDP_BUFFER_SIZE
42 # define PINBA_UDP_BUFFER_SIZE 65536
43 #endif
45 #ifndef PINBA_DEFAULT_NODE
46 # define PINBA_DEFAULT_NODE "127.0.0.1" /* FIXME */
47 #endif
49 #ifndef PINBA_DEFAULT_SERVICE
50 # define PINBA_DEFAULT_SERVICE "12345" /* FIXME */
51 #endif
53 #ifndef PINBA_MAX_SOCKETS
54 # define PINBA_MAX_SOCKETS 16
55 #endif
57 /*
58 * Private data structures
59 */
60 /* {{{ */
61 struct pinba_socket_s
62 {
63 struct pollfd fd[PINBA_MAX_SOCKETS];
64 nfds_t fd_num;
65 };
66 typedef struct pinba_socket_s pinba_socket_t;
68 /* Fixed point counter value. n is the decimal part multiplied by 10^9. */
69 struct float_counter_s
70 {
71 uint64_t i;
72 uint64_t n; /* nanos */
73 };
74 typedef struct float_counter_s float_counter_t;
76 struct pinba_statnode_s
77 {
78 /* collector name, used as plugin instance */
79 char *name;
81 /* query data */
82 char *host;
83 char *server;
84 char *script;
86 derive_t req_count;
88 float_counter_t req_time;
89 float_counter_t ru_utime;
90 float_counter_t ru_stime;
92 derive_t doc_size;
93 gauge_t mem_peak;
94 };
95 typedef struct pinba_statnode_s pinba_statnode_t;
96 /* }}} */
98 /*
99 * Module global variables
100 */
101 /* {{{ */
102 static pinba_statnode_t *stat_nodes = NULL;
103 static unsigned int stat_nodes_num = 0;
104 static pthread_mutex_t stat_nodes_lock;
106 static char *conf_node = NULL;
107 static char *conf_service = NULL;
109 static _Bool collector_thread_running = 0;
110 static _Bool collector_thread_do_shutdown = 0;
111 static pthread_t collector_thread_id;
112 /* }}} */
114 /*
115 * Functions
116 */
117 static void float_counter_add (float_counter_t *fc, float val) /* {{{ */
118 {
119 uint64_t tmp;
121 if (val < 0.0)
122 return;
124 tmp = (uint64_t) val;
125 val -= (double) tmp;
127 fc->i += tmp;
128 fc->n += (uint64_t) ((val * 1000000000.0) + .5);
130 if (fc->n >= 1000000000)
131 {
132 fc->i += 1;
133 fc->n -= 1000000000;
134 assert (fc->n < 1000000000);
135 }
136 } /* }}} void float_counter_add */
138 static derive_t float_counter_get (const float_counter_t *fc) /* {{{ */
139 {
140 if (fc->n >= 500000000)
141 return ((derive_t) (fc->i + 1));
142 else
143 return ((derive_t) fc->i);
144 } /* }}} derive_t float_counter_get */
146 static void strset (char **str, const char *new) /* {{{ */
147 {
148 char *tmp;
150 if (!str || !new)
151 return;
153 tmp = strdup (new);
154 if (tmp == NULL)
155 return;
157 sfree (*str);
158 *str = tmp;
159 } /* }}} void strset */
161 static void service_statnode_add(const char *name, /* {{{ */
162 const char *host,
163 const char *server,
164 const char *script)
165 {
166 pinba_statnode_t *node;
168 node = realloc (stat_nodes,
169 sizeof (*stat_nodes) * (stat_nodes_num + 1));
170 if (node == NULL)
171 {
172 ERROR ("pinba plugin: realloc failed");
173 return;
174 }
175 stat_nodes = node;
177 node = stat_nodes + stat_nodes_num;
178 memset (node, 0, sizeof (*node));
180 /* reset strings */
181 node->name = NULL;
182 node->host = NULL;
183 node->server = NULL;
184 node->script = NULL;
186 node->mem_peak = NAN;
188 /* fill query data */
189 strset(&node->name, name);
190 strset(&node->host, host);
191 strset(&node->server, server);
192 strset(&node->script, script);
194 /* increment counter */
195 stat_nodes_num++;
196 } /* }}} void service_statnode_add */
198 /* Copy the data from the global "stat_nodes" list into the buffer pointed to
199 * by "res", doing the derivation in the process. Returns the next index or
200 * zero if the end of the list has been reached. */
201 static unsigned int service_statnode_collect (pinba_statnode_t *res, /* {{{ */
202 unsigned int index)
203 {
204 pinba_statnode_t *node;
206 if (stat_nodes_num == 0)
207 return 0;
209 /* begin collecting */
210 if (index == 0)
211 pthread_mutex_lock (&stat_nodes_lock);
213 /* end collecting */
214 if (index >= stat_nodes_num)
215 {
216 pthread_mutex_unlock (&stat_nodes_lock);
217 return 0;
218 }
220 node = stat_nodes + index;
221 memcpy (res, node, sizeof (*res));
223 /* reset node */
224 node->mem_peak = NAN;
226 return (index + 1);
227 } /* }}} unsigned int service_statnode_collect */
229 static void service_statnode_process (pinba_statnode_t *node, /* {{{ */
230 Pinba__Request* request)
231 {
232 node->req_count++;
234 float_counter_add (&node->req_time, request->request_time);
235 float_counter_add (&node->ru_utime, request->ru_utime);
236 float_counter_add (&node->ru_stime, request->ru_stime);
238 node->doc_size += request->document_size;
240 if (isnan (node->mem_peak)
241 || (node->mem_peak < ((gauge_t) request->memory_peak)))
242 node->mem_peak = (gauge_t) request->memory_peak;
244 } /* }}} void service_statnode_process */
246 static void service_process_request (Pinba__Request *request) /* {{{ */
247 {
248 unsigned int i;
250 pthread_mutex_lock (&stat_nodes_lock);
252 for (i = 0; i < stat_nodes_num; i++)
253 {
254 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
255 continue;
256 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
257 continue;
258 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
259 continue;
261 service_statnode_process(&stat_nodes[i], request);
262 }
264 pthread_mutex_unlock(&stat_nodes_lock);
265 } /* }}} void service_process_request */
267 static int pb_del_socket (pinba_socket_t *s, /* {{{ */
268 nfds_t index)
269 {
270 if (index >= s->fd_num)
271 return (EINVAL);
273 close (s->fd[index].fd);
274 s->fd[index].fd = -1;
276 /* When deleting the last element in the list, no memmove is necessary. */
277 if (index < (s->fd_num - 1))
278 {
279 memmove (&s->fd[index], &s->fd[index + 1],
280 sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
281 }
283 s->fd_num--;
284 return (0);
285 } /* }}} int pb_del_socket */
287 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
288 const struct addrinfo *ai)
289 {
290 int fd;
291 int tmp;
292 int status;
294 if (s->fd_num == PINBA_MAX_SOCKETS)
295 {
296 WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
297 "%i sockets. Please complain to the collectd developers so we can "
298 "raise the limit.", PINBA_MAX_SOCKETS);
299 return (-1);
300 }
302 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
303 if (fd < 0)
304 {
305 char errbuf[1024];
306 ERROR ("pinba plugin: socket(2) failed: %s",
307 sstrerror (errno, errbuf, sizeof (errbuf)));
308 return (0);
309 }
311 tmp = 1;
312 status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
313 if (status != 0)
314 {
315 char errbuf[1024];
316 WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
317 sstrerror (errno, errbuf, sizeof (errbuf)));
318 }
320 status = bind (fd, ai->ai_addr, ai->ai_addrlen);
321 if (status != 0)
322 {
323 char errbuf[1024];
324 ERROR ("pinba plugin: bind(2) failed: %s",
325 sstrerror (errno, errbuf, sizeof (errbuf)));
326 return (0);
327 }
329 s->fd[s->fd_num].fd = fd;
330 s->fd[s->fd_num].events = POLLIN | POLLPRI;
331 s->fd[s->fd_num].revents = 0;
332 s->fd_num++;
334 return (0);
335 } /* }}} int pb_add_socket */
337 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
338 const char *service)
339 {
340 pinba_socket_t *s;
341 struct addrinfo *ai_list;
342 struct addrinfo *ai_ptr;
343 struct addrinfo ai_hints;
344 int status;
346 memset (&ai_hints, 0, sizeof (ai_hints));
347 ai_hints.ai_flags = AI_PASSIVE;
348 ai_hints.ai_family = AF_UNSPEC;
349 ai_hints.ai_socktype = SOCK_DGRAM;
350 ai_hints.ai_addr = NULL;
351 ai_hints.ai_canonname = NULL;
352 ai_hints.ai_next = NULL;
354 if (node == NULL)
355 node = PINBA_DEFAULT_NODE;
357 if (service == NULL)
358 service = PINBA_DEFAULT_SERVICE;
360 ai_list = NULL;
361 status = getaddrinfo (node, service,
362 &ai_hints, &ai_list);
363 if (status != 0)
364 {
365 ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
366 gai_strerror (status));
367 return (NULL);
368 }
369 assert (ai_list != NULL);
371 s = malloc (sizeof (*s));
372 if (s != NULL)
373 {
374 freeaddrinfo (ai_list);
375 ERROR ("pinba plugin: malloc failed.");
376 return (NULL);
377 }
378 memset (s, 0, sizeof (*s));
380 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
381 {
382 status = pb_add_socket (s, ai_ptr);
383 if (status != 0)
384 break;
385 } /* for (ai_list) */
387 freeaddrinfo (ai_list);
389 if (s->fd_num < 1)
390 {
391 WARNING ("pinba plugin: Unable to open socket for address %s.", node);
392 sfree (s);
393 s = NULL;
394 }
396 return (s);
397 } /* }}} pinba_socket_open */
399 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
400 {
401 nfds_t i;
403 if (!socket)
404 return;
406 for (i = 0; i < socket->fd_num; i++)
407 {
408 if (socket->fd[i].fd < 0)
409 continue;
410 close (socket->fd[i].fd);
411 socket->fd[i].fd = -1;
412 }
414 sfree(socket);
415 } /* }}} void pinba_socket_free */
417 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
418 size_t buffer_size)
419 {
420 Pinba__Request *request;
422 request = pinba__request__unpack (NULL, buffer_size, buffer);
424 if (!request)
425 return (-1);
427 service_process_request(request);
428 pinba__request__free_unpacked (request, NULL);
430 return (0);
431 } /* }}} int pinba_process_stats_packet */
433 static int pinba_udp_read_callback_fn (int sock) /* {{{ */
434 {
435 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
436 size_t buffer_size;
437 int status;
439 while (42)
440 {
441 buffer_size = sizeof (buffer);
442 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
443 if (status < 0)
444 {
445 char errbuf[1024];
447 if ((errno == EINTR)
448 #ifdef EWOULDBLOCK
449 || (errno == EWOULDBLOCK)
450 #endif
451 || (errno == EAGAIN))
452 {
453 continue;
454 }
456 WARNING("pinba plugin: recvfrom(2) failed: %s",
457 sstrerror (errno, errbuf, sizeof (errbuf)));
458 return (-1);
459 }
460 else if (status == 0)
461 {
462 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
463 return (-1);
464 }
465 else /* if (status > 0) */
466 {
467 assert (((size_t) status) < buffer_size);
468 buffer_size = (size_t) status;
469 buffer[buffer_size] = 0;
471 status = pinba_process_stats_packet (buffer, buffer_size);
472 if (status != 0)
473 DEBUG("pinba plugin: Parsing packet failed.");
474 return (status);
475 }
476 } /* while (42) */
478 /* not reached */
479 assert (23 == 42);
480 return (-1);
481 } /* }}} void pinba_udp_read_callback_fn */
483 static int receive_loop (void) /* {{{ */
484 {
485 pinba_socket_t *s;
487 s = pinba_socket_open (conf_node, conf_service);
488 if (s == NULL)
489 {
490 ERROR ("pinba plugin: Collector thread is exiting prematurely.");
491 return (-1);
492 }
494 while (!collector_thread_do_shutdown)
495 {
496 int status;
497 nfds_t i;
499 if (s->fd_num < 1)
500 break;
502 status = poll (s->fd, s->fd_num, /* timeout = */ 1000);
503 if (status == 0) /* timeout */
504 {
505 continue;
506 }
507 else if (status < 0)
508 {
509 char errbuf[1024];
511 if ((errno == EINTR) || (errno == EAGAIN))
512 continue;
514 ERROR ("pinba plugin: poll(2) failed: %s",
515 sstrerror (errno, errbuf, sizeof (errbuf)));
516 pinba_socket_free (s);
517 return (-1);
518 }
520 for (i = 0; i < s->fd_num; i++)
521 {
522 if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
523 {
524 pb_del_socket (s, i);
525 i--;
526 }
527 else if (s->fd[i].revents & (POLLIN | POLLPRI))
528 {
529 pinba_udp_read_callback_fn (s->fd[i].fd);
530 }
531 } /* for (s->fd) */
532 } /* while (!collector_thread_do_shutdown) */
534 pinba_socket_free (s);
535 s = NULL;
537 return (0);
538 } /* }}} int receive_loop */
540 static void *collector_thread (void *arg) /* {{{ */
541 {
542 receive_loop ();
544 memset (&collector_thread_id, 0, sizeof (collector_thread_id));
545 collector_thread_running = 0;
546 pthread_exit (NULL);
547 return (NULL);
548 } /* }}} void *collector_thread */
550 /*
551 * Plugin declaration section
552 */
553 static int pinba_config_view (const oconfig_item_t *ci) /* {{{ */
554 {
555 char *name = NULL;
556 char *host = NULL;
557 char *server = NULL;
558 char *script = NULL;
559 int status;
560 int i;
562 status = cf_util_get_string (ci, &name);
563 if (status != 0)
564 return (status);
566 for (i = 0; i < ci->children_num; i++)
567 {
568 oconfig_item_t *child = ci->children + i;
570 if (strcasecmp ("Host", child->key) == 0)
571 status = cf_util_get_string (child, &host);
572 else if (strcasecmp ("Server", child->key) == 0)
573 status = cf_util_get_string (child, &server);
574 else if (strcasecmp ("Script", child->key) == 0)
575 status = cf_util_get_string (child, &script);
576 else
577 {
578 WARNING ("pinba plugin: Unknown config option: %s", child->key);
579 status = -1;
580 }
582 if (status != 0)
583 break;
584 }
586 if (status == 0)
587 service_statnode_add (name, host, server, script);
589 sfree (name);
590 sfree (host);
591 sfree (server);
592 sfree (script);
594 return (status);
595 } /* }}} int pinba_config_view */
597 static int plugin_config (oconfig_item_t *ci) /* {{{ */
598 {
599 int i;
601 /* The lock should not be necessary in the config callback, but let's be
602 * sure.. */
603 pthread_mutex_lock (&stat_nodes_lock);
605 for (i = 0; i < ci->children_num; i++)
606 {
607 oconfig_item_t *child = ci->children + i;
609 if (strcasecmp ("Address", child->key) == 0)
610 cf_util_get_string (child, &conf_node);
611 else if (strcasecmp ("Port", child->key) == 0)
612 cf_util_get_string (child, &conf_service);
613 else if (strcasecmp ("View", child->key) == 0)
614 pinba_config_view (child);
615 else
616 WARNING ("pinba plugin: Unknown config option: %s", child->key);
617 }
619 pthread_mutex_unlock(&stat_nodes_lock);
621 return (0);
622 } /* }}} int pinba_config */
624 static int plugin_init (void) /* {{{ */
625 {
626 int status;
628 if (stat_nodes == NULL)
629 {
630 /* Collect the "total" data by default. */
631 service_statnode_add ("total",
632 /* host = */ NULL,
633 /* server = */ NULL,
634 /* script = */ NULL);
635 }
637 if (collector_thread_running)
638 return (0);
640 status = pthread_create (&collector_thread_id,
641 /* attrs = */ NULL,
642 collector_thread,
643 /* args = */ NULL);
644 if (status != 0)
645 {
646 char errbuf[1024];
647 ERROR ("pinba plugin: pthread_create(3) failed: %s",
648 sstrerror (errno, errbuf, sizeof (errbuf)));
649 return (-1);
650 }
651 collector_thread_running = 1;
653 return (0);
654 } /* }}} */
656 static int plugin_shutdown (void) /* {{{ */
657 {
658 if (collector_thread_running)
659 {
660 int status;
662 DEBUG ("pinba plugin: Shutting down collector thread.");
663 collector_thread_do_shutdown = 1;
665 status = pthread_join (collector_thread_id, /* retval = */ NULL);
666 if (status != 0)
667 {
668 char errbuf[1024];
669 ERROR ("pinba plugin: pthread_join(3) failed: %s",
670 sstrerror (status, errbuf, sizeof (errbuf)));
671 }
673 collector_thread_running = 0;
674 collector_thread_do_shutdown = 0;
675 } /* if (collector_thread_running) */
677 return (0);
678 } /* }}} int plugin_shutdown */
680 static int plugin_submit (const pinba_statnode_t *res) /* {{{ */
681 {
682 value_t value;
683 value_list_t vl = VALUE_LIST_INIT;
685 vl.values = &value;
686 vl.values_len = 1;
687 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
688 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
689 sstrncpy (vl.plugin_instance, res->name, sizeof (vl.plugin_instance));
691 value.derive = res->req_count;
692 sstrncpy (vl.type, "requests", sizeof (vl.type));
693 plugin_dispatch_values (&vl);
695 value.derive = float_counter_get (&res->req_time);
696 sstrncpy (vl.type, "total_time", sizeof (vl.type));
697 plugin_dispatch_values (&vl);
699 value.derive = float_counter_get (&res->ru_utime);
700 sstrncpy (vl.type, "cpu", sizeof (vl.type));
701 sstrncpy (vl.type_instance, "user", sizeof (vl.type_instance));
702 plugin_dispatch_values (&vl);
704 value.derive = float_counter_get (&res->ru_stime);
705 sstrncpy (vl.type, "cpu", sizeof (vl.type));
706 sstrncpy (vl.type_instance, "system", sizeof (vl.type_instance));
707 plugin_dispatch_values (&vl);
709 value.gauge = res->mem_peak;
710 sstrncpy (vl.type, "memory", sizeof (vl.type));
711 sstrncpy (vl.type_instance, "peak", sizeof (vl.type_instance));
712 plugin_dispatch_values (&vl);
714 return (0);
715 } /* }}} int plugin_submit */
717 static int plugin_read (void) /* {{{ */
718 {
719 unsigned int i=0;
720 pinba_statnode_t data;
722 while ((i = service_statnode_collect (&data, i)) != 0)
723 {
724 plugin_submit (&data);
725 }
727 return 0;
728 } /* }}} int plugin_read */
730 void module_register (void) /* {{{ */
731 {
732 plugin_register_complex_config ("pinba", plugin_config);
733 plugin_register_init ("pinba", plugin_init);
734 plugin_register_read ("pinba", plugin_read);
735 plugin_register_shutdown ("pinba", plugin_shutdown);
736 } /* }}} void module_register */
738 /* vim: set sw=2 sts=2 et fdm=marker : */