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;
88 #define MC_RECEIVE_PORT_DEFAULT "8649"
89 static char *mc_receive_port;
91 static struct pollfd *mc_receive_sockets;
92 static size_t mc_receive_sockets_num;
94 static socket_entry_t *mc_send_sockets;
95 static size_t mc_send_sockets_num;
96 static pthread_mutex_t mc_send_sockets_lock = PTHREAD_MUTEX_INITIALIZER;
98 static int mc_receive_thread_loop;
99 static int mc_receive_thread_running;
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;
126 static size_t metric_map_len;
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) {
220 ERROR("gmond plugin: getaddrinfo (%s, %s) failed: %s",
221 (node == NULL) ? "(null)" : node,
222 (service == NULL) ? "(null)" : service,
223 (ai_return == EAI_SYSTEM) ? STRERRNO : gai_strerror(ai_return));
227 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
228 ai_ptr = ai_ptr->ai_next) /* {{{ */
232 tmp = realloc(sockets, (sockets_num + 1) * sizeof(*sockets));
234 ERROR("gmond plugin: realloc failed.");
239 sockets[sockets_num].fd =
240 socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
241 if (sockets[sockets_num].fd < 0) {
242 ERROR("gmond plugin: socket failed: %s", STRERRNO);
246 assert(sizeof(sockets[sockets_num].addr) >= ai_ptr->ai_addrlen);
247 memcpy(&sockets[sockets_num].addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
248 sockets[sockets_num].addrlen = ai_ptr->ai_addrlen;
250 /* Sending socket: Open only one socket and don't bind it. */
257 status = setsockopt(sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR,
258 (void *)&yes, sizeof(yes));
260 WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO);
264 status = bind(sockets[sockets_num].fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
266 ERROR("gmond plugin: bind failed: %s", STRERRNO);
267 close(sockets[sockets_num].fd);
271 if (ai_ptr->ai_family == AF_INET) {
272 struct sockaddr_in *addr;
275 addr = (struct sockaddr_in *)ai_ptr->ai_addr;
277 if (!IN_MULTICAST(ntohl(addr->sin_addr.s_addr))) {
283 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IP,
284 IP_MULTICAST_LOOP, (void *)&loop, sizeof(loop));
286 WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO);
289 struct ip_mreq mreq = {.imr_multiaddr.s_addr = addr->sin_addr.s_addr,
290 .imr_interface.s_addr = htonl(INADDR_ANY)};
292 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IP,
293 IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq));
295 WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO);
297 } /* if (ai_ptr->ai_family == AF_INET) */
298 else if (ai_ptr->ai_family == AF_INET6) {
299 struct sockaddr_in6 *addr;
302 addr = (struct sockaddr_in6 *)ai_ptr->ai_addr;
304 if (!IN6_IS_ADDR_MULTICAST(&addr->sin6_addr)) {
310 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IPV6,
311 IPV6_MULTICAST_LOOP, (void *)&loop, sizeof(loop));
313 WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO);
316 struct ipv6_mreq mreq = {
317 .ipv6mr_interface = 0 /* any */
320 memcpy(&mreq.ipv6mr_multiaddr, &addr->sin6_addr, sizeof(addr->sin6_addr));
321 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IPV6,
322 IPV6_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq));
324 WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO);
326 } /* if (ai_ptr->ai_family == AF_INET6) */
329 } /* }}} for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
331 freeaddrinfo(ai_list);
333 if (sockets_num == 0) {
338 *ret_sockets = sockets;
339 *ret_sockets_num = sockets_num;
341 } /* }}} int create_sockets */
343 static int request_meta_data(const char *host, const char *name) /* {{{ */
345 Ganglia_metadata_msg msg = {0};
346 char buffer[BUFF_SIZE] = {0};
347 unsigned int buffer_size;
350 msg.id = gmetadata_request;
351 msg.Ganglia_metadata_msg_u.grequest.metric_id.host = strdup(host);
352 msg.Ganglia_metadata_msg_u.grequest.metric_id.name = strdup(name);
354 if ((msg.Ganglia_metadata_msg_u.grequest.metric_id.host == NULL) ||
355 (msg.Ganglia_metadata_msg_u.grequest.metric_id.name == NULL)) {
356 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
357 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
361 xdrmem_create(&xdr, buffer, sizeof(buffer), XDR_ENCODE);
363 if (!xdr_Ganglia_metadata_msg(&xdr, &msg)) {
364 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
365 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
369 buffer_size = xdr_getpos(&xdr);
371 DEBUG("gmond plugin: Requesting meta data for %s/%s.", host, name);
373 pthread_mutex_lock(&mc_send_sockets_lock);
374 for (size_t i = 0; i < mc_send_sockets_num; i++) {
376 sendto(mc_send_sockets[i].fd, buffer, (size_t)buffer_size,
377 /* flags = */ 0, (struct sockaddr *)&mc_send_sockets[i].addr,
378 mc_send_sockets[i].addrlen);
380 ERROR("gmond plugin: sendto(2) failed: %s", STRERRNO);
384 pthread_mutex_unlock(&mc_send_sockets_lock);
386 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
387 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
389 } /* }}} int request_meta_data */
391 static staging_entry_t *staging_entry_get(const char *host, /* {{{ */
392 const char *name, const char *type,
393 const char *type_instance,
395 char key[2 * DATA_MAX_NAME_LEN];
399 if (staging_tree == NULL)
402 snprintf(key, sizeof(key), "%s/%s/%s", host, type,
403 (type_instance != NULL) ? type_instance : "");
406 status = c_avl_get(staging_tree, key, (void *)&se);
410 /* insert new entry */
411 se = calloc(1, sizeof(*se));
415 sstrncpy(se->key, key, sizeof(se->key));
418 se->vl.values = (value_t *)calloc(values_len, sizeof(*se->vl.values));
419 if (se->vl.values == NULL) {
423 se->vl.values_len = values_len;
427 sstrncpy(se->vl.host, host, sizeof(se->vl.host));
428 sstrncpy(se->vl.plugin, "gmond", sizeof(se->vl.plugin));
429 sstrncpy(se->vl.type, type, sizeof(se->vl.type));
430 if (type_instance != NULL)
431 sstrncpy(se->vl.type_instance, type_instance, sizeof(se->vl.type_instance));
433 status = c_avl_insert(staging_tree, se->key, se);
435 ERROR("gmond plugin: c_avl_insert failed.");
436 sfree(se->vl.values);
442 } /* }}} staging_entry_t *staging_entry_get */
444 static int staging_entry_update(const char *host, const char *name, /* {{{ */
445 const char *type, const char *type_instance,
446 size_t ds_index, int ds_type, value_t value) {
447 const data_set_t *ds;
450 ds = plugin_get_ds(type);
452 ERROR("gmond plugin: Looking up type %s failed.", type);
456 if (ds->ds_num <= ds_index) {
457 ERROR("gmond plugin: Invalid index %" PRIsz ": %s has only %" PRIsz
459 ds_index, ds->type, ds->ds_num);
463 pthread_mutex_lock(&staging_lock);
465 se = staging_entry_get(host, name, type, type_instance, ds->ds_num);
467 pthread_mutex_unlock(&staging_lock);
468 ERROR("gmond plugin: staging_entry_get failed.");
471 if (se->vl.values_len != ds->ds_num) {
472 pthread_mutex_unlock(&staging_lock);
476 if (ds_type == DS_TYPE_COUNTER)
477 se->vl.values[ds_index].counter += value.counter;
478 else if (ds_type == DS_TYPE_GAUGE)
479 se->vl.values[ds_index].gauge = value.gauge;
480 else if (ds_type == DS_TYPE_DERIVE)
481 se->vl.values[ds_index].derive += value.derive;
482 else if (ds_type == DS_TYPE_ABSOLUTE)
483 se->vl.values[ds_index].absolute = value.absolute;
487 se->flags |= (0x01 << ds_index);
489 /* Check if all data sources have been set. If not, return here. */
490 if (se->flags != ((0x01 << se->vl.values_len) - 1)) {
491 pthread_mutex_unlock(&staging_lock);
495 /* Check if the interval of this metric is known. If not, request meta data
497 if (se->vl.interval == 0) {
498 /* No meta data has been received for this metric yet. */
500 pthread_mutex_unlock(&staging_lock);
502 request_meta_data(host, name);
506 plugin_dispatch_values(&se->vl);
509 pthread_mutex_unlock(&staging_lock);
512 } /* }}} int staging_entry_update */
514 static int mc_handle_value_msg(Ganglia_value_msg *msg) /* {{{ */
520 value_t value_counter;
522 value_t value_derive;
524 /* Fill in `host', `name', `value_counter', and `value_gauge' according to
525 * the value type, or return with an error. */
526 switch (msg->id) /* {{{ */
529 Ganglia_gmetric_uint msg_uint;
531 msg_uint = msg->Ganglia_value_msg_u.gu_int;
533 host = msg_uint.metric_id.host;
534 name = msg_uint.metric_id.name;
535 value_counter.counter = (counter_t)msg_uint.ui;
536 value_gauge.gauge = (gauge_t)msg_uint.ui;
537 value_derive.derive = (derive_t)msg_uint.ui;
541 case gmetric_string: {
542 Ganglia_gmetric_string msg_string;
545 msg_string = msg->Ganglia_value_msg_u.gstr;
547 host = msg_string.metric_id.host;
548 name = msg_string.metric_id.name;
550 status = parse_value(msg_string.str, &value_derive, DS_TYPE_DERIVE);
552 value_derive.derive = -1;
554 status = parse_value(msg_string.str, &value_gauge, DS_TYPE_GAUGE);
556 value_gauge.gauge = NAN;
558 status = parse_value(msg_string.str, &value_counter, DS_TYPE_COUNTER);
560 value_counter.counter = 0;
565 case gmetric_float: {
566 Ganglia_gmetric_float msg_float;
568 msg_float = msg->Ganglia_value_msg_u.gf;
570 host = msg_float.metric_id.host;
571 name = msg_float.metric_id.name;
572 value_counter.counter = (counter_t)msg_float.f;
573 value_gauge.gauge = (gauge_t)msg_float.f;
574 value_derive.derive = (derive_t)msg_float.f;
578 case gmetric_double: {
579 Ganglia_gmetric_double msg_double;
581 msg_double = msg->Ganglia_value_msg_u.gd;
583 host = msg_double.metric_id.host;
584 name = msg_double.metric_id.name;
585 value_counter.counter = (counter_t)msg_double.d;
586 value_gauge.gauge = (gauge_t)msg_double.d;
587 value_derive.derive = (derive_t)msg_double.d;
591 DEBUG("gmond plugin: Value type not handled: %i", msg->id);
593 } /* }}} switch (msg->id) */
595 assert(host != NULL);
596 assert(name != NULL);
598 map = metric_lookup(name);
602 if ((map->ds_type == DS_TYPE_COUNTER) || (map->ds_type == DS_TYPE_ABSOLUTE))
603 val_copy = value_counter;
604 else if (map->ds_type == DS_TYPE_GAUGE)
605 val_copy = value_gauge;
606 else if (map->ds_type == DS_TYPE_DERIVE)
607 val_copy = value_derive;
611 return staging_entry_update(host, name, map->type, map->type_instance,
612 map->ds_index, map->ds_type, val_copy);
615 DEBUG("gmond plugin: Cannot find a translation for %s.", name);
617 } /* }}} int mc_handle_value_msg */
619 static int mc_handle_metadata_msg(Ganglia_metadata_msg *msg) /* {{{ */
622 case gmetadata_full: {
623 Ganglia_metadatadef msg_meta;
625 const data_set_t *ds;
628 msg_meta = msg->Ganglia_metadata_msg_u.gfull;
630 if (msg_meta.metric.tmax == 0)
633 map = metric_lookup(msg_meta.metric_id.name);
635 DEBUG("gmond plugin: Not handling meta data %s.",
636 msg_meta.metric_id.name);
640 ds = plugin_get_ds(map->type);
642 WARNING("gmond plugin: Could not find data set %s.", map->type);
646 DEBUG("gmond plugin: Received meta data for %s/%s.",
647 msg_meta.metric_id.host, msg_meta.metric_id.name);
649 pthread_mutex_lock(&staging_lock);
650 se = staging_entry_get(msg_meta.metric_id.host, msg_meta.metric_id.name,
651 map->type, map->type_instance, ds->ds_num);
653 se->vl.interval = TIME_T_TO_CDTIME_T(msg_meta.metric.tmax);
654 pthread_mutex_unlock(&staging_lock);
657 ERROR("gmond plugin: staging_entry_get failed.");
664 default: { return -1; }
668 } /* }}} int mc_handle_metadata_msg */
670 static int mc_handle_metric(void *buffer, size_t buffer_size) /* {{{ */
673 Ganglia_msg_formats format;
675 xdrmem_create(&xdr, buffer, buffer_size, XDR_DECODE);
677 xdr_Ganglia_msg_formats(&xdr, &format);
687 case gmetric_double: {
688 Ganglia_value_msg msg = {0};
690 if (xdr_Ganglia_value_msg(&xdr, &msg))
691 mc_handle_value_msg(&msg);
696 case gmetadata_request: {
697 Ganglia_metadata_msg msg = {0};
698 if (xdr_Ganglia_metadata_msg(&xdr, &msg))
699 mc_handle_metadata_msg(&msg);
704 DEBUG("gmond plugin: Unknown format: %i", format);
706 } /* switch (format) */
709 } /* }}} int mc_handle_metric */
711 static int mc_handle_socket(struct pollfd *p) /* {{{ */
713 char buffer[BUFF_SIZE];
716 if ((p->revents & (POLLIN | POLLPRI)) == 0) {
721 buffer_size = recv(p->fd, buffer, sizeof(buffer), /* flags = */ 0);
722 if (buffer_size <= 0) {
723 ERROR("gmond plugin: recv failed: %s", STRERRNO);
728 mc_handle_metric(buffer, (size_t)buffer_size);
730 } /* }}} int mc_handle_socket */
732 static void *mc_receive_thread(void *arg) /* {{{ */
734 socket_entry_t *mc_receive_socket_entries;
737 mc_receive_socket_entries = NULL;
738 status = create_sockets(
739 &mc_receive_socket_entries, &mc_receive_sockets_num,
740 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
741 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
744 ERROR("gmond plugin: create_sockets failed.");
748 mc_receive_sockets = (struct pollfd *)calloc(mc_receive_sockets_num,
749 sizeof(*mc_receive_sockets));
750 if (mc_receive_sockets == NULL) {
751 ERROR("gmond plugin: calloc failed.");
752 for (size_t i = 0; i < mc_receive_sockets_num; i++)
753 close(mc_receive_socket_entries[i].fd);
754 free(mc_receive_socket_entries);
755 mc_receive_socket_entries = NULL;
756 mc_receive_sockets_num = 0;
760 for (size_t i = 0; i < mc_receive_sockets_num; i++) {
761 mc_receive_sockets[i].fd = mc_receive_socket_entries[i].fd;
762 mc_receive_sockets[i].events = POLLIN | POLLPRI;
763 mc_receive_sockets[i].revents = 0;
766 while (mc_receive_thread_loop != 0) {
767 status = poll(mc_receive_sockets, mc_receive_sockets_num, -1);
771 ERROR("gmond plugin: poll failed: %s", STRERRNO);
775 for (size_t i = 0; i < mc_receive_sockets_num; i++) {
776 if (mc_receive_sockets[i].revents != 0)
777 mc_handle_socket(mc_receive_sockets + i);
779 } /* while (mc_receive_thread_loop != 0) */
781 free(mc_receive_socket_entries);
783 } /* }}} void *mc_receive_thread */
785 static int mc_receive_thread_start(void) /* {{{ */
789 if (mc_receive_thread_running != 0)
792 mc_receive_thread_loop = 1;
795 plugin_thread_create(&mc_receive_thread_id, /* attr = */ NULL,
796 mc_receive_thread, /* args = */ NULL, "gmond recv");
798 ERROR("gmond plugin: Starting receive thread failed.");
799 mc_receive_thread_loop = 0;
803 mc_receive_thread_running = 1;
805 } /* }}} int start_receive_thread */
807 static int mc_receive_thread_stop(void) /* {{{ */
809 if (mc_receive_thread_running == 0)
812 mc_receive_thread_loop = 0;
814 INFO("gmond plugin: Stopping receive thread.");
815 pthread_kill(mc_receive_thread_id, SIGTERM);
816 pthread_join(mc_receive_thread_id, /* return value = */ NULL);
817 memset(&mc_receive_thread_id, 0, sizeof(mc_receive_thread_id));
819 mc_receive_thread_running = 0;
822 } /* }}} int mc_receive_thread_stop */
828 * MCReceiveFrom "239.2.11.71" "8649"
829 * <Metric "load_one">
831 * [TypeInstance "foo"]
836 static int gmond_config_set_string(oconfig_item_t *ci, char **str) /* {{{ */
840 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
841 WARNING("gmond plugin: The `%s' option needs "
842 "exactly one string argument.",
847 tmp = strdup(ci->values[0].value.string);
849 ERROR("gmond plugin: strdup failed.");
856 } /* }}} int gmond_config_set_string */
858 static int gmond_config_add_metric(oconfig_item_t *ci) /* {{{ */
862 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
863 WARNING("gmond plugin: `Metric' blocks need "
864 "exactly one string argument.");
868 map = realloc(metric_map, (metric_map_len + 1) * sizeof(*metric_map));
870 ERROR("gmond plugin: realloc failed.");
874 map = metric_map + metric_map_len;
876 memset(map, 0, sizeof(*map));
878 map->type_instance = NULL;
883 map->ganglia_name = strdup(ci->values[0].value.string);
884 if (map->ganglia_name == NULL) {
885 ERROR("gmond plugin: strdup failed.");
889 for (int i = 0; i < ci->children_num; i++) {
890 oconfig_item_t *child = ci->children + i;
891 if (strcasecmp("Type", child->key) == 0)
892 gmond_config_set_string(child, &map->type);
893 else if (strcasecmp("TypeInstance", child->key) == 0)
894 gmond_config_set_string(child, &map->type_instance);
895 else if (strcasecmp("DataSource", child->key) == 0)
896 gmond_config_set_string(child, &map->ds_name);
898 WARNING("gmond plugin: Unknown configuration option `%s' ignored.",
903 if (map->type == NULL) {
904 ERROR("gmond plugin: No type is set for metric %s.", map->ganglia_name);
905 sfree(map->ganglia_name);
906 sfree(map->type_instance);
912 } /* }}} int gmond_config_add_metric */
914 static int gmond_config_set_address(oconfig_item_t *ci, /* {{{ */
915 char **ret_addr, char **ret_port) {
919 if ((ci->values_num != 1) && (ci->values_num != 2)) {
920 WARNING("gmond plugin: The `%s' config option needs "
921 "one or two string arguments.",
925 if ((ci->values[0].type != OCONFIG_TYPE_STRING) ||
926 ((ci->values_num == 2) && (ci->values[1].type != OCONFIG_TYPE_STRING))) {
927 WARNING("gmond plugin: The `%s' config option needs "
928 "one or two string arguments.",
933 addr = strdup(ci->values[0].value.string);
934 if (ci->values_num == 2)
935 port = strdup(ci->values[1].value.string);
939 if ((addr == NULL) || ((ci->values_num == 2) && (port == NULL))) {
940 ERROR("gmond plugin: strdup failed.");
953 } /* }}} int gmond_config_set_address */
955 static int gmond_config(oconfig_item_t *ci) /* {{{ */
957 for (int i = 0; i < ci->children_num; i++) {
958 oconfig_item_t *child = ci->children + i;
959 if (strcasecmp("MCReceiveFrom", child->key) == 0)
960 gmond_config_set_address(child, &mc_receive_group, &mc_receive_port);
961 else if (strcasecmp("Metric", child->key) == 0)
962 gmond_config_add_metric(child);
964 WARNING("gmond plugin: Unknown configuration option `%s' ignored.",
970 } /* }}} int gmond_config */
972 static int gmond_init(void) /* {{{ */
975 &mc_send_sockets, &mc_send_sockets_num,
976 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
977 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
980 staging_tree = c_avl_create((int (*)(const void *, const void *))strcmp);
981 if (staging_tree == NULL) {
982 ERROR("gmond plugin: c_avl_create failed.");
986 mc_receive_thread_start();
989 } /* }}} int gmond_init */
991 static int gmond_shutdown(void) /* {{{ */
993 mc_receive_thread_stop();
995 pthread_mutex_lock(&mc_send_sockets_lock);
996 for (size_t i = 0; i < mc_send_sockets_num; i++) {
997 close(mc_send_sockets[i].fd);
998 mc_send_sockets[i].fd = -1;
1000 sfree(mc_send_sockets);
1001 mc_send_sockets_num = 0;
1002 pthread_mutex_unlock(&mc_send_sockets_lock);
1005 } /* }}} int gmond_shutdown */
1007 void module_register(void) {
1008 plugin_register_complex_config("gmond", gmond_config);
1009 plugin_register_init("gmond", gmond_init);
1010 plugin_register_shutdown("gmond", gmond_shutdown);