Code

{GPL, other}: Relicense to MIT license.
[collectd.git] / src / statsd.c
1 /**
2  * collectd - src/statsd.c
3  * Copyright (C) 2013       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  */
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31 #include "utils_avltree.h"
32 #include "utils_complain.h"
33 #include "utils_latency.h"
35 #include <pthread.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netdb.h>
40 #include <poll.h>
42 /* AIX doesn't have MSG_DONTWAIT */
43 #ifndef MSG_DONTWAIT
44 #  define MSG_DONTWAIT MSG_NONBLOCK
45 #endif
47 #ifndef STATSD_DEFAULT_NODE
48 # define STATSD_DEFAULT_NODE NULL
49 #endif
51 #ifndef STATSD_DEFAULT_SERVICE
52 # define STATSD_DEFAULT_SERVICE "8125"
53 #endif
55 enum metric_type_e
56 {
57   STATSD_COUNTER,
58   STATSD_TIMER,
59   STATSD_GAUGE,
60   STATSD_SET
61 };
62 typedef enum metric_type_e metric_type_t;
64 struct statsd_metric_s
65 {
66   metric_type_t type;
67   double value;
68   latency_counter_t *latency;
69   c_avl_tree_t *set;
70   unsigned long updates_num;
71 };
72 typedef struct statsd_metric_s statsd_metric_t;
74 static c_avl_tree_t   *metrics_tree = NULL;
75 static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_t network_thread;
78 static _Bool     network_thread_running = 0;
79 static _Bool     network_thread_shutdown = 0;
81 static char *conf_node = NULL;
82 static char *conf_service = NULL;
84 static _Bool conf_delete_counters = 0;
85 static _Bool conf_delete_timers   = 0;
86 static _Bool conf_delete_gauges   = 0;
87 static _Bool conf_delete_sets     = 0;
89 static double *conf_timer_percentile = NULL;
90 static size_t  conf_timer_percentile_num = 0;
92 static _Bool conf_timer_lower     = 0;
93 static _Bool conf_timer_upper     = 0;
94 static _Bool conf_timer_sum       = 0;
95 static _Bool conf_timer_count     = 0;
97 /* Must hold metrics_lock when calling this function. */
98 static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name, /* {{{ */
99     metric_type_t type)
101   char key[DATA_MAX_NAME_LEN + 2];
102   char *key_copy;
103   statsd_metric_t *metric;
104   int status;
106   switch (type)
107   {
108     case STATSD_COUNTER: key[0] = 'c'; break;
109     case STATSD_TIMER:   key[0] = 't'; break;
110     case STATSD_GAUGE:   key[0] = 'g'; break;
111     case STATSD_SET:     key[0] = 's'; break;
112     default: return (NULL);
113   }
115   key[1] = ':';
116   sstrncpy (&key[2], name, sizeof (key) - 2);
118   status = c_avl_get (metrics_tree, key, (void *) &metric);
119   if (status == 0)
120     return (metric);
122   key_copy = strdup (key);
123   if (key_copy == NULL)
124   {
125     ERROR ("statsd plugin: strdup failed.");
126     return (NULL);
127   }
129   metric = malloc (sizeof (*metric));
130   if (metric == NULL)
131   {
132     ERROR ("statsd plugin: malloc failed.");
133     sfree (key_copy);
134     return (NULL);
135   }
136   memset (metric, 0, sizeof (*metric));
138   metric->type = type;
139   metric->latency = NULL;
140   metric->set = NULL;
142   status = c_avl_insert (metrics_tree, key_copy, metric);
143   if (status != 0)
144   {
145     ERROR ("statsd plugin: c_avl_insert failed.");
146     sfree (key_copy);
147     sfree (metric);
148     return (NULL);
149   }
151   return (metric);
152 } /* }}} statsd_metric_lookup_unsafe */
154 static int statsd_metric_set (char const *name, double value, /* {{{ */
155     metric_type_t type)
157   statsd_metric_t *metric;
159   pthread_mutex_lock (&metrics_lock);
161   metric = statsd_metric_lookup_unsafe (name, type);
162   if (metric == NULL)
163   {
164     pthread_mutex_unlock (&metrics_lock);
165     return (-1);
166   }
168   metric->value = value;
169   metric->updates_num++;
171   pthread_mutex_unlock (&metrics_lock);
173   return (0);
174 } /* }}} int statsd_metric_set */
176 static int statsd_metric_add (char const *name, double delta, /* {{{ */
177     metric_type_t type)
179   statsd_metric_t *metric;
181   pthread_mutex_lock (&metrics_lock);
183   metric = statsd_metric_lookup_unsafe (name, type);
184   if (metric == NULL)
185   {
186     pthread_mutex_unlock (&metrics_lock);
187     return (-1);
188   }
190   metric->value += delta;
191   metric->updates_num++;
193   pthread_mutex_unlock (&metrics_lock);
195   return (0);
196 } /* }}} int statsd_metric_add */
198 static int statsd_parse_value (char const *str, value_t *ret_value) /* {{{ */
200   char *endptr = NULL;
202   ret_value->gauge = (gauge_t) strtod (str, &endptr);
203   if ((str == endptr) || ((endptr != NULL) && (*endptr != 0)))
204     return (-1);
206   return (0);
207 } /* }}} int statsd_parse_value */
209 static int statsd_handle_counter (char const *name, /* {{{ */
210     char const *value_str,
211     char const *extra)
213   value_t value;
214   value_t scale;
215   int status;
217   if ((extra != NULL) && (extra[0] != '@'))
218     return (-1);
220   scale.gauge = 1.0;
221   if (extra != NULL)
222   {
223     status = statsd_parse_value (extra + 1, &scale);
224     if (status != 0)
225       return (status);
227     if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
228       return (-1);
229   }
231   value.gauge = 1.0;
232   status = statsd_parse_value (value_str, &value);
233   if (status != 0)
234     return (status);
236   return (statsd_metric_add (name, (double) (value.gauge / scale.gauge),
237         STATSD_COUNTER));
238 } /* }}} int statsd_handle_counter */
240 static int statsd_handle_gauge (char const *name, /* {{{ */
241     char const *value_str)
243   value_t value;
244   int status;
246   value.gauge = 0;
247   status = statsd_parse_value (value_str, &value);
248   if (status != 0)
249     return (status);
251   if ((value_str[0] == '+') || (value_str[0] == '-'))
252     return (statsd_metric_add (name, (double) value.gauge, STATSD_GAUGE));
253   else
254     return (statsd_metric_set (name, (double) value.gauge, STATSD_GAUGE));
255 } /* }}} int statsd_handle_gauge */
257 static int statsd_handle_timer (char const *name, /* {{{ */
258     char const *value_str)
260   statsd_metric_t *metric;
261   value_t value_ms;
262   cdtime_t value;
263   int status;
265   value_ms.derive = 0;
266   status = statsd_parse_value (value_str, &value_ms);
267   if (status != 0)
268     return (status);
270   value = MS_TO_CDTIME_T (value_ms.gauge);
272   pthread_mutex_lock (&metrics_lock);
274   metric = statsd_metric_lookup_unsafe (name, STATSD_TIMER);
275   if (metric == NULL)
276   {
277     pthread_mutex_unlock (&metrics_lock);
278     return (-1);
279   }
281   if (metric->latency == NULL)
282     metric->latency = latency_counter_create ();
283   if (metric->latency == NULL)
284   {
285     pthread_mutex_unlock (&metrics_lock);
286     return (-1);
287   }
289   latency_counter_add (metric->latency, value);
290   metric->updates_num++;
292   pthread_mutex_unlock (&metrics_lock);
293   return (0);
294 } /* }}} int statsd_handle_timer */
296 static int statsd_handle_set (char const *name, /* {{{ */
297     char const *set_key_orig)
299   statsd_metric_t *metric = NULL;
300   char *set_key;
301   int status;
303   pthread_mutex_lock (&metrics_lock);
305   metric = statsd_metric_lookup_unsafe (name, STATSD_SET);
306   if (metric == NULL)
307   {
308     pthread_mutex_unlock (&metrics_lock);
309     return (-1);
310   }
312   /* Make sure metric->set exists. */
313   if (metric->set == NULL)
314     metric->set = c_avl_create ((void *) strcmp);
316   if (metric->set == NULL)
317   {
318     pthread_mutex_unlock (&metrics_lock);
319     ERROR ("statsd plugin: c_avl_create failed.");
320     return (-1);
321   }
323   set_key = strdup (set_key_orig);
324   if (set_key == NULL)
325   {
326     pthread_mutex_unlock (&metrics_lock);
327     ERROR ("statsd plugin: strdup failed.");
328     return (-1);
329   }
331   status = c_avl_insert (metric->set, set_key, /* value = */ NULL);
332   if (status < 0)
333   {
334     pthread_mutex_unlock (&metrics_lock);
335     if (status < 0)
336       ERROR ("statsd plugin: c_avl_insert (\"%s\") failed with status %i.",
337           set_key, status);
338     sfree (set_key);
339     return (-1);
340   }
341   else if (status > 0) /* key already exists */
342   {
343     sfree (set_key);
344   }
346   metric->updates_num++;
348   pthread_mutex_unlock (&metrics_lock);
349   return (0);
350 } /* }}} int statsd_handle_set */
352 static int statsd_parse_line (char *buffer) /* {{{ */
354   char *name = buffer;
355   char *value;
356   char *type;
357   char *extra;
359   type = strchr (name, '|');
360   if (type == NULL)
361     return (-1);
362   *type = 0;
363   type++;
365   value = strrchr (name, ':');
366   if (value == NULL)
367     return (-1);
368   *value = 0;
369   value++;
371   extra = strchr (type, '|');
372   if (extra != NULL)
373   {
374     *extra = 0;
375     extra++;
376   }
378   if (strcmp ("c", type) == 0)
379     return (statsd_handle_counter (name, value, extra));
381   /* extra is only valid for counters */
382   if (extra != NULL)
383     return (-1);
385   if (strcmp ("g", type) == 0)
386     return (statsd_handle_gauge (name, value));
387   else if (strcmp ("ms", type) == 0)
388     return (statsd_handle_timer (name, value));
389   else if (strcmp ("s", type) == 0)
390     return (statsd_handle_set (name, value));
391   else
392     return (-1);
393 } /* }}} void statsd_parse_line */
395 static void statsd_parse_buffer (char *buffer) /* {{{ */
397   while (buffer != NULL)
398   {
399     char orig[64];
400     char *next;
401     int status;
403     next = strchr (buffer, '\n');
404     if (next != NULL)
405     {
406       *next = 0;
407       next++;
408     }
410     if (*buffer == 0)
411     {
412       buffer = next;
413       continue;
414     }
416     sstrncpy (orig, buffer, sizeof (orig));
418     status = statsd_parse_line (buffer);
419     if (status != 0)
420       ERROR ("statsd plugin: Unable to parse line: \"%s\"", orig);
422     buffer = next;
423   }
424 } /* }}} void statsd_parse_buffer */
426 static void statsd_network_read (int fd) /* {{{ */
428   char buffer[4096];
429   size_t buffer_size;
430   ssize_t status;
432   status = recv (fd, buffer, sizeof (buffer), /* flags = */ MSG_DONTWAIT);
433   if (status < 0)
434   {
435     char errbuf[1024];
437     if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
438       return;
440     ERROR ("statsd plugin: recv(2) failed: %s",
441         sstrerror (errno, errbuf, sizeof (errbuf)));
442     return;
443   }
445   buffer_size = (size_t) status;
446   if (buffer_size >= sizeof (buffer))
447     buffer_size = sizeof (buffer) - 1;
448   buffer[buffer_size] = 0;
450   statsd_parse_buffer (buffer);
451 } /* }}} void statsd_network_read */
453 static int statsd_network_init (struct pollfd **ret_fds, /* {{{ */
454     size_t *ret_fds_num)
456   struct pollfd *fds = NULL;
457   size_t fds_num = 0;
459   struct addrinfo ai_hints;
460   struct addrinfo *ai_list = NULL;
461   struct addrinfo *ai_ptr;
462   int status;
464   char const *node = (conf_node != NULL) ? conf_node : STATSD_DEFAULT_NODE;
465   char const *service = (conf_service != NULL)
466     ? conf_service : STATSD_DEFAULT_SERVICE;
468   memset (&ai_hints, 0, sizeof (ai_hints));
469   ai_hints.ai_flags = AI_PASSIVE;
470 #ifdef AI_ADDRCONFIG
471   ai_hints.ai_flags |= AI_ADDRCONFIG;
472 #endif
473   ai_hints.ai_family = AF_UNSPEC;
474   ai_hints.ai_socktype = SOCK_DGRAM;
476   status = getaddrinfo (node, service, &ai_hints, &ai_list);
477   if (status != 0)
478   {
479     ERROR ("statsd plugin: getaddrinfo (\"%s\", \"%s\") failed: %s",
480         node, service, gai_strerror (status));
481     return (status);
482   }
484   for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
485   {
486     int fd;
487     struct pollfd *tmp;
489     char dbg_node[NI_MAXHOST];
490     char dbg_service[NI_MAXSERV];
492     fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
493     if (fd < 0)
494     {
495       char errbuf[1024];
496       ERROR ("statsd plugin: socket(2) failed: %s",
497           sstrerror (errno, errbuf, sizeof (errbuf)));
498       continue;
499     }
501     getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
502         dbg_node, sizeof (dbg_node), dbg_service, sizeof (dbg_service),
503         NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV);
504     DEBUG ("statsd plugin: Trying to bind to [%s]:%s ...", dbg_node, dbg_service);
506     status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
507     if (status != 0)
508     {
509       char errbuf[1024];
510       ERROR ("statsd plugin: bind(2) failed: %s",
511           sstrerror (errno, errbuf, sizeof (errbuf)));
512       close (fd);
513       continue;
514     }
516     tmp = realloc (fds, sizeof (*fds) * (fds_num + 1));
517     if (tmp == NULL)
518     {
519       ERROR ("statsd plugin: realloc failed.");
520       continue;
521     }
522     fds = tmp;
523     tmp = fds + fds_num;
524     fds_num++;
526     memset (tmp, 0, sizeof (*tmp));
527     tmp->fd = fd;
528     tmp->events = POLLIN | POLLPRI;
529   }
531   freeaddrinfo (ai_list);
533   if (fds_num == 0)
534   {
535     ERROR ("statsd plugin: Unable to create listening socket for [%s]:%s.",
536         (node != NULL) ? node : "::", service);
537     return (ENOENT);
538   }
540   *ret_fds = fds;
541   *ret_fds_num = fds_num;
542   return (0);
543 } /* }}} int statsd_network_init */
545 static void *statsd_network_thread (void *args) /* {{{ */
547   struct pollfd *fds = NULL;
548   size_t fds_num = 0;
549   int status;
550   size_t i;
552   status = statsd_network_init (&fds, &fds_num);
553   if (status != 0)
554   {
555     ERROR ("statsd plugin: Unable to open listening sockets.");
556     pthread_exit ((void *) 0);
557   }
559   while (!network_thread_shutdown)
560   {
561     status = poll (fds, (nfds_t) fds_num, /* timeout = */ -1);
562     if (status < 0)
563     {
564       char errbuf[1024];
566       if ((errno == EINTR) || (errno == EAGAIN))
567         continue;
569       ERROR ("statsd plugin: poll(2) failed: %s",
570           sstrerror (errno, errbuf, sizeof (errbuf)));
571       break;
572     }
574     for (i = 0; i < fds_num; i++)
575     {
576       if ((fds[i].revents & (POLLIN | POLLPRI)) == 0)
577         continue;
579       statsd_network_read (fds[i].fd);
580       fds[i].revents = 0;
581     }
582   } /* while (!network_thread_shutdown) */
584   /* Clean up */
585   for (i = 0; i < fds_num; i++)
586     close (fds[i].fd);
587   sfree (fds);
589   return ((void *) 0);
590 } /* }}} void *statsd_network_thread */
592 static int statsd_config_timer_percentile (oconfig_item_t *ci) /* {{{ */
594   double percent = NAN;
595   double *tmp;
596   int status;
598   status = cf_util_get_double (ci, &percent);
599   if (status != 0)
600     return (status);
602   if ((percent <= 0.0) || (percent >= 100))
603   {
604     ERROR ("statsd plugin: The value for \"%s\" must be between 0 and 100, "
605         "exclusively.", ci->key);
606     return (ERANGE);
607   }
609   tmp = realloc (conf_timer_percentile,
610       sizeof (*conf_timer_percentile) * (conf_timer_percentile_num + 1));
611   if (tmp == NULL)
612   {
613     ERROR ("statsd plugin: realloc failed.");
614     return (ENOMEM);
615   }
616   conf_timer_percentile = tmp;
617   conf_timer_percentile[conf_timer_percentile_num] = percent;
618   conf_timer_percentile_num++;
620   return (0);
621 } /* }}} int statsd_config_timer_percentile */
623 static int statsd_config (oconfig_item_t *ci) /* {{{ */
625   int i;
627   for (i = 0; i < ci->children_num; i++)
628   {
629     oconfig_item_t *child = ci->children + i;
631     if (strcasecmp ("Host", child->key) == 0)
632       cf_util_get_string (child, &conf_node);
633     else if (strcasecmp ("Port", child->key) == 0)
634       cf_util_get_service (child, &conf_service);
635     else if (strcasecmp ("DeleteCounters", child->key) == 0)
636       cf_util_get_boolean (child, &conf_delete_counters);
637     else if (strcasecmp ("DeleteTimers", child->key) == 0)
638       cf_util_get_boolean (child, &conf_delete_timers);
639     else if (strcasecmp ("DeleteGauges", child->key) == 0)
640       cf_util_get_boolean (child, &conf_delete_gauges);
641     else if (strcasecmp ("DeleteSets", child->key) == 0)
642       cf_util_get_boolean (child, &conf_delete_sets);
643     else if (strcasecmp ("TimerLower", child->key) == 0)
644       cf_util_get_boolean (child, &conf_timer_lower);
645     else if (strcasecmp ("TimerUpper", child->key) == 0)
646       cf_util_get_boolean (child, &conf_timer_upper);
647     else if (strcasecmp ("TimerSum", child->key) == 0)
648       cf_util_get_boolean (child, &conf_timer_sum);
649     else if (strcasecmp ("TimerCount", child->key) == 0)
650       cf_util_get_boolean (child, &conf_timer_count);
651     else if (strcasecmp ("TimerPercentile", child->key) == 0)
652       statsd_config_timer_percentile (child);
653     else
654       ERROR ("statsd plugin: The \"%s\" config option is not valid.",
655           child->key);
656   }
658   return (0);
659 } /* }}} int statsd_config */
661 static int statsd_init (void) /* {{{ */
663   pthread_mutex_lock (&metrics_lock);
664   if (metrics_tree == NULL)
665     metrics_tree = c_avl_create ((void *) strcmp);
667   if (!network_thread_running)
668   {
669     int status;
671     status = pthread_create (&network_thread,
672         /* attr = */ NULL,
673         statsd_network_thread,
674         /* args = */ NULL);
675     if (status != 0)
676     {
677       char errbuf[1024];
678       pthread_mutex_unlock (&metrics_lock);
679       ERROR ("statsd plugin: pthread_create failed: %s",
680           sstrerror (errno, errbuf, sizeof (errbuf)));
681       return (status);
682     }
683   }
684   network_thread_running = 1;
686   pthread_mutex_unlock (&metrics_lock);
688   return (0);
689 } /* }}} int statsd_init */
691 /* Must hold metrics_lock when calling this function. */
692 static int statsd_metric_clear_set_unsafe (statsd_metric_t *metric) /* {{{ */
694   void *key;
695   void *value;
697   if ((metric == NULL) || (metric->type != STATSD_SET))
698     return (EINVAL);
700   if (metric->set == NULL)
701     return (0);
703   while (c_avl_pick (metric->set, &key, &value) == 0)
704   {
705     sfree (key);
706     sfree (value);
707   }
709   return (0);
710 } /* }}} int statsd_metric_clear_set_unsafe */
712 /* Must hold metrics_lock when calling this function. */
713 static int statsd_metric_submit_unsafe (char const *name, /* {{{ */
714     statsd_metric_t const *metric)
716   value_t values[1];
717   value_list_t vl = VALUE_LIST_INIT;
719   vl.values = values;
720   vl.values_len = 1;
721   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
722   sstrncpy (vl.plugin, "statsd", sizeof (vl.plugin));
724   if (metric->type == STATSD_GAUGE)
725     sstrncpy (vl.type, "gauge", sizeof (vl.type));
726   else if (metric->type == STATSD_TIMER)
727     sstrncpy (vl.type, "latency", sizeof (vl.type));
728   else if (metric->type == STATSD_SET)
729     sstrncpy (vl.type, "objects", sizeof (vl.type));
730   else /* if (metric->type == STATSD_COUNTER) */
731     sstrncpy (vl.type, "derive", sizeof (vl.type));
733   sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
735   if (metric->type == STATSD_GAUGE)
736     values[0].gauge = (gauge_t) metric->value;
737   else if (metric->type == STATSD_TIMER)
738   {
739     size_t i;
741     if (metric->updates_num == 0)
742       return (0);
744     vl.time = cdtime ();
746     ssnprintf (vl.type_instance, sizeof (vl.type_instance),
747         "%s-average", name);
748     values[0].gauge = CDTIME_T_TO_DOUBLE (
749         latency_counter_get_average (metric->latency));
750     plugin_dispatch_values (&vl);
752     if (conf_timer_lower) {
753       ssnprintf (vl.type_instance, sizeof (vl.type_instance),
754           "%s-lower", name);
755       values[0].gauge = CDTIME_T_TO_DOUBLE (
756           latency_counter_get_min (metric->latency));
757       plugin_dispatch_values (&vl);
758     }
760     if (conf_timer_upper) {
761       ssnprintf (vl.type_instance, sizeof (vl.type_instance),
762           "%s-upper", name);
763       values[0].gauge = CDTIME_T_TO_DOUBLE (
764           latency_counter_get_max (metric->latency));
765       plugin_dispatch_values (&vl);
766     }
768     if (conf_timer_sum) {
769       ssnprintf (vl.type_instance, sizeof (vl.type_instance),
770           "%s-sum", name);
771       values[0].gauge = CDTIME_T_TO_DOUBLE (
772           latency_counter_get_sum (metric->latency));
773       plugin_dispatch_values (&vl);
774     }
776     for (i = 0; i < conf_timer_percentile_num; i++)
777     {
778       ssnprintf (vl.type_instance, sizeof (vl.type_instance),
779           "%s-percentile-%.0f", name, conf_timer_percentile[i]);
780       values[0].gauge = CDTIME_T_TO_DOUBLE (
781           latency_counter_get_percentile (
782             metric->latency, conf_timer_percentile[i]));
783       plugin_dispatch_values (&vl);
784     }
786     /* Keep this at the end, since vl.type is set to "gauge" here. The
787      * vl.type's above are implicitly set to "latency". */
788     if (conf_timer_count) {
789       sstrncpy (vl.type, "gauge", sizeof (vl.type));
790       ssnprintf (vl.type_instance, sizeof (vl.type_instance),
791           "%s-count", name);
792       values[0].gauge = latency_counter_get_num (metric->latency);
793       plugin_dispatch_values (&vl);
794     }
796     latency_counter_reset (metric->latency);
797     return (0);
798   }
799   else if (metric->type == STATSD_SET)
800   {
801     if (metric->set == NULL)
802       values[0].gauge = 0.0;
803     else
804       values[0].gauge = (gauge_t) c_avl_size (metric->set);
805   }
806   else
807     values[0].derive = (derive_t) metric->value;
809   return (plugin_dispatch_values (&vl));
810 } /* }}} int statsd_metric_submit_unsafe */
812 static int statsd_read (void) /* {{{ */
814   c_avl_iterator_t *iter;
815   char *name;
816   statsd_metric_t *metric;
818   char **to_be_deleted = NULL;
819   size_t to_be_deleted_num = 0;
820   size_t i;
822   pthread_mutex_lock (&metrics_lock);
824   if (metrics_tree == NULL)
825   {
826     pthread_mutex_unlock (&metrics_lock);
827     return (0);
828   }
830   iter = c_avl_get_iterator (metrics_tree);
831   while (c_avl_iterator_next (iter, (void *) &name, (void *) &metric) == 0)
832   {
833     if ((metric->updates_num == 0)
834         && ((conf_delete_counters && (metric->type == STATSD_COUNTER))
835           || (conf_delete_timers && (metric->type == STATSD_TIMER))
836           || (conf_delete_gauges && (metric->type == STATSD_GAUGE))
837           || (conf_delete_sets && (metric->type == STATSD_SET))))
838     {
839       DEBUG ("statsd plugin: Deleting metric \"%s\".", name);
840       strarray_add (&to_be_deleted, &to_be_deleted_num, name);
841       continue;
842     }
844     /* Names have a prefix, e.g. "c:", which determines the (statsd) type.
845      * Remove this here. */
846     statsd_metric_submit_unsafe (name + 2, metric);
848     /* Reset the metric. */
849     metric->updates_num = 0;
850     if (metric->type == STATSD_SET)
851       statsd_metric_clear_set_unsafe (metric);
852   }
853   c_avl_iterator_destroy (iter);
855   for (i = 0; i < to_be_deleted_num; i++)
856   {
857     int status;
859     status = c_avl_remove (metrics_tree, to_be_deleted[i],
860         (void *) &name, (void *) &metric);
861     if (status != 0)
862     {
863       ERROR ("stats plugin: c_avl_remove (\"%s\") failed with status %i.",
864           to_be_deleted[i], status);
865       continue;
866     }
868     sfree (name);
869     sfree (metric);
870   }
872   pthread_mutex_unlock (&metrics_lock);
874   strarray_free (to_be_deleted, to_be_deleted_num);
876   return (0);
877 } /* }}} int statsd_read */
879 static int statsd_shutdown (void) /* {{{ */
881   void *key;
882   void *value;
884   pthread_mutex_lock (&metrics_lock);
886   if (network_thread_running)
887   {
888     network_thread_shutdown = 1;
889     pthread_kill (network_thread, SIGTERM);
890     pthread_join (network_thread, /* retval = */ NULL);
891   }
892   network_thread_running = 0;
894   while (c_avl_pick (metrics_tree, &key, &value) == 0)
895   {
896     sfree (key);
897     sfree (value);
898   }
899   c_avl_destroy (metrics_tree);
900   metrics_tree = NULL;
902   sfree (conf_node);
903   sfree (conf_service);
905   pthread_mutex_unlock (&metrics_lock);
907   return (0);
908 } /* }}} int statsd_shutdown */
910 void module_register (void)
912   plugin_register_complex_config ("statsd", statsd_config);
913   plugin_register_init ("statsd", statsd_init);
914   plugin_register_read ("statsd", statsd_read);
915   plugin_register_shutdown ("statsd", statsd_shutdown);
918 /* vim: set sw=2 sts=2 et fdm=marker : */