2 * collectd - src/pinba.c
3 * Copyright (C) 2010 Phoenix Kayo
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Phoenix Kayo <kayo.k11.4 at gmail.com>
22 #define _XOPEN_SOURCE 500
27 #include "configfile.h"
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
34 #include "pinba.pb-c.h"
36 typedef uint8_t u_char;
41 * Service declaration section
43 #ifndef PINBA_UDP_BUFFER_SIZE
44 # define PINBA_UDP_BUFFER_SIZE 65536
47 typedef struct _pinba_statres_ pinba_statres;
48 struct _pinba_statres_ {
58 typedef struct _pinba_socket_ pinba_socket;
59 struct _pinba_socket_ {
61 struct event *accept_event;
64 typedef double pinba_time;
65 typedef uint32_t pinba_size;
67 static pinba_time now (void)
69 static struct timeval tv;
71 gettimeofday (&tv, /* tz = */ NULL);
73 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
76 static pthread_rwlock_t temp_lock;
78 static struct event_base *temp_base = NULL;
80 static pinba_socket *temp_sock = NULL;
82 static pthread_t temp_thrd;
84 typedef struct _pinba_statnode_ pinba_statnode;
85 struct _pinba_statnode_{
102 static unsigned int stat_nodes_count=0;
104 static pinba_statnode *stat_nodes = NULL;
106 char service_status=0;
107 char *service_address = PINBA_DEFAULT_ADDRESS;
108 unsigned int service_port=PINBA_DEFAULT_PORT;
110 static void service_statnode_reset (pinba_statnode *node) /* {{{ */
112 node->last_coll=now();
119 } /* }}} void service_statnode_reset */
121 static void strset (char **str, const char *new) /* {{{ */
134 } /* }}} void strset */
136 static void service_statnode_add(const char *name, /* {{{ */
141 pinba_statnode *node;
142 DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
144 stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode)*(stat_nodes_count+1));
146 ERROR("Realloc failed!");
150 node=&stat_nodes[stat_nodes_count];
152 /* reset stat data */
153 service_statnode_reset(node);
161 /* fill query data */
162 strset(&node->name, name);
163 strset(&node->host, host);
164 strset(&node->server, server);
165 strset(&node->script, script);
167 /* increment counter */
169 } /* }}} void service_statnode_add */
171 static void service_statnode_free (void)
175 if(stat_nodes_count < 1)
178 for (i = 0; i < stat_nodes_count; i++)
180 sfree (stat_nodes[i].name);
181 sfree (stat_nodes[i].host);
182 sfree (stat_nodes[i].server);
183 sfree (stat_nodes[i].script);
187 stat_nodes_count = 0;
189 pthread_rwlock_destroy (&temp_lock);
192 static void service_statnode_init (void)
194 /* only total info collect by default */
195 service_statnode_free();
197 DEBUG("initializing collector..");
198 pthread_rwlock_init(&temp_lock, 0);
201 static void service_statnode_begin (void)
203 service_statnode_init();
204 pthread_rwlock_wrlock(&temp_lock);
206 service_statnode_add("total", NULL, NULL, NULL);
209 static void service_statnode_end (void)
211 pthread_rwlock_unlock(&temp_lock);
214 static unsigned int service_statnode_collect (pinba_statres *res,
217 pinba_statnode* node;
219 if(stat_nodes_count==0) return 0;
221 /* begin collecting */
223 pthread_rwlock_wrlock(&temp_lock);
226 /* find non-empty node */
227 //for(node=stat_nodes+i; node->req_count==0 && ++i<stat_nodes_count; node=stat_nodes+i);
230 if(i>=stat_nodes_count){
231 pthread_rwlock_unlock(&temp_lock);
237 pinba_time delta=now()-node->last_coll;
239 res->name=node->name;
241 res->req_per_sec=node->req_count/delta;
243 if(node->req_count==0)node->req_count=1;
244 res->req_time=node->req_time/node->req_count;
245 res->ru_utime=node->ru_utime/node->req_count;
246 res->ru_stime=node->ru_stime/node->req_count;
247 res->ru_stime=node->ru_stime/node->req_count;
248 res->doc_size=node->doc_size/node->req_count;
249 res->mem_peak=node->mem_peak/node->req_count;
251 service_statnode_reset(node);
255 static void service_statnode_process (pinba_statnode *node,
256 Pinba__Request* request)
259 node->req_time+=request->request_time;
260 node->ru_utime+=request->ru_utime;
261 node->ru_stime+=request->ru_stime;
262 node->doc_size+=request->document_size;
263 node->mem_peak+=request->memory_peak;
266 static void service_process_request (Pinba__Request *request)
270 pthread_rwlock_wrlock (&temp_lock);
272 for (i = 0; i < stat_nodes_count; i++)
274 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
276 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
278 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
281 service_statnode_process(&stat_nodes[i], request);
284 pthread_rwlock_unlock(&temp_lock);
287 static void *pinba_main (void *arg)
289 DEBUG("entering listen-loop..");
292 event_base_dispatch(temp_base);
298 static void pinba_socket_free (pinba_socket *socket) /* {{{ */
303 if (socket->listen_sock >= 0)
305 close(socket->listen_sock);
306 socket->listen_sock = -1;
309 if (socket->accept_event)
311 event_del(socket->accept_event);
312 free(socket->accept_event);
313 socket->accept_event = NULL;
317 } /* }}} void pinba_socket_free */
319 static int pinba_process_stats_packet (const unsigned char *buf,
322 Pinba__Request *request;
324 request = pinba__request__unpack(NULL, buf_len, buf);
329 service_process_request(request);
331 pinba__request__free_unpacked(request, NULL);
337 static void pinba_udp_read_callback_fn (int sock, short event, void *arg)
339 if (event & EV_READ) {
341 unsigned char buf[PINBA_UDP_BUFFER_SIZE];
342 struct sockaddr_in from;
343 socklen_t fromlen = sizeof(struct sockaddr_in);
345 ret = recvfrom(sock, buf, PINBA_UDP_BUFFER_SIZE-1, MSG_DONTWAIT, (struct sockaddr *)&from, &fromlen);
347 if (pinba_process_stats_packet(buf, ret) != P_SUCCESS) {
348 DEBUG("failed to parse data received from %s", inet_ntoa(from.sin_addr));
350 } else if (ret < 0) {
351 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
354 WARNING("recv() failed: %s (%d)", strerror(errno), errno);
356 WARNING("recv() returned 0");
361 static pinba_socket *pinba_socket_open (const char *ip, /* {{{ */
364 struct sockaddr_in addr;
366 int sfd, flags, yes = 1;
368 if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
369 ERROR("socket() failed: %s (%d)", strerror(errno), errno);
373 if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
378 if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
383 s = (pinba_socket *)calloc(1, sizeof(pinba_socket));
387 s->listen_sock = sfd;
389 memset(&addr, 0, sizeof(addr));
391 addr.sin_family = AF_INET;
392 addr.sin_port = htons(listen_port);
393 addr.sin_addr.s_addr = htonl(INADDR_ANY);
398 if (inet_aton(ip, &tmp)) {
399 addr.sin_addr.s_addr = tmp.s_addr;
401 WARNING("inet_aton(%s) failed, listening on ANY IP-address", ip);
405 if (bind(s->listen_sock, (struct sockaddr *)&addr, sizeof(addr))) {
406 pinba_socket_free(s);
407 ERROR("bind() failed: %s (%d)", strerror(errno), errno);
411 s->accept_event = (struct event *)calloc(1, sizeof(struct event));
412 if (!s->accept_event) {
413 ERROR("calloc() failed: %s (%d)", strerror(errno), errno);
414 pinba_socket_free(s);
418 event_set(s->accept_event, s->listen_sock, EV_READ | EV_PERSIST, pinba_udp_read_callback_fn, s);
419 event_base_set(temp_base, s->accept_event);
420 event_add(s->accept_event, NULL);
425 static int service_cleanup (void)
427 DEBUG("closing socket..");
429 pthread_rwlock_wrlock(&temp_lock);
430 pinba_socket_free(temp_sock);
431 pthread_rwlock_unlock(&temp_lock);
434 DEBUG("shutdowning event..");
435 event_base_free(temp_base);
437 DEBUG("shutting down..");
442 static int service_start(void)
444 DEBUG("starting up..");
446 DEBUG("initializing event..");
447 temp_base = event_base_new();
449 DEBUG("opening socket..");
451 temp_sock = pinba_socket_open(service_address, service_port);
458 if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
466 static int service_stop (void)
468 pthread_cancel(temp_thrd);
469 pthread_join(temp_thrd, NULL);
471 DEBUG("terminating listen-loop..");
478 static void service_config (const char *address, unsigned int port) /* {{{ */
480 int need_restart = 0;
482 if (address && service_address && (strcmp(service_address, address) != 0))
484 strset (&service_address, address);
488 if ((port > 0) && (port < 65536) && (service_port != port))
494 if(service_status && need_restart)
499 } /* }}} void service_config */
502 * Plugin declaration section
505 static int config_set (char **var, const char *value)
507 /* code from nginx plugin for collectd */
513 if ((*var = strdup (value)) == NULL) return (1);
517 static int plugin_config (oconfig_item_t *ci)
521 char *pinba_address = NULL;
523 INFO("Pinba Configure..");
525 service_statnode_begin();
527 /* Set default values */
528 config_set(&pinba_address, PINBA_DEFAULT_ADDRESS);
529 pinba_port = PINBA_DEFAULT_PORT;
531 for (i = 0; i < ci->children_num; i++) {
532 oconfig_item_t *child = ci->children + i;
533 if (strcasecmp ("Address", child->key) == 0) {
534 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
535 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
538 config_set(&pinba_address, child->values[0].value.string);
539 } else if (strcasecmp ("Port", child->key) == 0) {
540 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_NUMBER)){
541 WARNING ("pinba plugin: `Port' needs exactly one number argument.");
544 pinba_port=child->values[0].value.number;
545 } else if (strcasecmp ("View", child->key) == 0) {
546 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
547 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
548 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
551 name = child->values[0].value.string;
552 for(o=0; o<child->children_num; o++){
553 oconfig_item_t *node = child->children + o;
554 if (strcasecmp ("Host", node->key) == 0) {
555 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
556 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
559 host = node->values[0].value.string;
560 } else if (strcasecmp ("Server", node->key) == 0) {
561 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
562 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
565 server = node->values[0].value.string;
566 } else if (strcasecmp ("Script", node->key) == 0) {
567 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
568 WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
571 script = node->values[0].value.string;
573 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
577 /* add new statnode */
578 service_statnode_add(name, host, server, script);
580 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
585 service_statnode_end();
587 service_config(pinba_address, pinba_port);
588 } /* int pinba_config */
590 static int plugin_init (void)
592 INFO("Pinba Starting..");
597 static int plugin_shutdown (void)
599 INFO("Pinba Stopping..");
601 service_statnode_free();
606 plugin_submit (const char *plugin_instance,
608 const pinba_statres *res) {
610 value_list_t vl = VALUE_LIST_INIT;
612 values[0].gauge = res->req_per_sec;
613 values[1].gauge = res->req_time;
614 values[2].gauge = res->ru_utime;
615 values[3].gauge = res->ru_stime;
616 values[4].gauge = res->doc_size;
617 values[5].gauge = res->mem_peak;
621 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
622 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
623 sstrncpy (vl.plugin_instance, plugin_instance,
624 sizeof(vl.plugin_instance));
625 sstrncpy (vl.type, type, sizeof (vl.type));
626 INFO("Pinba Dispatch");
627 plugin_dispatch_values (&vl);
632 static int plugin_read (void)
635 static pinba_statres res;
637 while ((i = service_statnode_collect (&res, i)) != 0)
639 plugin_submit(res.name, "pinba_view", &res);
645 void module_register (void)
647 plugin_register_complex_config ("pinba", plugin_config);
648 plugin_register_init ("pinba", plugin_init);
649 plugin_register_read ("pinba", plugin_read);
650 plugin_register_shutdown ("pinba", plugin_shutdown);
651 } /* void module_register */
653 /* vim: set sw=2 sts=2 et : */