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 "::0"
47 #endif
49 #ifndef PINBA_DEFAULT_SERVICE
50 # define PINBA_DEFAULT_SERVICE "30002"
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 uint64_t factor)
140 {
141 derive_t ret;
143 ret = (derive_t) (fc->i * factor);
144 ret += (derive_t) (fc->n / (1000000000 / factor));
146 return (ret);
147 } /* }}} derive_t float_counter_get */
149 static void strset (char **str, const char *new) /* {{{ */
150 {
151 char *tmp;
153 if (!str || !new)
154 return;
156 tmp = strdup (new);
157 if (tmp == NULL)
158 return;
160 sfree (*str);
161 *str = tmp;
162 } /* }}} void strset */
164 static void service_statnode_add(const char *name, /* {{{ */
165 const char *host,
166 const char *server,
167 const char *script)
168 {
169 pinba_statnode_t *node;
171 node = realloc (stat_nodes,
172 sizeof (*stat_nodes) * (stat_nodes_num + 1));
173 if (node == NULL)
174 {
175 ERROR ("pinba plugin: realloc failed");
176 return;
177 }
178 stat_nodes = node;
180 node = stat_nodes + stat_nodes_num;
181 memset (node, 0, sizeof (*node));
183 /* reset strings */
184 node->name = NULL;
185 node->host = NULL;
186 node->server = NULL;
187 node->script = NULL;
189 node->mem_peak = NAN;
191 /* fill query data */
192 strset (&node->name, name);
193 strset (&node->host, host);
194 strset (&node->server, server);
195 strset (&node->script, script);
197 /* increment counter */
198 stat_nodes_num++;
199 } /* }}} void service_statnode_add */
201 /* Copy the data from the global "stat_nodes" list into the buffer pointed to
202 * by "res", doing the derivation in the process. Returns the next index or
203 * zero if the end of the list has been reached. */
204 static unsigned int service_statnode_collect (pinba_statnode_t *res, /* {{{ */
205 unsigned int index)
206 {
207 pinba_statnode_t *node;
209 if (stat_nodes_num == 0)
210 return 0;
212 /* begin collecting */
213 if (index == 0)
214 pthread_mutex_lock (&stat_nodes_lock);
216 /* end collecting */
217 if (index >= stat_nodes_num)
218 {
219 pthread_mutex_unlock (&stat_nodes_lock);
220 return 0;
221 }
223 node = stat_nodes + index;
224 memcpy (res, node, sizeof (*res));
226 /* reset node */
227 node->mem_peak = NAN;
229 return (index + 1);
230 } /* }}} unsigned int service_statnode_collect */
232 static void service_statnode_process (pinba_statnode_t *node, /* {{{ */
233 Pinba__Request* request)
234 {
235 node->req_count++;
237 float_counter_add (&node->req_time, request->request_time);
238 float_counter_add (&node->ru_utime, request->ru_utime);
239 float_counter_add (&node->ru_stime, request->ru_stime);
241 node->doc_size += request->document_size;
243 if (isnan (node->mem_peak)
244 || (node->mem_peak < ((gauge_t) request->memory_peak)))
245 node->mem_peak = (gauge_t) request->memory_peak;
247 } /* }}} void service_statnode_process */
249 static void service_process_request (Pinba__Request *request) /* {{{ */
250 {
251 unsigned int i;
253 pthread_mutex_lock (&stat_nodes_lock);
255 for (i = 0; i < stat_nodes_num; i++)
256 {
257 if ((stat_nodes[i].host != NULL)
258 && (strcmp (request->hostname, stat_nodes[i].host) != 0))
259 continue;
261 if ((stat_nodes[i].server != NULL)
262 && (strcmp (request->server_name, stat_nodes[i].server) != 0))
263 continue;
265 if ((stat_nodes[i].script != NULL)
266 && (strcmp (request->script_name, stat_nodes[i].script) != 0))
267 continue;
269 service_statnode_process(&stat_nodes[i], request);
270 }
272 pthread_mutex_unlock(&stat_nodes_lock);
273 } /* }}} void service_process_request */
275 static int pb_del_socket (pinba_socket_t *s, /* {{{ */
276 nfds_t index)
277 {
278 if (index >= s->fd_num)
279 return (EINVAL);
281 close (s->fd[index].fd);
282 s->fd[index].fd = -1;
284 /* When deleting the last element in the list, no memmove is necessary. */
285 if (index < (s->fd_num - 1))
286 {
287 memmove (&s->fd[index], &s->fd[index + 1],
288 sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
289 }
291 s->fd_num--;
292 return (0);
293 } /* }}} int pb_del_socket */
295 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
296 const struct addrinfo *ai)
297 {
298 int fd;
299 int tmp;
300 int status;
302 if (s->fd_num == PINBA_MAX_SOCKETS)
303 {
304 WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
305 "%i sockets. Please complain to the collectd developers so we can "
306 "raise the limit.", PINBA_MAX_SOCKETS);
307 return (-1);
308 }
310 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
311 if (fd < 0)
312 {
313 char errbuf[1024];
314 ERROR ("pinba plugin: socket(2) failed: %s",
315 sstrerror (errno, errbuf, sizeof (errbuf)));
316 return (0);
317 }
319 tmp = 1;
320 status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
321 if (status != 0)
322 {
323 char errbuf[1024];
324 WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
325 sstrerror (errno, errbuf, sizeof (errbuf)));
326 }
328 status = bind (fd, ai->ai_addr, ai->ai_addrlen);
329 if (status != 0)
330 {
331 char errbuf[1024];
332 ERROR ("pinba plugin: bind(2) failed: %s",
333 sstrerror (errno, errbuf, sizeof (errbuf)));
334 return (0);
335 }
337 s->fd[s->fd_num].fd = fd;
338 s->fd[s->fd_num].events = POLLIN | POLLPRI;
339 s->fd[s->fd_num].revents = 0;
340 s->fd_num++;
342 return (0);
343 } /* }}} int pb_add_socket */
345 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
346 const char *service)
347 {
348 pinba_socket_t *s;
349 struct addrinfo *ai_list;
350 struct addrinfo *ai_ptr;
351 struct addrinfo ai_hints;
352 int status;
354 memset (&ai_hints, 0, sizeof (ai_hints));
355 ai_hints.ai_flags = AI_PASSIVE;
356 ai_hints.ai_family = AF_UNSPEC;
357 ai_hints.ai_socktype = SOCK_DGRAM;
358 ai_hints.ai_addr = NULL;
359 ai_hints.ai_canonname = NULL;
360 ai_hints.ai_next = NULL;
362 if (node == NULL)
363 node = PINBA_DEFAULT_NODE;
365 if (service == NULL)
366 service = PINBA_DEFAULT_SERVICE;
368 ai_list = NULL;
369 status = getaddrinfo (node, service,
370 &ai_hints, &ai_list);
371 if (status != 0)
372 {
373 ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
374 gai_strerror (status));
375 return (NULL);
376 }
377 assert (ai_list != NULL);
379 s = malloc (sizeof (*s));
380 if (s == NULL)
381 {
382 freeaddrinfo (ai_list);
383 ERROR ("pinba plugin: malloc failed.");
384 return (NULL);
385 }
386 memset (s, 0, sizeof (*s));
388 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
389 {
390 status = pb_add_socket (s, ai_ptr);
391 if (status != 0)
392 break;
393 } /* for (ai_list) */
395 freeaddrinfo (ai_list);
397 if (s->fd_num < 1)
398 {
399 WARNING ("pinba plugin: Unable to open socket for address %s.", node);
400 sfree (s);
401 s = NULL;
402 }
404 return (s);
405 } /* }}} pinba_socket_open */
407 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
408 {
409 nfds_t i;
411 if (!socket)
412 return;
414 for (i = 0; i < socket->fd_num; i++)
415 {
416 if (socket->fd[i].fd < 0)
417 continue;
418 close (socket->fd[i].fd);
419 socket->fd[i].fd = -1;
420 }
422 sfree(socket);
423 } /* }}} void pinba_socket_free */
425 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
426 size_t buffer_size)
427 {
428 Pinba__Request *request;
430 request = pinba__request__unpack (NULL, buffer_size, buffer);
432 if (!request)
433 return (-1);
435 service_process_request(request);
436 pinba__request__free_unpacked (request, NULL);
438 return (0);
439 } /* }}} int pinba_process_stats_packet */
441 static int pinba_udp_read_callback_fn (int sock) /* {{{ */
442 {
443 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
444 size_t buffer_size;
445 int status;
447 while (42)
448 {
449 buffer_size = sizeof (buffer);
450 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
451 if (status < 0)
452 {
453 char errbuf[1024];
455 if ((errno == EINTR)
456 #ifdef EWOULDBLOCK
457 || (errno == EWOULDBLOCK)
458 #endif
459 || (errno == EAGAIN))
460 {
461 continue;
462 }
464 WARNING("pinba plugin: recvfrom(2) failed: %s",
465 sstrerror (errno, errbuf, sizeof (errbuf)));
466 return (-1);
467 }
468 else if (status == 0)
469 {
470 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
471 return (-1);
472 }
473 else /* if (status > 0) */
474 {
475 assert (((size_t) status) < buffer_size);
476 buffer_size = (size_t) status;
477 buffer[buffer_size] = 0;
479 status = pinba_process_stats_packet (buffer, buffer_size);
480 if (status != 0)
481 DEBUG("pinba plugin: Parsing packet failed.");
482 return (status);
483 }
484 } /* while (42) */
486 /* not reached */
487 assert (23 == 42);
488 return (-1);
489 } /* }}} void pinba_udp_read_callback_fn */
491 static int receive_loop (void) /* {{{ */
492 {
493 pinba_socket_t *s;
495 s = pinba_socket_open (conf_node, conf_service);
496 if (s == NULL)
497 {
498 ERROR ("pinba plugin: Collector thread is exiting prematurely.");
499 return (-1);
500 }
502 while (!collector_thread_do_shutdown)
503 {
504 int status;
505 nfds_t i;
507 if (s->fd_num < 1)
508 break;
510 status = poll (s->fd, s->fd_num, /* timeout = */ 1000);
511 if (status == 0) /* timeout */
512 {
513 continue;
514 }
515 else if (status < 0)
516 {
517 char errbuf[1024];
519 if ((errno == EINTR) || (errno == EAGAIN))
520 continue;
522 ERROR ("pinba plugin: poll(2) failed: %s",
523 sstrerror (errno, errbuf, sizeof (errbuf)));
524 pinba_socket_free (s);
525 return (-1);
526 }
528 for (i = 0; i < s->fd_num; i++)
529 {
530 if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
531 {
532 pb_del_socket (s, i);
533 i--;
534 }
535 else if (s->fd[i].revents & (POLLIN | POLLPRI))
536 {
537 pinba_udp_read_callback_fn (s->fd[i].fd);
538 }
539 } /* for (s->fd) */
540 } /* while (!collector_thread_do_shutdown) */
542 pinba_socket_free (s);
543 s = NULL;
545 return (0);
546 } /* }}} int receive_loop */
548 static void *collector_thread (void *arg) /* {{{ */
549 {
550 receive_loop ();
552 memset (&collector_thread_id, 0, sizeof (collector_thread_id));
553 collector_thread_running = 0;
554 pthread_exit (NULL);
555 return (NULL);
556 } /* }}} void *collector_thread */
558 /*
559 * Plugin declaration section
560 */
561 static int pinba_config_view (const oconfig_item_t *ci) /* {{{ */
562 {
563 char *name = NULL;
564 char *host = NULL;
565 char *server = NULL;
566 char *script = NULL;
567 int status;
568 int i;
570 status = cf_util_get_string (ci, &name);
571 if (status != 0)
572 return (status);
574 for (i = 0; i < ci->children_num; i++)
575 {
576 oconfig_item_t *child = ci->children + i;
578 if (strcasecmp ("Host", child->key) == 0)
579 status = cf_util_get_string (child, &host);
580 else if (strcasecmp ("Server", child->key) == 0)
581 status = cf_util_get_string (child, &server);
582 else if (strcasecmp ("Script", child->key) == 0)
583 status = cf_util_get_string (child, &script);
584 else
585 {
586 WARNING ("pinba plugin: Unknown config option: %s", child->key);
587 status = -1;
588 }
590 if (status != 0)
591 break;
592 }
594 if (status == 0)
595 service_statnode_add (name, host, server, script);
597 sfree (name);
598 sfree (host);
599 sfree (server);
600 sfree (script);
602 return (status);
603 } /* }}} int pinba_config_view */
605 static int plugin_config (oconfig_item_t *ci) /* {{{ */
606 {
607 int i;
609 /* The lock should not be necessary in the config callback, but let's be
610 * sure.. */
611 pthread_mutex_lock (&stat_nodes_lock);
613 for (i = 0; i < ci->children_num; i++)
614 {
615 oconfig_item_t *child = ci->children + i;
617 if (strcasecmp ("Address", child->key) == 0)
618 cf_util_get_string (child, &conf_node);
619 else if (strcasecmp ("Port", child->key) == 0)
620 cf_util_get_service (child, &conf_service);
621 else if (strcasecmp ("View", child->key) == 0)
622 pinba_config_view (child);
623 else
624 WARNING ("pinba plugin: Unknown config option: %s", child->key);
625 }
627 pthread_mutex_unlock(&stat_nodes_lock);
629 return (0);
630 } /* }}} int pinba_config */
632 static int plugin_init (void) /* {{{ */
633 {
634 int status;
636 if (stat_nodes == NULL)
637 {
638 /* Collect the "total" data by default. */
639 service_statnode_add ("total",
640 /* host = */ NULL,
641 /* server = */ NULL,
642 /* script = */ NULL);
643 }
645 if (collector_thread_running)
646 return (0);
648 status = pthread_create (&collector_thread_id,
649 /* attrs = */ NULL,
650 collector_thread,
651 /* args = */ NULL);
652 if (status != 0)
653 {
654 char errbuf[1024];
655 ERROR ("pinba plugin: pthread_create(3) failed: %s",
656 sstrerror (errno, errbuf, sizeof (errbuf)));
657 return (-1);
658 }
659 collector_thread_running = 1;
661 return (0);
662 } /* }}} */
664 static int plugin_shutdown (void) /* {{{ */
665 {
666 if (collector_thread_running)
667 {
668 int status;
670 DEBUG ("pinba plugin: Shutting down collector thread.");
671 collector_thread_do_shutdown = 1;
673 status = pthread_join (collector_thread_id, /* retval = */ NULL);
674 if (status != 0)
675 {
676 char errbuf[1024];
677 ERROR ("pinba plugin: pthread_join(3) failed: %s",
678 sstrerror (status, errbuf, sizeof (errbuf)));
679 }
681 collector_thread_running = 0;
682 collector_thread_do_shutdown = 0;
683 } /* if (collector_thread_running) */
685 return (0);
686 } /* }}} int plugin_shutdown */
688 static int plugin_submit (const pinba_statnode_t *res) /* {{{ */
689 {
690 value_t value;
691 value_list_t vl = VALUE_LIST_INIT;
693 vl.values = &value;
694 vl.values_len = 1;
695 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
696 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
697 sstrncpy (vl.plugin_instance, res->name, sizeof (vl.plugin_instance));
699 value.derive = res->req_count;
700 sstrncpy (vl.type, "total_requests", sizeof (vl.type));
701 plugin_dispatch_values (&vl);
703 value.derive = float_counter_get (&res->req_time, /* factor = */ 1000);
704 sstrncpy (vl.type, "total_time_in_ms", sizeof (vl.type));
705 plugin_dispatch_values (&vl);
707 value.derive = res->doc_size;
708 sstrncpy (vl.type, "total_bytes", sizeof (vl.type));
709 plugin_dispatch_values (&vl);
711 value.derive = float_counter_get (&res->ru_utime, /* factor = */ 100);
712 sstrncpy (vl.type, "cpu", sizeof (vl.type));
713 sstrncpy (vl.type_instance, "user", sizeof (vl.type_instance));
714 plugin_dispatch_values (&vl);
716 value.derive = float_counter_get (&res->ru_stime, /* factor = */ 100);
717 sstrncpy (vl.type, "cpu", sizeof (vl.type));
718 sstrncpy (vl.type_instance, "system", sizeof (vl.type_instance));
719 plugin_dispatch_values (&vl);
721 value.gauge = res->mem_peak;
722 sstrncpy (vl.type, "memory", sizeof (vl.type));
723 sstrncpy (vl.type_instance, "peak", sizeof (vl.type_instance));
724 plugin_dispatch_values (&vl);
726 return (0);
727 } /* }}} int plugin_submit */
729 static int plugin_read (void) /* {{{ */
730 {
731 unsigned int i=0;
732 pinba_statnode_t data;
734 while ((i = service_statnode_collect (&data, i)) != 0)
735 {
736 plugin_submit (&data);
737 }
739 return 0;
740 } /* }}} int plugin_read */
742 void module_register (void) /* {{{ */
743 {
744 plugin_register_complex_config ("pinba", plugin_config);
745 plugin_register_init ("pinba", plugin_init);
746 plugin_register_read ("pinba", plugin_read);
747 plugin_register_shutdown ("pinba", plugin_shutdown);
748 } /* }}} void module_register */
750 /* vim: set sw=2 sts=2 et fdm=marker : */