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 /* AIX doesn't have MSG_DONTWAIT */
38 #ifndef MSG_DONTWAIT
39 # define MSG_DONTWAIT MSG_NONBLOCK
40 #endif
42 #ifndef STATSD_DEFAULT_NODE
43 # define STATSD_DEFAULT_NODE NULL
44 #endif
46 #ifndef STATSD_DEFAULT_SERVICE
47 # define STATSD_DEFAULT_SERVICE "8125"
48 #endif
50 enum metric_type_e
51 {
52 STATSD_COUNTER,
53 STATSD_TIMER,
54 STATSD_GAUGE,
55 STATSD_SET
56 };
57 typedef enum metric_type_e metric_type_t;
59 struct statsd_metric_s
60 {
61 metric_type_t type;
62 double value;
63 latency_counter_t *latency;
64 c_avl_tree_t *set;
65 unsigned long updates_num;
66 };
67 typedef struct statsd_metric_s statsd_metric_t;
69 static c_avl_tree_t *metrics_tree = NULL;
70 static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER;
72 static pthread_t network_thread;
73 static _Bool network_thread_running = 0;
74 static _Bool network_thread_shutdown = 0;
76 static char *conf_node = NULL;
77 static char *conf_service = NULL;
79 static _Bool conf_delete_counters = 0;
80 static _Bool conf_delete_timers = 0;
81 static _Bool conf_delete_gauges = 0;
82 static _Bool conf_delete_sets = 0;
84 static double *conf_timer_percentile = NULL;
85 static size_t conf_timer_percentile_num = 0;
87 static _Bool conf_timer_lower = 0;
88 static _Bool conf_timer_upper = 0;
89 static _Bool conf_timer_sum = 0;
90 static _Bool conf_timer_count = 0;
92 /* Must hold metrics_lock when calling this function. */
93 static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name, /* {{{ */
94 metric_type_t type)
95 {
96 char key[DATA_MAX_NAME_LEN + 2];
97 char *key_copy;
98 statsd_metric_t *metric;
99 int status;
101 switch (type)
102 {
103 case STATSD_COUNTER: key[0] = 'c'; break;
104 case STATSD_TIMER: key[0] = 't'; break;
105 case STATSD_GAUGE: key[0] = 'g'; break;
106 case STATSD_SET: key[0] = 's'; break;
107 default: return (NULL);
108 }
110 key[1] = ':';
111 sstrncpy (&key[2], name, sizeof (key) - 2);
113 status = c_avl_get (metrics_tree, key, (void *) &metric);
114 if (status == 0)
115 return (metric);
117 key_copy = strdup (key);
118 if (key_copy == NULL)
119 {
120 ERROR ("statsd plugin: strdup failed.");
121 return (NULL);
122 }
124 metric = malloc (sizeof (*metric));
125 if (metric == NULL)
126 {
127 ERROR ("statsd plugin: malloc failed.");
128 sfree (key_copy);
129 return (NULL);
130 }
131 memset (metric, 0, sizeof (*metric));
133 metric->type = type;
134 metric->latency = NULL;
135 metric->set = NULL;
137 status = c_avl_insert (metrics_tree, key_copy, metric);
138 if (status != 0)
139 {
140 ERROR ("statsd plugin: c_avl_insert failed.");
141 sfree (key_copy);
142 sfree (metric);
143 return (NULL);
144 }
146 return (metric);
147 } /* }}} statsd_metric_lookup_unsafe */
149 static int statsd_metric_set (char const *name, double value, /* {{{ */
150 metric_type_t type)
151 {
152 statsd_metric_t *metric;
154 pthread_mutex_lock (&metrics_lock);
156 metric = statsd_metric_lookup_unsafe (name, type);
157 if (metric == NULL)
158 {
159 pthread_mutex_unlock (&metrics_lock);
160 return (-1);
161 }
163 metric->value = value;
164 metric->updates_num++;
166 pthread_mutex_unlock (&metrics_lock);
168 return (0);
169 } /* }}} int statsd_metric_set */
171 static int statsd_metric_add (char const *name, double delta, /* {{{ */
172 metric_type_t type)
173 {
174 statsd_metric_t *metric;
176 pthread_mutex_lock (&metrics_lock);
178 metric = statsd_metric_lookup_unsafe (name, type);
179 if (metric == NULL)
180 {
181 pthread_mutex_unlock (&metrics_lock);
182 return (-1);
183 }
185 metric->value += delta;
186 metric->updates_num++;
188 pthread_mutex_unlock (&metrics_lock);
190 return (0);
191 } /* }}} int statsd_metric_add */
193 static int statsd_parse_value (char const *str, value_t *ret_value) /* {{{ */
194 {
195 char *endptr = NULL;
197 ret_value->gauge = (gauge_t) strtod (str, &endptr);
198 if ((str == endptr) || ((endptr != NULL) && (*endptr != 0)))
199 return (-1);
201 return (0);
202 } /* }}} int statsd_parse_value */
204 static int statsd_handle_counter (char const *name, /* {{{ */
205 char const *value_str,
206 char const *extra)
207 {
208 value_t value;
209 value_t scale;
210 int status;
212 if ((extra != NULL) && (extra[0] != '@'))
213 return (-1);
215 scale.gauge = 1.0;
216 if (extra != NULL)
217 {
218 status = statsd_parse_value (extra + 1, &scale);
219 if (status != 0)
220 return (status);
222 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
223 return (-1);
224 }
226 value.gauge = 1.0;
227 status = statsd_parse_value (value_str, &value);
228 if (status != 0)
229 return (status);
231 return (statsd_metric_add (name, (double) (value.gauge / scale.gauge),
232 STATSD_COUNTER));
233 } /* }}} int statsd_handle_counter */
235 static int statsd_handle_gauge (char const *name, /* {{{ */
236 char const *value_str)
237 {
238 value_t value;
239 int status;
241 value.gauge = 0;
242 status = statsd_parse_value (value_str, &value);
243 if (status != 0)
244 return (status);
246 if ((value_str[0] == '+') || (value_str[0] == '-'))
247 return (statsd_metric_add (name, (double) value.gauge, STATSD_GAUGE));
248 else
249 return (statsd_metric_set (name, (double) value.gauge, STATSD_GAUGE));
250 } /* }}} int statsd_handle_gauge */
252 static int statsd_handle_timer (char const *name, /* {{{ */
253 char const *value_str,
254 char const *extra)
255 {
256 statsd_metric_t *metric;
257 value_t value_ms;
258 value_t scale;
259 cdtime_t value;
260 int status;
262 if ((extra != NULL) && (extra[0] != '@'))
263 return (-1);
265 scale.gauge = 1.0;
266 if (extra != NULL)
267 {
268 status = statsd_parse_value (extra + 1, &scale);
269 if (status != 0)
270 return (status);
272 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
273 return (-1);
274 }
276 value_ms.derive = 0;
277 status = statsd_parse_value (value_str, &value_ms);
278 if (status != 0)
279 return (status);
281 value = MS_TO_CDTIME_T (value_ms.gauge / scale.gauge);
283 pthread_mutex_lock (&metrics_lock);
285 metric = statsd_metric_lookup_unsafe (name, STATSD_TIMER);
286 if (metric == NULL)
287 {
288 pthread_mutex_unlock (&metrics_lock);
289 return (-1);
290 }
292 if (metric->latency == NULL)
293 metric->latency = latency_counter_create ();
294 if (metric->latency == NULL)
295 {
296 pthread_mutex_unlock (&metrics_lock);
297 return (-1);
298 }
300 latency_counter_add (metric->latency, value);
301 metric->updates_num++;
303 pthread_mutex_unlock (&metrics_lock);
304 return (0);
305 } /* }}} int statsd_handle_timer */
307 static int statsd_handle_set (char const *name, /* {{{ */
308 char const *set_key_orig)
309 {
310 statsd_metric_t *metric = NULL;
311 char *set_key;
312 int status;
314 pthread_mutex_lock (&metrics_lock);
316 metric = statsd_metric_lookup_unsafe (name, STATSD_SET);
317 if (metric == NULL)
318 {
319 pthread_mutex_unlock (&metrics_lock);
320 return (-1);
321 }
323 /* Make sure metric->set exists. */
324 if (metric->set == NULL)
325 metric->set = c_avl_create ((void *) strcmp);
327 if (metric->set == NULL)
328 {
329 pthread_mutex_unlock (&metrics_lock);
330 ERROR ("statsd plugin: c_avl_create failed.");
331 return (-1);
332 }
334 set_key = strdup (set_key_orig);
335 if (set_key == NULL)
336 {
337 pthread_mutex_unlock (&metrics_lock);
338 ERROR ("statsd plugin: strdup failed.");
339 return (-1);
340 }
342 status = c_avl_insert (metric->set, set_key, /* value = */ NULL);
343 if (status < 0)
344 {
345 pthread_mutex_unlock (&metrics_lock);
346 if (status < 0)
347 ERROR ("statsd plugin: c_avl_insert (\"%s\") failed with status %i.",
348 set_key, status);
349 sfree (set_key);
350 return (-1);
351 }
352 else if (status > 0) /* key already exists */
353 {
354 sfree (set_key);
355 }
357 metric->updates_num++;
359 pthread_mutex_unlock (&metrics_lock);
360 return (0);
361 } /* }}} int statsd_handle_set */
363 static int statsd_parse_line (char *buffer) /* {{{ */
364 {
365 char *name = buffer;
366 char *value;
367 char *type;
368 char *extra;
370 type = strchr (name, '|');
371 if (type == NULL)
372 return (-1);
373 *type = 0;
374 type++;
376 value = strrchr (name, ':');
377 if (value == NULL)
378 return (-1);
379 *value = 0;
380 value++;
382 extra = strchr (type, '|');
383 if (extra != NULL)
384 {
385 *extra = 0;
386 extra++;
387 }
389 if (strcmp ("c", type) == 0)
390 return (statsd_handle_counter (name, value, extra));
391 else if (strcmp ("ms", type) == 0)
392 return (statsd_handle_timer (name, value, extra));
394 /* extra is only valid for counters and timers */
395 if (extra != NULL)
396 return (-1);
398 if (strcmp ("g", type) == 0)
399 return (statsd_handle_gauge (name, value));
400 else if (strcmp ("s", type) == 0)
401 return (statsd_handle_set (name, value));
402 else
403 return (-1);
404 } /* }}} void statsd_parse_line */
406 static void statsd_parse_buffer (char *buffer) /* {{{ */
407 {
408 while (buffer != NULL)
409 {
410 char orig[64];
411 char *next;
412 int status;
414 next = strchr (buffer, '\n');
415 if (next != NULL)
416 {
417 *next = 0;
418 next++;
419 }
421 if (*buffer == 0)
422 {
423 buffer = next;
424 continue;
425 }
427 sstrncpy (orig, buffer, sizeof (orig));
429 status = statsd_parse_line (buffer);
430 if (status != 0)
431 ERROR ("statsd plugin: Unable to parse line: \"%s\"", orig);
433 buffer = next;
434 }
435 } /* }}} void statsd_parse_buffer */
437 static void statsd_network_read (int fd) /* {{{ */
438 {
439 char buffer[4096];
440 size_t buffer_size;
441 ssize_t status;
443 status = recv (fd, buffer, sizeof (buffer), /* flags = */ MSG_DONTWAIT);
444 if (status < 0)
445 {
446 char errbuf[1024];
448 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
449 return;
451 ERROR ("statsd plugin: recv(2) failed: %s",
452 sstrerror (errno, errbuf, sizeof (errbuf)));
453 return;
454 }
456 buffer_size = (size_t) status;
457 if (buffer_size >= sizeof (buffer))
458 buffer_size = sizeof (buffer) - 1;
459 buffer[buffer_size] = 0;
461 statsd_parse_buffer (buffer);
462 } /* }}} void statsd_network_read */
464 static int statsd_network_init (struct pollfd **ret_fds, /* {{{ */
465 size_t *ret_fds_num)
466 {
467 struct pollfd *fds = NULL;
468 size_t fds_num = 0;
470 struct addrinfo ai_hints;
471 struct addrinfo *ai_list = NULL;
472 struct addrinfo *ai_ptr;
473 int status;
475 char const *node = (conf_node != NULL) ? conf_node : STATSD_DEFAULT_NODE;
476 char const *service = (conf_service != NULL)
477 ? conf_service : STATSD_DEFAULT_SERVICE;
479 memset (&ai_hints, 0, sizeof (ai_hints));
480 ai_hints.ai_flags = AI_PASSIVE;
481 #ifdef AI_ADDRCONFIG
482 ai_hints.ai_flags |= AI_ADDRCONFIG;
483 #endif
484 ai_hints.ai_family = AF_UNSPEC;
485 ai_hints.ai_socktype = SOCK_DGRAM;
487 status = getaddrinfo (node, service, &ai_hints, &ai_list);
488 if (status != 0)
489 {
490 ERROR ("statsd plugin: getaddrinfo (\"%s\", \"%s\") failed: %s",
491 node, service, gai_strerror (status));
492 return (status);
493 }
495 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
496 {
497 int fd;
498 struct pollfd *tmp;
500 char dbg_node[NI_MAXHOST];
501 char dbg_service[NI_MAXSERV];
503 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
504 if (fd < 0)
505 {
506 char errbuf[1024];
507 ERROR ("statsd plugin: socket(2) failed: %s",
508 sstrerror (errno, errbuf, sizeof (errbuf)));
509 continue;
510 }
512 getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
513 dbg_node, sizeof (dbg_node), dbg_service, sizeof (dbg_service),
514 NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV);
515 DEBUG ("statsd plugin: Trying to bind to [%s]:%s ...", dbg_node, dbg_service);
517 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
518 if (status != 0)
519 {
520 char errbuf[1024];
521 ERROR ("statsd plugin: bind(2) failed: %s",
522 sstrerror (errno, errbuf, sizeof (errbuf)));
523 close (fd);
524 continue;
525 }
527 tmp = realloc (fds, sizeof (*fds) * (fds_num + 1));
528 if (tmp == NULL)
529 {
530 ERROR ("statsd plugin: realloc failed.");
531 continue;
532 }
533 fds = tmp;
534 tmp = fds + fds_num;
535 fds_num++;
537 memset (tmp, 0, sizeof (*tmp));
538 tmp->fd = fd;
539 tmp->events = POLLIN | POLLPRI;
540 }
542 freeaddrinfo (ai_list);
544 if (fds_num == 0)
545 {
546 ERROR ("statsd plugin: Unable to create listening socket for [%s]:%s.",
547 (node != NULL) ? node : "::", service);
548 return (ENOENT);
549 }
551 *ret_fds = fds;
552 *ret_fds_num = fds_num;
553 return (0);
554 } /* }}} int statsd_network_init */
556 static void *statsd_network_thread (void *args) /* {{{ */
557 {
558 struct pollfd *fds = NULL;
559 size_t fds_num = 0;
560 int status;
561 size_t i;
563 status = statsd_network_init (&fds, &fds_num);
564 if (status != 0)
565 {
566 ERROR ("statsd plugin: Unable to open listening sockets.");
567 pthread_exit ((void *) 0);
568 }
570 while (!network_thread_shutdown)
571 {
572 status = poll (fds, (nfds_t) fds_num, /* timeout = */ -1);
573 if (status < 0)
574 {
575 char errbuf[1024];
577 if ((errno == EINTR) || (errno == EAGAIN))
578 continue;
580 ERROR ("statsd plugin: poll(2) failed: %s",
581 sstrerror (errno, errbuf, sizeof (errbuf)));
582 break;
583 }
585 for (i = 0; i < fds_num; i++)
586 {
587 if ((fds[i].revents & (POLLIN | POLLPRI)) == 0)
588 continue;
590 statsd_network_read (fds[i].fd);
591 fds[i].revents = 0;
592 }
593 } /* while (!network_thread_shutdown) */
595 /* Clean up */
596 for (i = 0; i < fds_num; i++)
597 close (fds[i].fd);
598 sfree (fds);
600 return ((void *) 0);
601 } /* }}} void *statsd_network_thread */
603 static int statsd_config_timer_percentile (oconfig_item_t *ci) /* {{{ */
604 {
605 double percent = NAN;
606 double *tmp;
607 int status;
609 status = cf_util_get_double (ci, &percent);
610 if (status != 0)
611 return (status);
613 if ((percent <= 0.0) || (percent >= 100))
614 {
615 ERROR ("statsd plugin: The value for \"%s\" must be between 0 and 100, "
616 "exclusively.", ci->key);
617 return (ERANGE);
618 }
620 tmp = realloc (conf_timer_percentile,
621 sizeof (*conf_timer_percentile) * (conf_timer_percentile_num + 1));
622 if (tmp == NULL)
623 {
624 ERROR ("statsd plugin: realloc failed.");
625 return (ENOMEM);
626 }
627 conf_timer_percentile = tmp;
628 conf_timer_percentile[conf_timer_percentile_num] = percent;
629 conf_timer_percentile_num++;
631 return (0);
632 } /* }}} int statsd_config_timer_percentile */
634 static int statsd_config (oconfig_item_t *ci) /* {{{ */
635 {
636 int i;
638 for (i = 0; i < ci->children_num; i++)
639 {
640 oconfig_item_t *child = ci->children + i;
642 if (strcasecmp ("Host", child->key) == 0)
643 cf_util_get_string (child, &conf_node);
644 else if (strcasecmp ("Port", child->key) == 0)
645 cf_util_get_service (child, &conf_service);
646 else if (strcasecmp ("DeleteCounters", child->key) == 0)
647 cf_util_get_boolean (child, &conf_delete_counters);
648 else if (strcasecmp ("DeleteTimers", child->key) == 0)
649 cf_util_get_boolean (child, &conf_delete_timers);
650 else if (strcasecmp ("DeleteGauges", child->key) == 0)
651 cf_util_get_boolean (child, &conf_delete_gauges);
652 else if (strcasecmp ("DeleteSets", child->key) == 0)
653 cf_util_get_boolean (child, &conf_delete_sets);
654 else if (strcasecmp ("TimerLower", child->key) == 0)
655 cf_util_get_boolean (child, &conf_timer_lower);
656 else if (strcasecmp ("TimerUpper", child->key) == 0)
657 cf_util_get_boolean (child, &conf_timer_upper);
658 else if (strcasecmp ("TimerSum", child->key) == 0)
659 cf_util_get_boolean (child, &conf_timer_sum);
660 else if (strcasecmp ("TimerCount", child->key) == 0)
661 cf_util_get_boolean (child, &conf_timer_count);
662 else if (strcasecmp ("TimerPercentile", child->key) == 0)
663 statsd_config_timer_percentile (child);
664 else
665 ERROR ("statsd plugin: The \"%s\" config option is not valid.",
666 child->key);
667 }
669 return (0);
670 } /* }}} int statsd_config */
672 static int statsd_init (void) /* {{{ */
673 {
674 pthread_mutex_lock (&metrics_lock);
675 if (metrics_tree == NULL)
676 metrics_tree = c_avl_create ((void *) strcmp);
678 if (!network_thread_running)
679 {
680 int status;
682 status = pthread_create (&network_thread,
683 /* attr = */ NULL,
684 statsd_network_thread,
685 /* args = */ NULL);
686 if (status != 0)
687 {
688 char errbuf[1024];
689 pthread_mutex_unlock (&metrics_lock);
690 ERROR ("statsd plugin: pthread_create failed: %s",
691 sstrerror (errno, errbuf, sizeof (errbuf)));
692 return (status);
693 }
694 }
695 network_thread_running = 1;
697 pthread_mutex_unlock (&metrics_lock);
699 return (0);
700 } /* }}} int statsd_init */
702 /* Must hold metrics_lock when calling this function. */
703 static int statsd_metric_clear_set_unsafe (statsd_metric_t *metric) /* {{{ */
704 {
705 void *key;
706 void *value;
708 if ((metric == NULL) || (metric->type != STATSD_SET))
709 return (EINVAL);
711 if (metric->set == NULL)
712 return (0);
714 while (c_avl_pick (metric->set, &key, &value) == 0)
715 {
716 sfree (key);
717 sfree (value);
718 }
720 return (0);
721 } /* }}} int statsd_metric_clear_set_unsafe */
723 /* Must hold metrics_lock when calling this function. */
724 static int statsd_metric_submit_unsafe (char const *name, /* {{{ */
725 statsd_metric_t const *metric)
726 {
727 value_t values[1];
728 value_list_t vl = VALUE_LIST_INIT;
730 vl.values = values;
731 vl.values_len = 1;
732 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
733 sstrncpy (vl.plugin, "statsd", sizeof (vl.plugin));
735 if (metric->type == STATSD_GAUGE)
736 sstrncpy (vl.type, "gauge", sizeof (vl.type));
737 else if (metric->type == STATSD_TIMER)
738 sstrncpy (vl.type, "latency", sizeof (vl.type));
739 else if (metric->type == STATSD_SET)
740 sstrncpy (vl.type, "objects", sizeof (vl.type));
741 else /* if (metric->type == STATSD_COUNTER) */
742 sstrncpy (vl.type, "derive", sizeof (vl.type));
744 sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
746 if (metric->type == STATSD_GAUGE)
747 values[0].gauge = (gauge_t) metric->value;
748 else if (metric->type == STATSD_TIMER)
749 {
750 size_t i;
752 if (metric->updates_num == 0)
753 return (0);
755 vl.time = cdtime ();
757 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
758 "%s-average", name);
759 values[0].gauge = CDTIME_T_TO_DOUBLE (
760 latency_counter_get_average (metric->latency));
761 plugin_dispatch_values (&vl);
763 if (conf_timer_lower) {
764 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
765 "%s-lower", name);
766 values[0].gauge = CDTIME_T_TO_DOUBLE (
767 latency_counter_get_min (metric->latency));
768 plugin_dispatch_values (&vl);
769 }
771 if (conf_timer_upper) {
772 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
773 "%s-upper", name);
774 values[0].gauge = CDTIME_T_TO_DOUBLE (
775 latency_counter_get_max (metric->latency));
776 plugin_dispatch_values (&vl);
777 }
779 if (conf_timer_sum) {
780 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
781 "%s-sum", name);
782 values[0].gauge = CDTIME_T_TO_DOUBLE (
783 latency_counter_get_sum (metric->latency));
784 plugin_dispatch_values (&vl);
785 }
787 for (i = 0; i < conf_timer_percentile_num; i++)
788 {
789 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
790 "%s-percentile-%.0f", name, conf_timer_percentile[i]);
791 values[0].gauge = CDTIME_T_TO_DOUBLE (
792 latency_counter_get_percentile (
793 metric->latency, conf_timer_percentile[i]));
794 plugin_dispatch_values (&vl);
795 }
797 /* Keep this at the end, since vl.type is set to "gauge" here. The
798 * vl.type's above are implicitly set to "latency". */
799 if (conf_timer_count) {
800 sstrncpy (vl.type, "gauge", sizeof (vl.type));
801 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
802 "%s-count", name);
803 values[0].gauge = latency_counter_get_num (metric->latency);
804 plugin_dispatch_values (&vl);
805 }
807 latency_counter_reset (metric->latency);
808 return (0);
809 }
810 else if (metric->type == STATSD_SET)
811 {
812 if (metric->set == NULL)
813 values[0].gauge = 0.0;
814 else
815 values[0].gauge = (gauge_t) c_avl_size (metric->set);
816 }
817 else
818 values[0].derive = (derive_t) metric->value;
820 return (plugin_dispatch_values (&vl));
821 } /* }}} int statsd_metric_submit_unsafe */
823 static int statsd_read (void) /* {{{ */
824 {
825 c_avl_iterator_t *iter;
826 char *name;
827 statsd_metric_t *metric;
829 char **to_be_deleted = NULL;
830 size_t to_be_deleted_num = 0;
831 size_t i;
833 pthread_mutex_lock (&metrics_lock);
835 if (metrics_tree == NULL)
836 {
837 pthread_mutex_unlock (&metrics_lock);
838 return (0);
839 }
841 iter = c_avl_get_iterator (metrics_tree);
842 while (c_avl_iterator_next (iter, (void *) &name, (void *) &metric) == 0)
843 {
844 if ((metric->updates_num == 0)
845 && ((conf_delete_counters && (metric->type == STATSD_COUNTER))
846 || (conf_delete_timers && (metric->type == STATSD_TIMER))
847 || (conf_delete_gauges && (metric->type == STATSD_GAUGE))
848 || (conf_delete_sets && (metric->type == STATSD_SET))))
849 {
850 DEBUG ("statsd plugin: Deleting metric \"%s\".", name);
851 strarray_add (&to_be_deleted, &to_be_deleted_num, name);
852 continue;
853 }
855 /* Names have a prefix, e.g. "c:", which determines the (statsd) type.
856 * Remove this here. */
857 statsd_metric_submit_unsafe (name + 2, metric);
859 /* Reset the metric. */
860 metric->updates_num = 0;
861 if (metric->type == STATSD_SET)
862 statsd_metric_clear_set_unsafe (metric);
863 }
864 c_avl_iterator_destroy (iter);
866 for (i = 0; i < to_be_deleted_num; i++)
867 {
868 int status;
870 status = c_avl_remove (metrics_tree, to_be_deleted[i],
871 (void *) &name, (void *) &metric);
872 if (status != 0)
873 {
874 ERROR ("stats plugin: c_avl_remove (\"%s\") failed with status %i.",
875 to_be_deleted[i], status);
876 continue;
877 }
879 sfree (name);
880 sfree (metric);
881 }
883 pthread_mutex_unlock (&metrics_lock);
885 strarray_free (to_be_deleted, to_be_deleted_num);
887 return (0);
888 } /* }}} int statsd_read */
890 static int statsd_shutdown (void) /* {{{ */
891 {
892 void *key;
893 void *value;
895 pthread_mutex_lock (&metrics_lock);
897 if (network_thread_running)
898 {
899 network_thread_shutdown = 1;
900 pthread_kill (network_thread, SIGTERM);
901 pthread_join (network_thread, /* retval = */ NULL);
902 }
903 network_thread_running = 0;
905 while (c_avl_pick (metrics_tree, &key, &value) == 0)
906 {
907 sfree (key);
908 sfree (value);
909 }
910 c_avl_destroy (metrics_tree);
911 metrics_tree = NULL;
913 sfree (conf_node);
914 sfree (conf_service);
916 pthread_mutex_unlock (&metrics_lock);
918 return (0);
919 } /* }}} int statsd_shutdown */
921 void module_register (void)
922 {
923 plugin_register_complex_config ("statsd", statsd_config);
924 plugin_register_init ("statsd", statsd_init);
925 plugin_register_read ("statsd", statsd_read);
926 plugin_register_shutdown ("statsd", statsd_shutdown);
927 }
929 /* vim: set sw=2 sts=2 et fdm=marker : */