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;
78 typedef uint32_t pinba_size;
80 static pinba_time 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 *temp_sock = NULL;
95 static pthread_t temp_thrd;
97 typedef struct _pinba_statnode_ pinba_statnode;
98 struct _pinba_statnode_{
106 pinba_time last_coll;
107 pinba_size req_count;
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;
232 if(stat_nodes_count==0) return 0;
234 /* begin collecting */
236 pthread_rwlock_wrlock(&temp_lock);
239 /* find non-empty node */
240 //for(node=stat_nodes+i; node->req_count==0 && ++i<stat_nodes_count; node=stat_nodes+i);
243 if(i>=stat_nodes_count){
244 pthread_rwlock_unlock(&temp_lock);
250 pinba_time delta=now()-node->last_coll;
252 res->name=node->name;
254 res->req_per_sec=node->req_count/delta;
256 if(node->req_count==0)node->req_count=1;
257 res->req_time=node->req_time/node->req_count;
258 res->ru_utime=node->ru_utime/node->req_count;
259 res->ru_stime=node->ru_stime/node->req_count;
260 res->ru_stime=node->ru_stime/node->req_count;
261 res->doc_size=node->doc_size/node->req_count;
262 res->mem_peak=node->mem_peak/node->req_count;
264 service_statnode_reset(node);
268 static void service_statnode_process (pinba_statnode *node,
269 Pinba__Request* request)
272 node->req_time+=request->request_time;
273 node->ru_utime+=request->ru_utime;
274 node->ru_stime+=request->ru_stime;
275 node->doc_size+=request->document_size;
276 node->mem_peak+=request->memory_peak;
279 static void service_process_request (Pinba__Request *request)
283 pthread_rwlock_wrlock (&temp_lock);
285 for (i = 0; i < stat_nodes_count; i++)
287 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
289 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
291 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
294 service_statnode_process(&stat_nodes[i], request);
297 pthread_rwlock_unlock(&temp_lock);
300 static void *pinba_main (void *arg)
302 DEBUG("entering listen-loop..");
305 event_base_dispatch(temp_base);
311 static void pinba_socket_free (pinba_socket *socket) /* {{{ */
316 if (socket->listen_sock >= 0)
318 close(socket->listen_sock);
319 socket->listen_sock = -1;
322 if (socket->accept_event)
324 event_del(socket->accept_event);
325 free(socket->accept_event);
326 socket->accept_event = NULL;
330 } /* }}} void pinba_socket_free */
332 static int pinba_process_stats_packet (const unsigned char *buf,
335 Pinba__Request *request;
337 request = pinba__request__unpack(NULL, buf_len, buf);
342 service_process_request(request);
344 pinba__request__free_unpacked(request, NULL);
350 static void pinba_udp_read_callback_fn (int sock, short event, void *arg)
352 if (event & EV_READ) {
354 unsigned char buf[PINBA_UDP_BUFFER_SIZE];
355 struct sockaddr_in from;
356 socklen_t fromlen = sizeof(struct sockaddr_in);
358 ret = recvfrom(sock, buf, PINBA_UDP_BUFFER_SIZE-1, MSG_DONTWAIT, (struct sockaddr *)&from, &fromlen);
360 if (pinba_process_stats_packet(buf, ret) != P_SUCCESS) {
361 DEBUG("failed to parse data received from %s", inet_ntoa(from.sin_addr));
363 } else if (ret < 0) {
364 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
367 WARNING("recv() failed: %s (%d)", strerror(errno), errno);
369 WARNING("recv() returned 0");
374 static pinba_socket *pinba_socket_open (const char *ip, /* {{{ */
377 struct sockaddr_in addr;
379 int sfd, flags, yes = 1;
381 if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
382 ERROR("socket() failed: %s (%d)", strerror(errno), errno);
386 if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
391 if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
396 s = (pinba_socket *)calloc(1, sizeof(pinba_socket));
400 s->listen_sock = sfd;
402 memset(&addr, 0, sizeof(addr));
404 addr.sin_family = AF_INET;
405 addr.sin_port = htons(listen_port);
406 addr.sin_addr.s_addr = htonl(INADDR_ANY);
411 if (inet_aton(ip, &tmp)) {
412 addr.sin_addr.s_addr = tmp.s_addr;
414 WARNING("inet_aton(%s) failed, listening on ANY IP-address", ip);
418 if (bind(s->listen_sock, (struct sockaddr *)&addr, sizeof(addr))) {
419 pinba_socket_free(s);
420 ERROR("bind() failed: %s (%d)", strerror(errno), errno);
424 s->accept_event = (struct event *)calloc(1, sizeof(struct event));
425 if (!s->accept_event) {
426 ERROR("calloc() failed: %s (%d)", strerror(errno), errno);
427 pinba_socket_free(s);
431 event_set(s->accept_event, s->listen_sock, EV_READ | EV_PERSIST, pinba_udp_read_callback_fn, s);
432 event_base_set(temp_base, s->accept_event);
433 event_add(s->accept_event, NULL);
438 static int service_cleanup (void)
440 DEBUG("closing socket..");
442 pthread_rwlock_wrlock(&temp_lock);
443 pinba_socket_free(temp_sock);
444 pthread_rwlock_unlock(&temp_lock);
447 DEBUG("shutdowning event..");
448 event_base_free(temp_base);
450 DEBUG("shutting down..");
455 static int service_start(void)
457 DEBUG("starting up..");
459 DEBUG("initializing event..");
460 temp_base = event_base_new();
462 DEBUG("opening socket..");
464 temp_sock = pinba_socket_open(service_address, service_port);
471 if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
479 static int service_stop (void)
481 pthread_cancel(temp_thrd);
482 pthread_join(temp_thrd, NULL);
484 DEBUG("terminating listen-loop..");
491 static void service_config (const char *address, unsigned int port) /* {{{ */
493 int need_restart = 0;
495 if (address && service_address && (strcmp(service_address, address) != 0))
497 strset (&service_address, address);
501 if ((port > 0) && (port < 65536) && (service_port != port))
507 if(service_status && need_restart)
512 } /* }}} void service_config */
515 * Plugin declaration section
518 static int config_set (char **var, const char *value)
520 /* code from nginx plugin for collectd */
526 if ((*var = strdup (value)) == NULL) return (1);
530 static int plugin_config (oconfig_item_t *ci)
534 char *pinba_address = NULL;
536 INFO("Pinba Configure..");
538 service_statnode_begin();
540 /* Set default values */
541 config_set(&pinba_address, PINBA_DEFAULT_ADDRESS);
542 pinba_port = PINBA_DEFAULT_PORT;
544 for (i = 0; i < ci->children_num; i++) {
545 oconfig_item_t *child = ci->children + i;
546 if (strcasecmp ("Address", child->key) == 0) {
547 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
548 WARNING ("pinba plugin: `Address' needs exactly one string argument.");
551 config_set(&pinba_address, child->values[0].value.string);
552 } else if (strcasecmp ("Port", child->key) == 0) {
553 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_NUMBER)){
554 WARNING ("pinba plugin: `Port' needs exactly one number argument.");
557 pinba_port=child->values[0].value.number;
558 } else if (strcasecmp ("View", child->key) == 0) {
559 const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
560 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
561 WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
564 name = child->values[0].value.string;
565 for(o=0; o<child->children_num; o++){
566 oconfig_item_t *node = child->children + o;
567 if (strcasecmp ("Host", node->key) == 0) {
568 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
569 WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
572 host = node->values[0].value.string;
573 } else if (strcasecmp ("Server", node->key) == 0) {
574 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
575 WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
578 server = node->values[0].value.string;
579 } else if (strcasecmp ("Script", node->key) == 0) {
580 if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
581 WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
584 script = node->values[0].value.string;
586 WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
590 /* add new statnode */
591 service_statnode_add(name, host, server, script);
593 WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
598 service_statnode_end();
600 service_config(pinba_address, pinba_port);
601 } /* int pinba_config */
603 static int plugin_init (void)
605 INFO("Pinba Starting..");
610 static int plugin_shutdown (void)
612 INFO("Pinba Stopping..");
614 service_statnode_free();
619 plugin_submit (const char *plugin_instance,
621 const pinba_statres *res) {
623 value_list_t vl = VALUE_LIST_INIT;
625 values[0].gauge = res->req_per_sec;
626 values[1].gauge = res->req_time;
627 values[2].gauge = res->ru_utime;
628 values[3].gauge = res->ru_stime;
629 values[4].gauge = res->doc_size;
630 values[5].gauge = res->mem_peak;
634 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
635 sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
636 sstrncpy (vl.plugin_instance, plugin_instance,
637 sizeof(vl.plugin_instance));
638 sstrncpy (vl.type, type, sizeof (vl.type));
639 INFO("Pinba Dispatch");
640 plugin_dispatch_values (&vl);
645 static int plugin_read (void)
648 static pinba_statres res;
650 while ((i = service_statnode_collect (&res, i)) != 0)
652 plugin_submit(res.name, "pinba_view", &res);
658 void module_register (void)
660 plugin_register_complex_config ("pinba", plugin_config);
661 plugin_register_init ("pinba", plugin_init);
662 plugin_register_read ("pinba", plugin_read);
663 plugin_register_shutdown ("pinba", plugin_shutdown);
664 } /* void module_register */
666 /* vim: set sw=2 sts=2 et : */