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 typedef struct _pinba_statres_ pinba_statres;
50 struct _pinba_statres_ {
60 typedef struct _pinba_socket_ pinba_socket;
61 struct _pinba_socket_ {
63 struct event *accept_event;
66 typedef double pinba_time;
67 typedef uint32_t pinba_size;
69 static pinba_time now (void)
71 static struct timeval tv;
73 gettimeofday (&tv, /* tz = */ NULL);
75 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
78 static pthread_rwlock_t temp_lock;
80 static struct event_base *temp_base = NULL;
82 static pinba_socket *temp_sock = NULL;
84 static pthread_t temp_thrd;
86 typedef struct _pinba_statnode_ pinba_statnode;
87 struct _pinba_statnode_{
104 static unsigned int stat_nodes_count=0;
106 static pinba_statnode *stat_nodes = NULL;
108 char service_status=0;
109 char *service_address = PINBA_DEFAULT_ADDRESS;
110 unsigned int service_port=PINBA_DEFAULT_PORT;
112 static void service_statnode_reset (pinba_statnode *node) /* {{{ */
114 node->last_coll=now();
121 } /* }}} void service_statnode_reset */
123 static void strset (char **str, const char *new) /* {{{ */
136 } /* }}} void strset */
138 static void service_statnode_add(const char *name, /* {{{ */
143 pinba_statnode *node;
144 DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
146 stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode)*(stat_nodes_count+1));
148 ERROR("Realloc failed!");
152 node=&stat_nodes[stat_nodes_count];
154 /* reset stat data */
155 service_statnode_reset(node);
163 /* fill query data */
164 strset(&node->name, name);
165 strset(&node->host, host);
166 strset(&node->server, server);
167 strset(&node->script, script);
169 /* increment counter */
171 } /* }}} void service_statnode_add */
173 static void service_statnode_free (void)
177 if(stat_nodes_count < 1)
180 for (i = 0; i < stat_nodes_count; i++)
182 sfree (stat_nodes[i].name);
183 sfree (stat_nodes[i].host);
184 sfree (stat_nodes[i].server);
185 sfree (stat_nodes[i].script);
189 stat_nodes_count = 0;
191 pthread_rwlock_destroy (&temp_lock);
194 static void service_statnode_init (void)
196 /* only total info collect by default */
197 service_statnode_free();
199 DEBUG("initializing collector..");
200 pthread_rwlock_init(&temp_lock, 0);
203 static void service_statnode_begin (void)
205 service_statnode_init();
206 pthread_rwlock_wrlock(&temp_lock);
208 service_statnode_add("total", NULL, NULL, NULL);
211 static void service_statnode_end (void)
213 pthread_rwlock_unlock(&temp_lock);
216 static unsigned int service_statnode_collect (pinba_statres *res,
219 pinba_statnode* node;
221 if(stat_nodes_count==0) return 0;
223 /* begin collecting */
225 pthread_rwlock_wrlock(&temp_lock);
228 /* find non-empty node */
229 //for(node=stat_nodes+i; node->req_count==0 && ++i<stat_nodes_count; node=stat_nodes+i);
232 if(i>=stat_nodes_count){
233 pthread_rwlock_unlock(&temp_lock);
239 pinba_time delta=now()-node->last_coll;
241 res->name=node->name;
243 res->req_per_sec=node->req_count/delta;
245 if(node->req_count==0)node->req_count=1;
246 res->req_time=node->req_time/node->req_count;
247 res->ru_utime=node->ru_utime/node->req_count;
248 res->ru_stime=node->ru_stime/node->req_count;
249 res->ru_stime=node->ru_stime/node->req_count;
250 res->doc_size=node->doc_size/node->req_count;
251 res->mem_peak=node->mem_peak/node->req_count;
253 service_statnode_reset(node);
257 static void service_statnode_process (pinba_statnode *node,
258 Pinba__Request* request)
261 node->req_time+=request->request_time;
262 node->ru_utime+=request->ru_utime;
263 node->ru_stime+=request->ru_stime;
264 node->doc_size+=request->document_size;
265 node->mem_peak+=request->memory_peak;
268 static void service_process_request (Pinba__Request *request)
272 pthread_rwlock_wrlock (&temp_lock);
274 for (i = 0; i < stat_nodes_count; i++)
276 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
278 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
280 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
283 service_statnode_process(&stat_nodes[i], request);
286 pthread_rwlock_unlock(&temp_lock);
289 static void *pinba_main (void *arg)
291 DEBUG("entering listen-loop..");
294 event_base_dispatch(temp_base);
300 static void pinba_socket_free (pinba_socket *socket) /* {{{ */
305 if (socket->listen_sock >= 0)
307 close(socket->listen_sock);
308 socket->listen_sock = -1;
311 if (socket->accept_event)
313 event_del(socket->accept_event);
314 free(socket->accept_event);
315 socket->accept_event = NULL;
319 } /* }}} void pinba_socket_free */
321 static int pinba_process_stats_packet (const unsigned char *buf,
324 Pinba__Request *request;
326 request = pinba__request__unpack(NULL, buf_len, buf);
331 service_process_request(request);
333 pinba__request__free_unpacked(request, NULL);
339 static void pinba_udp_read_callback_fn (int sock, short event, void *arg)
341 if (event & EV_READ) {
343 unsigned char buf[PINBA_UDP_BUFFER_SIZE];
344 struct sockaddr_in from;
345 socklen_t fromlen = sizeof(struct sockaddr_in);
347 ret = recvfrom(sock, buf, PINBA_UDP_BUFFER_SIZE-1, MSG_DONTWAIT, (struct sockaddr *)&from, &fromlen);
349 if (pinba_process_stats_packet(buf, ret) != P_SUCCESS) {
350 DEBUG("failed to parse data received from %s", inet_ntoa(from.sin_addr));
352 } else if (ret < 0) {
353 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
356 WARNING("recv() failed: %s (%d)", strerror(errno), errno);
358 WARNING("recv() returned 0");
363 static pinba_socket *pinba_socket_open (const char *ip, /* {{{ */
366 struct sockaddr_in addr;
368 int sfd, flags, yes = 1;
370 if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
371 ERROR("socket() failed: %s (%d)", strerror(errno), errno);
375 if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
380 if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
385 s = (pinba_socket *)calloc(1, sizeof(pinba_socket));
389 s->listen_sock = sfd;
391 memset(&addr, 0, sizeof(addr));
393 addr.sin_family = AF_INET;
394 addr.sin_port = htons(listen_port);
395 addr.sin_addr.s_addr = htonl(INADDR_ANY);
400 if (inet_aton(ip, &tmp)) {
401 addr.sin_addr.s_addr = tmp.s_addr;
403 WARNING("inet_aton(%s) failed, listening on ANY IP-address", ip);
407 if (bind(s->listen_sock, (struct sockaddr *)&addr, sizeof(addr))) {
408 pinba_socket_free(s);
409 ERROR("bind() failed: %s (%d)", strerror(errno), errno);
413 s->accept_event = (struct event *)calloc(1, sizeof(struct event));
414 if (!s->accept_event) {
415 ERROR("calloc() failed: %s (%d)", strerror(errno), errno);
416 pinba_socket_free(s);
420 event_set(s->accept_event, s->listen_sock, EV_READ | EV_PERSIST, pinba_udp_read_callback_fn, s);
421 event_base_set(temp_base, s->accept_event);
422 event_add(s->accept_event, NULL);
427 static int service_cleanup (void)
429 DEBUG("closing socket..");
431 pthread_rwlock_wrlock(&temp_lock);
432 pinba_socket_free(temp_sock);
433 pthread_rwlock_unlock(&temp_lock);
436 DEBUG("shutdowning event..");
437 event_base_free(temp_base);
439 DEBUG("shutting down..");
444 static int service_start(void)
446 DEBUG("starting up..");
448 DEBUG("initializing event..");
449 temp_base = event_base_new();
451 DEBUG("opening socket..");
453 temp_sock = pinba_socket_open(service_address, service_port);
460 if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
468 static int service_stop (void)
470 pthread_cancel(temp_thrd);
471 pthread_join(temp_thrd, NULL);
473 DEBUG("terminating listen-loop..");
480 static void service_config (const char *address, unsigned int port) /* {{{ */
482 int need_restart = 0;
484 if (address && service_address && (strcmp(service_address, address) != 0))
486 strset (&service_address, address);
490 if ((port > 0) && (port < 65536) && (service_port != port))
496 if(service_status && need_restart)
501 } /* }}} void service_config */
504 * Plugin declaration section
507 static int config_set (char **var, const char *value)
509 /* code from nginx plugin for collectd */
515 if ((*var = strdup (value)) == NULL) return (1);
519 static int plugin_config (oconfig_item_t *ci)
523 char *pinba_address = NULL;
525 INFO("Pinba Configure..");
527 service_statnode_begin();
529 /* Set default values */
530 config_set(&pinba_address, PINBA_DEFAULT_ADDRESS);
531 pinba_port = PINBA_DEFAULT_PORT;
533 for (i = 0; i < ci->children_num; i++) {
534 oconfig_item_t *child = ci->children + i;
535 if (strcasecmp ("Address", child->key) == 0) {
536 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
537 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
540 config_set(&pinba_address, child->values[0].value.string);
541 } else if (strcasecmp ("Port", child->key) == 0) {
542 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_NUMBER)){
543 WARNING ("pinba plugin: `Port' needs exactly one number argument.");
546 pinba_port=child->values[0].value.number;
547 } else if (strcasecmp ("View", child->key) == 0) {
548 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
549 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
550 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
553 name = child->values[0].value.string;
554 for(o=0; o<child->children_num; o++){
555 oconfig_item_t *node = child->children + o;
556 if (strcasecmp ("Host", node->key) == 0) {
557 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
558 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
561 host = node->values[0].value.string;
562 } else if (strcasecmp ("Server", node->key) == 0) {
563 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
564 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
567 server = node->values[0].value.string;
568 } else if (strcasecmp ("Script", node->key) == 0) {
569 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
570 WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
573 script = node->values[0].value.string;
575 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
579 /* add new statnode */
580 service_statnode_add(name, host, server, script);
582 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
587 service_statnode_end();
589 service_config(pinba_address, pinba_port);
590 } /* int pinba_config */
592 static int plugin_init (void)
594 INFO("Pinba Starting..");
599 static int plugin_shutdown (void)
601 INFO("Pinba Stopping..");
603 service_statnode_free();
608 plugin_submit (const char *plugin_instance,
610 const pinba_statres *res) {
612 value_list_t vl = VALUE_LIST_INIT;
614 values[0].gauge = res->req_per_sec;
615 values[1].gauge = res->req_time;
616 values[2].gauge = res->ru_utime;
617 values[3].gauge = res->ru_stime;
618 values[4].gauge = res->doc_size;
619 values[5].gauge = res->mem_peak;
623 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
624 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
625 sstrncpy (vl.plugin_instance, plugin_instance,
626 sizeof(vl.plugin_instance));
627 sstrncpy (vl.type, type, sizeof (vl.type));
628 INFO("Pinba Dispatch");
629 plugin_dispatch_values (&vl);
634 static int plugin_read (void)
637 static pinba_statres res;
639 while ((i = service_statnode_collect (&res, i)) != 0)
641 plugin_submit(res.name, "pinba_view", &res);
647 void module_register (void)
649 plugin_register_complex_config ("pinba", plugin_config);
650 plugin_register_init ("pinba", plugin_init);
651 plugin_register_read ("pinba", plugin_read);
652 plugin_register_shutdown ("pinba", plugin_shutdown);
653 } /* void module_register */
655 /* vim: set sw=2 sts=2 et : */