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>
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
64 struct pinba_socket_s {
65 struct pollfd fd[PINBA_MAX_SOCKETS];
68 typedef struct pinba_socket_s pinba_socket_t;
70 /* Fixed point counter value. n is the decimal part multiplied by 10^9. */
71 struct float_counter_s {
73 uint64_t n; /* nanos */
75 typedef struct float_counter_s float_counter_t;
77 struct pinba_statnode_s {
78 /* collector name, used as plugin instance */
88 float_counter_t req_time;
89 float_counter_t ru_utime;
90 float_counter_t ru_stime;
95 typedef struct pinba_statnode_s pinba_statnode_t;
99 * Module global variables
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;
117 static void float_counter_add(float_counter_t *fc, float val) /* {{{ */
128 fc->n += (uint64_t)((val * 1000000000.0) + .5);
130 if (fc->n >= 1000000000) {
133 assert(fc->n < 1000000000);
135 } /* }}} void float_counter_add */
137 static derive_t float_counter_get(const float_counter_t *fc, /* {{{ */
141 ret = (derive_t)(fc->i * factor);
142 ret += (derive_t)(fc->n / (1000000000 / factor));
145 } /* }}} derive_t float_counter_get */
147 static void strset(char **str, const char *new) /* {{{ */
160 } /* }}} void strset */
162 static void service_statnode_add(const char *name, /* {{{ */
163 const char *host, const char *server,
164 const char *script) {
165 pinba_statnode_t *node;
167 node = realloc(stat_nodes, sizeof(*stat_nodes) * (stat_nodes_num + 1));
169 ERROR("pinba plugin: realloc failed");
174 node = stat_nodes + stat_nodes_num;
175 memset(node, 0, sizeof(*node));
183 node->mem_peak = NAN;
185 /* fill query data */
186 strset(&node->name, name);
187 strset(&node->host, host);
188 strset(&node->server, server);
189 strset(&node->script, script);
191 /* increment counter */
193 } /* }}} void service_statnode_add */
195 /* Copy the data from the global "stat_nodes" list into the buffer pointed to
196 * by "res", doing the derivation in the process. Returns the next index or
197 * zero if the end of the list has been reached. */
198 static unsigned int service_statnode_collect(pinba_statnode_t *res, /* {{{ */
199 unsigned int index) {
200 pinba_statnode_t *node;
202 if (stat_nodes_num == 0)
205 /* begin collecting */
207 pthread_mutex_lock(&stat_nodes_lock);
210 if (index >= stat_nodes_num) {
211 pthread_mutex_unlock(&stat_nodes_lock);
215 node = stat_nodes + index;
216 memcpy(res, node, sizeof(*res));
219 node->mem_peak = NAN;
222 } /* }}} unsigned int service_statnode_collect */
224 static void service_statnode_process(pinba_statnode_t *node, /* {{{ */
225 Pinba__Request *request) {
228 float_counter_add(&node->req_time, request->request_time);
229 float_counter_add(&node->ru_utime, request->ru_utime);
230 float_counter_add(&node->ru_stime, request->ru_stime);
232 node->doc_size += request->document_size;
234 if (isnan(node->mem_peak) ||
235 (node->mem_peak < ((gauge_t)request->memory_peak)))
236 node->mem_peak = (gauge_t)request->memory_peak;
238 } /* }}} void service_statnode_process */
240 static void service_process_request(Pinba__Request *request) /* {{{ */
242 pthread_mutex_lock(&stat_nodes_lock);
244 for (unsigned int i = 0; i < stat_nodes_num; i++) {
245 if ((stat_nodes[i].host != NULL) &&
246 (strcmp(request->hostname, stat_nodes[i].host) != 0))
249 if ((stat_nodes[i].server != NULL) &&
250 (strcmp(request->server_name, stat_nodes[i].server) != 0))
253 if ((stat_nodes[i].script != NULL) &&
254 (strcmp(request->script_name, stat_nodes[i].script) != 0))
257 service_statnode_process(&stat_nodes[i], request);
260 pthread_mutex_unlock(&stat_nodes_lock);
261 } /* }}} void service_process_request */
263 static int pb_del_socket(pinba_socket_t *s, /* {{{ */
265 if (index >= s->fd_num)
268 close(s->fd[index].fd);
269 s->fd[index].fd = -1;
271 /* When deleting the last element in the list, no memmove is necessary. */
272 if (index < (s->fd_num - 1)) {
273 memmove(&s->fd[index], &s->fd[index + 1],
274 sizeof(s->fd[0]) * (s->fd_num - (index + 1)));
279 } /* }}} int pb_del_socket */
281 static int pb_add_socket(pinba_socket_t *s, /* {{{ */
282 const struct addrinfo *ai) {
287 if (s->fd_num == PINBA_MAX_SOCKETS) {
288 WARNING("pinba plugin: Sorry, you have hit the built-in limit of "
289 "%i sockets. Please complain to the collectd developers so we can "
295 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
297 ERROR("pinba plugin: socket(2) failed: %s", STRERRNO);
302 status = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
304 WARNING("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s", STRERRNO);
307 status = bind(fd, ai->ai_addr, ai->ai_addrlen);
309 ERROR("pinba plugin: bind(2) failed: %s", STRERRNO);
314 s->fd[s->fd_num].fd = fd;
315 s->fd[s->fd_num].events = POLLIN | POLLPRI;
316 s->fd[s->fd_num].revents = 0;
320 } /* }}} int pb_add_socket */
322 static pinba_socket_t *pinba_socket_open(const char *node, /* {{{ */
323 const char *service) {
325 struct addrinfo *ai_list;
329 node = PINBA_DEFAULT_NODE;
332 service = PINBA_DEFAULT_SERVICE;
334 struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
335 .ai_flags = AI_PASSIVE,
336 .ai_socktype = SOCK_DGRAM};
338 status = getaddrinfo(node, service, &ai_hints, &ai_list);
340 ERROR("pinba plugin: getaddrinfo(3) failed: %s", gai_strerror(status));
343 assert(ai_list != NULL);
345 s = calloc(1, sizeof(*s));
347 freeaddrinfo(ai_list);
348 ERROR("pinba plugin: calloc failed.");
352 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
353 ai_ptr = ai_ptr->ai_next) {
354 status = pb_add_socket(s, ai_ptr);
357 } /* for (ai_list) */
359 freeaddrinfo(ai_list);
362 WARNING("pinba plugin: Unable to open socket for address %s.", node);
368 } /* }}} pinba_socket_open */
370 static void pinba_socket_free(pinba_socket_t *socket) /* {{{ */
375 for (nfds_t i = 0; i < socket->fd_num; i++) {
376 if (socket->fd[i].fd < 0)
378 close(socket->fd[i].fd);
379 socket->fd[i].fd = -1;
383 } /* }}} void pinba_socket_free */
385 static int pinba_process_stats_packet(const uint8_t *buffer, /* {{{ */
386 size_t buffer_size) {
387 Pinba__Request *request;
389 request = pinba__request__unpack(NULL, buffer_size, buffer);
394 service_process_request(request);
395 pinba__request__free_unpacked(request, NULL);
398 } /* }}} int pinba_process_stats_packet */
400 static int pinba_udp_read_callback_fn(int sock) /* {{{ */
402 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
407 buffer_size = sizeof(buffer);
408 status = recvfrom(sock, buffer, buffer_size - 1, MSG_DONTWAIT,
409 /* from = */ NULL, /* from len = */ 0);
414 || (errno == EWOULDBLOCK)
416 || (errno == EAGAIN)) {
420 WARNING("pinba plugin: recvfrom(2) failed: %s", STRERRNO);
422 } else if (status == 0) {
423 DEBUG("pinba plugin: recvfrom(2) returned unexpected status zero.");
425 } else /* if (status > 0) */
427 assert(((size_t)status) < buffer_size);
428 buffer_size = (size_t)status;
429 buffer[buffer_size] = 0;
431 status = pinba_process_stats_packet(buffer, buffer_size);
433 DEBUG("pinba plugin: Parsing packet failed.");
441 } /* }}} void pinba_udp_read_callback_fn */
443 static int receive_loop(void) /* {{{ */
447 s = pinba_socket_open(conf_node, conf_service);
449 ERROR("pinba plugin: Collector thread is exiting prematurely.");
453 while (!collector_thread_do_shutdown) {
459 status = poll(s->fd, s->fd_num, /* timeout = */ 1000);
460 if (status == 0) /* timeout */
463 } else if (status < 0) {
464 if ((errno == EINTR) || (errno == EAGAIN))
467 ERROR("pinba plugin: poll(2) failed: %s", STRERRNO);
468 pinba_socket_free(s);
472 for (nfds_t i = 0; i < s->fd_num; i++) {
473 if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL)) {
476 } else if (s->fd[i].revents & (POLLIN | POLLPRI)) {
477 pinba_udp_read_callback_fn(s->fd[i].fd);
480 } /* while (!collector_thread_do_shutdown) */
482 pinba_socket_free(s);
486 } /* }}} int receive_loop */
488 static void *collector_thread(void *arg) /* {{{ */
492 memset(&collector_thread_id, 0, sizeof(collector_thread_id));
493 collector_thread_running = 0;
496 } /* }}} void *collector_thread */
499 * Plugin declaration section
501 static int pinba_config_view(const oconfig_item_t *ci) /* {{{ */
509 status = cf_util_get_string(ci, &name);
513 for (int i = 0; i < ci->children_num; i++) {
514 oconfig_item_t *child = ci->children + i;
516 if (strcasecmp("Host", child->key) == 0)
517 status = cf_util_get_string(child, &host);
518 else if (strcasecmp("Server", child->key) == 0)
519 status = cf_util_get_string(child, &server);
520 else if (strcasecmp("Script", child->key) == 0)
521 status = cf_util_get_string(child, &script);
523 WARNING("pinba plugin: Unknown config option: %s", child->key);
532 service_statnode_add(name, host, server, script);
540 } /* }}} int pinba_config_view */
542 static int plugin_config(oconfig_item_t *ci) /* {{{ */
544 /* The lock should not be necessary in the config callback, but let's be
546 pthread_mutex_lock(&stat_nodes_lock);
548 for (int i = 0; i < ci->children_num; i++) {
549 oconfig_item_t *child = ci->children + i;
551 if (strcasecmp("Address", child->key) == 0)
552 cf_util_get_string(child, &conf_node);
553 else if (strcasecmp("Port", child->key) == 0)
554 cf_util_get_service(child, &conf_service);
555 else if (strcasecmp("View", child->key) == 0)
556 pinba_config_view(child);
558 WARNING("pinba plugin: Unknown config option: %s", child->key);
561 pthread_mutex_unlock(&stat_nodes_lock);
564 } /* }}} int pinba_config */
566 static int plugin_init(void) /* {{{ */
570 if (stat_nodes == NULL) {
571 /* Collect the "total" data by default. */
572 service_statnode_add("total",
575 /* script = */ NULL);
578 if (collector_thread_running)
581 status = plugin_thread_create(&collector_thread_id,
582 /* attrs = */ NULL, collector_thread,
583 /* args = */ NULL, "pinba collector");
585 ERROR("pinba plugin: pthread_create(3) failed: %s", STRERRNO);
588 collector_thread_running = 1;
593 static int plugin_shutdown(void) /* {{{ */
595 if (collector_thread_running) {
598 DEBUG("pinba plugin: Shutting down collector thread.");
599 collector_thread_do_shutdown = 1;
601 status = pthread_join(collector_thread_id, /* retval = */ NULL);
603 ERROR("pinba plugin: pthread_join(3) failed: %s", STRERROR(status));
606 collector_thread_running = 0;
607 collector_thread_do_shutdown = 0;
608 } /* if (collector_thread_running) */
611 } /* }}} int plugin_shutdown */
613 static int plugin_submit(const pinba_statnode_t *res) /* {{{ */
615 value_list_t vl = VALUE_LIST_INIT;
618 sstrncpy(vl.plugin, "pinba", sizeof(vl.plugin));
619 sstrncpy(vl.plugin_instance, res->name, sizeof(vl.plugin_instance));
621 vl.values = &(value_t){.derive = res->req_count};
622 sstrncpy(vl.type, "total_requests", sizeof(vl.type));
623 plugin_dispatch_values(&vl);
625 vl.values = &(value_t){
626 .derive = float_counter_get(&res->req_time, /* factor = */ 1000)};
627 sstrncpy(vl.type, "total_time_in_ms", sizeof(vl.type));
628 plugin_dispatch_values(&vl);
630 vl.values = &(value_t){.derive = res->doc_size};
631 sstrncpy(vl.type, "total_bytes", sizeof(vl.type));
632 plugin_dispatch_values(&vl);
634 vl.values = &(value_t){
635 .derive = float_counter_get(&res->ru_utime, /* factor = */ 100)};
636 sstrncpy(vl.type, "cpu", sizeof(vl.type));
637 sstrncpy(vl.type_instance, "user", sizeof(vl.type_instance));
638 plugin_dispatch_values(&vl);
640 vl.values = &(value_t){
641 .derive = float_counter_get(&res->ru_stime, /* factor = */ 100)};
642 sstrncpy(vl.type, "cpu", sizeof(vl.type));
643 sstrncpy(vl.type_instance, "system", sizeof(vl.type_instance));
644 plugin_dispatch_values(&vl);
646 vl.values = &(value_t){.gauge = res->mem_peak};
647 sstrncpy(vl.type, "memory", sizeof(vl.type));
648 sstrncpy(vl.type_instance, "peak", sizeof(vl.type_instance));
649 plugin_dispatch_values(&vl);
652 } /* }}} int plugin_submit */
654 static int plugin_read(void) /* {{{ */
657 pinba_statnode_t data;
659 while ((i = service_statnode_collect(&data, i)) != 0) {
660 plugin_submit(&data);
664 } /* }}} int plugin_read */
666 void module_register(void) /* {{{ */
668 plugin_register_complex_config("pinba", plugin_config);
669 plugin_register_init("pinba", plugin_init);
670 plugin_register_read("pinba", plugin_read);
671 plugin_register_shutdown("pinba", plugin_shutdown);
672 } /* }}} void module_register */