2 * collectd - src/gmond.c
3 * Copyright (C) 2009-2015 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
31 #include "utils_avltree.h"
37 #include <netinet/in.h>
40 #include <arpa/inet.h>
46 #include <gm_protocol.h>
48 #ifndef IPV6_ADD_MEMBERSHIP
49 #ifdef IPV6_JOIN_GROUP
50 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
52 #error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
54 #endif /* !IP_ADD_MEMBERSHIP */
56 #ifdef GANGLIA_MAX_MESSAGE_LEN
57 #define BUFF_SIZE GANGLIA_MAX_MESSAGE_LEN
59 #define BUFF_SIZE 1400
62 struct socket_entry_s {
64 struct sockaddr_storage addr;
67 typedef struct socket_entry_s socket_entry_t;
69 struct staging_entry_s {
70 char key[2 * DATA_MAX_NAME_LEN];
74 typedef struct staging_entry_s staging_entry_t;
84 typedef struct metric_map_s metric_map_t;
86 #define MC_RECEIVE_GROUP_DEFAULT "239.2.11.71"
87 static char *mc_receive_group = NULL;
88 #define MC_RECEIVE_PORT_DEFAULT "8649"
89 static char *mc_receive_port = NULL;
91 static struct pollfd *mc_receive_sockets = NULL;
92 static size_t mc_receive_sockets_num = 0;
94 static socket_entry_t *mc_send_sockets = NULL;
95 static size_t mc_send_sockets_num = 0;
96 static pthread_mutex_t mc_send_sockets_lock = PTHREAD_MUTEX_INITIALIZER;
98 static int mc_receive_thread_loop = 0;
99 static int mc_receive_thread_running = 0;
100 static pthread_t mc_receive_thread_id;
102 static metric_map_t metric_map_default[] =
103 {/*---------------+-------------+-----------+-------------+------+-----*
104 * ganglia_name ! type ! type_inst ! data_source ! type ! idx *
105 *---------------+-------------+-----------+-------------+------+-----*/
106 {"load_one", "load", "", "shortterm", -1, -1},
107 {"load_five", "load", "", "midterm", -1, -1},
108 {"load_fifteen", "load", "", "longterm", -1, -1},
109 {"cpu_user", "cpu", "user", "value", -1, -1},
110 {"cpu_system", "cpu", "system", "value", -1, -1},
111 {"cpu_idle", "cpu", "idle", "value", -1, -1},
112 {"cpu_nice", "cpu", "nice", "value", -1, -1},
113 {"cpu_wio", "cpu", "wait", "value", -1, -1},
114 {"mem_free", "memory", "free", "value", -1, -1},
115 {"mem_shared", "memory", "shared", "value", -1, -1},
116 {"mem_buffers", "memory", "buffered", "value", -1, -1},
117 {"mem_cached", "memory", "cached", "value", -1, -1},
118 {"mem_total", "memory", "total", "value", -1, -1},
119 {"bytes_in", "if_octets", "", "rx", -1, -1},
120 {"bytes_out", "if_octets", "", "tx", -1, -1},
121 {"pkts_in", "if_packets", "", "rx", -1, -1},
122 {"pkts_out", "if_packets", "", "tx", -1, -1}};
123 static size_t metric_map_len_default = STATIC_ARRAY_SIZE(metric_map_default);
125 static metric_map_t *metric_map = NULL;
126 static size_t metric_map_len = 0;
128 static c_avl_tree_t *staging_tree;
129 static pthread_mutex_t staging_lock = PTHREAD_MUTEX_INITIALIZER;
131 static metric_map_t *metric_lookup(const char *key) /* {{{ */
137 /* Search the user-supplied table first.. */
139 map_len = metric_map_len;
140 for (i = 0; i < map_len; i++)
141 if (strcmp(map[i].ganglia_name, key) == 0)
144 /* .. and fall back to the built-in table if nothing is found. */
146 map = metric_map_default;
147 map_len = metric_map_len_default;
149 for (i = 0; i < map_len; i++)
150 if (strcmp(map[i].ganglia_name, key) == 0)
157 /* Look up the DS type and ds_index. */
158 if (map[i].ds_type < 0) /* {{{ */
160 const data_set_t *ds;
162 ds = plugin_get_ds(map[i].type);
164 WARNING("gmond plugin: Type not defined: %s", map[i].type);
168 if ((map[i].ds_name == NULL) && (ds->ds_num != 1)) {
169 WARNING("gmond plugin: No data source name defined for metric %s, "
170 "but type %s has more than one data source.",
171 map[i].ganglia_name, map[i].type);
175 if (map[i].ds_name == NULL) {
180 for (j = 0; j < ds->ds_num; j++)
181 if (strcasecmp(ds->ds[j].name, map[i].ds_name) == 0)
184 if (j >= ds->ds_num) {
185 WARNING("gmond plugin: There is no data source "
186 "named `%s' in type `%s'.",
187 map[i].ds_name, ds->type);
193 map[i].ds_type = ds->ds[map[i].ds_index].type;
194 } /* }}} if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) */
197 } /* }}} metric_map_t *metric_lookup */
199 static int create_sockets(socket_entry_t **ret_sockets, /* {{{ */
200 size_t *ret_sockets_num, const char *node,
201 const char *service, int listen) {
202 struct addrinfo *ai_list;
205 socket_entry_t *sockets = NULL;
206 size_t sockets_num = 0;
210 if (*ret_sockets != NULL)
213 struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
214 .ai_flags = AI_ADDRCONFIG | AI_PASSIVE,
215 .ai_protocol = IPPROTO_UDP,
216 .ai_socktype = SOCK_DGRAM};
218 ai_return = getaddrinfo(node, service, &ai_hints, &ai_list);
219 if (ai_return != 0) {
221 ERROR("gmond plugin: getaddrinfo (%s, %s) failed: %s",
222 (node == NULL) ? "(null)" : node,
223 (service == NULL) ? "(null)" : service,
224 (ai_return == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
225 : gai_strerror(ai_return));
229 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
230 ai_ptr = ai_ptr->ai_next) /* {{{ */
234 tmp = realloc(sockets, (sockets_num + 1) * sizeof(*sockets));
236 ERROR("gmond plugin: realloc failed.");
241 sockets[sockets_num].fd =
242 socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
243 if (sockets[sockets_num].fd < 0) {
245 ERROR("gmond plugin: socket failed: %s",
246 sstrerror(errno, errbuf, sizeof(errbuf)));
250 assert(sizeof(sockets[sockets_num].addr) >= ai_ptr->ai_addrlen);
251 memcpy(&sockets[sockets_num].addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
252 sockets[sockets_num].addrlen = ai_ptr->ai_addrlen;
254 /* Sending socket: Open only one socket and don't bind it. */
261 status = setsockopt(sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR,
262 (void *)&yes, sizeof(yes));
265 WARNING("gmond plugin: setsockopt(2) failed: %s",
266 sstrerror(errno, errbuf, sizeof(errbuf)));
270 status = bind(sockets[sockets_num].fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
273 ERROR("gmond plugin: bind failed: %s",
274 sstrerror(errno, errbuf, sizeof(errbuf)));
275 close(sockets[sockets_num].fd);
279 if (ai_ptr->ai_family == AF_INET) {
280 struct sockaddr_in *addr;
283 addr = (struct sockaddr_in *)ai_ptr->ai_addr;
285 if (!IN_MULTICAST(ntohl(addr->sin_addr.s_addr))) {
291 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IP,
292 IP_MULTICAST_LOOP, (void *)&loop, sizeof(loop));
295 WARNING("gmond plugin: setsockopt(2) failed: %s",
296 sstrerror(errno, errbuf, sizeof(errbuf)));
299 struct ip_mreq mreq = {.imr_multiaddr.s_addr = addr->sin_addr.s_addr,
300 .imr_interface.s_addr = htonl(INADDR_ANY)};
302 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IP,
303 IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq));
306 WARNING("gmond plugin: setsockopt(2) failed: %s",
307 sstrerror(errno, errbuf, sizeof(errbuf)));
309 } /* if (ai_ptr->ai_family == AF_INET) */
310 else if (ai_ptr->ai_family == AF_INET6) {
311 struct sockaddr_in6 *addr;
314 addr = (struct sockaddr_in6 *)ai_ptr->ai_addr;
316 if (!IN6_IS_ADDR_MULTICAST(&addr->sin6_addr)) {
322 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IPV6,
323 IPV6_MULTICAST_LOOP, (void *)&loop, sizeof(loop));
326 WARNING("gmond plugin: setsockopt(2) failed: %s",
327 sstrerror(errno, errbuf, sizeof(errbuf)));
330 struct ipv6_mreq mreq = {
331 .ipv6mr_interface = 0 /* any */
334 memcpy(&mreq.ipv6mr_multiaddr, &addr->sin6_addr, sizeof(addr->sin6_addr));
335 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IPV6,
336 IPV6_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq));
339 WARNING("gmond plugin: setsockopt(2) failed: %s",
340 sstrerror(errno, errbuf, sizeof(errbuf)));
342 } /* if (ai_ptr->ai_family == AF_INET6) */
345 } /* }}} for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
347 freeaddrinfo(ai_list);
349 if (sockets_num == 0) {
354 *ret_sockets = sockets;
355 *ret_sockets_num = sockets_num;
357 } /* }}} int create_sockets */
359 static int request_meta_data(const char *host, const char *name) /* {{{ */
361 Ganglia_metadata_msg msg = {0};
362 char buffer[BUFF_SIZE] = {0};
363 unsigned int buffer_size;
366 msg.id = gmetadata_request;
367 msg.Ganglia_metadata_msg_u.grequest.metric_id.host = strdup(host);
368 msg.Ganglia_metadata_msg_u.grequest.metric_id.name = strdup(name);
370 if ((msg.Ganglia_metadata_msg_u.grequest.metric_id.host == NULL) ||
371 (msg.Ganglia_metadata_msg_u.grequest.metric_id.name == NULL)) {
372 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
373 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
377 xdrmem_create(&xdr, buffer, sizeof(buffer), XDR_ENCODE);
379 if (!xdr_Ganglia_metadata_msg(&xdr, &msg)) {
380 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
381 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
385 buffer_size = xdr_getpos(&xdr);
387 DEBUG("gmond plugin: Requesting meta data for %s/%s.", host, name);
389 pthread_mutex_lock(&mc_send_sockets_lock);
390 for (size_t i = 0; i < mc_send_sockets_num; i++) {
392 sendto(mc_send_sockets[i].fd, buffer, (size_t)buffer_size,
393 /* flags = */ 0, (struct sockaddr *)&mc_send_sockets[i].addr,
394 mc_send_sockets[i].addrlen);
397 ERROR("gmond plugin: sendto(2) failed: %s",
398 sstrerror(errno, errbuf, sizeof(errbuf)));
402 pthread_mutex_unlock(&mc_send_sockets_lock);
404 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
405 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
407 } /* }}} int request_meta_data */
409 static staging_entry_t *staging_entry_get(const char *host, /* {{{ */
410 const char *name, const char *type,
411 const char *type_instance,
413 char key[2 * DATA_MAX_NAME_LEN];
417 if (staging_tree == NULL)
420 snprintf(key, sizeof(key), "%s/%s/%s", host, type,
421 (type_instance != NULL) ? type_instance : "");
424 status = c_avl_get(staging_tree, key, (void *)&se);
428 /* insert new entry */
429 se = calloc(1, sizeof(*se));
433 sstrncpy(se->key, key, sizeof(se->key));
436 se->vl.values = (value_t *)calloc(values_len, sizeof(*se->vl.values));
437 if (se->vl.values == NULL) {
441 se->vl.values_len = values_len;
445 sstrncpy(se->vl.host, host, sizeof(se->vl.host));
446 sstrncpy(se->vl.plugin, "gmond", sizeof(se->vl.plugin));
447 sstrncpy(se->vl.type, type, sizeof(se->vl.type));
448 if (type_instance != NULL)
449 sstrncpy(se->vl.type_instance, type_instance, sizeof(se->vl.type_instance));
451 status = c_avl_insert(staging_tree, se->key, se);
453 ERROR("gmond plugin: c_avl_insert failed.");
454 sfree(se->vl.values);
460 } /* }}} staging_entry_t *staging_entry_get */
462 static int staging_entry_update(const char *host, const char *name, /* {{{ */
463 const char *type, const char *type_instance,
464 size_t ds_index, int ds_type, value_t value) {
465 const data_set_t *ds;
468 ds = plugin_get_ds(type);
470 ERROR("gmond plugin: Looking up type %s failed.", type);
474 if (ds->ds_num <= ds_index) {
475 ERROR("gmond plugin: Invalid index %zu: %s has only %zu data source(s).",
476 ds_index, ds->type, ds->ds_num);
480 pthread_mutex_lock(&staging_lock);
482 se = staging_entry_get(host, name, type, type_instance, ds->ds_num);
484 pthread_mutex_unlock(&staging_lock);
485 ERROR("gmond plugin: staging_entry_get failed.");
488 if (se->vl.values_len != ds->ds_num) {
489 pthread_mutex_unlock(&staging_lock);
493 if (ds_type == DS_TYPE_COUNTER)
494 se->vl.values[ds_index].counter += value.counter;
495 else if (ds_type == DS_TYPE_GAUGE)
496 se->vl.values[ds_index].gauge = value.gauge;
497 else if (ds_type == DS_TYPE_DERIVE)
498 se->vl.values[ds_index].derive += value.derive;
499 else if (ds_type == DS_TYPE_ABSOLUTE)
500 se->vl.values[ds_index].absolute = value.absolute;
504 se->flags |= (0x01 << ds_index);
506 /* Check if all data sources have been set. If not, return here. */
507 if (se->flags != ((0x01 << se->vl.values_len) - 1)) {
508 pthread_mutex_unlock(&staging_lock);
512 /* Check if the interval of this metric is known. If not, request meta data
514 if (se->vl.interval == 0) {
515 /* No meta data has been received for this metric yet. */
517 pthread_mutex_unlock(&staging_lock);
519 request_meta_data(host, name);
523 plugin_dispatch_values(&se->vl);
526 pthread_mutex_unlock(&staging_lock);
529 } /* }}} int staging_entry_update */
531 static int mc_handle_value_msg(Ganglia_value_msg *msg) /* {{{ */
537 value_t value_counter;
539 value_t value_derive;
541 /* Fill in `host', `name', `value_counter', and `value_gauge' according to
542 * the value type, or return with an error. */
543 switch (msg->id) /* {{{ */
546 Ganglia_gmetric_uint msg_uint;
548 msg_uint = msg->Ganglia_value_msg_u.gu_int;
550 host = msg_uint.metric_id.host;
551 name = msg_uint.metric_id.name;
552 value_counter.counter = (counter_t)msg_uint.ui;
553 value_gauge.gauge = (gauge_t)msg_uint.ui;
554 value_derive.derive = (derive_t)msg_uint.ui;
558 case gmetric_string: {
559 Ganglia_gmetric_string msg_string;
562 msg_string = msg->Ganglia_value_msg_u.gstr;
564 host = msg_string.metric_id.host;
565 name = msg_string.metric_id.name;
567 status = parse_value(msg_string.str, &value_derive, DS_TYPE_DERIVE);
569 value_derive.derive = -1;
571 status = parse_value(msg_string.str, &value_gauge, DS_TYPE_GAUGE);
573 value_gauge.gauge = NAN;
575 status = parse_value(msg_string.str, &value_counter, DS_TYPE_COUNTER);
577 value_counter.counter = 0;
582 case gmetric_float: {
583 Ganglia_gmetric_float msg_float;
585 msg_float = msg->Ganglia_value_msg_u.gf;
587 host = msg_float.metric_id.host;
588 name = msg_float.metric_id.name;
589 value_counter.counter = (counter_t)msg_float.f;
590 value_gauge.gauge = (gauge_t)msg_float.f;
591 value_derive.derive = (derive_t)msg_float.f;
595 case gmetric_double: {
596 Ganglia_gmetric_double msg_double;
598 msg_double = msg->Ganglia_value_msg_u.gd;
600 host = msg_double.metric_id.host;
601 name = msg_double.metric_id.name;
602 value_counter.counter = (counter_t)msg_double.d;
603 value_gauge.gauge = (gauge_t)msg_double.d;
604 value_derive.derive = (derive_t)msg_double.d;
608 DEBUG("gmond plugin: Value type not handled: %i", msg->id);
610 } /* }}} switch (msg->id) */
612 assert(host != NULL);
613 assert(name != NULL);
615 map = metric_lookup(name);
619 if ((map->ds_type == DS_TYPE_COUNTER) || (map->ds_type == DS_TYPE_ABSOLUTE))
620 val_copy = value_counter;
621 else if (map->ds_type == DS_TYPE_GAUGE)
622 val_copy = value_gauge;
623 else if (map->ds_type == DS_TYPE_DERIVE)
624 val_copy = value_derive;
628 return staging_entry_update(host, name, map->type, map->type_instance,
629 map->ds_index, map->ds_type, val_copy);
632 DEBUG("gmond plugin: Cannot find a translation for %s.", name);
634 } /* }}} int mc_handle_value_msg */
636 static int mc_handle_metadata_msg(Ganglia_metadata_msg *msg) /* {{{ */
639 case gmetadata_full: {
640 Ganglia_metadatadef msg_meta;
642 const data_set_t *ds;
645 msg_meta = msg->Ganglia_metadata_msg_u.gfull;
647 if (msg_meta.metric.tmax == 0)
650 map = metric_lookup(msg_meta.metric_id.name);
652 DEBUG("gmond plugin: Not handling meta data %s.",
653 msg_meta.metric_id.name);
657 ds = plugin_get_ds(map->type);
659 WARNING("gmond plugin: Could not find data set %s.", map->type);
663 DEBUG("gmond plugin: Received meta data for %s/%s.",
664 msg_meta.metric_id.host, msg_meta.metric_id.name);
666 pthread_mutex_lock(&staging_lock);
667 se = staging_entry_get(msg_meta.metric_id.host, msg_meta.metric_id.name,
668 map->type, map->type_instance, ds->ds_num);
670 se->vl.interval = TIME_T_TO_CDTIME_T(msg_meta.metric.tmax);
671 pthread_mutex_unlock(&staging_lock);
674 ERROR("gmond plugin: staging_entry_get failed.");
681 default: { return -1; }
685 } /* }}} int mc_handle_metadata_msg */
687 static int mc_handle_metric(void *buffer, size_t buffer_size) /* {{{ */
690 Ganglia_msg_formats format;
692 xdrmem_create(&xdr, buffer, buffer_size, XDR_DECODE);
694 xdr_Ganglia_msg_formats(&xdr, &format);
704 case gmetric_double: {
705 Ganglia_value_msg msg = {0};
707 if (xdr_Ganglia_value_msg(&xdr, &msg))
708 mc_handle_value_msg(&msg);
713 case gmetadata_request: {
714 Ganglia_metadata_msg msg = {0};
715 if (xdr_Ganglia_metadata_msg(&xdr, &msg))
716 mc_handle_metadata_msg(&msg);
721 DEBUG("gmond plugin: Unknown format: %i", format);
723 } /* switch (format) */
726 } /* }}} int mc_handle_metric */
728 static int mc_handle_socket(struct pollfd *p) /* {{{ */
730 char buffer[BUFF_SIZE];
733 if ((p->revents & (POLLIN | POLLPRI)) == 0) {
738 buffer_size = recv(p->fd, buffer, sizeof(buffer), /* flags = */ 0);
739 if (buffer_size <= 0) {
741 ERROR("gmond plugin: recv failed: %s",
742 sstrerror(errno, errbuf, sizeof(errbuf)));
747 mc_handle_metric(buffer, (size_t)buffer_size);
749 } /* }}} int mc_handle_socket */
751 static void *mc_receive_thread(void *arg) /* {{{ */
753 socket_entry_t *mc_receive_socket_entries;
756 mc_receive_socket_entries = NULL;
757 status = create_sockets(
758 &mc_receive_socket_entries, &mc_receive_sockets_num,
759 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
760 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
763 ERROR("gmond plugin: create_sockets failed.");
767 mc_receive_sockets = (struct pollfd *)calloc(mc_receive_sockets_num,
768 sizeof(*mc_receive_sockets));
769 if (mc_receive_sockets == NULL) {
770 ERROR("gmond plugin: calloc failed.");
771 for (size_t i = 0; i < mc_receive_sockets_num; i++)
772 close(mc_receive_socket_entries[i].fd);
773 free(mc_receive_socket_entries);
774 mc_receive_socket_entries = NULL;
775 mc_receive_sockets_num = 0;
779 for (size_t i = 0; i < mc_receive_sockets_num; i++) {
780 mc_receive_sockets[i].fd = mc_receive_socket_entries[i].fd;
781 mc_receive_sockets[i].events = POLLIN | POLLPRI;
782 mc_receive_sockets[i].revents = 0;
785 while (mc_receive_thread_loop != 0) {
786 status = poll(mc_receive_sockets, mc_receive_sockets_num, -1);
791 ERROR("gmond plugin: poll failed: %s",
792 sstrerror(errno, errbuf, sizeof(errbuf)));
796 for (size_t i = 0; i < mc_receive_sockets_num; i++) {
797 if (mc_receive_sockets[i].revents != 0)
798 mc_handle_socket(mc_receive_sockets + i);
800 } /* while (mc_receive_thread_loop != 0) */
802 free(mc_receive_socket_entries);
804 } /* }}} void *mc_receive_thread */
806 static int mc_receive_thread_start(void) /* {{{ */
810 if (mc_receive_thread_running != 0)
813 mc_receive_thread_loop = 1;
816 plugin_thread_create(&mc_receive_thread_id, /* attr = */ NULL,
817 mc_receive_thread, /* args = */ NULL, "gmond recv");
819 ERROR("gmond plugin: Starting receive thread failed.");
820 mc_receive_thread_loop = 0;
824 mc_receive_thread_running = 1;
826 } /* }}} int start_receive_thread */
828 static int mc_receive_thread_stop(void) /* {{{ */
830 if (mc_receive_thread_running == 0)
833 mc_receive_thread_loop = 0;
835 INFO("gmond plugin: Stopping receive thread.");
836 pthread_kill(mc_receive_thread_id, SIGTERM);
837 pthread_join(mc_receive_thread_id, /* return value = */ NULL);
838 memset(&mc_receive_thread_id, 0, sizeof(mc_receive_thread_id));
840 mc_receive_thread_running = 0;
843 } /* }}} int mc_receive_thread_stop */
849 * MCReceiveFrom "239.2.11.71" "8649"
850 * <Metric "load_one">
852 * [TypeInstance "foo"]
857 static int gmond_config_set_string(oconfig_item_t *ci, char **str) /* {{{ */
861 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
862 WARNING("gmond plugin: The `%s' option needs "
863 "exactly one string argument.",
868 tmp = strdup(ci->values[0].value.string);
870 ERROR("gmond plugin: strdup failed.");
877 } /* }}} int gmond_config_set_string */
879 static int gmond_config_add_metric(oconfig_item_t *ci) /* {{{ */
883 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
884 WARNING("gmond plugin: `Metric' blocks need "
885 "exactly one string argument.");
889 map = realloc(metric_map, (metric_map_len + 1) * sizeof(*metric_map));
891 ERROR("gmond plugin: realloc failed.");
895 map = metric_map + metric_map_len;
897 memset(map, 0, sizeof(*map));
899 map->type_instance = NULL;
904 map->ganglia_name = strdup(ci->values[0].value.string);
905 if (map->ganglia_name == NULL) {
906 ERROR("gmond plugin: strdup failed.");
910 for (int i = 0; i < ci->children_num; i++) {
911 oconfig_item_t *child = ci->children + i;
912 if (strcasecmp("Type", child->key) == 0)
913 gmond_config_set_string(child, &map->type);
914 else if (strcasecmp("TypeInstance", child->key) == 0)
915 gmond_config_set_string(child, &map->type_instance);
916 else if (strcasecmp("DataSource", child->key) == 0)
917 gmond_config_set_string(child, &map->ds_name);
919 WARNING("gmond plugin: Unknown configuration option `%s' ignored.",
924 if (map->type == NULL) {
925 ERROR("gmond plugin: No type is set for metric %s.", map->ganglia_name);
926 sfree(map->ganglia_name);
927 sfree(map->type_instance);
933 } /* }}} int gmond_config_add_metric */
935 static int gmond_config_set_address(oconfig_item_t *ci, /* {{{ */
936 char **ret_addr, char **ret_port) {
940 if ((ci->values_num != 1) && (ci->values_num != 2)) {
941 WARNING("gmond plugin: The `%s' config option needs "
942 "one or two string arguments.",
946 if ((ci->values[0].type != OCONFIG_TYPE_STRING) ||
947 ((ci->values_num == 2) && (ci->values[1].type != OCONFIG_TYPE_STRING))) {
948 WARNING("gmond plugin: The `%s' config option needs "
949 "one or two string arguments.",
954 addr = strdup(ci->values[0].value.string);
955 if (ci->values_num == 2)
956 port = strdup(ci->values[1].value.string);
960 if ((addr == NULL) || ((ci->values_num == 2) && (port == NULL))) {
961 ERROR("gmond plugin: strdup failed.");
974 } /* }}} int gmond_config_set_address */
976 static int gmond_config(oconfig_item_t *ci) /* {{{ */
978 for (int i = 0; i < ci->children_num; i++) {
979 oconfig_item_t *child = ci->children + i;
980 if (strcasecmp("MCReceiveFrom", child->key) == 0)
981 gmond_config_set_address(child, &mc_receive_group, &mc_receive_port);
982 else if (strcasecmp("Metric", child->key) == 0)
983 gmond_config_add_metric(child);
985 WARNING("gmond plugin: Unknown configuration option `%s' ignored.",
991 } /* }}} int gmond_config */
993 static int gmond_init(void) /* {{{ */
996 &mc_send_sockets, &mc_send_sockets_num,
997 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
998 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
1001 staging_tree = c_avl_create((int (*)(const void *, const void *))strcmp);
1002 if (staging_tree == NULL) {
1003 ERROR("gmond plugin: c_avl_create failed.");
1007 mc_receive_thread_start();
1010 } /* }}} int gmond_init */
1012 static int gmond_shutdown(void) /* {{{ */
1014 mc_receive_thread_stop();
1016 pthread_mutex_lock(&mc_send_sockets_lock);
1017 for (size_t i = 0; i < mc_send_sockets_num; i++) {
1018 close(mc_send_sockets[i].fd);
1019 mc_send_sockets[i].fd = -1;
1021 sfree(mc_send_sockets);
1022 mc_send_sockets_num = 0;
1023 pthread_mutex_unlock(&mc_send_sockets_lock);
1026 } /* }}} int gmond_shutdown */
1028 void module_register(void) {
1029 plugin_register_complex_config("gmond", gmond_config);
1030 plugin_register_init("gmond", gmond_init);
1031 plugin_register_shutdown("gmond", gmond_shutdown);