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