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