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
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Antony Dovgal <tony at daylessday.org>
21 * Phoenix Kayo <kayo.k11.4 at gmail.com>
24 #define _XOPEN_SOURCE 500
29 #include "configfile.h"
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
36 #include "pinba.pb-c.h"
38 typedef uint8_t u_char;
43 * Service declaration section
45 #ifndef PINBA_UDP_BUFFER_SIZE
46 # define PINBA_UDP_BUFFER_SIZE 65536
49 #ifndef PINBA_DEFAULT_ADDRESS
50 # define PINBA_DEFAULT_ADDRESS "127.0.0.1" /* FIXME */
53 #ifndef PINBA_DEFAULT_PORT
54 # define PINBA_DEFAULT_PORT 12345 /* FIXME */
58 * Private data structures
60 typedef struct _pinba_statres_ pinba_statres;
61 struct _pinba_statres_ {
71 typedef struct _pinba_socket_ pinba_socket;
72 struct _pinba_socket_ {
74 struct event *accept_event;
77 typedef double pinba_time_t;
78 typedef uint32_t pinba_size_t;
80 static pinba_time_t now (void)
82 static struct timeval tv;
84 gettimeofday (&tv, /* tz = */ NULL);
86 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
89 static pthread_rwlock_t temp_lock;
91 static struct event_base *temp_base = NULL;
93 static pinba_socket_t *temp_sock = NULL;
95 static pthread_t temp_thrd;
97 typedef struct _pinba_statnode_ pinba_statnode;
98 struct _pinba_statnode_{
106 pinba_time_t last_coll;
107 pinba_size_t req_count;
108 pinba_time_t req_time;
109 pinba_time_t ru_utime;
110 pinba_time_t ru_stime;
111 pinba_size_t doc_size;
112 pinba_size_t mem_peak;
115 static unsigned int stat_nodes_count=0;
117 static pinba_statnode *stat_nodes = NULL;
119 char service_status=0;
120 char *service_address = PINBA_DEFAULT_ADDRESS;
121 unsigned int service_port=PINBA_DEFAULT_PORT;
123 static void service_statnode_reset (pinba_statnode *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 *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)*(stat_nodes_count+1));
159 ERROR("Realloc failed!");
163 node=&stat_nodes[stat_nodes_count];
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_count < 1)
191 for (i = 0; i < stat_nodes_count; 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);
200 stat_nodes_count = 0;
202 pthread_rwlock_destroy (&temp_lock);
205 static void service_statnode_init (void)
207 /* only total info collect by default */
208 service_statnode_free();
210 DEBUG("initializing collector..");
211 pthread_rwlock_init(&temp_lock, 0);
214 static void service_statnode_begin (void)
216 service_statnode_init();
217 pthread_rwlock_wrlock(&temp_lock);
219 service_statnode_add("total", NULL, NULL, NULL);
222 static void service_statnode_end (void)
224 pthread_rwlock_unlock(&temp_lock);
227 static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
230 pinba_statnode* node;
233 if (stat_nodes_count == 0)
236 /* begin collecting */
238 pthread_rwlock_wrlock (&temp_lock);
241 if (index >= stat_nodes_count)
243 pthread_rwlock_unlock (&temp_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 *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;
278 static void service_process_request (Pinba__Request *request)
282 pthread_rwlock_wrlock (&temp_lock);
284 for (i = 0; i < stat_nodes_count; 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_rwlock_unlock(&temp_lock);
299 static void *pinba_main (void *arg)
301 DEBUG("entering listen-loop..");
304 event_base_dispatch(temp_base);
310 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
315 if (socket->listen_sock >= 0)
317 close(socket->listen_sock);
318 socket->listen_sock = -1;
321 if (socket->accept_event)
323 event_del(socket->accept_event);
324 free(socket->accept_event);
325 socket->accept_event = NULL;
329 } /* }}} void pinba_socket_free */
331 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
334 Pinba__Request *request;
336 request = pinba__request__unpack (NULL, buffer_size, buffer);
341 service_process_request(request);
342 pinba__request__free_unpacked (request, NULL);
345 } /* }}} int pinba_process_stats_packet */
347 static void pinba_udp_read_callback_fn (int sock, short event, void *arg) /* {{{ */
349 uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
353 if ((event & EV_READ) == 0)
358 buffer_size = sizeof (buffer);
359 status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
366 || (errno == EWOULDBLOCK)
368 || (errno == EAGAIN))
373 WARNING("pinba plugin: recvfrom(2) failed: %s",
374 sstrerror (errno, errbuf, sizeof (errbuf)));
377 else if (status == 0)
379 DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
382 else /* if (status > 0) */
384 assert (((size_t) status) < buffer_size);
385 buffer_size = (size_t) status;
386 buffer[buffer_size] = 0;
388 status = pinba_process_stats_packet (buffer, buffer_size);
390 DEBUG("pinba plugin: Parsing packet failed.");
397 } /* }}} void pinba_udp_read_callback_fn */
399 static pinba_socket_t *pinba_socket_open (const char *ip, /* {{{ */
402 struct sockaddr_in addr;
404 int sfd, flags, yes = 1;
406 if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
407 ERROR("socket() failed: %s (%d)", strerror(errno), errno);
411 if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
416 if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
421 s = (pinba_socket_t *)calloc(1, sizeof(pinba_socket_t));
425 s->listen_sock = sfd;
427 memset(&addr, 0, sizeof(addr));
429 addr.sin_family = AF_INET;
430 addr.sin_port = htons(listen_port);
431 addr.sin_addr.s_addr = htonl(INADDR_ANY);
436 if (inet_aton(ip, &tmp)) {
437 addr.sin_addr.s_addr = tmp.s_addr;
439 WARNING("inet_aton(%s) failed, listening on ANY IP-address", ip);
443 if (bind(s->listen_sock, (struct sockaddr *)&addr, sizeof(addr))) {
444 pinba_socket_free(s);
445 ERROR("bind() failed: %s (%d)", strerror(errno), errno);
449 s->accept_event = (struct event *)calloc(1, sizeof(struct event));
450 if (!s->accept_event) {
451 ERROR("calloc() failed: %s (%d)", strerror(errno), errno);
452 pinba_socket_free(s);
456 event_set(s->accept_event, s->listen_sock, EV_READ | EV_PERSIST, pinba_udp_read_callback_fn, s);
457 event_base_set(temp_base, s->accept_event);
458 event_add(s->accept_event, NULL);
461 } /* }}} pinba_socket_open */
463 static int service_cleanup (void)
465 DEBUG("closing socket..");
467 pthread_rwlock_wrlock(&temp_lock);
468 pinba_socket_free(temp_sock);
469 pthread_rwlock_unlock(&temp_lock);
472 DEBUG("shutdowning event..");
473 event_base_free(temp_base);
475 DEBUG("shutting down..");
480 static int service_start(void)
482 DEBUG("starting up..");
484 DEBUG("initializing event..");
485 temp_base = event_base_new();
487 DEBUG("opening socket..");
489 temp_sock = pinba_socket_open(service_address, service_port);
496 if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
504 static int service_stop (void)
506 pthread_cancel(temp_thrd);
507 pthread_join(temp_thrd, NULL);
509 DEBUG("terminating listen-loop..");
516 static void service_config (const char *address, unsigned int port) /* {{{ */
518 int need_restart = 0;
520 if (address && service_address && (strcmp(service_address, address) != 0))
522 strset (&service_address, address);
526 if ((port > 0) && (port < 65536) && (service_port != port))
532 if(service_status && need_restart)
537 } /* }}} void service_config */
540 * Plugin declaration section
543 static int config_set (char **var, const char *value)
545 /* code from nginx plugin for collectd */
551 if ((*var = strdup (value)) == NULL) return (1);
555 static int plugin_config (oconfig_item_t *ci)
559 char *pinba_address = NULL;
561 INFO("Pinba Configure..");
563 service_statnode_begin();
565 /* Set default values */
566 config_set(&pinba_address, PINBA_DEFAULT_ADDRESS);
567 pinba_port = PINBA_DEFAULT_PORT;
569 for (i = 0; i < ci->children_num; i++) {
570 oconfig_item_t *child = ci->children + i;
571 if (strcasecmp ("Address", child->key) == 0) {
572 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
573 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
576 config_set(&pinba_address, child->values[0].value.string);
577 } else if (strcasecmp ("Port", child->key) == 0) {
578 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_NUMBER)){
579 WARNING ("pinba plugin: `Port' needs exactly one number argument.");
582 pinba_port=child->values[0].value.number;
583 } else if (strcasecmp ("View", child->key) == 0) {
584 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
585 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
586 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
589 name = child->values[0].value.string;
590 for(o=0; o<child->children_num; o++){
591 oconfig_item_t *node = child->children + o;
592 if (strcasecmp ("Host", node->key) == 0) {
593 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
594 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
597 host = node->values[0].value.string;
598 } else if (strcasecmp ("Server", node->key) == 0) {
599 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
600 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
603 server = node->values[0].value.string;
604 } else if (strcasecmp ("Script", node->key) == 0) {
605 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
606 WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
609 script = node->values[0].value.string;
611 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
615 /* add new statnode */
616 service_statnode_add(name, host, server, script);
618 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
623 service_statnode_end();
625 service_config(pinba_address, pinba_port);
626 } /* int pinba_config */
628 static int plugin_init (void)
630 INFO("Pinba Starting..");
635 static int plugin_shutdown (void)
637 INFO("Pinba Stopping..");
639 service_statnode_free();
643 static int plugin_submit (const char *plugin_instance,
645 const pinba_statres *res) {
647 value_list_t vl = VALUE_LIST_INIT;
649 values[0].gauge = res->req_per_sec;
650 values[1].gauge = res->req_time;
651 values[2].gauge = res->ru_utime;
652 values[3].gauge = res->ru_stime;
653 values[4].gauge = res->doc_size;
654 values[5].gauge = res->mem_peak;
658 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
659 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
660 sstrncpy (vl.plugin_instance, plugin_instance,
661 sizeof(vl.plugin_instance));
662 sstrncpy (vl.type, type, sizeof (vl.type));
663 INFO("Pinba Dispatch");
664 plugin_dispatch_values (&vl);
669 static int plugin_read (void)
672 static pinba_statres res;
674 while ((i = service_statnode_collect (&res, i)) != 0)
676 plugin_submit(res.name, "pinba_view", &res);
682 void module_register (void)
684 plugin_register_complex_config ("pinba", plugin_config);
685 plugin_register_init ("pinba", plugin_init);
686 plugin_register_read ("pinba", plugin_read);
687 plugin_register_shutdown ("pinba", plugin_shutdown);
688 } /* void module_register */
690 /* vim: set sw=2 sts=2 et fdm=marker : */