b697a347c838a61d6c28223e2c8a3e8014572e50
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 /* 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;
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 }
100 key[1] = ':';
101 sstrncpy (&key[2], name, sizeof (key) - 2);
103 status = c_avl_get (metrics_tree, key, (void *) &metric);
104 if (status == 0)
105 return (metric);
107 key_copy = strdup (key);
108 if (key_copy == NULL)
109 {
110 ERROR ("statsd plugin: strdup failed.");
111 return (NULL);
112 }
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));
123 metric->type = type;
124 metric->latency = NULL;
125 metric->set = NULL;
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 }
136 return (metric);
137 } /* }}} statsd_metric_lookup_unsafe */
139 static int statsd_metric_set (char const *name, double value, /* {{{ */
140 metric_type_t type)
141 {
142 statsd_metric_t *metric;
144 pthread_mutex_lock (&metrics_lock);
146 metric = statsd_metric_lookup_unsafe (name, type);
147 if (metric == NULL)
148 {
149 pthread_mutex_unlock (&metrics_lock);
150 return (-1);
151 }
153 metric->value = value;
154 metric->updates_num++;
156 pthread_mutex_unlock (&metrics_lock);
158 return (0);
159 } /* }}} int statsd_metric_set */
161 static int statsd_metric_add (char const *name, double delta, /* {{{ */
162 metric_type_t type)
163 {
164 statsd_metric_t *metric;
166 pthread_mutex_lock (&metrics_lock);
168 metric = statsd_metric_lookup_unsafe (name, type);
169 if (metric == NULL)
170 {
171 pthread_mutex_unlock (&metrics_lock);
172 return (-1);
173 }
175 metric->value += delta;
176 metric->updates_num++;
178 pthread_mutex_unlock (&metrics_lock);
180 return (0);
181 } /* }}} int statsd_metric_add */
183 static int statsd_parse_value (char const *str, value_t *ret_value) /* {{{ */
184 {
185 char *endptr = NULL;
187 ret_value->gauge = (gauge_t) strtod (str, &endptr);
188 if ((str == endptr) || ((endptr != NULL) && (*endptr != 0)))
189 return (-1);
191 return (0);
192 } /* }}} int statsd_parse_value */
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;
202 if ((extra != NULL) && (extra[0] != '@'))
203 return (-1);
205 scale.gauge = 1.0;
206 if (extra != NULL)
207 {
208 status = statsd_parse_value (extra + 1, &scale);
209 if (status != 0)
210 return (status);
212 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
213 return (-1);
214 }
216 value.gauge = 1.0;
217 status = statsd_parse_value (value_str, &value);
218 if (status != 0)
219 return (status);
221 if (value.derive < 1)
222 return (-1);
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 ("TimerPercentile", child->key) == 0)
632 statsd_config_timer_percentile (child);
633 else
634 ERROR ("statsd plugin: The \"%s\" config option is not valid.",
635 child->key);
636 }
638 return (0);
639 } /* }}} int statsd_config */
641 static int statsd_init (void) /* {{{ */
642 {
643 pthread_mutex_lock (&metrics_lock);
644 if (metrics_tree == NULL)
645 metrics_tree = c_avl_create ((void *) strcmp);
647 if (!network_thread_running)
648 {
649 int status;
651 status = pthread_create (&network_thread,
652 /* attr = */ NULL,
653 statsd_network_thread,
654 /* args = */ NULL);
655 if (status != 0)
656 {
657 char errbuf[1024];
658 pthread_mutex_unlock (&metrics_lock);
659 ERROR ("statsd plugin: pthread_create failed: %s",
660 sstrerror (errno, errbuf, sizeof (errbuf)));
661 return (status);
662 }
663 }
664 network_thread_running = 1;
666 pthread_mutex_unlock (&metrics_lock);
668 return (0);
669 } /* }}} int statsd_init */
671 /* Must hold metrics_lock when calling this function. */
672 static int statsd_metric_clear_set_unsafe (statsd_metric_t *metric) /* {{{ */
673 {
674 void *key;
675 void *value;
677 if ((metric == NULL) || (metric->type != STATSD_SET))
678 return (EINVAL);
680 if (metric->set == NULL)
681 return (0);
683 while (c_avl_pick (metric->set, &key, &value) == 0)
684 {
685 sfree (key);
686 sfree (value);
687 }
689 return (0);
690 } /* }}} int statsd_metric_clear_set_unsafe */
692 /* Must hold metrics_lock when calling this function. */
693 static int statsd_metric_submit_unsafe (char const *name, /* {{{ */
694 statsd_metric_t const *metric)
695 {
696 value_t values[1];
697 value_list_t vl = VALUE_LIST_INIT;
699 vl.values = values;
700 vl.values_len = 1;
701 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
702 sstrncpy (vl.plugin, "statsd", sizeof (vl.plugin));
704 if (metric->type == STATSD_GAUGE)
705 sstrncpy (vl.type, "gauge", sizeof (vl.type));
706 else if (metric->type == STATSD_TIMER)
707 sstrncpy (vl.type, "latency", sizeof (vl.type));
708 else if (metric->type == STATSD_SET)
709 sstrncpy (vl.type, "objects", sizeof (vl.type));
710 else /* if (metric->type == STATSD_COUNTER) */
711 sstrncpy (vl.type, "derive", sizeof (vl.type));
713 sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
715 if (metric->type == STATSD_GAUGE)
716 values[0].gauge = (gauge_t) metric->value;
717 else if (metric->type == STATSD_TIMER)
718 {
719 size_t i;
721 if (metric->updates_num == 0)
722 return (0);
724 vl.time = cdtime ();
726 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
727 "%s-average", name);
728 values[0].gauge = CDTIME_T_TO_DOUBLE (
729 latency_counter_get_average (metric->latency));
730 plugin_dispatch_values (&vl);
732 for (i = 0; i < conf_timer_percentile_num; i++)
733 {
734 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
735 "%s-percentile-%.0f", name, conf_timer_percentile[i]);
736 values[0].gauge = CDTIME_T_TO_DOUBLE (
737 latency_counter_get_percentile (
738 metric->latency, conf_timer_percentile[i]));
739 plugin_dispatch_values (&vl);
740 }
742 latency_counter_reset (metric->latency);
743 return (0);
744 }
745 else if (metric->type == STATSD_SET)
746 {
747 if (metric->set == NULL)
748 values[0].gauge = 0.0;
749 else
750 values[0].gauge = (gauge_t) c_avl_size (metric->set);
751 }
752 else
753 values[0].derive = (derive_t) metric->value;
755 return (plugin_dispatch_values (&vl));
756 } /* }}} int statsd_metric_submit_unsafe */
758 static int statsd_read (void) /* {{{ */
759 {
760 c_avl_iterator_t *iter;
761 char *name;
762 statsd_metric_t *metric;
764 char **to_be_deleted = NULL;
765 size_t to_be_deleted_num = 0;
766 size_t i;
768 pthread_mutex_lock (&metrics_lock);
770 if (metrics_tree == NULL)
771 {
772 pthread_mutex_unlock (&metrics_lock);
773 return (0);
774 }
776 iter = c_avl_get_iterator (metrics_tree);
777 while (c_avl_iterator_next (iter, (void *) &name, (void *) &metric) == 0)
778 {
779 if ((metric->updates_num == 0)
780 && ((conf_delete_counters && (metric->type == STATSD_COUNTER))
781 || (conf_delete_timers && (metric->type == STATSD_TIMER))
782 || (conf_delete_gauges && (metric->type == STATSD_GAUGE))
783 || (conf_delete_sets && (metric->type == STATSD_SET))))
784 {
785 DEBUG ("statsd plugin: Deleting metric \"%s\".", name);
786 strarray_add (&to_be_deleted, &to_be_deleted_num, name);
787 continue;
788 }
790 /* Names have a prefix, e.g. "c:", which determines the (statsd) type.
791 * Remove this here. */
792 statsd_metric_submit_unsafe (name + 2, metric);
794 /* Reset the metric. */
795 metric->updates_num = 0;
796 if (metric->type == STATSD_SET)
797 statsd_metric_clear_set_unsafe (metric);
798 }
799 c_avl_iterator_destroy (iter);
801 for (i = 0; i < to_be_deleted_num; i++)
802 {
803 int status;
805 status = c_avl_remove (metrics_tree, to_be_deleted[i],
806 (void *) &name, (void *) &metric);
807 if (status != 0)
808 {
809 ERROR ("stats plugin: c_avl_remove (\"%s\") failed with status %i.",
810 to_be_deleted[i], status);
811 continue;
812 }
814 sfree (name);
815 sfree (metric);
816 }
818 pthread_mutex_unlock (&metrics_lock);
820 strarray_free (to_be_deleted, to_be_deleted_num);
822 return (0);
823 } /* }}} int statsd_read */
825 static int statsd_shutdown (void) /* {{{ */
826 {
827 void *key;
828 void *value;
830 pthread_mutex_lock (&metrics_lock);
832 if (network_thread_running)
833 {
834 network_thread_shutdown = 1;
835 pthread_kill (network_thread, SIGTERM);
836 pthread_join (network_thread, /* retval = */ NULL);
837 }
838 network_thread_running = 0;
840 while (c_avl_pick (metrics_tree, &key, &value) == 0)
841 {
842 sfree (key);
843 sfree (value);
844 }
845 c_avl_destroy (metrics_tree);
846 metrics_tree = NULL;
848 sfree (conf_node);
849 sfree (conf_service);
851 pthread_mutex_unlock (&metrics_lock);
853 return (0);
854 } /* }}} int statsd_shutdown */
856 void module_register (void)
857 {
858 plugin_register_complex_config ("statsd", statsd_config);
859 plugin_register_init ("statsd", statsd_init);
860 plugin_register_read ("statsd", statsd_read);
861 plugin_register_shutdown ("statsd", statsd_shutdown);
862 }
864 /* vim: set sw=2 sts=2 et fdm=marker : */