2 * collectd - src/statsd.c
4 * Copyright (C) 2013 Florian octo Forster
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.
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.
19 * Florian octo Forster <octo at collectd.org>
25 #include "configfile.h"
26 #include "utils_avltree.h"
27 #include "utils_complain.h"
31 #include <sys/types.h>
32 #include <sys/socket.h>
36 #ifndef STATSD_DEFAULT_NODE
37 # define STATSD_DEFAULT_NODE NULL
40 #ifndef STATSD_DEFAULT_SERVICE
41 # define STATSD_DEFAULT_SERVICE "8125"
50 typedef enum metric_type_e metric_type_t;
52 struct statsd_metric_s
58 typedef struct statsd_metric_s statsd_metric_t;
60 static c_avl_tree_t *metrics_tree = NULL;
61 static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER;
63 static pthread_t network_thread;
64 static _Bool network_thread_running = 0;
65 static _Bool network_thread_shutdown = 0;
67 static char *conf_node = NULL;
68 static char *conf_service = NULL;
70 /* Must hold metrics_lock when calling this function. */
71 static int statsd_metric_set_unsafe (char const *name, int64_t value, /* {{{ */
75 statsd_metric_t *metric;
81 status = c_avl_get (metrics_tree, name, (void *) &metric);
84 metric->value = value;
85 metric->last_update = now;
91 metric = calloc (1, sizeof (*metric));
92 if ((key == NULL) || (metric == NULL))
100 metric->value = value;
101 metric->last_update = now;
103 status = c_avl_insert (metrics_tree, key, metric);
113 } /* }}} int statsd_metric_set_unsafe */
115 static int statsd_metric_set (char const *name, int64_t value, /* {{{ */
120 pthread_mutex_lock (&metrics_lock);
121 status = statsd_metric_set_unsafe (name, value, type);
122 pthread_mutex_unlock (&metrics_lock);
125 } /* }}} int statsd_metric_set */
127 static int statsd_metric_add (char const *name, int64_t delta, /* {{{ */
131 statsd_metric_t *metric;
135 pthread_mutex_lock (&metrics_lock);
137 status = c_avl_get (metrics_tree, name, (void *) &metric);
140 metric->value += delta;
141 metric->last_update = now;
143 pthread_mutex_unlock (&metrics_lock);
146 else /* no such value yet */
148 status = statsd_metric_set_unsafe (name, delta, type);
150 pthread_mutex_unlock (&metrics_lock);
153 } /* }}} int statsd_metric_add */
155 static int statsd_handle_counter (char const *name, /* {{{ */
156 char const *value_str,
163 if ((extra != NULL) && (extra[0] != '@'))
169 status = parse_value (extra + 1, &scale, DS_TYPE_GAUGE);
173 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
178 status = parse_value (value_str, &value, DS_TYPE_DERIVE);
182 if (value.derive < 1)
185 return (statsd_metric_add (name,
186 (int64_t) (((gauge_t) value.derive) / scale.gauge),
188 } /* }}} int statsd_handle_counter */
190 static int statsd_handle_gauge (char const *name, /* {{{ */
191 char const *value_str)
197 status = parse_value (value_str, &value, DS_TYPE_DERIVE);
201 if ((value_str[0] == '+') || (value_str[0] == '-'))
202 return (statsd_metric_add (name, (int64_t) value.derive, STATSD_GAUGE));
204 return (statsd_metric_set (name, (int64_t) value.derive, STATSD_GAUGE));
205 } /* }}} int statsd_handle_gauge */
207 static int statsd_handle_timer (char const *name, /* {{{ */
208 char const *value_str)
214 status = parse_value (value_str, &value, DS_TYPE_DERIVE);
218 return (statsd_metric_add (name, (int64_t) value.derive, STATSD_TIMER));
219 } /* }}} int statsd_handle_timer */
221 static int statsd_handle_set (char const *name __attribute__((unused)), /* {{{ */
222 char const *value_str __attribute__((unused)))
224 static c_complain_t c = C_COMPLAIN_INIT_STATIC;
226 c_complain (LOG_WARNING, &c,
227 "statsd plugin: Support for sets is not yet implemented.");
230 } /* }}} int statsd_handle_set */
232 static int statsd_parse_line (char *buffer) /* {{{ */
239 type = strchr (name, '|');
245 value = strrchr (name, ':');
251 extra = strchr (type, '|');
258 if (strcmp ("c", type) == 0)
259 return (statsd_handle_counter (name, value, extra));
261 /* extra is only valid for counters */
265 if (strcmp ("g", type) == 0)
266 return (statsd_handle_gauge (name, value));
267 else if (strcmp ("ms", type) == 0)
268 return (statsd_handle_timer (name, value));
269 else if (strcmp ("s", type) == 0)
270 return (statsd_handle_set (name, value));
273 } /* }}} void statsd_parse_line */
275 static void statsd_parse_buffer (char *buffer) /* {{{ */
278 char *saveptr = NULL;
282 (ptr = strtok_r (dummy, "\r\n", &saveptr)) != NULL;
285 char *line_orig = sstrdup (ptr);
288 status = statsd_parse_line (ptr);
290 ERROR ("statsd plugin: Unable to parse line: \"%s\"", line_orig);
294 } /* }}} void statsd_parse_buffer */
296 static void statsd_network_read (int fd) /* {{{ */
302 status = recv (fd, buffer, sizeof (buffer), /* flags = */ MSG_DONTWAIT);
307 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
310 ERROR ("statsd plugin: recv(2) failed: %s",
311 sstrerror (errno, errbuf, sizeof (errbuf)));
315 buffer_size = (size_t) status;
316 if (buffer_size >= sizeof (buffer))
317 buffer_size = sizeof (buffer) - 1;
318 buffer[buffer_size] = 0;
320 statsd_parse_buffer (buffer);
321 } /* }}} void statsd_network_read */
323 static int statsd_network_init (struct pollfd **ret_fds, /* {{{ */
326 struct pollfd *fds = NULL;
329 struct addrinfo ai_hints;
330 struct addrinfo *ai_list = NULL;
331 struct addrinfo *ai_ptr;
334 char const *node = (conf_node != NULL) ? conf_node : STATSD_DEFAULT_NODE;
335 char const *service = (conf_service != NULL)
336 ? conf_service : STATSD_DEFAULT_SERVICE;
338 memset (&ai_hints, 0, sizeof (ai_hints));
339 ai_hints.ai_flags = AI_PASSIVE;
341 ai_hints.ai_flags |= AI_ADDRCONFIG;
343 ai_hints.ai_family = AF_UNSPEC;
344 ai_hints.ai_socktype = SOCK_DGRAM;
346 status = getaddrinfo (node, service, &ai_hints, &ai_list);
349 ERROR ("statsd plugin: getaddrinfo (\"%s\", \"%s\") failed: %s",
350 node, service, gai_strerror (status));
354 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
359 char dbg_node[NI_MAXHOST];
360 char dbg_service[NI_MAXSERV];
362 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
366 ERROR ("statsd plugin: socket(2) failed: %s",
367 sstrerror (errno, errbuf, sizeof (errbuf)));
371 getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
372 dbg_node, sizeof (dbg_node), dbg_service, sizeof (dbg_service),
373 NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV);
374 DEBUG ("statsd plugin: Trying to bind to [%s]:%s ...", dbg_node, dbg_service);
376 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
380 ERROR ("statsd plugin: bind(2) failed: %s",
381 sstrerror (errno, errbuf, sizeof (errbuf)));
386 tmp = realloc (fds, sizeof (*fds) * (fds_num + 1));
389 ERROR ("statsd plugin: realloc failed.");
396 memset (tmp, 0, sizeof (*tmp));
398 tmp->events = POLLIN | POLLPRI;
401 freeaddrinfo (ai_list);
405 ERROR ("statsd plugin: Unable to create listening socket for [%s]:%s.",
406 (node != NULL) ? node : "::", service);
411 *ret_fds_num = fds_num;
413 } /* }}} int statsd_network_init */
415 static void *statsd_network_thread (void *args) /* {{{ */
417 struct pollfd *fds = NULL;
422 status = statsd_network_init (&fds, &fds_num);
425 ERROR ("statsd plugin: Unable to open listening sockets.");
426 pthread_exit ((void *) 0);
429 while (!network_thread_shutdown)
431 status = poll (fds, (nfds_t) fds_num, /* timeout = */ -1);
436 if ((errno == EINTR) || (errno == EAGAIN))
439 ERROR ("statsd plugin: poll(2) failed: %s",
440 sstrerror (errno, errbuf, sizeof (errbuf)));
444 for (i = 0; i < fds_num; i++)
446 if ((fds[i].revents & (POLLIN | POLLPRI)) == 0)
449 statsd_network_read (fds[i].fd);
452 } /* while (!network_thread_shutdown) */
455 for (i = 0; i < fds_num; i++)
460 } /* }}} void *statsd_network_thread */
462 static int statsd_config (oconfig_item_t *ci) /* {{{ */
466 for (i = 0; i < ci->children_num; i++)
468 oconfig_item_t *child = ci->children + i;
470 if (strcasecmp ("Host", child->key) == 0)
471 cf_util_get_string (child, &conf_node);
472 else if (strcasecmp ("Port", child->key) == 0)
473 cf_util_get_service (child, &conf_service);
474 /* TODO: Add configuration for Delete{Counters,Timers,Gauges} */
476 ERROR ("statsd plugin: The \"%s\" config option is not valid.",
481 } /* }}} int statsd_config */
483 static int statsd_init (void) /* {{{ */
485 pthread_mutex_lock (&metrics_lock);
486 if (metrics_tree == NULL)
487 metrics_tree = c_avl_create ((void *) strcasecmp);
489 if (!network_thread_running)
493 status = pthread_create (&network_thread,
495 statsd_network_thread,
500 pthread_mutex_unlock (&metrics_lock);
501 ERROR ("statsd plugin: pthread_create failed: %s",
502 sstrerror (errno, errbuf, sizeof (errbuf)));
506 network_thread_running = 1;
508 pthread_mutex_unlock (&metrics_lock);
511 } /* }}} int statsd_init */
513 static int statsd_metric_submit (char const *name, /* {{{ */
514 statsd_metric_t const *metric)
517 value_list_t vl = VALUE_LIST_INIT;
519 if (metric->type == STATSD_GAUGE)
520 values[0].gauge = (gauge_t) metric->value;
522 values[0].derive = (derive_t) metric->value;
526 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
527 sstrncpy (vl.plugin, "statsd", sizeof (vl.plugin));
529 if (metric->type == STATSD_GAUGE)
530 sstrncpy (vl.type, "gauge", sizeof (vl.type));
531 else if (metric->type == STATSD_TIMER)
532 sstrncpy (vl.type, "total_time_in_ms", sizeof (vl.type));
533 else /* if (metric->type == STATSD_COUNTER) */
534 sstrncpy (vl.type, "derive", sizeof (vl.type));
536 sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
538 return (plugin_dispatch_values (&vl));
539 } /* }}} int statsd_metric_submit */
541 static int statsd_read (void) /* {{{ */
545 statsd_metric_t *metric;
547 pthread_mutex_lock (&metrics_lock);
549 if (metrics_tree == NULL)
551 pthread_mutex_unlock (&metrics_lock);
555 i = c_avl_get_iterator (metrics_tree);
556 /* TODO: Delete legacy metrics */
557 while (c_avl_iterator_next (i, (void *) &name, (void *) &metric) == 0)
558 statsd_metric_submit (name, metric);
559 c_avl_iterator_destroy (i);
561 pthread_mutex_unlock (&metrics_lock);
564 } /* }}} int statsd_read */
566 static int statsd_shutdown (void) /* {{{ */
571 pthread_mutex_lock (&metrics_lock);
573 if (network_thread_running)
575 network_thread_shutdown = 1;
576 pthread_kill (network_thread, SIGTERM);
577 pthread_join (network_thread, /* retval = */ NULL);
579 network_thread_running = 0;
581 while (c_avl_pick (metrics_tree, &key, &value) == 0)
586 c_avl_destroy (metrics_tree);
590 sfree (conf_service);
592 pthread_mutex_unlock (&metrics_lock);
595 } /* }}} int statsd_shutdown */
597 void module_register (void)
599 plugin_register_complex_config ("statsd", statsd_config);
600 plugin_register_init ("statsd", statsd_init);
601 plugin_register_read ("statsd", statsd_read);
602 plugin_register_shutdown ("statsd", statsd_shutdown);
605 /* vim: set sw=2 sts=2 et fdm=marker : */