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"
39 * Service declaration section
41 #ifndef PINBA_UDP_BUFFER_SIZE
42 # define PINBA_UDP_BUFFER_SIZE 65536
45 typedef struct _pinba_statres_ pinba_statres;
46 struct _pinba_statres_ {
56 typedef struct _pinba_socket_ pinba_socket;
57 struct _pinba_socket_ {
59 struct event *accept_event;
62 typedef double pinba_time;
63 typedef uint32_t pinba_size;
65 static pinba_time now(){
66 static struct timeval tv;
68 gettimeofday (&tv, /* tz = */ NULL);
70 return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
73 static pthread_rwlock_t temp_lock;
75 static struct event_base *temp_base = NULL;
77 static pinba_socket *temp_sock = NULL;
79 static pthread_t temp_thrd;
81 typedef struct _pinba_statnode_ pinba_statnode;
82 struct _pinba_statnode_{
99 static unsigned int stat_nodes_count=0;
101 static pinba_statnode *stat_nodes = NULL;
103 char service_status=0;
104 char *service_address = PINBA_DEFAULT_ADDRESS;
105 unsigned int service_port=PINBA_DEFAULT_PORT;
108 service_statnode_reset(pinba_statnode *node){
109 node->last_coll=now();
118 static void strset (char **str, const char *new)
134 service_statnode_add(const char *name,
138 pinba_statnode *node;
139 DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
141 stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode)*(stat_nodes_count+1));
143 ERROR("Realloc failed!");
147 node=&stat_nodes[stat_nodes_count];
149 /* reset stat data */
150 service_statnode_reset(node);
158 /* fill query data */
159 strset(&node->name, name);
160 strset(&node->host, host);
161 strset(&node->server, server);
162 strset(&node->script, script);
164 /* increment counter */
168 static void service_statnode_free (void)
172 if(stat_nodes_count < 1)
175 for (i = 0; i < stat_nodes_count; i++)
177 sfree (stat_nodes[i].name);
178 sfree (stat_nodes[i].host);
179 sfree (stat_nodes[i].server);
180 sfree (stat_nodes[i].script);
184 stat_nodes_count = 0;
186 pthread_rwlock_destroy (&temp_lock);
190 service_statnode_init(){
191 /* only total info collect by default */
192 service_statnode_free();
194 DEBUG("initializing collector..");
195 pthread_rwlock_init(&temp_lock, 0);
199 service_statnode_begin(){
200 service_statnode_init();
201 pthread_rwlock_wrlock(&temp_lock);
203 service_statnode_add("total", NULL, NULL, NULL);
207 service_statnode_end(){
208 pthread_rwlock_unlock(&temp_lock);
212 service_statnode_collect(pinba_statres *res, unsigned int i){
213 pinba_statnode* node;
215 if(stat_nodes_count==0) return 0;
217 /* begin collecting */
219 pthread_rwlock_wrlock(&temp_lock);
222 /* find non-empty node */
223 //for(node=stat_nodes+i; node->req_count==0 && ++i<stat_nodes_count; node=stat_nodes+i);
226 if(i>=stat_nodes_count){
227 pthread_rwlock_unlock(&temp_lock);
233 pinba_time delta=now()-node->last_coll;
235 res->name=node->name;
237 res->req_per_sec=node->req_count/delta;
239 if(node->req_count==0)node->req_count=1;
240 res->req_time=node->req_time/node->req_count;
241 res->ru_utime=node->ru_utime/node->req_count;
242 res->ru_stime=node->ru_stime/node->req_count;
243 res->ru_stime=node->ru_stime/node->req_count;
244 res->doc_size=node->doc_size/node->req_count;
245 res->mem_peak=node->mem_peak/node->req_count;
247 service_statnode_reset(node);
252 service_statnode_process(pinba_statnode *node,
253 Pinba__Request* request){
255 node->req_time+=request->request_time;
256 node->ru_utime+=request->ru_utime;
257 node->ru_stime+=request->ru_stime;
258 node->doc_size+=request->document_size;
259 node->mem_peak+=request->memory_peak;
262 static void service_process_request(Pinba__Request *request)
266 pthread_rwlock_wrlock (&temp_lock);
268 for (i = 0; i < stat_nodes_count; i++)
270 if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
272 if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
274 if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
277 service_statnode_process(&stat_nodes[i], request);
280 pthread_rwlock_unlock(&temp_lock);
284 service_config(const char* address,
288 if(address && (service_address && strcmp(service_address, address))){
289 strset(&service_address, address);
293 if(port>0 && port<65536 && service_port!=port){
298 if(service_status && need_restart){
305 pinba_socket_free (pinba_socket *socket);
307 static pinba_socket *
308 pinba_socket_open (const char *ip,
312 pinba_main(void *arg){
313 DEBUG("entering listen-loop..");
316 event_base_dispatch(temp_base);
324 DEBUG("closing socket..");
326 pthread_rwlock_wrlock(&temp_lock);
327 pinba_socket_free(temp_sock);
328 pthread_rwlock_unlock(&temp_lock);
331 DEBUG("shutdowning event..");
332 event_base_free(temp_base);
334 DEBUG("shutting down..");
339 DEBUG("starting up..");
341 DEBUG("initializing event..");
342 temp_base = event_base_new();
344 DEBUG("opening socket..");
346 temp_sock = pinba_socket_open(service_address, service_port);
353 if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
363 pthread_cancel(temp_thrd);
364 pthread_join(temp_thrd, NULL);
366 DEBUG("terminating listen-loop..");
374 pinba_process_stats_packet (const unsigned char *buf,
376 Pinba__Request *request;
378 request = pinba__request__unpack(NULL, buf_len, buf);
383 service_process_request(request);
385 pinba__request__free_unpacked(request, NULL);
392 pinba_udp_read_callback_fn (int sock,
395 if (event & EV_READ) {
397 unsigned char buf[PINBA_UDP_BUFFER_SIZE];
398 struct sockaddr_in from;
399 socklen_t fromlen = sizeof(struct sockaddr_in);
401 ret = recvfrom(sock, buf, PINBA_UDP_BUFFER_SIZE-1, MSG_DONTWAIT, (struct sockaddr *)&from, &fromlen);
403 if (pinba_process_stats_packet(buf, ret) != P_SUCCESS) {
404 DEBUG("failed to parse data received from %s", inet_ntoa(from.sin_addr));
406 } else if (ret < 0) {
407 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
410 WARNING("recv() failed: %s (%d)", strerror(errno), errno);
412 WARNING("recv() returned 0");
418 pinba_socket_free (pinba_socket *socket) {
421 if (socket->listen_sock >= 0) {
422 close(socket->listen_sock);
423 socket->listen_sock = -1;
426 if (socket->accept_event) {
427 event_del(socket->accept_event);
428 free(socket->accept_event);
429 socket->accept_event = NULL;
435 static pinba_socket *
436 pinba_socket_open (const char *ip,
438 struct sockaddr_in addr;
440 int sfd, flags, yes = 1;
442 if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
443 ERROR("socket() failed: %s (%d)", strerror(errno), errno);
447 if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
452 if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
457 s = (pinba_socket *)calloc(1, sizeof(pinba_socket));
461 s->listen_sock = sfd;
463 memset(&addr, 0, sizeof(addr));
465 addr.sin_family = AF_INET;
466 addr.sin_port = htons(listen_port);
467 addr.sin_addr.s_addr = htonl(INADDR_ANY);
472 if (inet_aton(ip, &tmp)) {
473 addr.sin_addr.s_addr = tmp.s_addr;
475 WARNING("inet_aton(%s) failed, listening on ANY IP-address", ip);
479 if (bind(s->listen_sock, (struct sockaddr *)&addr, sizeof(addr))) {
480 pinba_socket_free(s);
481 ERROR("bind() failed: %s (%d)", strerror(errno), errno);
485 s->accept_event = (struct event *)calloc(1, sizeof(struct event));
486 if (!s->accept_event) {
487 ERROR("calloc() failed: %s (%d)", strerror(errno), errno);
488 pinba_socket_free(s);
492 event_set(s->accept_event, s->listen_sock, EV_READ | EV_PERSIST, pinba_udp_read_callback_fn, s);
493 event_base_set(temp_base, s->accept_event);
494 event_add(s->accept_event, NULL);
500 * Plugin declaration section
506 config_set (char **var, const char *value) {
507 /* code from nginx plugin for collectd */
513 if ((*var = strdup (value)) == NULL) return (1);
518 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 */
592 INFO("Pinba Starting..");
598 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) {
646 plugin_register_complex_config ("pinba", plugin_config);
647 plugin_register_init ("pinba", plugin_init);
648 plugin_register_read ("pinba", plugin_read);
649 plugin_register_shutdown ("pinba", plugin_shutdown);
650 } /* void module_register */
652 /* vim: set sw=2 sts=2 et : */