Code

Pinba plugin: Reorder the functions to get the callgraph right.
[collectd.git] / src / pinba.c
1 /**
2  * collectd - src/pinba.c
3  * Copyright (C) 2010       Phoenix Kayo
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Phoenix Kayo <kayo.k11.4 at gmail.com>
20  **/
22 #define _XOPEN_SOURCE 500
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
29 #include <pthread.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;
38 #include <event.h>
40 /*
41  *  Service declaration section
42  */
43 #ifndef PINBA_UDP_BUFFER_SIZE
44 # define PINBA_UDP_BUFFER_SIZE 65536
45 #endif
47 typedef struct _pinba_statres_ pinba_statres;
48 struct _pinba_statres_ {
49   const char *name;
50   double req_per_sec;
51   double req_time;
52   double ru_utime;
53   double ru_stime;
54   double doc_size;
55   double mem_peak;
56 };
58 typedef struct _pinba_socket_ pinba_socket;
59 struct _pinba_socket_ {
60   int listen_sock;
61   struct event *accept_event;
62 };
64 typedef double pinba_time;
65 typedef uint32_t pinba_size;
67 static pinba_time now (void)
68 {
69   static struct timeval tv;
70   
71   gettimeofday (&tv, /* tz = */ NULL);
72   
73   return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
74 }
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_{
86   /* collector name */
87   char* name;
88   /* query data */
89   char *host;
90   char *server;
91   char *script;
92   /* collected data */
93   pinba_time last_coll;
94   pinba_size req_count;
95   pinba_time req_time;
96   pinba_time ru_utime;
97   pinba_time ru_stime;
98   pinba_size doc_size;
99   pinba_size mem_peak;
100 };
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();
113   node->req_count=0;
114   node->req_time=0.0;
115   node->ru_utime=0.0;
116   node->ru_stime=0.0;
117   node->doc_size=0;
118   node->mem_peak=0;
119 } /* }}} void service_statnode_reset */
121 static void strset (char **str, const char *new) /* {{{ */
123   char *tmp;
125   if (!str || !new)
126     return;
128   tmp = strdup (new);
129   if (tmp == NULL)
130     return;
132   sfree (*str);
133   *str = tmp;
134 } /* }}} void strset */
136 static void service_statnode_add(const char *name, /* {{{ */
137     const char *host,
138     const char *server,
139     const char *script)
141   pinba_statnode *node;
142   DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
143   
144   stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode)*(stat_nodes_count+1));
145   if(!stat_nodes){
146     ERROR("Realloc failed!");
147     exit(-1);
148   }
149   
150   node=&stat_nodes[stat_nodes_count];
151   
152   /* reset stat data */
153   service_statnode_reset(node);
154   
155   /* reset strings */
156   node->name=NULL;
157   node->host=NULL;
158   node->server=NULL;
159   node->script=NULL;
160   
161   /* fill query data */
162   strset(&node->name, name);
163   strset(&node->host, host);
164   strset(&node->server, server);
165   strset(&node->script, script);
166   
167   /* increment counter */
168   stat_nodes_count++;
169 } /* }}} void service_statnode_add */
171 static void service_statnode_free (void)
173   unsigned int i;
175   if(stat_nodes_count < 1)
176     return;
178   for (i = 0; i < stat_nodes_count; i++)
179   {
180     sfree (stat_nodes[i].name);
181     sfree (stat_nodes[i].host);
182     sfree (stat_nodes[i].server);
183     sfree (stat_nodes[i].script);
184   }
186   sfree (stat_nodes);
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();
196   
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);
205   
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,
215     unsigned int i)
217   pinba_statnode* node;
218   
219   if(stat_nodes_count==0) return 0;
220   
221   /* begin collecting */
222   if(i==0){
223     pthread_rwlock_wrlock(&temp_lock);
224   }
225   
226   /* find non-empty node */
227   //for(node=stat_nodes+i; node->req_count==0 && ++i<stat_nodes_count; node=stat_nodes+i);
228   
229   /* end collecting */
230   if(i>=stat_nodes_count){
231     pthread_rwlock_unlock(&temp_lock);
232     return 0;
233   }
234   
235   node=stat_nodes+i;
236   
237   pinba_time delta=now()-node->last_coll;
238   
239   res->name=node->name;
240   
241   res->req_per_sec=node->req_count/delta;
242   
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;
250   
251   service_statnode_reset(node);
252   return ++i;
255 static void service_statnode_process (pinba_statnode *node,
256     Pinba__Request* request)
258   node->req_count++;
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)
268   unsigned int i;
270   pthread_rwlock_wrlock (&temp_lock);
271   
272   for (i = 0; i < stat_nodes_count; i++)
273   {
274     if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
275       continue;
276     if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
277       continue;
278     if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
279       continue;
281     service_statnode_process(&stat_nodes[i], request);
282   }
283   
284   pthread_rwlock_unlock(&temp_lock);
287 static void *pinba_main (void *arg)
289   DEBUG("entering listen-loop..");
290   
291   service_status=1;
292   event_base_dispatch(temp_base);
293   
294   /* unreachable */
295   return NULL;
298 static void pinba_socket_free (pinba_socket *socket) /* {{{ */
300   if (!socket)
301     return;
302   
303   if (socket->listen_sock >= 0)
304   {
305     close(socket->listen_sock);
306     socket->listen_sock = -1;
307   }
308   
309   if (socket->accept_event)
310   {
311     event_del(socket->accept_event);
312     free(socket->accept_event);
313     socket->accept_event = NULL;
314   }
315   
316   free(socket);
317 } /* }}} void pinba_socket_free */
319 static int pinba_process_stats_packet (const unsigned char *buf,
320     int buf_len)
322   Pinba__Request *request;  
323   
324   request = pinba__request__unpack(NULL, buf_len, buf);
325   
326   if (!request) {
327     return P_FAILURE;
328   } else {
329     service_process_request(request);
330     
331     pinba__request__free_unpacked(request, NULL);
332     
333     return P_SUCCESS;
334   }
337 static void pinba_udp_read_callback_fn (int sock, short event, void *arg)
339   if (event & EV_READ) {
340     int ret;
341     unsigned char buf[PINBA_UDP_BUFFER_SIZE];
342     struct sockaddr_in from;
343     socklen_t fromlen = sizeof(struct sockaddr_in);
344     
345     ret = recvfrom(sock, buf, PINBA_UDP_BUFFER_SIZE-1, MSG_DONTWAIT, (struct sockaddr *)&from, &fromlen);
346     if (ret > 0) {
347       if (pinba_process_stats_packet(buf, ret) != P_SUCCESS) {
348         DEBUG("failed to parse data received from %s", inet_ntoa(from.sin_addr));
349       }
350     } else if (ret < 0) {
351       if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
352         return;
353       }
354       WARNING("recv() failed: %s (%d)", strerror(errno), errno);
355     } else {
356       WARNING("recv() returned 0");
357     }
358   }
361 static pinba_socket *pinba_socket_open (const char *ip, /* {{{ */
362     int listen_port)
364   struct sockaddr_in addr;
365   pinba_socket *s;
366   int sfd, flags, yes = 1;
367   
368   if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
369     ERROR("socket() failed: %s (%d)", strerror(errno), errno);
370     return NULL;
371   }
372   
373   if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
374     close(sfd);
375     return NULL;
376   }
377   
378   if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
379     close(sfd);
380     return NULL;
381   }
382   
383   s = (pinba_socket *)calloc(1, sizeof(pinba_socket));
384   if (!s) {
385     return NULL;
386   }
387   s->listen_sock = sfd;
388   
389   memset(&addr, 0, sizeof(addr));
390   
391   addr.sin_family = AF_INET;
392   addr.sin_port = htons(listen_port);
393   addr.sin_addr.s_addr = htonl(INADDR_ANY);
394   
395   if (ip && *ip) {
396     struct in_addr tmp;
397     
398     if (inet_aton(ip, &tmp)) {
399       addr.sin_addr.s_addr = tmp.s_addr;
400     } else {
401       WARNING("inet_aton(%s) failed, listening on ANY IP-address", ip);
402     }
403   }
404   
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);
408     return NULL;
409   }
410   
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);
415     return NULL;
416   }
417   
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);
421   
422   return s;
423 } /* }}} */
425 static int service_cleanup (void)
427   DEBUG("closing socket..");
428   if(temp_sock){
429     pthread_rwlock_wrlock(&temp_lock);
430     pinba_socket_free(temp_sock);
431     pthread_rwlock_unlock(&temp_lock);
432   }
433   
434   DEBUG("shutdowning event..");
435   event_base_free(temp_base);
436   
437   DEBUG("shutting down..");
439   return (0);
442 static int service_start(void)
444   DEBUG("starting up..");
445   
446   DEBUG("initializing event..");
447   temp_base = event_base_new();
448   
449   DEBUG("opening socket..");
450   
451   temp_sock = pinba_socket_open(service_address, service_port);
452   
453   if (!temp_sock) {
454     service_cleanup();
455     return 1;
456   }
457   
458   if (pthread_create(&temp_thrd, NULL, pinba_main, NULL)) {
459     service_cleanup();
460     return 1;
461   }
462   
463   return 0;
466 static int service_stop (void)
468   pthread_cancel(temp_thrd);
469   pthread_join(temp_thrd, NULL);
470   service_status=0;
471   DEBUG("terminating listen-loop..");
472   
473   service_cleanup();
474   
475   return 0;
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))
483   {
484     strset (&service_address, address);
485     need_restart++;
486   }
488   if ((port > 0) && (port < 65536) && (service_port != port))
489   {
490     service_port=port;
491     need_restart++;
492   }
494   if(service_status && need_restart)
495   {
496     service_stop();
497     service_start();
498   }
499 } /* }}} void service_config */
501 /*
502  * Plugin declaration section
503  */
505 static int config_set (char **var, const char *value)
507   /* code from nginx plugin for collectd */
508   if (*var != NULL) {
509     free (*var);
510     *var = NULL;
511   }
512   
513   if ((*var = strdup (value)) == NULL) return (1);
514   else return (0);
517 static int plugin_config (oconfig_item_t *ci)
519   unsigned int i, o;
520   int pinba_port = 0;
521   char *pinba_address = NULL;
522   
523   INFO("Pinba Configure..");
524   
525   service_statnode_begin();
526   
527   /* Set default values */
528   config_set(&pinba_address, PINBA_DEFAULT_ADDRESS);
529   pinba_port = PINBA_DEFAULT_PORT;
530   
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.");
536         return (-1);
537       }
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.");
542         return (-1);
543       }
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.");
549         return (-1);
550       }
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.");
557             return (-1);
558           }
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.");
563             return (-1);
564           }
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.");
569             return (-1);
570           }
571           script = node->values[0].value.string;
572         } else {
573           WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
574           return (-1);
575         }
576       }
577       /* add new statnode */
578       service_statnode_add(name, host, server, script);
579     } else {
580       WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
581       return (-1);
582     }
583   }
584   
585   service_statnode_end();
586   
587   service_config(pinba_address, pinba_port);
588 } /* int pinba_config */
590 static int plugin_init (void)
592   INFO("Pinba Starting..");
593   service_start();
594   return 0;
597 static int plugin_shutdown (void)
599   INFO("Pinba Stopping..");
600   service_stop();
601   service_statnode_free();
602   return 0;
605 static int
606 plugin_submit (const char *plugin_instance,
607                const char *type,
608                const pinba_statres *res) {
609   value_t values[6];
610   value_list_t vl = VALUE_LIST_INIT;
611   
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;
618   
619   vl.values = values;
620   vl.values_len = 6;
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);
629   return (0);
632 static int plugin_read (void)
634   unsigned int i=0;
635   static pinba_statres res;
636   
637   while ((i = service_statnode_collect (&res, i)) != 0)
638   {
639     plugin_submit(res.name, "pinba_view", &res);
640   }
641   
642   return 0;
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 : */