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