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>
25 #include "configfile.h"
28 * Service declaration section
31 #ifndef PINBA_UDP_BUFFER_SIZE
32 #define PINBA_UDP_BUFFER_SIZE 65536
35 typedef struct _pinba_statres_ pinba_statres;
36 struct _pinba_statres_ {
47 * Fetch collection datas
49 * Return value: id of next data, zero if no data.
52 service_statnode_collect(pinba_statres *res,
55 service_statnode_begin();
57 service_statnode_end();
59 service_statnode_free();
61 service_statnode_init();
63 service_statnode_add(const char *name,
69 service_config(const char* address,
77 * Service implementation section
80 #include"pinba.pb-c.h"
97 #include<netinet/in.h>
99 #include<sys/socket.h>
101 typedef struct _pinba_socket_ pinba_socket;
102 struct _pinba_socket_ {
104 struct event *accept_event;
107 typedef double pinba_time;
108 typedef uint32_t pinba_size;
112 static struct timeval tv;
113 static struct timezone tz;
115 gettimeofday(&tv, &tz);
117 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
120 static pthread_rwlock_t
123 static struct event_base *
126 static pinba_socket *
132 typedef struct _pinba_statnode_ pinba_statnode;
133 struct _pinba_statnode_{
141 pinba_time last_coll;
142 pinba_size req_count;
153 static pinba_statnode *
157 service_statnode_reset(pinba_statnode *node){
158 node->last_coll=now();
174 strset(char **str, const char *new){
176 new && (*str=strdup(new));
180 service_statnode_add(const char *name,
184 pinba_statnode *node;
185 DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
187 stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode)*(stat_nodes_count+1));
189 ERROR("Realloc failed!");
193 node=&stat_nodes[stat_nodes_count];
195 /* reset stat data */
196 service_statnode_reset(node);
204 /* fill query data */
205 strset(&node->name, name);
206 strset(&node->host, host);
207 strset(&node->server, server);
208 strset(&node->script, script);
210 /* increment counter */
215 service_statnode_free(){
216 if(stat_nodes_count){
217 DEBUG("shutdowning collector..");
220 for(i=0;i<stat_nodes_count;i++){
222 strdel(&stat_nodes[i].name);
223 strdel(&stat_nodes[i].host);
224 strdel(&stat_nodes[i].server);
225 strdel(&stat_nodes[i].script);
231 pthread_rwlock_destroy(&temp_lock);
236 service_statnode_init(){
237 /* only total info collect by default */
238 service_statnode_free();
240 DEBUG("initializing collector..");
241 pthread_rwlock_init(&temp_lock, 0);
245 service_statnode_begin(){
246 service_statnode_init();
247 pthread_rwlock_wrlock(&temp_lock);
249 service_statnode_add("total", NULL, NULL, NULL);
253 service_statnode_end(){
254 pthread_rwlock_unlock(&temp_lock);
258 service_statnode_collect(pinba_statres *res, unsigned int i){
259 pinba_statnode* node;
261 if(stat_nodes_count==0) return 0;
263 /* begin collecting */
265 pthread_rwlock_wrlock(&temp_lock);
268 /* find non-empty node */
269 //for(node=stat_nodes+i; node->req_count==0 && ++i<stat_nodes_count; node=stat_nodes+i);
272 if(i>=stat_nodes_count){
273 pthread_rwlock_unlock(&temp_lock);
279 pinba_time delta=now()-node->last_coll;
281 res->name=node->name;
283 res->req_per_sec=node->req_count/delta;
285 if(node->req_count==0)node->req_count=1;
286 res->req_time=node->req_time/node->req_count;
287 res->ru_utime=node->ru_utime/node->req_count;
288 res->ru_stime=node->ru_stime/node->req_count;
289 res->ru_stime=node->ru_stime/node->req_count;
290 res->doc_size=node->doc_size/node->req_count;
291 res->mem_peak=node->mem_peak/node->req_count;
293 service_statnode_reset(node);
298 service_statnode_process(pinba_statnode *node,
299 Pinba__Request* request){
301 node->req_time+=request->request_time;
302 node->ru_utime+=request->ru_utime;
303 node->ru_stime+=request->ru_stime;
304 node->doc_size+=request->document_size;
305 node->mem_peak+=request->memory_peak;
309 service_process_request(Pinba__Request* request){
311 pthread_rwlock_wrlock(&temp_lock);
313 DEBUG("Process request..");
314 for(i=0;i<stat_nodes_count;i++){
315 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))continue;
316 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))continue;
317 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))continue;
318 service_statnode_process(&stat_nodes[i], request);
321 pthread_rwlock_unlock(&temp_lock);
328 service_address=PINBA_DEFAULT_ADDRESS;
331 service_port=PINBA_DEFAULT_PORT;
334 service_config(const char* address,
338 if(address && (service_address && strcmp(service_address, address))){
339 strset(&service_address, address);
343 if(port>0 && port<65536 && service_port!=port){
348 if(service_status && need_restart){
355 pinba_socket_free (pinba_socket *socket);
357 static pinba_socket *
358 pinba_socket_open (const char *ip,
362 pinba_main(void *arg){
363 DEBUG("entering listen-loop..");
366 event_base_dispatch(temp_base);
374 DEBUG("closing socket..");
376 pthread_rwlock_wrlock(&temp_lock);
377 pinba_socket_free(temp_sock);
378 pthread_rwlock_unlock(&temp_lock);
381 DEBUG("shutdowning event..");
382 event_base_free(temp_base);
384 DEBUG("shutting down..");
389 DEBUG("starting up..");
391 DEBUG("initializing event..");
392 temp_base = event_base_new();
394 DEBUG("opening socket..");
396 temp_sock = pinba_socket_open(service_address, service_port);
403 if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
413 pthread_cancel(temp_thrd);
414 pthread_join(temp_thrd, NULL);
416 DEBUG("terminating listen-loop..");
424 pinba_process_stats_packet (const unsigned char *buf,
426 Pinba__Request *request;
428 request = pinba__request__unpack(NULL, buf_len, buf);
433 service_process_request(request);
435 pinba__request__free_unpacked(request, NULL);
442 pinba_udp_read_callback_fn (int sock,
445 if (event & EV_READ) {
447 unsigned char buf[PINBA_UDP_BUFFER_SIZE];
448 struct sockaddr_in from;
449 socklen_t fromlen = sizeof(struct sockaddr_in);
451 ret = recvfrom(sock, buf, PINBA_UDP_BUFFER_SIZE-1, MSG_DONTWAIT, (struct sockaddr *)&from, &fromlen);
453 if (pinba_process_stats_packet(buf, ret) != P_SUCCESS) {
454 DEBUG("failed to parse data received from %s", inet_ntoa(from.sin_addr));
456 } else if (ret < 0) {
457 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
460 WARNING("recv() failed: %s (%d)", strerror(errno), errno);
462 WARNING("recv() returned 0");
468 pinba_socket_free (pinba_socket *socket) {
471 if (socket->listen_sock >= 0) {
472 close(socket->listen_sock);
473 socket->listen_sock = -1;
476 if (socket->accept_event) {
477 event_del(socket->accept_event);
478 free(socket->accept_event);
479 socket->accept_event = NULL;
485 static pinba_socket *
486 pinba_socket_open (const char *ip,
488 struct sockaddr_in addr;
490 int sfd, flags, yes = 1;
492 if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
493 ERROR("socket() failed: %s (%d)", strerror(errno), errno);
497 if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
502 if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
507 s = (pinba_socket *)calloc(1, sizeof(pinba_socket));
511 s->listen_sock = sfd;
513 memset(&addr, 0, sizeof(addr));
515 addr.sin_family = AF_INET;
516 addr.sin_port = htons(listen_port);
517 addr.sin_addr.s_addr = htonl(INADDR_ANY);
522 if (inet_aton(ip, &tmp)) {
523 addr.sin_addr.s_addr = tmp.s_addr;
525 WARNING("inet_aton(%s) failed, listening on ANY IP-address", ip);
529 if (bind(s->listen_sock, (struct sockaddr *)&addr, sizeof(addr))) {
530 pinba_socket_free(s);
531 ERROR("bind() failed: %s (%d)", strerror(errno), errno);
535 s->accept_event = (struct event *)calloc(1, sizeof(struct event));
536 if (!s->accept_event) {
537 ERROR("calloc() failed: %s (%d)", strerror(errno), errno);
538 pinba_socket_free(s);
542 event_set(s->accept_event, s->listen_sock, EV_READ | EV_PERSIST, pinba_udp_read_callback_fn, s);
543 event_base_set(temp_base, s->accept_event);
544 event_add(s->accept_event, NULL);
550 * Plugin declaration section
556 config_set (char **var, const char *value) {
557 /* code from nginx plugin for collectd */
563 if ((*var = strdup (value)) == NULL) return (1);
568 plugin_config (oconfig_item_t *ci) {
571 char *pinba_address = NULL;
573 INFO("Pinba Configure..");
575 service_statnode_begin();
577 /* Set default values */
578 config_set(&pinba_address, PINBA_DEFAULT_ADDRESS);
579 pinba_port = PINBA_DEFAULT_PORT;
581 for (i = 0; i < ci->children_num; i++) {
582 oconfig_item_t *child = ci->children + i;
583 if (strcasecmp ("Address", child->key) == 0) {
584 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
585 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
588 config_set(&pinba_address, child->values[0].value.string);
589 } else if (strcasecmp ("Port", child->key) == 0) {
590 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_NUMBER)){
591 WARNING ("pinba plugin: `Port' needs exactly one number argument.");
594 pinba_port=child->values[0].value.number;
595 } else if (strcasecmp ("View", child->key) == 0) {
596 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
597 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
598 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
601 name = child->values[0].value.string;
602 for(o=0; o<child->children_num; o++){
603 oconfig_item_t *node = child->children + o;
604 if (strcasecmp ("Host", 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->Host' needs exactly one non-empty string argument.");
609 host = node->values[0].value.string;
610 } else if (strcasecmp ("Server", node->key) == 0) {
611 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
612 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
615 server = node->values[0].value.string;
616 } else if (strcasecmp ("Script", node->key) == 0) {
617 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
618 WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
621 script = node->values[0].value.string;
623 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
627 /* add new statnode */
628 service_statnode_add(name, host, server, script);
630 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
635 service_statnode_end();
637 service_config(pinba_address, pinba_port);
638 } /* int pinba_config */
642 INFO("Pinba Starting..");
648 plugin_shutdown (void) {
649 INFO("Pinba Stopping..");
651 service_statnode_free();
656 plugin_submit (const char *plugin_instance,
658 const pinba_statres *res) {
660 value_list_t vl = VALUE_LIST_INIT;
662 values[0].gauge = res->req_per_sec;
663 values[1].gauge = res->req_time;
664 values[2].gauge = res->ru_utime;
665 values[3].gauge = res->ru_stime;
666 values[4].gauge = res->doc_size;
667 values[5].gauge = res->mem_peak;
671 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
672 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
673 sstrncpy (vl.plugin_instance, plugin_instance,
674 sizeof(vl.plugin_instance));
675 sstrncpy (vl.type, type, sizeof (vl.type));
676 INFO("Pinba Dispatch");
677 plugin_dispatch_values (&vl);
682 DEBUG("Pinba Read..");
684 static pinba_statres res;
686 for(; i=service_statnode_collect(&res, i); ){
687 plugin_submit(res.name, "pinba_view", &res);
693 void module_register (void) {
694 plugin_register_complex_config ("pinba", plugin_config);
695 plugin_register_init ("pinba", plugin_init);
696 plugin_register_read ("pinba", plugin_read);
697 plugin_register_shutdown ("pinba", plugin_shutdown);
698 } /* void module_register */