1 /**
2 * collectd - src/statsd.c
3 * Copyright (C) 2013 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 */
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31 #include "utils_avltree.h"
32 #include "utils_complain.h"
33 #include "utils_latency.h"
35 #include <pthread.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netdb.h>
40 #include <poll.h>
42 /* AIX doesn't have MSG_DONTWAIT */
43 #ifndef MSG_DONTWAIT
44 # define MSG_DONTWAIT MSG_NONBLOCK
45 #endif
47 #ifndef STATSD_DEFAULT_NODE
48 # define STATSD_DEFAULT_NODE NULL
49 #endif
51 #ifndef STATSD_DEFAULT_SERVICE
52 # define STATSD_DEFAULT_SERVICE "8125"
53 #endif
55 enum metric_type_e
56 {
57 STATSD_COUNTER,
58 STATSD_TIMER,
59 STATSD_GAUGE,
60 STATSD_SET
61 };
62 typedef enum metric_type_e metric_type_t;
64 struct statsd_metric_s
65 {
66 metric_type_t type;
67 double value;
68 latency_counter_t *latency;
69 c_avl_tree_t *set;
70 unsigned long updates_num;
71 };
72 typedef struct statsd_metric_s statsd_metric_t;
74 static c_avl_tree_t *metrics_tree = NULL;
75 static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_t network_thread;
78 static _Bool network_thread_running = 0;
79 static _Bool network_thread_shutdown = 0;
81 static char *conf_node = NULL;
82 static char *conf_service = NULL;
84 static _Bool conf_delete_counters = 0;
85 static _Bool conf_delete_timers = 0;
86 static _Bool conf_delete_gauges = 0;
87 static _Bool conf_delete_sets = 0;
89 static double *conf_timer_percentile = NULL;
90 static size_t conf_timer_percentile_num = 0;
92 static _Bool conf_timer_lower = 0;
93 static _Bool conf_timer_upper = 0;
94 static _Bool conf_timer_sum = 0;
95 static _Bool conf_timer_count = 0;
97 /* Must hold metrics_lock when calling this function. */
98 static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name, /* {{{ */
99 metric_type_t type)
100 {
101 char key[DATA_MAX_NAME_LEN + 2];
102 char *key_copy;
103 statsd_metric_t *metric;
104 int status;
106 switch (type)
107 {
108 case STATSD_COUNTER: key[0] = 'c'; break;
109 case STATSD_TIMER: key[0] = 't'; break;
110 case STATSD_GAUGE: key[0] = 'g'; break;
111 case STATSD_SET: key[0] = 's'; break;
112 default: return (NULL);
113 }
115 key[1] = ':';
116 sstrncpy (&key[2], name, sizeof (key) - 2);
118 status = c_avl_get (metrics_tree, key, (void *) &metric);
119 if (status == 0)
120 return (metric);
122 key_copy = strdup (key);
123 if (key_copy == NULL)
124 {
125 ERROR ("statsd plugin: strdup failed.");
126 return (NULL);
127 }
129 metric = malloc (sizeof (*metric));
130 if (metric == NULL)
131 {
132 ERROR ("statsd plugin: malloc failed.");
133 sfree (key_copy);
134 return (NULL);
135 }
136 memset (metric, 0, sizeof (*metric));
138 metric->type = type;
139 metric->latency = NULL;
140 metric->set = NULL;
142 status = c_avl_insert (metrics_tree, key_copy, metric);
143 if (status != 0)
144 {
145 ERROR ("statsd plugin: c_avl_insert failed.");
146 sfree (key_copy);
147 sfree (metric);
148 return (NULL);
149 }
151 return (metric);
152 } /* }}} statsd_metric_lookup_unsafe */
154 static int statsd_metric_set (char const *name, double value, /* {{{ */
155 metric_type_t type)
156 {
157 statsd_metric_t *metric;
159 pthread_mutex_lock (&metrics_lock);
161 metric = statsd_metric_lookup_unsafe (name, type);
162 if (metric == NULL)
163 {
164 pthread_mutex_unlock (&metrics_lock);
165 return (-1);
166 }
168 metric->value = value;
169 metric->updates_num++;
171 pthread_mutex_unlock (&metrics_lock);
173 return (0);
174 } /* }}} int statsd_metric_set */
176 static int statsd_metric_add (char const *name, double delta, /* {{{ */
177 metric_type_t type)
178 {
179 statsd_metric_t *metric;
181 pthread_mutex_lock (&metrics_lock);
183 metric = statsd_metric_lookup_unsafe (name, type);
184 if (metric == NULL)
185 {
186 pthread_mutex_unlock (&metrics_lock);
187 return (-1);
188 }
190 metric->value += delta;
191 metric->updates_num++;
193 pthread_mutex_unlock (&metrics_lock);
195 return (0);
196 } /* }}} int statsd_metric_add */
198 static void statsd_metric_free (statsd_metric_t *metric) /* {{{ */
199 {
200 if (metric == NULL)
201 return;
203 if (metric->latency != NULL)
204 {
205 latency_counter_destroy (metric->latency);
206 metric->latency = NULL;
207 }
209 if (metric->set != NULL)
210 {
211 void *key;
212 void *value;
214 while (c_avl_pick (metric->set, &key, &value) == 0)
215 {
216 sfree (key);
217 assert (value == NULL);
218 }
220 c_avl_destroy (metric->set);
221 metric->set = NULL;
222 }
224 sfree (metric);
225 } /* }}} void statsd_metric_free */
227 static int statsd_parse_value (char const *str, value_t *ret_value) /* {{{ */
228 {
229 char *endptr = NULL;
231 ret_value->gauge = (gauge_t) strtod (str, &endptr);
232 if ((str == endptr) || ((endptr != NULL) && (*endptr != 0)))
233 return (-1);
235 return (0);
236 } /* }}} int statsd_parse_value */
238 static int statsd_handle_counter (char const *name, /* {{{ */
239 char const *value_str,
240 char const *extra)
241 {
242 value_t value;
243 value_t scale;
244 int status;
246 if ((extra != NULL) && (extra[0] != '@'))
247 return (-1);
249 scale.gauge = 1.0;
250 if (extra != NULL)
251 {
252 status = statsd_parse_value (extra + 1, &scale);
253 if (status != 0)
254 return (status);
256 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
257 return (-1);
258 }
260 value.gauge = 1.0;
261 status = statsd_parse_value (value_str, &value);
262 if (status != 0)
263 return (status);
265 return (statsd_metric_add (name, (double) (value.gauge / scale.gauge),
266 STATSD_COUNTER));
267 } /* }}} int statsd_handle_counter */
269 static int statsd_handle_gauge (char const *name, /* {{{ */
270 char const *value_str)
271 {
272 value_t value;
273 int status;
275 value.gauge = 0;
276 status = statsd_parse_value (value_str, &value);
277 if (status != 0)
278 return (status);
280 if ((value_str[0] == '+') || (value_str[0] == '-'))
281 return (statsd_metric_add (name, (double) value.gauge, STATSD_GAUGE));
282 else
283 return (statsd_metric_set (name, (double) value.gauge, STATSD_GAUGE));
284 } /* }}} int statsd_handle_gauge */
286 static int statsd_handle_timer (char const *name, /* {{{ */
287 char const *value_str,
288 char const *extra)
289 {
290 statsd_metric_t *metric;
291 value_t value_ms;
292 value_t scale;
293 cdtime_t value;
294 int status;
296 if ((extra != NULL) && (extra[0] != '@'))
297 return (-1);
299 scale.gauge = 1.0;
300 if (extra != NULL)
301 {
302 status = statsd_parse_value (extra + 1, &scale);
303 if (status != 0)
304 return (status);
306 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
307 return (-1);
308 }
310 value_ms.derive = 0;
311 status = statsd_parse_value (value_str, &value_ms);
312 if (status != 0)
313 return (status);
315 value = MS_TO_CDTIME_T (value_ms.gauge / scale.gauge);
317 pthread_mutex_lock (&metrics_lock);
319 metric = statsd_metric_lookup_unsafe (name, STATSD_TIMER);
320 if (metric == NULL)
321 {
322 pthread_mutex_unlock (&metrics_lock);
323 return (-1);
324 }
326 if (metric->latency == NULL)
327 metric->latency = latency_counter_create ();
328 if (metric->latency == NULL)
329 {
330 pthread_mutex_unlock (&metrics_lock);
331 return (-1);
332 }
334 latency_counter_add (metric->latency, value);
335 metric->updates_num++;
337 pthread_mutex_unlock (&metrics_lock);
338 return (0);
339 } /* }}} int statsd_handle_timer */
341 static int statsd_handle_set (char const *name, /* {{{ */
342 char const *set_key_orig)
343 {
344 statsd_metric_t *metric = NULL;
345 char *set_key;
346 int status;
348 pthread_mutex_lock (&metrics_lock);
350 metric = statsd_metric_lookup_unsafe (name, STATSD_SET);
351 if (metric == NULL)
352 {
353 pthread_mutex_unlock (&metrics_lock);
354 return (-1);
355 }
357 /* Make sure metric->set exists. */
358 if (metric->set == NULL)
359 metric->set = c_avl_create ((void *) strcmp);
361 if (metric->set == NULL)
362 {
363 pthread_mutex_unlock (&metrics_lock);
364 ERROR ("statsd plugin: c_avl_create failed.");
365 return (-1);
366 }
368 set_key = strdup (set_key_orig);
369 if (set_key == NULL)
370 {
371 pthread_mutex_unlock (&metrics_lock);
372 ERROR ("statsd plugin: strdup failed.");
373 return (-1);
374 }
376 status = c_avl_insert (metric->set, set_key, /* value = */ NULL);
377 if (status < 0)
378 {
379 pthread_mutex_unlock (&metrics_lock);
380 if (status < 0)
381 ERROR ("statsd plugin: c_avl_insert (\"%s\") failed with status %i.",
382 set_key, status);
383 sfree (set_key);
384 return (-1);
385 }
386 else if (status > 0) /* key already exists */
387 {
388 sfree (set_key);
389 }
391 metric->updates_num++;
393 pthread_mutex_unlock (&metrics_lock);
394 return (0);
395 } /* }}} int statsd_handle_set */
397 static int statsd_parse_line (char *buffer) /* {{{ */
398 {
399 char *name = buffer;
400 char *value;
401 char *type;
402 char *extra;
404 type = strchr (name, '|');
405 if (type == NULL)
406 return (-1);
407 *type = 0;
408 type++;
410 value = strrchr (name, ':');
411 if (value == NULL)
412 return (-1);
413 *value = 0;
414 value++;
416 extra = strchr (type, '|');
417 if (extra != NULL)
418 {
419 *extra = 0;
420 extra++;
421 }
423 if (strcmp ("c", type) == 0)
424 return (statsd_handle_counter (name, value, extra));
425 else if (strcmp ("ms", type) == 0)
426 return (statsd_handle_timer (name, value, extra));
428 /* extra is only valid for counters and timers */
429 if (extra != NULL)
430 return (-1);
432 if (strcmp ("g", type) == 0)
433 return (statsd_handle_gauge (name, value));
434 else if (strcmp ("s", type) == 0)
435 return (statsd_handle_set (name, value));
436 else
437 return (-1);
438 } /* }}} void statsd_parse_line */
440 static void statsd_parse_buffer (char *buffer) /* {{{ */
441 {
442 while (buffer != NULL)
443 {
444 char orig[64];
445 char *next;
446 int status;
448 next = strchr (buffer, '\n');
449 if (next != NULL)
450 {
451 *next = 0;
452 next++;
453 }
455 if (*buffer == 0)
456 {
457 buffer = next;
458 continue;
459 }
461 sstrncpy (orig, buffer, sizeof (orig));
463 status = statsd_parse_line (buffer);
464 if (status != 0)
465 ERROR ("statsd plugin: Unable to parse line: \"%s\"", orig);
467 buffer = next;
468 }
469 } /* }}} void statsd_parse_buffer */
471 static void statsd_network_read (int fd) /* {{{ */
472 {
473 char buffer[4096];
474 size_t buffer_size;
475 ssize_t status;
477 status = recv (fd, buffer, sizeof (buffer), /* flags = */ MSG_DONTWAIT);
478 if (status < 0)
479 {
480 char errbuf[1024];
482 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
483 return;
485 ERROR ("statsd plugin: recv(2) failed: %s",
486 sstrerror (errno, errbuf, sizeof (errbuf)));
487 return;
488 }
490 buffer_size = (size_t) status;
491 if (buffer_size >= sizeof (buffer))
492 buffer_size = sizeof (buffer) - 1;
493 buffer[buffer_size] = 0;
495 statsd_parse_buffer (buffer);
496 } /* }}} void statsd_network_read */
498 static int statsd_network_init (struct pollfd **ret_fds, /* {{{ */
499 size_t *ret_fds_num)
500 {
501 struct pollfd *fds = NULL;
502 size_t fds_num = 0;
504 struct addrinfo ai_hints;
505 struct addrinfo *ai_list = NULL;
506 struct addrinfo *ai_ptr;
507 int status;
509 char const *node = (conf_node != NULL) ? conf_node : STATSD_DEFAULT_NODE;
510 char const *service = (conf_service != NULL)
511 ? conf_service : STATSD_DEFAULT_SERVICE;
513 memset (&ai_hints, 0, sizeof (ai_hints));
514 ai_hints.ai_flags = AI_PASSIVE;
515 #ifdef AI_ADDRCONFIG
516 ai_hints.ai_flags |= AI_ADDRCONFIG;
517 #endif
518 ai_hints.ai_family = AF_UNSPEC;
519 ai_hints.ai_socktype = SOCK_DGRAM;
521 status = getaddrinfo (node, service, &ai_hints, &ai_list);
522 if (status != 0)
523 {
524 ERROR ("statsd plugin: getaddrinfo (\"%s\", \"%s\") failed: %s",
525 node, service, gai_strerror (status));
526 return (status);
527 }
529 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
530 {
531 int fd;
532 struct pollfd *tmp;
534 char dbg_node[NI_MAXHOST];
535 char dbg_service[NI_MAXSERV];
537 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
538 if (fd < 0)
539 {
540 char errbuf[1024];
541 ERROR ("statsd plugin: socket(2) failed: %s",
542 sstrerror (errno, errbuf, sizeof (errbuf)));
543 continue;
544 }
546 getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
547 dbg_node, sizeof (dbg_node), dbg_service, sizeof (dbg_service),
548 NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV);
549 DEBUG ("statsd plugin: Trying to bind to [%s]:%s ...", dbg_node, dbg_service);
551 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
552 if (status != 0)
553 {
554 char errbuf[1024];
555 ERROR ("statsd plugin: bind(2) failed: %s",
556 sstrerror (errno, errbuf, sizeof (errbuf)));
557 close (fd);
558 continue;
559 }
561 tmp = realloc (fds, sizeof (*fds) * (fds_num + 1));
562 if (tmp == NULL)
563 {
564 ERROR ("statsd plugin: realloc failed.");
565 close (fd);
566 continue;
567 }
568 fds = tmp;
569 tmp = fds + fds_num;
570 fds_num++;
572 memset (tmp, 0, sizeof (*tmp));
573 tmp->fd = fd;
574 tmp->events = POLLIN | POLLPRI;
575 }
577 freeaddrinfo (ai_list);
579 if (fds_num == 0)
580 {
581 ERROR ("statsd plugin: Unable to create listening socket for [%s]:%s.",
582 (node != NULL) ? node : "::", service);
583 return (ENOENT);
584 }
586 *ret_fds = fds;
587 *ret_fds_num = fds_num;
588 return (0);
589 } /* }}} int statsd_network_init */
591 static void *statsd_network_thread (void *args) /* {{{ */
592 {
593 struct pollfd *fds = NULL;
594 size_t fds_num = 0;
595 int status;
596 size_t i;
598 status = statsd_network_init (&fds, &fds_num);
599 if (status != 0)
600 {
601 ERROR ("statsd plugin: Unable to open listening sockets.");
602 pthread_exit ((void *) 0);
603 }
605 while (!network_thread_shutdown)
606 {
607 status = poll (fds, (nfds_t) fds_num, /* timeout = */ -1);
608 if (status < 0)
609 {
610 char errbuf[1024];
612 if ((errno == EINTR) || (errno == EAGAIN))
613 continue;
615 ERROR ("statsd plugin: poll(2) failed: %s",
616 sstrerror (errno, errbuf, sizeof (errbuf)));
617 break;
618 }
620 for (i = 0; i < fds_num; i++)
621 {
622 if ((fds[i].revents & (POLLIN | POLLPRI)) == 0)
623 continue;
625 statsd_network_read (fds[i].fd);
626 fds[i].revents = 0;
627 }
628 } /* while (!network_thread_shutdown) */
630 /* Clean up */
631 for (i = 0; i < fds_num; i++)
632 close (fds[i].fd);
633 sfree (fds);
635 return ((void *) 0);
636 } /* }}} void *statsd_network_thread */
638 static int statsd_config_timer_percentile (oconfig_item_t *ci) /* {{{ */
639 {
640 double percent = NAN;
641 double *tmp;
642 int status;
644 status = cf_util_get_double (ci, &percent);
645 if (status != 0)
646 return (status);
648 if ((percent <= 0.0) || (percent >= 100))
649 {
650 ERROR ("statsd plugin: The value for \"%s\" must be between 0 and 100, "
651 "exclusively.", ci->key);
652 return (ERANGE);
653 }
655 tmp = realloc (conf_timer_percentile,
656 sizeof (*conf_timer_percentile) * (conf_timer_percentile_num + 1));
657 if (tmp == NULL)
658 {
659 ERROR ("statsd plugin: realloc failed.");
660 return (ENOMEM);
661 }
662 conf_timer_percentile = tmp;
663 conf_timer_percentile[conf_timer_percentile_num] = percent;
664 conf_timer_percentile_num++;
666 return (0);
667 } /* }}} int statsd_config_timer_percentile */
669 static int statsd_config (oconfig_item_t *ci) /* {{{ */
670 {
671 int i;
673 for (i = 0; i < ci->children_num; i++)
674 {
675 oconfig_item_t *child = ci->children + i;
677 if (strcasecmp ("Host", child->key) == 0)
678 cf_util_get_string (child, &conf_node);
679 else if (strcasecmp ("Port", child->key) == 0)
680 cf_util_get_service (child, &conf_service);
681 else if (strcasecmp ("DeleteCounters", child->key) == 0)
682 cf_util_get_boolean (child, &conf_delete_counters);
683 else if (strcasecmp ("DeleteTimers", child->key) == 0)
684 cf_util_get_boolean (child, &conf_delete_timers);
685 else if (strcasecmp ("DeleteGauges", child->key) == 0)
686 cf_util_get_boolean (child, &conf_delete_gauges);
687 else if (strcasecmp ("DeleteSets", child->key) == 0)
688 cf_util_get_boolean (child, &conf_delete_sets);
689 else if (strcasecmp ("TimerLower", child->key) == 0)
690 cf_util_get_boolean (child, &conf_timer_lower);
691 else if (strcasecmp ("TimerUpper", child->key) == 0)
692 cf_util_get_boolean (child, &conf_timer_upper);
693 else if (strcasecmp ("TimerSum", child->key) == 0)
694 cf_util_get_boolean (child, &conf_timer_sum);
695 else if (strcasecmp ("TimerCount", child->key) == 0)
696 cf_util_get_boolean (child, &conf_timer_count);
697 else if (strcasecmp ("TimerPercentile", child->key) == 0)
698 statsd_config_timer_percentile (child);
699 else
700 ERROR ("statsd plugin: The \"%s\" config option is not valid.",
701 child->key);
702 }
704 return (0);
705 } /* }}} int statsd_config */
707 static int statsd_init (void) /* {{{ */
708 {
709 pthread_mutex_lock (&metrics_lock);
710 if (metrics_tree == NULL)
711 metrics_tree = c_avl_create ((void *) strcmp);
713 if (!network_thread_running)
714 {
715 int status;
717 status = pthread_create (&network_thread,
718 /* attr = */ NULL,
719 statsd_network_thread,
720 /* args = */ NULL);
721 if (status != 0)
722 {
723 char errbuf[1024];
724 pthread_mutex_unlock (&metrics_lock);
725 ERROR ("statsd plugin: pthread_create failed: %s",
726 sstrerror (errno, errbuf, sizeof (errbuf)));
727 return (status);
728 }
729 }
730 network_thread_running = 1;
732 pthread_mutex_unlock (&metrics_lock);
734 return (0);
735 } /* }}} int statsd_init */
737 /* Must hold metrics_lock when calling this function. */
738 static int statsd_metric_clear_set_unsafe (statsd_metric_t *metric) /* {{{ */
739 {
740 void *key;
741 void *value;
743 if ((metric == NULL) || (metric->type != STATSD_SET))
744 return (EINVAL);
746 if (metric->set == NULL)
747 return (0);
749 while (c_avl_pick (metric->set, &key, &value) == 0)
750 {
751 sfree (key);
752 sfree (value);
753 }
755 return (0);
756 } /* }}} int statsd_metric_clear_set_unsafe */
758 /* Must hold metrics_lock when calling this function. */
759 static int statsd_metric_submit_unsafe (char const *name, /* {{{ */
760 statsd_metric_t const *metric)
761 {
762 value_t values[1];
763 value_list_t vl = VALUE_LIST_INIT;
765 vl.values = values;
766 vl.values_len = 1;
767 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
768 sstrncpy (vl.plugin, "statsd", sizeof (vl.plugin));
770 if (metric->type == STATSD_GAUGE)
771 sstrncpy (vl.type, "gauge", sizeof (vl.type));
772 else if (metric->type == STATSD_TIMER)
773 sstrncpy (vl.type, "latency", sizeof (vl.type));
774 else if (metric->type == STATSD_SET)
775 sstrncpy (vl.type, "objects", sizeof (vl.type));
776 else /* if (metric->type == STATSD_COUNTER) */
777 sstrncpy (vl.type, "derive", sizeof (vl.type));
779 sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
781 if (metric->type == STATSD_GAUGE)
782 values[0].gauge = (gauge_t) metric->value;
783 else if (metric->type == STATSD_TIMER)
784 {
785 size_t i;
786 _Bool have_events = (metric->updates_num > 0);
788 /* Make sure all timer metrics share the *same* timestamp. */
789 vl.time = cdtime ();
791 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
792 "%s-average", name);
793 values[0].gauge = have_events
794 ? CDTIME_T_TO_DOUBLE (latency_counter_get_average (metric->latency))
795 : NAN;
796 plugin_dispatch_values (&vl);
798 if (conf_timer_lower) {
799 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
800 "%s-lower", name);
801 values[0].gauge = have_events
802 ? CDTIME_T_TO_DOUBLE (latency_counter_get_min (metric->latency))
803 : NAN;
804 plugin_dispatch_values (&vl);
805 }
807 if (conf_timer_upper) {
808 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
809 "%s-upper", name);
810 values[0].gauge = have_events
811 ? CDTIME_T_TO_DOUBLE (latency_counter_get_max (metric->latency))
812 : NAN;
813 plugin_dispatch_values (&vl);
814 }
816 if (conf_timer_sum) {
817 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
818 "%s-sum", name);
819 values[0].gauge = have_events
820 ? CDTIME_T_TO_DOUBLE (latency_counter_get_sum (metric->latency))
821 : NAN;
822 plugin_dispatch_values (&vl);
823 }
825 for (i = 0; i < conf_timer_percentile_num; i++)
826 {
827 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
828 "%s-percentile-%.0f", name, conf_timer_percentile[i]);
829 values[0].gauge = have_events
830 ? CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (metric->latency, conf_timer_percentile[i]))
831 : NAN;
832 plugin_dispatch_values (&vl);
833 }
835 /* Keep this at the end, since vl.type is set to "gauge" here. The
836 * vl.type's above are implicitly set to "latency". */
837 if (conf_timer_count) {
838 sstrncpy (vl.type, "gauge", sizeof (vl.type));
839 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
840 "%s-count", name);
841 values[0].gauge = latency_counter_get_num (metric->latency);
842 plugin_dispatch_values (&vl);
843 }
845 latency_counter_reset (metric->latency);
846 return (0);
847 }
848 else if (metric->type == STATSD_SET)
849 {
850 if (metric->set == NULL)
851 values[0].gauge = 0.0;
852 else
853 values[0].gauge = (gauge_t) c_avl_size (metric->set);
854 }
855 else { /* STATSD_COUNTER */
856 /*
857 * Expand a single value to two metrics:
858 *
859 * - The absolute counter, as a gauge
860 * - A derived rate for this counter
861 */
862 values[0].derive = (derive_t) metric->value;
863 plugin_dispatch_values(&vl);
865 sstrncpy(vl.type, "gauge", sizeof (vl.type));
866 values[0].gauge = (gauge_t) metric->value;
867 }
869 return (plugin_dispatch_values (&vl));
870 } /* }}} int statsd_metric_submit_unsafe */
872 static int statsd_read (void) /* {{{ */
873 {
874 c_avl_iterator_t *iter;
875 char *name;
876 statsd_metric_t *metric;
878 char **to_be_deleted = NULL;
879 size_t to_be_deleted_num = 0;
880 size_t i;
882 pthread_mutex_lock (&metrics_lock);
884 if (metrics_tree == NULL)
885 {
886 pthread_mutex_unlock (&metrics_lock);
887 return (0);
888 }
890 iter = c_avl_get_iterator (metrics_tree);
891 while (c_avl_iterator_next (iter, (void *) &name, (void *) &metric) == 0)
892 {
893 if ((metric->updates_num == 0)
894 && ((conf_delete_counters && (metric->type == STATSD_COUNTER))
895 || (conf_delete_timers && (metric->type == STATSD_TIMER))
896 || (conf_delete_gauges && (metric->type == STATSD_GAUGE))
897 || (conf_delete_sets && (metric->type == STATSD_SET))))
898 {
899 DEBUG ("statsd plugin: Deleting metric \"%s\".", name);
900 strarray_add (&to_be_deleted, &to_be_deleted_num, name);
901 continue;
902 }
904 /* Names have a prefix, e.g. "c:", which determines the (statsd) type.
905 * Remove this here. */
906 statsd_metric_submit_unsafe (name + 2, metric);
908 /* Reset the metric. */
909 metric->updates_num = 0;
910 if (metric->type == STATSD_SET)
911 statsd_metric_clear_set_unsafe (metric);
912 }
913 c_avl_iterator_destroy (iter);
915 for (i = 0; i < to_be_deleted_num; i++)
916 {
917 int status;
919 status = c_avl_remove (metrics_tree, to_be_deleted[i],
920 (void *) &name, (void *) &metric);
921 if (status != 0)
922 {
923 ERROR ("stats plugin: c_avl_remove (\"%s\") failed with status %i.",
924 to_be_deleted[i], status);
925 continue;
926 }
928 sfree (name);
929 statsd_metric_free (metric);
930 }
932 pthread_mutex_unlock (&metrics_lock);
934 strarray_free (to_be_deleted, to_be_deleted_num);
936 return (0);
937 } /* }}} int statsd_read */
939 static int statsd_shutdown (void) /* {{{ */
940 {
941 void *key;
942 void *value;
944 pthread_mutex_lock (&metrics_lock);
946 if (network_thread_running)
947 {
948 network_thread_shutdown = 1;
949 pthread_kill (network_thread, SIGTERM);
950 pthread_join (network_thread, /* retval = */ NULL);
951 }
952 network_thread_running = 0;
954 while (c_avl_pick (metrics_tree, &key, &value) == 0)
955 {
956 sfree (key);
957 statsd_metric_free (value);
958 }
959 c_avl_destroy (metrics_tree);
960 metrics_tree = NULL;
962 sfree (conf_node);
963 sfree (conf_service);
965 pthread_mutex_unlock (&metrics_lock);
967 return (0);
968 } /* }}} int statsd_shutdown */
970 void module_register (void)
971 {
972 plugin_register_complex_config ("statsd", statsd_config);
973 plugin_register_init ("statsd", statsd_init);
974 plugin_register_read ("statsd", statsd_read);
975 plugin_register_shutdown ("statsd", statsd_shutdown);
976 }
978 /* vim: set sw=2 sts=2 et fdm=marker : */