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