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 */
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"
30 #include <pthread.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netdb.h>
35 #include <poll.h>
37 #ifndef STATSD_DEFAULT_NODE
38 # define STATSD_DEFAULT_NODE NULL
39 #endif
41 #ifndef STATSD_DEFAULT_SERVICE
42 # define STATSD_DEFAULT_SERVICE "8125"
43 #endif
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;
54 struct statsd_metric_s
55 {
56 metric_type_t type;
57 double value;
58 latency_counter_t *latency;
59 c_avl_tree_t *set;
60 unsigned long updates_num;
61 };
62 typedef struct statsd_metric_s statsd_metric_t;
64 static c_avl_tree_t *metrics_tree = NULL;
65 static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER;
67 static pthread_t network_thread;
68 static _Bool network_thread_running = 0;
69 static _Bool network_thread_shutdown = 0;
71 static char *conf_node = NULL;
72 static char *conf_service = NULL;
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;
79 static double *conf_timer_percentile = NULL;
80 static size_t conf_timer_percentile_num = 0;
82 static _Bool conf_timer_lower = 0;
83 static _Bool conf_timer_upper = 0;
85 /* Must hold metrics_lock when calling this function. */
86 static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name, /* {{{ */
87 metric_type_t type)
88 {
89 char key[DATA_MAX_NAME_LEN + 2];
90 char *key_copy;
91 statsd_metric_t *metric;
92 int status;
94 switch (type)
95 {
96 case STATSD_COUNTER: key[0] = 'c'; break;
97 case STATSD_TIMER: key[0] = 't'; break;
98 case STATSD_GAUGE: key[0] = 'g'; break;
99 case STATSD_SET: key[0] = 's'; break;
100 default: return (NULL);
101 }
103 key[1] = ':';
104 sstrncpy (&key[2], name, sizeof (key) - 2);
106 status = c_avl_get (metrics_tree, key, (void *) &metric);
107 if (status == 0)
108 return (metric);
110 key_copy = strdup (key);
111 if (key_copy == NULL)
112 {
113 ERROR ("statsd plugin: strdup failed.");
114 return (NULL);
115 }
117 metric = malloc (sizeof (*metric));
118 if (metric == NULL)
119 {
120 ERROR ("statsd plugin: malloc failed.");
121 sfree (key_copy);
122 return (NULL);
123 }
124 memset (metric, 0, sizeof (*metric));
126 metric->type = type;
127 metric->latency = NULL;
128 metric->set = NULL;
130 status = c_avl_insert (metrics_tree, key_copy, metric);
131 if (status != 0)
132 {
133 ERROR ("statsd plugin: c_avl_insert failed.");
134 sfree (key_copy);
135 sfree (metric);
136 return (NULL);
137 }
139 return (metric);
140 } /* }}} statsd_metric_lookup_unsafe */
142 static int statsd_metric_set (char const *name, double value, /* {{{ */
143 metric_type_t type)
144 {
145 statsd_metric_t *metric;
147 pthread_mutex_lock (&metrics_lock);
149 metric = statsd_metric_lookup_unsafe (name, type);
150 if (metric == NULL)
151 {
152 pthread_mutex_unlock (&metrics_lock);
153 return (-1);
154 }
156 metric->value = value;
157 metric->updates_num++;
159 pthread_mutex_unlock (&metrics_lock);
161 return (0);
162 } /* }}} int statsd_metric_set */
164 static int statsd_metric_add (char const *name, double delta, /* {{{ */
165 metric_type_t type)
166 {
167 statsd_metric_t *metric;
169 pthread_mutex_lock (&metrics_lock);
171 metric = statsd_metric_lookup_unsafe (name, type);
172 if (metric == NULL)
173 {
174 pthread_mutex_unlock (&metrics_lock);
175 return (-1);
176 }
178 metric->value += delta;
179 metric->updates_num++;
181 pthread_mutex_unlock (&metrics_lock);
183 return (0);
184 } /* }}} int statsd_metric_add */
186 static int statsd_parse_value (char const *str, value_t *ret_value) /* {{{ */
187 {
188 char *endptr = NULL;
190 ret_value->gauge = (gauge_t) strtod (str, &endptr);
191 if ((str == endptr) || ((endptr != NULL) && (*endptr != 0)))
192 return (-1);
194 return (0);
195 } /* }}} int statsd_parse_value */
197 static int statsd_handle_counter (char const *name, /* {{{ */
198 char const *value_str,
199 char const *extra)
200 {
201 value_t value;
202 value_t scale;
203 int status;
205 if ((extra != NULL) && (extra[0] != '@'))
206 return (-1);
208 scale.gauge = 1.0;
209 if (extra != NULL)
210 {
211 status = statsd_parse_value (extra + 1, &scale);
212 if (status != 0)
213 return (status);
215 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
216 return (-1);
217 }
219 value.gauge = 1.0;
220 status = statsd_parse_value (value_str, &value);
221 if (status != 0)
222 return (status);
224 return (statsd_metric_add (name, (double) (value.gauge / scale.gauge),
225 STATSD_COUNTER));
226 } /* }}} int statsd_handle_counter */
228 static int statsd_handle_gauge (char const *name, /* {{{ */
229 char const *value_str)
230 {
231 value_t value;
232 int status;
234 value.gauge = 0;
235 status = statsd_parse_value (value_str, &value);
236 if (status != 0)
237 return (status);
239 if ((value_str[0] == '+') || (value_str[0] == '-'))
240 return (statsd_metric_add (name, (double) value.gauge, STATSD_GAUGE));
241 else
242 return (statsd_metric_set (name, (double) value.gauge, STATSD_GAUGE));
243 } /* }}} int statsd_handle_gauge */
245 static int statsd_handle_timer (char const *name, /* {{{ */
246 char const *value_str)
247 {
248 statsd_metric_t *metric;
249 value_t value_ms;
250 cdtime_t value;
251 int status;
253 value_ms.derive = 0;
254 status = statsd_parse_value (value_str, &value_ms);
255 if (status != 0)
256 return (status);
258 value = MS_TO_CDTIME_T (value_ms.gauge);
260 pthread_mutex_lock (&metrics_lock);
262 metric = statsd_metric_lookup_unsafe (name, STATSD_TIMER);
263 if (metric == NULL)
264 {
265 pthread_mutex_unlock (&metrics_lock);
266 return (-1);
267 }
269 if (metric->latency == NULL)
270 metric->latency = latency_counter_create ();
271 if (metric->latency == NULL)
272 {
273 pthread_mutex_unlock (&metrics_lock);
274 return (-1);
275 }
277 latency_counter_add (metric->latency, value);
278 metric->updates_num++;
280 pthread_mutex_unlock (&metrics_lock);
281 return (0);
282 } /* }}} int statsd_handle_timer */
284 static int statsd_handle_set (char const *name, /* {{{ */
285 char const *set_key_orig)
286 {
287 statsd_metric_t *metric = NULL;
288 char *set_key;
289 int status;
291 pthread_mutex_lock (&metrics_lock);
293 metric = statsd_metric_lookup_unsafe (name, STATSD_SET);
294 if (metric == NULL)
295 {
296 pthread_mutex_unlock (&metrics_lock);
297 return (-1);
298 }
300 /* Make sure metric->set exists. */
301 if (metric->set == NULL)
302 metric->set = c_avl_create ((void *) strcmp);
304 if (metric->set == NULL)
305 {
306 pthread_mutex_unlock (&metrics_lock);
307 ERROR ("statsd plugin: c_avl_create failed.");
308 return (-1);
309 }
311 set_key = strdup (set_key_orig);
312 if (set_key == NULL)
313 {
314 pthread_mutex_unlock (&metrics_lock);
315 ERROR ("statsd plugin: strdup failed.");
316 return (-1);
317 }
319 status = c_avl_insert (metric->set, set_key, /* value = */ NULL);
320 if (status < 0)
321 {
322 pthread_mutex_unlock (&metrics_lock);
323 if (status < 0)
324 ERROR ("statsd plugin: c_avl_insert (\"%s\") failed with status %i.",
325 set_key, status);
326 sfree (set_key);
327 return (-1);
328 }
329 else if (status > 0) /* key already exists */
330 {
331 sfree (set_key);
332 }
334 metric->updates_num++;
336 pthread_mutex_unlock (&metrics_lock);
337 return (0);
338 } /* }}} int statsd_handle_set */
340 static int statsd_parse_line (char *buffer) /* {{{ */
341 {
342 char *name = buffer;
343 char *value;
344 char *type;
345 char *extra;
347 type = strchr (name, '|');
348 if (type == NULL)
349 return (-1);
350 *type = 0;
351 type++;
353 value = strrchr (name, ':');
354 if (value == NULL)
355 return (-1);
356 *value = 0;
357 value++;
359 extra = strchr (type, '|');
360 if (extra != NULL)
361 {
362 *extra = 0;
363 extra++;
364 }
366 if (strcmp ("c", type) == 0)
367 return (statsd_handle_counter (name, value, extra));
369 /* extra is only valid for counters */
370 if (extra != NULL)
371 return (-1);
373 if (strcmp ("g", type) == 0)
374 return (statsd_handle_gauge (name, value));
375 else if (strcmp ("ms", type) == 0)
376 return (statsd_handle_timer (name, value));
377 else if (strcmp ("s", type) == 0)
378 return (statsd_handle_set (name, value));
379 else
380 return (-1);
381 } /* }}} void statsd_parse_line */
383 static void statsd_parse_buffer (char *buffer) /* {{{ */
384 {
385 while (buffer != NULL)
386 {
387 char orig[64];
388 char *next;
389 int status;
391 next = strchr (buffer, '\n');
392 if (next != NULL)
393 {
394 *next = 0;
395 next++;
396 }
398 if (*buffer == 0)
399 {
400 buffer = next;
401 continue;
402 }
404 sstrncpy (orig, buffer, sizeof (orig));
406 status = statsd_parse_line (buffer);
407 if (status != 0)
408 ERROR ("statsd plugin: Unable to parse line: \"%s\"", orig);
410 buffer = next;
411 }
412 } /* }}} void statsd_parse_buffer */
414 static void statsd_network_read (int fd) /* {{{ */
415 {
416 char buffer[4096];
417 size_t buffer_size;
418 ssize_t status;
420 status = recv (fd, buffer, sizeof (buffer), /* flags = */ MSG_DONTWAIT);
421 if (status < 0)
422 {
423 char errbuf[1024];
425 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
426 return;
428 ERROR ("statsd plugin: recv(2) failed: %s",
429 sstrerror (errno, errbuf, sizeof (errbuf)));
430 return;
431 }
433 buffer_size = (size_t) status;
434 if (buffer_size >= sizeof (buffer))
435 buffer_size = sizeof (buffer) - 1;
436 buffer[buffer_size] = 0;
438 statsd_parse_buffer (buffer);
439 } /* }}} void statsd_network_read */
441 static int statsd_network_init (struct pollfd **ret_fds, /* {{{ */
442 size_t *ret_fds_num)
443 {
444 struct pollfd *fds = NULL;
445 size_t fds_num = 0;
447 struct addrinfo ai_hints;
448 struct addrinfo *ai_list = NULL;
449 struct addrinfo *ai_ptr;
450 int status;
452 char const *node = (conf_node != NULL) ? conf_node : STATSD_DEFAULT_NODE;
453 char const *service = (conf_service != NULL)
454 ? conf_service : STATSD_DEFAULT_SERVICE;
456 memset (&ai_hints, 0, sizeof (ai_hints));
457 ai_hints.ai_flags = AI_PASSIVE;
458 #ifdef AI_ADDRCONFIG
459 ai_hints.ai_flags |= AI_ADDRCONFIG;
460 #endif
461 ai_hints.ai_family = AF_UNSPEC;
462 ai_hints.ai_socktype = SOCK_DGRAM;
464 status = getaddrinfo (node, service, &ai_hints, &ai_list);
465 if (status != 0)
466 {
467 ERROR ("statsd plugin: getaddrinfo (\"%s\", \"%s\") failed: %s",
468 node, service, gai_strerror (status));
469 return (status);
470 }
472 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
473 {
474 int fd;
475 struct pollfd *tmp;
477 char dbg_node[NI_MAXHOST];
478 char dbg_service[NI_MAXSERV];
480 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
481 if (fd < 0)
482 {
483 char errbuf[1024];
484 ERROR ("statsd plugin: socket(2) failed: %s",
485 sstrerror (errno, errbuf, sizeof (errbuf)));
486 continue;
487 }
489 getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
490 dbg_node, sizeof (dbg_node), dbg_service, sizeof (dbg_service),
491 NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV);
492 DEBUG ("statsd plugin: Trying to bind to [%s]:%s ...", dbg_node, dbg_service);
494 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
495 if (status != 0)
496 {
497 char errbuf[1024];
498 ERROR ("statsd plugin: bind(2) failed: %s",
499 sstrerror (errno, errbuf, sizeof (errbuf)));
500 close (fd);
501 continue;
502 }
504 tmp = realloc (fds, sizeof (*fds) * (fds_num + 1));
505 if (tmp == NULL)
506 {
507 ERROR ("statsd plugin: realloc failed.");
508 continue;
509 }
510 fds = tmp;
511 tmp = fds + fds_num;
512 fds_num++;
514 memset (tmp, 0, sizeof (*tmp));
515 tmp->fd = fd;
516 tmp->events = POLLIN | POLLPRI;
517 }
519 freeaddrinfo (ai_list);
521 if (fds_num == 0)
522 {
523 ERROR ("statsd plugin: Unable to create listening socket for [%s]:%s.",
524 (node != NULL) ? node : "::", service);
525 return (ENOENT);
526 }
528 *ret_fds = fds;
529 *ret_fds_num = fds_num;
530 return (0);
531 } /* }}} int statsd_network_init */
533 static void *statsd_network_thread (void *args) /* {{{ */
534 {
535 struct pollfd *fds = NULL;
536 size_t fds_num = 0;
537 int status;
538 size_t i;
540 status = statsd_network_init (&fds, &fds_num);
541 if (status != 0)
542 {
543 ERROR ("statsd plugin: Unable to open listening sockets.");
544 pthread_exit ((void *) 0);
545 }
547 while (!network_thread_shutdown)
548 {
549 status = poll (fds, (nfds_t) fds_num, /* timeout = */ -1);
550 if (status < 0)
551 {
552 char errbuf[1024];
554 if ((errno == EINTR) || (errno == EAGAIN))
555 continue;
557 ERROR ("statsd plugin: poll(2) failed: %s",
558 sstrerror (errno, errbuf, sizeof (errbuf)));
559 break;
560 }
562 for (i = 0; i < fds_num; i++)
563 {
564 if ((fds[i].revents & (POLLIN | POLLPRI)) == 0)
565 continue;
567 statsd_network_read (fds[i].fd);
568 fds[i].revents = 0;
569 }
570 } /* while (!network_thread_shutdown) */
572 /* Clean up */
573 for (i = 0; i < fds_num; i++)
574 close (fds[i].fd);
575 sfree (fds);
577 return ((void *) 0);
578 } /* }}} void *statsd_network_thread */
580 static int statsd_config_timer_percentile (oconfig_item_t *ci) /* {{{ */
581 {
582 double percent = NAN;
583 double *tmp;
584 int status;
586 status = cf_util_get_double (ci, &percent);
587 if (status != 0)
588 return (status);
590 if ((percent <= 0.0) || (percent >= 100))
591 {
592 ERROR ("statsd plugin: The value for \"%s\" must be between 0 and 100, "
593 "exclusively.", ci->key);
594 return (ERANGE);
595 }
597 tmp = realloc (conf_timer_percentile,
598 sizeof (*conf_timer_percentile) * (conf_timer_percentile_num + 1));
599 if (tmp == NULL)
600 {
601 ERROR ("statsd plugin: realloc failed.");
602 return (ENOMEM);
603 }
604 conf_timer_percentile = tmp;
605 conf_timer_percentile[conf_timer_percentile_num] = percent;
606 conf_timer_percentile_num++;
608 return (0);
609 } /* }}} int statsd_config_timer_percentile */
611 static int statsd_config (oconfig_item_t *ci) /* {{{ */
612 {
613 int i;
615 for (i = 0; i < ci->children_num; i++)
616 {
617 oconfig_item_t *child = ci->children + i;
619 if (strcasecmp ("Host", child->key) == 0)
620 cf_util_get_string (child, &conf_node);
621 else if (strcasecmp ("Port", child->key) == 0)
622 cf_util_get_service (child, &conf_service);
623 else if (strcasecmp ("DeleteCounters", child->key) == 0)
624 cf_util_get_boolean (child, &conf_delete_counters);
625 else if (strcasecmp ("DeleteTimers", child->key) == 0)
626 cf_util_get_boolean (child, &conf_delete_timers);
627 else if (strcasecmp ("DeleteGauges", child->key) == 0)
628 cf_util_get_boolean (child, &conf_delete_gauges);
629 else if (strcasecmp ("DeleteSets", child->key) == 0)
630 cf_util_get_boolean (child, &conf_delete_sets);
631 else if (strcasecmp ("TimerLower", child->key) == 0)
632 cf_util_get_boolean (child, &conf_timer_lower);
633 else if (strcasecmp ("TimerUpper", child->key) == 0)
634 cf_util_get_boolean (child, &conf_timer_upper);
635 else if (strcasecmp ("TimerPercentile", child->key) == 0)
636 statsd_config_timer_percentile (child);
637 else
638 ERROR ("statsd plugin: The \"%s\" config option is not valid.",
639 child->key);
640 }
642 return (0);
643 } /* }}} int statsd_config */
645 static int statsd_init (void) /* {{{ */
646 {
647 pthread_mutex_lock (&metrics_lock);
648 if (metrics_tree == NULL)
649 metrics_tree = c_avl_create ((void *) strcmp);
651 if (!network_thread_running)
652 {
653 int status;
655 status = pthread_create (&network_thread,
656 /* attr = */ NULL,
657 statsd_network_thread,
658 /* args = */ NULL);
659 if (status != 0)
660 {
661 char errbuf[1024];
662 pthread_mutex_unlock (&metrics_lock);
663 ERROR ("statsd plugin: pthread_create failed: %s",
664 sstrerror (errno, errbuf, sizeof (errbuf)));
665 return (status);
666 }
667 }
668 network_thread_running = 1;
670 pthread_mutex_unlock (&metrics_lock);
672 return (0);
673 } /* }}} int statsd_init */
675 /* Must hold metrics_lock when calling this function. */
676 static int statsd_metric_clear_set_unsafe (statsd_metric_t *metric) /* {{{ */
677 {
678 void *key;
679 void *value;
681 if ((metric == NULL) || (metric->type != STATSD_SET))
682 return (EINVAL);
684 if (metric->set == NULL)
685 return (0);
687 while (c_avl_pick (metric->set, &key, &value) == 0)
688 {
689 sfree (key);
690 sfree (value);
691 }
693 return (0);
694 } /* }}} int statsd_metric_clear_set_unsafe */
696 /* Must hold metrics_lock when calling this function. */
697 static int statsd_metric_submit_unsafe (char const *name, /* {{{ */
698 statsd_metric_t const *metric)
699 {
700 value_t values[1];
701 value_list_t vl = VALUE_LIST_INIT;
703 vl.values = values;
704 vl.values_len = 1;
705 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
706 sstrncpy (vl.plugin, "statsd", sizeof (vl.plugin));
708 if (metric->type == STATSD_GAUGE)
709 sstrncpy (vl.type, "gauge", sizeof (vl.type));
710 else if (metric->type == STATSD_TIMER)
711 sstrncpy (vl.type, "latency", sizeof (vl.type));
712 else if (metric->type == STATSD_SET)
713 sstrncpy (vl.type, "objects", sizeof (vl.type));
714 else /* if (metric->type == STATSD_COUNTER) */
715 sstrncpy (vl.type, "derive", sizeof (vl.type));
717 sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
719 if (metric->type == STATSD_GAUGE)
720 values[0].gauge = (gauge_t) metric->value;
721 else if (metric->type == STATSD_TIMER)
722 {
723 size_t i;
725 if (metric->updates_num == 0)
726 return (0);
728 vl.time = cdtime ();
730 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
731 "%s-average", name);
732 values[0].gauge = CDTIME_T_TO_DOUBLE (
733 latency_counter_get_average (metric->latency));
734 plugin_dispatch_values (&vl);
736 if (conf_timer_lower) {
737 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
738 "%s-lower", name);
739 values[0].gauge = CDTIME_T_TO_DOUBLE (
740 latency_counter_get_min (metric->latency));
741 plugin_dispatch_values (&vl);
742 }
744 if (conf_timer_upper) {
745 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
746 "%s-upper", name);
747 values[0].gauge = CDTIME_T_TO_DOUBLE (
748 latency_counter_get_max (metric->latency));
749 plugin_dispatch_values (&vl);
750 }
752 for (i = 0; i < conf_timer_percentile_num; i++)
753 {
754 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
755 "%s-percentile-%.0f", name, conf_timer_percentile[i]);
756 values[0].gauge = CDTIME_T_TO_DOUBLE (
757 latency_counter_get_percentile (
758 metric->latency, conf_timer_percentile[i]));
759 plugin_dispatch_values (&vl);
760 }
762 latency_counter_reset (metric->latency);
763 return (0);
764 }
765 else if (metric->type == STATSD_SET)
766 {
767 if (metric->set == NULL)
768 values[0].gauge = 0.0;
769 else
770 values[0].gauge = (gauge_t) c_avl_size (metric->set);
771 }
772 else
773 values[0].derive = (derive_t) metric->value;
775 return (plugin_dispatch_values (&vl));
776 } /* }}} int statsd_metric_submit_unsafe */
778 static int statsd_read (void) /* {{{ */
779 {
780 c_avl_iterator_t *iter;
781 char *name;
782 statsd_metric_t *metric;
784 char **to_be_deleted = NULL;
785 size_t to_be_deleted_num = 0;
786 size_t i;
788 pthread_mutex_lock (&metrics_lock);
790 if (metrics_tree == NULL)
791 {
792 pthread_mutex_unlock (&metrics_lock);
793 return (0);
794 }
796 iter = c_avl_get_iterator (metrics_tree);
797 while (c_avl_iterator_next (iter, (void *) &name, (void *) &metric) == 0)
798 {
799 if ((metric->updates_num == 0)
800 && ((conf_delete_counters && (metric->type == STATSD_COUNTER))
801 || (conf_delete_timers && (metric->type == STATSD_TIMER))
802 || (conf_delete_gauges && (metric->type == STATSD_GAUGE))
803 || (conf_delete_sets && (metric->type == STATSD_SET))))
804 {
805 DEBUG ("statsd plugin: Deleting metric \"%s\".", name);
806 strarray_add (&to_be_deleted, &to_be_deleted_num, name);
807 continue;
808 }
810 /* Names have a prefix, e.g. "c:", which determines the (statsd) type.
811 * Remove this here. */
812 statsd_metric_submit_unsafe (name + 2, metric);
814 /* Reset the metric. */
815 metric->updates_num = 0;
816 if (metric->type == STATSD_SET)
817 statsd_metric_clear_set_unsafe (metric);
818 }
819 c_avl_iterator_destroy (iter);
821 for (i = 0; i < to_be_deleted_num; i++)
822 {
823 int status;
825 status = c_avl_remove (metrics_tree, to_be_deleted[i],
826 (void *) &name, (void *) &metric);
827 if (status != 0)
828 {
829 ERROR ("stats plugin: c_avl_remove (\"%s\") failed with status %i.",
830 to_be_deleted[i], status);
831 continue;
832 }
834 sfree (name);
835 sfree (metric);
836 }
838 pthread_mutex_unlock (&metrics_lock);
840 strarray_free (to_be_deleted, to_be_deleted_num);
842 return (0);
843 } /* }}} int statsd_read */
845 static int statsd_shutdown (void) /* {{{ */
846 {
847 void *key;
848 void *value;
850 pthread_mutex_lock (&metrics_lock);
852 if (network_thread_running)
853 {
854 network_thread_shutdown = 1;
855 pthread_kill (network_thread, SIGTERM);
856 pthread_join (network_thread, /* retval = */ NULL);
857 }
858 network_thread_running = 0;
860 while (c_avl_pick (metrics_tree, &key, &value) == 0)
861 {
862 sfree (key);
863 sfree (value);
864 }
865 c_avl_destroy (metrics_tree);
866 metrics_tree = NULL;
868 sfree (conf_node);
869 sfree (conf_service);
871 pthread_mutex_unlock (&metrics_lock);
873 return (0);
874 } /* }}} int statsd_shutdown */
876 void module_register (void)
877 {
878 plugin_register_complex_config ("statsd", statsd_config);
879 plugin_register_init ("statsd", statsd_init);
880 plugin_register_read ("statsd", statsd_read);
881 plugin_register_shutdown ("statsd", statsd_shutdown);
882 }
884 /* vim: set sw=2 sts=2 et fdm=marker : */