statsd plugin: Dont'a call common.c:parse_value().
[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 key[DATA_MAX_NAME_LEN + 2];
87   char *key_copy;
88   statsd_metric_t *metric;
89   int status;
90
91   switch (type)
92   {
93     case STATSD_COUNTER: key[0] = 'c'; break;
94     case STATSD_TIMER:   key[0] = 't'; break;
95     case STATSD_GAUGE:   key[0] = 'g'; break;
96     case STATSD_SET:     key[0] = 's'; break;
97     default: return (NULL);
98   }
99
100   key[1] = ':';
101   sstrncpy (&key[2], name, sizeof (key) - 2);
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_parse_value (char const *str, value_t *ret_value) /* {{{ */
184 {
185   char *endptr = NULL;
186
187   ret_value->derive = (derive_t) strtoll (str, &endptr, /* base = */ 0);
188   if ((str == endptr) || ((endptr != NULL) && (*endptr != 0)))
189     return (-1);
190
191   return (0);
192 } /* }}} int statsd_parse_value */
193
194 static int statsd_handle_counter (char const *name, /* {{{ */
195     char const *value_str,
196     char const *extra)
197 {
198   value_t value;
199   value_t scale;
200   int status;
201
202   if ((extra != NULL) && (extra[0] != '@'))
203     return (-1);
204
205   scale.gauge = 1.0;
206   if (extra != NULL)
207   {
208     status = parse_value (extra + 1, &scale, DS_TYPE_GAUGE);
209     if (status != 0)
210       return (status);
211
212     if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
213       return (-1);
214   }
215
216   value.derive = 1;
217   status = statsd_parse_value (value_str, &value);
218   if (status != 0)
219     return (status);
220
221   if (value.derive < 1)
222     return (-1);
223
224   return (statsd_metric_add (name,
225         (int64_t) (((gauge_t) value.derive) / 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.derive = 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, (int64_t) value.derive, STATSD_GAUGE));
242   else
243     return (statsd_metric_set (name, (int64_t) value.derive, 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.derive);
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   char *dummy;
387   char *saveptr = NULL;
388   char *ptr;
389
390   for (dummy = buffer;
391       (ptr = strtok_r (dummy, "\r\n", &saveptr)) != NULL;
392       dummy = NULL)
393   {
394     char *line_orig = sstrdup (ptr);
395     int status;
396
397     status = statsd_parse_line (ptr);
398     if (status != 0)
399       ERROR ("statsd plugin: Unable to parse line: \"%s\"", line_orig);
400
401     sfree (line_orig);
402   }
403 } /* }}} void statsd_parse_buffer */
404
405 static void statsd_network_read (int fd) /* {{{ */
406 {
407   char buffer[4096];
408   size_t buffer_size;
409   ssize_t status;
410
411   status = recv (fd, buffer, sizeof (buffer), /* flags = */ MSG_DONTWAIT);
412   if (status < 0)
413   {
414     char errbuf[1024];
415
416     if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
417       return;
418
419     ERROR ("statsd plugin: recv(2) failed: %s",
420         sstrerror (errno, errbuf, sizeof (errbuf)));
421     return;
422   }
423
424   buffer_size = (size_t) status;
425   if (buffer_size >= sizeof (buffer))
426     buffer_size = sizeof (buffer) - 1;
427   buffer[buffer_size] = 0;
428
429   statsd_parse_buffer (buffer);
430 } /* }}} void statsd_network_read */
431
432 static int statsd_network_init (struct pollfd **ret_fds, /* {{{ */
433     size_t *ret_fds_num)
434 {
435   struct pollfd *fds = NULL;
436   size_t fds_num = 0;
437
438   struct addrinfo ai_hints;
439   struct addrinfo *ai_list = NULL;
440   struct addrinfo *ai_ptr;
441   int status;
442
443   char const *node = (conf_node != NULL) ? conf_node : STATSD_DEFAULT_NODE;
444   char const *service = (conf_service != NULL)
445     ? conf_service : STATSD_DEFAULT_SERVICE;
446
447   memset (&ai_hints, 0, sizeof (ai_hints));
448   ai_hints.ai_flags = AI_PASSIVE;
449 #ifdef AI_ADDRCONFIG
450   ai_hints.ai_flags |= AI_ADDRCONFIG;
451 #endif
452   ai_hints.ai_family = AF_UNSPEC;
453   ai_hints.ai_socktype = SOCK_DGRAM;
454
455   status = getaddrinfo (node, service, &ai_hints, &ai_list);
456   if (status != 0)
457   {
458     ERROR ("statsd plugin: getaddrinfo (\"%s\", \"%s\") failed: %s",
459         node, service, gai_strerror (status));
460     return (status);
461   }
462
463   for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
464   {
465     int fd;
466     struct pollfd *tmp;
467
468     char dbg_node[NI_MAXHOST];
469     char dbg_service[NI_MAXSERV];
470
471     fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
472     if (fd < 0)
473     {
474       char errbuf[1024];
475       ERROR ("statsd plugin: socket(2) failed: %s",
476           sstrerror (errno, errbuf, sizeof (errbuf)));
477       continue;
478     }
479
480     getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
481         dbg_node, sizeof (dbg_node), dbg_service, sizeof (dbg_service),
482         NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV);
483     DEBUG ("statsd plugin: Trying to bind to [%s]:%s ...", dbg_node, dbg_service);
484
485     status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
486     if (status != 0)
487     {
488       char errbuf[1024];
489       ERROR ("statsd plugin: bind(2) failed: %s",
490           sstrerror (errno, errbuf, sizeof (errbuf)));
491       close (fd);
492       continue;
493     }
494
495     tmp = realloc (fds, sizeof (*fds) * (fds_num + 1));
496     if (tmp == NULL)
497     {
498       ERROR ("statsd plugin: realloc failed.");
499       continue;
500     }
501     fds = tmp;
502     tmp = fds + fds_num;
503     fds_num++;
504
505     memset (tmp, 0, sizeof (*tmp));
506     tmp->fd = fd;
507     tmp->events = POLLIN | POLLPRI;
508   }
509
510   freeaddrinfo (ai_list);
511
512   if (fds_num == 0)
513   {
514     ERROR ("statsd plugin: Unable to create listening socket for [%s]:%s.",
515         (node != NULL) ? node : "::", service);
516     return (ENOENT);
517   }
518
519   *ret_fds = fds;
520   *ret_fds_num = fds_num;
521   return (0);
522 } /* }}} int statsd_network_init */
523
524 static void *statsd_network_thread (void *args) /* {{{ */
525 {
526   struct pollfd *fds = NULL;
527   size_t fds_num = 0;
528   int status;
529   size_t i;
530
531   status = statsd_network_init (&fds, &fds_num);
532   if (status != 0)
533   {
534     ERROR ("statsd plugin: Unable to open listening sockets.");
535     pthread_exit ((void *) 0);
536   }
537
538   while (!network_thread_shutdown)
539   {
540     status = poll (fds, (nfds_t) fds_num, /* timeout = */ -1);
541     if (status < 0)
542     {
543       char errbuf[1024];
544
545       if ((errno == EINTR) || (errno == EAGAIN))
546         continue;
547
548       ERROR ("statsd plugin: poll(2) failed: %s",
549           sstrerror (errno, errbuf, sizeof (errbuf)));
550       break;
551     }
552
553     for (i = 0; i < fds_num; i++)
554     {
555       if ((fds[i].revents & (POLLIN | POLLPRI)) == 0)
556         continue;
557
558       statsd_network_read (fds[i].fd);
559       fds[i].revents = 0;
560     }
561   } /* while (!network_thread_shutdown) */
562
563   /* Clean up */
564   for (i = 0; i < fds_num; i++)
565     close (fds[i].fd);
566   sfree (fds);
567
568   return ((void *) 0);
569 } /* }}} void *statsd_network_thread */
570
571 static int statsd_config_timer_percentile (oconfig_item_t *ci) /* {{{ */
572 {
573   double percent = NAN;
574   double *tmp;
575   int status;
576
577   status = cf_util_get_double (ci, &percent);
578   if (status != 0)
579     return (status);
580
581   if ((percent <= 0.0) || (percent >= 100))
582   {
583     ERROR ("statsd plugin: The value for \"%s\" must be between 0 and 100, "
584         "exclusively.", ci->key);
585     return (ERANGE);
586   }
587
588   tmp = realloc (conf_timer_percentile,
589       sizeof (*conf_timer_percentile) * (conf_timer_percentile_num + 1));
590   if (tmp == NULL)
591   {
592     ERROR ("statsd plugin: realloc failed.");
593     return (ENOMEM);
594   }
595   conf_timer_percentile = tmp;
596   conf_timer_percentile[conf_timer_percentile_num] = percent;
597   conf_timer_percentile_num++;
598
599   return (0);
600 } /* }}} int statsd_config_timer_percentile */
601
602 static int statsd_config (oconfig_item_t *ci) /* {{{ */
603 {
604   int i;
605
606   for (i = 0; i < ci->children_num; i++)
607   {
608     oconfig_item_t *child = ci->children + i;
609
610     if (strcasecmp ("Host", child->key) == 0)
611       cf_util_get_string (child, &conf_node);
612     else if (strcasecmp ("Port", child->key) == 0)
613       cf_util_get_service (child, &conf_service);
614     else if (strcasecmp ("DeleteCounters", child->key) == 0)
615       cf_util_get_boolean (child, &conf_delete_counters);
616     else if (strcasecmp ("DeleteTimers", child->key) == 0)
617       cf_util_get_boolean (child, &conf_delete_timers);
618     else if (strcasecmp ("DeleteGauges", child->key) == 0)
619       cf_util_get_boolean (child, &conf_delete_gauges);
620     else if (strcasecmp ("DeleteSets", child->key) == 0)
621       cf_util_get_boolean (child, &conf_delete_sets);
622     else if (strcasecmp ("TimerPercentile", child->key) == 0)
623       statsd_config_timer_percentile (child);
624     else
625       ERROR ("statsd plugin: The \"%s\" config option is not valid.",
626           child->key);
627   }
628
629   return (0);
630 } /* }}} int statsd_config */
631
632 static int statsd_init (void) /* {{{ */
633 {
634   pthread_mutex_lock (&metrics_lock);
635   if (metrics_tree == NULL)
636     metrics_tree = c_avl_create ((void *) strcasecmp);
637
638   if (!network_thread_running)
639   {
640     int status;
641
642     status = pthread_create (&network_thread,
643         /* attr = */ NULL,
644         statsd_network_thread,
645         /* args = */ NULL);
646     if (status != 0)
647     {
648       char errbuf[1024];
649       pthread_mutex_unlock (&metrics_lock);
650       ERROR ("statsd plugin: pthread_create failed: %s",
651           sstrerror (errno, errbuf, sizeof (errbuf)));
652       return (status);
653     }
654   }
655   network_thread_running = 1;
656
657   pthread_mutex_unlock (&metrics_lock);
658
659   return (0);
660 } /* }}} int statsd_init */
661
662 /* Must hold metrics_lock when calling this function. */
663 static int statsd_metric_clear_set_unsafe (statsd_metric_t *metric) /* {{{ */
664 {
665   void *key;
666   void *value;
667
668   if ((metric == NULL) || (metric->type != STATSD_SET))
669     return (EINVAL);
670
671   if (metric->set == NULL)
672     return (0);
673
674   while (c_avl_pick (metric->set, &key, &value) == 0)
675   {
676     sfree (key);
677     sfree (value);
678   }
679
680   return (0);
681 } /* }}} int statsd_metric_clear_set_unsafe */
682
683 /* Must hold metrics_lock when calling this function. */
684 static int statsd_metric_submit_unsafe (char const *name, /* {{{ */
685     statsd_metric_t const *metric)
686 {
687   value_t values[1];
688   value_list_t vl = VALUE_LIST_INIT;
689
690   vl.values = values;
691   vl.values_len = 1;
692   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
693   sstrncpy (vl.plugin, "statsd", sizeof (vl.plugin));
694
695   if (metric->type == STATSD_GAUGE)
696     sstrncpy (vl.type, "gauge", sizeof (vl.type));
697   else if (metric->type == STATSD_TIMER)
698     sstrncpy (vl.type, "latency", sizeof (vl.type));
699   else if (metric->type == STATSD_SET)
700     sstrncpy (vl.type, "objects", sizeof (vl.type));
701   else /* if (metric->type == STATSD_COUNTER) */
702     sstrncpy (vl.type, "derive", sizeof (vl.type));
703
704   sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
705
706   if (metric->type == STATSD_GAUGE)
707     values[0].gauge = (gauge_t) metric->value;
708   else if (metric->type == STATSD_TIMER)
709   {
710     size_t i;
711
712     if (metric->updates_num == 0)
713       return (0);
714
715     vl.time = cdtime ();
716
717     ssnprintf (vl.type_instance, sizeof (vl.type_instance),
718         "%s-average", name);
719     values[0].gauge = CDTIME_T_TO_DOUBLE (
720         latency_counter_get_average (metric->latency));
721     plugin_dispatch_values (&vl);
722
723     for (i = 0; i < conf_timer_percentile_num; i++)
724     {
725       ssnprintf (vl.type_instance, sizeof (vl.type_instance),
726           "%s-percentile-%.0f", name, conf_timer_percentile[i]);
727       values[0].gauge = CDTIME_T_TO_DOUBLE (
728           latency_counter_get_percentile (
729             metric->latency, conf_timer_percentile[i]));
730       plugin_dispatch_values (&vl);
731     }
732
733     latency_counter_reset (metric->latency);
734     return (0);
735   }
736   else if (metric->type == STATSD_SET)
737   {
738     if (metric->set == NULL)
739       values[0].gauge = 0.0;
740     else
741       values[0].gauge = (gauge_t) c_avl_size (metric->set);
742   }
743   else
744     values[0].derive = (derive_t) metric->value;
745
746   return (plugin_dispatch_values (&vl));
747 } /* }}} int statsd_metric_submit_unsafe */
748
749 static int statsd_read (void) /* {{{ */
750 {
751   c_avl_iterator_t *iter;
752   char *name;
753   statsd_metric_t *metric;
754
755   char **to_be_deleted = NULL;
756   size_t to_be_deleted_num = 0;
757   size_t i;
758
759   pthread_mutex_lock (&metrics_lock);
760
761   if (metrics_tree == NULL)
762   {
763     pthread_mutex_unlock (&metrics_lock);
764     return (0);
765   }
766
767   iter = c_avl_get_iterator (metrics_tree);
768   while (c_avl_iterator_next (iter, (void *) &name, (void *) &metric) == 0)
769   {
770     if ((metric->updates_num == 0)
771         && ((conf_delete_counters && (metric->type == STATSD_COUNTER))
772           || (conf_delete_timers && (metric->type == STATSD_TIMER))
773           || (conf_delete_gauges && (metric->type == STATSD_GAUGE))
774           || (conf_delete_sets && (metric->type == STATSD_SET))))
775     {
776       DEBUG ("statsd plugin: Deleting metric \"%s\".", name);
777       strarray_add (&to_be_deleted, &to_be_deleted_num, name);
778       continue;
779     }
780
781     /* Names have a prefix, e.g. "c:", which determines the (statsd) type.
782      * Remove this here. */
783     statsd_metric_submit_unsafe (name + 2, metric);
784
785     /* Reset the metric. */
786     metric->updates_num = 0;
787     if (metric->type == STATSD_SET)
788       statsd_metric_clear_set_unsafe (metric);
789   }
790   c_avl_iterator_destroy (iter);
791
792   for (i = 0; i < to_be_deleted_num; i++)
793   {
794     int status;
795
796     status = c_avl_remove (metrics_tree, to_be_deleted[i],
797         (void *) &name, (void *) &metric);
798     if (status != 0)
799     {
800       ERROR ("stats plugin: c_avl_remove (\"%s\") failed with status %i.",
801           to_be_deleted[i], status);
802       continue;
803     }
804
805     sfree (name);
806     sfree (metric);
807   }
808
809   pthread_mutex_unlock (&metrics_lock);
810
811   strarray_free (to_be_deleted, to_be_deleted_num);
812
813   return (0);
814 } /* }}} int statsd_read */
815
816 static int statsd_shutdown (void) /* {{{ */
817 {
818   void *key;
819   void *value;
820
821   pthread_mutex_lock (&metrics_lock);
822
823   if (network_thread_running)
824   {
825     network_thread_shutdown = 1;
826     pthread_kill (network_thread, SIGTERM);
827     pthread_join (network_thread, /* retval = */ NULL);
828   }
829   network_thread_running = 0;
830
831   while (c_avl_pick (metrics_tree, &key, &value) == 0)
832   {
833     sfree (key);
834     sfree (value);
835   }
836   c_avl_destroy (metrics_tree);
837   metrics_tree = NULL;
838
839   sfree (conf_node);
840   sfree (conf_service);
841
842   pthread_mutex_unlock (&metrics_lock);
843
844   return (0);
845 } /* }}} int statsd_shutdown */
846
847 void module_register (void)
848 {
849   plugin_register_complex_config ("statsd", statsd_config);
850   plugin_register_init ("statsd", statsd_init);
851   plugin_register_read ("statsd", statsd_read);
852   plugin_register_shutdown ("statsd", statsd_shutdown);
853 }
854
855 /* vim: set sw=2 sts=2 et fdm=marker : */