Code

pinba plugin: Remove the "NI_MAXSERV" define.
[collectd.git] / src / pinba.c
1 /**
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
5  * Copyright (C) 2010       Florian Forster
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; only version 2 of the License is applicable.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Antony Dovgal <tony at daylessday.org>
22  *   Phoenix Kayo <kayo.k11.4 at gmail.com>
23  *   Florian Forster <octo at verplant.org>
24  **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
31 #include <pthread.h>
32 #include <sys/socket.h>
33 #include <netdb.h>
34 #include <poll.h>
36 #include "pinba.pb-c.h"
38 /*
39  *  Service declaration section
40  */
41 #ifndef PINBA_UDP_BUFFER_SIZE
42 # define PINBA_UDP_BUFFER_SIZE 65536
43 #endif
45 #ifndef PINBA_DEFAULT_NODE
46 # define PINBA_DEFAULT_NODE "127.0.0.1" /* FIXME */
47 #endif
49 #ifndef PINBA_DEFAULT_SERVICE
50 # define PINBA_DEFAULT_SERVICE "12345" /* FIXME */
51 #endif
53 #ifndef PINBA_MAX_SOCKETS
54 # define PINBA_MAX_SOCKETS 16
55 #endif
57 /*
58  * Private data structures
59  */
60 typedef struct _pinba_statres_ pinba_statres;
61 struct _pinba_statres_ {
62   const char *name;
63   double req_per_sec;
64   double req_time;
65   double ru_utime;
66   double ru_stime;
67   double doc_size;
68   double mem_peak;
69 };
71 struct pinba_socket_s {
72   struct pollfd fd[PINBA_MAX_SOCKETS];
73   nfds_t fd_num;
74 };
75 typedef struct pinba_socket_s pinba_socket_t;
77 typedef double pinba_time_t;
78 typedef uint32_t pinba_size_t;
80 typedef struct pinba_statnode_s pinba_statnode_t;
81 struct pinba_statnode_s
82 {
83   /* collector name */
84   char *name;
85   /* query data */
86   char *host;
87   char *server;
88   char *script;
89   /* collected data */
90   pinba_time_t last_coll;
91   pinba_size_t req_count;
92   pinba_time_t req_time;
93   pinba_time_t ru_utime;
94   pinba_time_t ru_stime;
95   pinba_size_t doc_size;
96   pinba_size_t mem_peak;
97 };
99 static pinba_statnode_t *stat_nodes = NULL;
100 static unsigned int stat_nodes_num = 0;
101 static pthread_mutex_t stat_nodes_lock;
103 static char *conf_node = NULL;
104 static char *conf_service = NULL;
106 static _Bool collector_thread_running = 0;
107 static _Bool collector_thread_do_shutdown = 0;
108 static pthread_t collector_thread_id;
110 static pinba_time_t now (void) /* {{{ */
112   static struct timeval tv;
113   
114   gettimeofday (&tv, /* tz = */ NULL);
115   
116   return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
117 } /* }}} pinba_time_t now */
119 static void service_statnode_reset (pinba_statnode_t *node) /* {{{ */
121   node->last_coll=now();
122   node->req_count=0;
123   node->req_time=0.0;
124   node->ru_utime=0.0;
125   node->ru_stime=0.0;
126   node->doc_size=0;
127   node->mem_peak=0;
128 } /* }}} void service_statnode_reset */
130 static void strset (char **str, const char *new) /* {{{ */
132   char *tmp;
134   if (!str || !new)
135     return;
137   tmp = strdup (new);
138   if (tmp == NULL)
139     return;
141   sfree (*str);
142   *str = tmp;
143 } /* }}} void strset */
145 static void service_statnode_add(const char *name, /* {{{ */
146     const char *host,
147     const char *server,
148     const char *script)
150   pinba_statnode_t *node;
151   DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
152   
153   stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode_t)*(stat_nodes_num+1));
154   if(!stat_nodes){
155     ERROR("Realloc failed!");
156     exit(-1);
157   }
158   
159   node=&stat_nodes[stat_nodes_num];
160   
161   /* reset stat data */
162   service_statnode_reset(node);
163   
164   /* reset strings */
165   node->name=NULL;
166   node->host=NULL;
167   node->server=NULL;
168   node->script=NULL;
169   
170   /* fill query data */
171   strset(&node->name, name);
172   strset(&node->host, host);
173   strset(&node->server, server);
174   strset(&node->script, script);
175   
176   /* increment counter */
177   stat_nodes_num++;
178 } /* }}} void service_statnode_add */
180 static void service_statnode_free (void) /* {{{ */
182   unsigned int i;
184   if(stat_nodes_num < 1)
185     return;
187   for (i = 0; i < stat_nodes_num; i++)
188   {
189     sfree (stat_nodes[i].name);
190     sfree (stat_nodes[i].host);
191     sfree (stat_nodes[i].server);
192     sfree (stat_nodes[i].script);
193   }
195   sfree (stat_nodes);
196   stat_nodes_num = 0;
198   pthread_mutex_destroy (&stat_nodes_lock);
199 } /* }}} void service_statnode_free */
201 static void service_statnode_init (void) /* {{{ */
203   /* only total info collect by default */
204   service_statnode_free();
205   
206   DEBUG("initializing collector..");
207   pthread_mutex_init(&stat_nodes_lock, 0);
208 } /* }}} void service_statnode_init */
210 static void service_statnode_begin (void) /* {{{ */
212   service_statnode_init();
213   pthread_mutex_lock(&stat_nodes_lock);
214   
215   service_statnode_add("total", NULL, NULL, NULL);
216 } /* }}} void service_statnode_begin */
218 static void service_statnode_end (void) /* {{{ */
220   pthread_mutex_unlock(&stat_nodes_lock);
221 } /* }}} void service_statnode_end */
223 static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
224     unsigned int index)
226   pinba_statnode_t* node;
227   pinba_time_t delta;
228   
229   if (stat_nodes_num == 0)
230     return 0;
231   
232   /* begin collecting */
233   if (index == 0)
234     pthread_mutex_lock (&stat_nodes_lock);
235   
236   /* end collecting */
237   if (index >= stat_nodes_num)
238   {
239     pthread_mutex_unlock (&stat_nodes_lock);
240     return 0;
241   }
242   
243   node = stat_nodes + index;
244   delta = now() - node->last_coll;
245   
246   res->name = node->name;
247   res->req_per_sec = node->req_count / delta;
248   
249   if (node->req_count == 0)
250     node->req_count = 1;
252   res->req_time = node->req_time / node->req_count;
253   res->ru_utime = node->ru_utime / node->req_count;
254   res->ru_stime = node->ru_stime / node->req_count;
255   res->ru_stime = node->ru_stime / node->req_count;
256   res->doc_size = node->doc_size / node->req_count;
257   res->mem_peak = node->mem_peak / node->req_count;
258   
259   service_statnode_reset (node);
260   return (index + 1);
261 } /* }}} unsigned int service_statnode_collect */
263 static void service_statnode_process (pinba_statnode_t *node, /* {{{ */
264     Pinba__Request* request)
266   node->req_count++;
267   node->req_time+=request->request_time;
268   node->ru_utime+=request->ru_utime;
269   node->ru_stime+=request->ru_stime;
270   node->doc_size+=request->document_size;
271   node->mem_peak+=request->memory_peak;
272 } /* }}} void service_statnode_process */
274 static void service_process_request (Pinba__Request *request) /* {{{ */
276   unsigned int i;
278   pthread_mutex_lock (&stat_nodes_lock);
279   
280   for (i = 0; i < stat_nodes_num; i++)
281   {
282     if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
283       continue;
284     if(stat_nodes[i].server && strcmp(request->server_name, stat_nodes[i].server))
285       continue;
286     if(stat_nodes[i].script && strcmp(request->script_name, stat_nodes[i].script))
287       continue;
289     service_statnode_process(&stat_nodes[i], request);
290   }
291   
292   pthread_mutex_unlock(&stat_nodes_lock);
293 } /* }}} void service_process_request */
295 static int pb_del_socket (pinba_socket_t *s, /* {{{ */
296     nfds_t index)
298   if (index >= s->fd_num)
299     return (EINVAL);
301   close (s->fd[index].fd);
302   s->fd[index].fd = -1;
304   /* When deleting the last element in the list, no memmove is necessary. */
305   if (index < (s->fd_num - 1))
306   {
307     memmove (&s->fd[index], &s->fd[index + 1],
308         sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
309   }
311   s->fd_num--;
312   return (0);
313 } /* }}} int pb_del_socket */
315 static int pb_add_socket (pinba_socket_t *s, /* {{{ */
316     const struct addrinfo *ai)
318   int fd;
319   int tmp;
320   int status;
322   if (s->fd_num == PINBA_MAX_SOCKETS)
323   {
324     WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
325         "%i sockets. Please complain to the collectd developers so we can "
326         "raise the limit.", PINBA_MAX_SOCKETS);
327     return (-1);
328   }
330   fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
331   if (fd < 0)
332   {
333     char errbuf[1024];
334     ERROR ("pinba plugin: socket(2) failed: %s",
335         sstrerror (errno, errbuf, sizeof (errbuf)));
336     return (0);
337   }
339   tmp = 1;
340   status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
341   if (status != 0)
342   {
343     char errbuf[1024];
344     WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
345         sstrerror (errno, errbuf, sizeof (errbuf)));
346   }
348   status = bind (fd, ai->ai_addr, ai->ai_addrlen);
349   if (status != 0)
350   {
351     char errbuf[1024];
352     ERROR ("pinba plugin: bind(2) failed: %s",
353         sstrerror (errno, errbuf, sizeof (errbuf)));
354     return (0);
355   }
357   s->fd[s->fd_num].fd = fd;
358   s->fd[s->fd_num].events = POLLIN | POLLPRI;
359   s->fd[s->fd_num].revents = 0;
360   s->fd_num++;
362   return (0);
363 } /* }}} int pb_add_socket */
365 static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
366     const char *service)
368   pinba_socket_t *s;
369   struct addrinfo *ai_list;
370   struct addrinfo *ai_ptr;
371   struct addrinfo  ai_hints;
372   int status;
374   memset (&ai_hints, 0, sizeof (ai_hints));
375   ai_hints.ai_flags = AI_PASSIVE;
376   ai_hints.ai_family = AF_UNSPEC;
377   ai_hints.ai_socktype = SOCK_DGRAM;
378   ai_hints.ai_addr = NULL;
379   ai_hints.ai_canonname = NULL;
380   ai_hints.ai_next = NULL;
382   if (node == NULL)
383     node = PINBA_DEFAULT_NODE;
385   if (service == NULL)
386     service = PINBA_DEFAULT_SERVICE;
388   ai_list = NULL;
389   status = getaddrinfo (node, service,
390       &ai_hints, &ai_list);
391   if (status != 0)
392   {
393     ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
394         gai_strerror (status));
395     return (NULL);
396   }
397   assert (ai_list != NULL);
399   s = malloc (sizeof (*s));
400   if (s != NULL)
401   {
402     freeaddrinfo (ai_list);
403     ERROR ("pinba plugin: malloc failed.");
404     return (NULL);
405   }
406   memset (s, 0, sizeof (*s));
408   for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
409   {
410     status = pb_add_socket (s, ai_ptr);
411     if (status != 0)
412       break;
413   } /* for (ai_list) */
414   
415   freeaddrinfo (ai_list);
417   if (s->fd_num < 1)
418   {
419     WARNING ("pinba plugin: Unable to open socket for address %s.", node);
420     sfree (s);
421     s = NULL;
422   }
424   return (s);
425 } /* }}} pinba_socket_open */
427 static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
429   nfds_t i;
431   if (!socket)
432     return;
433   
434   for (i = 0; i < socket->fd_num; i++)
435   {
436     if (socket->fd[i].fd < 0)
437       continue;
438     close (socket->fd[i].fd);
439     socket->fd[i].fd = -1;
440   }
441   
442   sfree(socket);
443 } /* }}} void pinba_socket_free */
445 static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
446     size_t buffer_size)
448   Pinba__Request *request;  
449   
450   request = pinba__request__unpack (NULL, buffer_size, buffer);
451   
452   if (!request)
453     return (-1);
455   service_process_request(request);
456   pinba__request__free_unpacked (request, NULL);
457     
458   return (0);
459 } /* }}} int pinba_process_stats_packet */
461 static int pinba_udp_read_callback_fn (int sock) /* {{{ */
463   uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
464   size_t buffer_size;
465   int status;
467   while (42)
468   {
469     buffer_size = sizeof (buffer);
470     status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
471     if (status < 0)
472     {
473       char errbuf[1024];
475       if ((errno == EINTR)
476 #ifdef EWOULDBLOCK
477           || (errno == EWOULDBLOCK)
478 #endif
479           || (errno == EAGAIN))
480       {
481         continue;
482       }
484       WARNING("pinba plugin: recvfrom(2) failed: %s",
485           sstrerror (errno, errbuf, sizeof (errbuf)));
486       return (-1);
487     }
488     else if (status == 0)
489     {
490       DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
491       return (-1);
492     }
493     else /* if (status > 0) */
494     {
495       assert (((size_t) status) < buffer_size);
496       buffer_size = (size_t) status;
497       buffer[buffer_size] = 0;
499       status = pinba_process_stats_packet (buffer, buffer_size);
500       if (status != 0)
501         DEBUG("pinba plugin: Parsing packet failed.");
502       return (status);
503     }
504   } /* while (42) */
506   /* not reached */
507   assert (23 == 42);
508   return (-1);
509 } /* }}} void pinba_udp_read_callback_fn */
511 static int receive_loop (void) /* {{{ */
513   pinba_socket_t *s;
515   s = pinba_socket_open (conf_node, conf_service);
516   if (s == NULL)
517   {
518     ERROR ("pinba plugin: Collector thread is exiting prematurely.");
519     return (-1);
520   }
522   while (!collector_thread_do_shutdown)
523   {
524     int status;
525     nfds_t i;
527     if (s->fd_num < 1)
528       break;
530     status = poll (s->fd, s->fd_num, /* timeout = */ 1000);
531     if (status == 0) /* timeout */
532     {
533       continue;
534     }
535     else if (status < 0)
536     {
537       char errbuf[1024];
539       if ((errno == EINTR) || (errno == EAGAIN))
540         continue;
542       ERROR ("pinba plugin: poll(2) failed: %s",
543           sstrerror (errno, errbuf, sizeof (errbuf)));
544       pinba_socket_free (s);
545       return (-1);
546     }
548     for (i = 0; i < s->fd_num; i++)
549     {
550       if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
551       {
552         pb_del_socket (s, i);
553         i--;
554       }
555       else if (s->fd[i].revents & (POLLIN | POLLPRI))
556       {
557         pinba_udp_read_callback_fn (s->fd[i].fd);
558       }
559     } /* for (s->fd) */
560   } /* while (!collector_thread_do_shutdown) */
562   pinba_socket_free (s);
563   s = NULL;
565   return (0);
566 } /* }}} int receive_loop */
568 static void *collector_thread (void *arg) /* {{{ */
570   receive_loop ();
572   memset (&collector_thread_id, 0, sizeof (collector_thread_id));
573   collector_thread_running = 0;
574   pthread_exit (NULL);
575   return (NULL);
576 } /* }}} void *collector_thread */
578 /*
579  * Plugin declaration section
580  */
582 static int config_set (char **var, const char *value) /* {{{ */
584   /* code from nginx plugin for collectd */
585   if (*var != NULL) {
586     free (*var);
587     *var = NULL;
588   }
589   
590   if ((*var = strdup (value)) == NULL) return (1);
591   else return (0);
592 } /* }}} int config_set */
594 static int plugin_config (oconfig_item_t *ci) /* {{{ */
596   unsigned int i, o;
597   
598   service_statnode_begin();
599   
600   for (i = 0; i < ci->children_num; i++) {
601     oconfig_item_t *child = ci->children + i;
602     if (strcasecmp ("Address", child->key) == 0) {
603       if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
604         WARNING ("pinba plugin: `Address' needs exactly one string argument.");
605         return (-1);
606       }
607       config_set(&conf_node, child->values[0].value.string);
608     } else if (strcasecmp ("Port", child->key) == 0) {
609       if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING)){
610         WARNING ("pinba plugin: `Port' needs exactly one string argument.");
611         return (-1);
612       }
613       config_set(&conf_service, child->values[0].value.string);
614     } else if (strcasecmp ("View", child->key) == 0) {
615       const char *name=NULL, *host=NULL, *server=NULL, *script=NULL;
616       if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING) || strlen(child->values[0].value.string)==0){
617         WARNING ("pinba plugin: `View' needs exactly one non-empty string argument.");
618         return (-1);
619       }
620       name = child->values[0].value.string;
621       for(o=0; o<child->children_num; o++){
622         oconfig_item_t *node = child->children + o;
623         if (strcasecmp ("Host", node->key) == 0) {
624           if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
625             WARNING ("pinba plugin: `View->Host' needs exactly one non-empty string argument.");
626             return (-1);
627           }
628           host = node->values[0].value.string;
629         } else if (strcasecmp ("Server", node->key) == 0) {
630           if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
631             WARNING ("pinba plugin: `View->Server' needs exactly one non-empty string argument.");
632             return (-1);
633           }
634           server = node->values[0].value.string;
635         } else if (strcasecmp ("Script", node->key) == 0) {
636           if ((node->values_num != 1) || (node->values[0].type != OCONFIG_TYPE_STRING) || strlen(node->values[0].value.string)==0){
637             WARNING ("pinba plugin: `View->Script' needs exactly one non-empty string argument.");
638             return (-1);
639           }
640           script = node->values[0].value.string;
641         } else {
642           WARNING ("pinba plugin: In `<View>' context allowed only `Host', `Server' and `Script' options but not the `%s'.", node->key);
643           return (-1);
644         }
645       }
646       /* add new statnode */
647       service_statnode_add(name, host, server, script);
648     } else {
649       WARNING ("pinba plugin: In `<Plugin pinba>' context allowed only `Address', `Port' and `Observe' options but not the `%s'.", child->key);
650       return (-1);
651     }
652   }
653   
654   service_statnode_end();
655   
656   return (0);
657 } /* int pinba_config */
659 static int plugin_init (void) /* {{{ */
661   int status;
663   if (collector_thread_running)
664     return (0);
666   status = pthread_create (&collector_thread_id,
667       /* attrs = */ NULL,
668       collector_thread,
669       /* args = */ NULL);
670   if (status != 0)
671   {
672     char errbuf[1024];
673     ERROR ("pinba plugin: pthread_create(3) failed: %s",
674         sstrerror (errno, errbuf, sizeof (errbuf)));
675     return (-1);
676   }
677   collector_thread_running = 1;
679   return (0);
680 } /* }}} */
682 static int plugin_shutdown (void) /* {{{ */
684   if (collector_thread_running)
685   {
686     int status;
688     DEBUG ("pinba plugin: Shutting down collector thread.");
689     collector_thread_do_shutdown = 1;
691     status = pthread_join (collector_thread_id, /* retval = */ NULL);
692     if (status != 0)
693     {
694       char errbuf[1024];
695       ERROR ("pinba plugin: pthread_join(3) failed: %s",
696           sstrerror (status, errbuf, sizeof (errbuf)));
697     }
699     collector_thread_running = 0;
700     collector_thread_do_shutdown = 0;
701   } /* if (collector_thread_running) */
703   return (0);
704 } /* }}} int plugin_shutdown */
706 static int plugin_submit (const char *plugin_instance, /* {{{ */
707                const char *type,
708                const pinba_statres *res) {
709   value_t values[6];
710   value_list_t vl = VALUE_LIST_INIT;
711   
712   values[0].gauge = res->req_per_sec;
713   values[1].gauge = res->req_time;
714   values[2].gauge = res->ru_utime;
715   values[3].gauge = res->ru_stime;
716   values[4].gauge = res->doc_size;
717   values[5].gauge = res->mem_peak;
718   
719   vl.values = values;
720   vl.values_len = 6;
721   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
722   sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
723   sstrncpy (vl.plugin_instance, plugin_instance,
724             sizeof(vl.plugin_instance));
725   sstrncpy (vl.type, type, sizeof (vl.type));
726   INFO("Pinba Dispatch");
727   plugin_dispatch_values (&vl);
729   return (0);
730 } /* }}} int plugin_submit */
732 static int plugin_read (void) /* {{{ */
734   unsigned int i=0;
735   static pinba_statres res;
736   
737   while ((i = service_statnode_collect (&res, i)) != 0)
738   {
739     plugin_submit(res.name, "pinba_view", &res);
740   }
741   
742   return 0;
743 } /* }}} int plugin_read */
745 void module_register (void) /* {{{ */
747   plugin_register_complex_config ("pinba", plugin_config);
748   plugin_register_init ("pinba", plugin_init);
749   plugin_register_read ("pinba", plugin_read);
750   plugin_register_shutdown ("pinba", plugin_shutdown);
751 } /* }}} void module_register */
753 /* vim: set sw=2 sts=2 et fdm=marker : */