2 * collectd - src/gmond.c
3 * Copyright (C) 2009,2010 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>
30 #include "configfile.h"
31 #include "utils_avltree.h"
37 # include <sys/socket.h>
43 # include <netinet/in.h>
46 # include <arpa/inet.h>
52 #include <gm_protocol.h>
54 #ifndef IPV6_ADD_MEMBERSHIP
55 # ifdef IPV6_JOIN_GROUP
56 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
58 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
60 #endif /* !IP_ADD_MEMBERSHIP */
62 #ifdef GANGLIA_MAX_MESSAGE_LEN
63 # define BUFF_SIZE GANGLIA_MAX_MESSAGE_LEN
65 # define BUFF_SIZE 1400
71 struct sockaddr_storage addr;
74 typedef struct socket_entry_s socket_entry_t;
76 struct staging_entry_s
78 char key[2 * DATA_MAX_NAME_LEN];
82 typedef struct staging_entry_s staging_entry_t;
93 typedef struct metric_map_s metric_map_t;
95 #define MC_RECEIVE_GROUP_DEFAULT "239.2.11.71"
96 static char *mc_receive_group = NULL;
97 #define MC_RECEIVE_PORT_DEFAULT "8649"
98 static char *mc_receive_port = NULL;
100 static struct pollfd *mc_receive_sockets = NULL;
101 static size_t mc_receive_sockets_num = 0;
103 static socket_entry_t *mc_send_sockets = NULL;
104 static size_t mc_send_sockets_num = 0;
105 static pthread_mutex_t mc_send_sockets_lock = PTHREAD_MUTEX_INITIALIZER;
107 static int mc_receive_thread_loop = 0;
108 static int mc_receive_thread_running = 0;
109 static pthread_t mc_receive_thread_id;
111 static metric_map_t metric_map_default[] =
112 { /*---------------+-------------+-----------+-------------+------+-----*
113 * ganglia_name ! type ! type_inst ! data_source ! type ! idx *
114 *---------------+-------------+-----------+-------------+------+-----*/
115 { "load_one", "load", "", "shortterm", -1, -1 },
116 { "load_five", "load", "", "midterm", -1, -1 },
117 { "load_fifteen", "load", "", "longterm", -1, -1 },
118 { "cpu_user", "cpu", "user", "value", -1, -1 },
119 { "cpu_system", "cpu", "system", "value", -1, -1 },
120 { "cpu_idle", "cpu", "idle", "value", -1, -1 },
121 { "cpu_nice", "cpu", "nice", "value", -1, -1 },
122 { "cpu_wio", "cpu", "wait", "value", -1, -1 },
123 { "mem_free", "memory", "free", "value", -1, -1 },
124 { "mem_shared", "memory", "shared", "value", -1, -1 },
125 { "mem_buffers", "memory", "buffered", "value", -1, -1 },
126 { "mem_cached", "memory", "cached", "value", -1, -1 },
127 { "mem_total", "memory", "total", "value", -1, -1 },
128 { "bytes_in", "if_octets", "", "rx", -1, -1 },
129 { "bytes_out", "if_octets", "", "tx", -1, -1 },
130 { "pkts_in", "if_packets", "", "rx", -1, -1 },
131 { "pkts_out", "if_packets", "", "tx", -1, -1 }
133 static size_t metric_map_len_default = STATIC_ARRAY_SIZE (metric_map_default);
135 static metric_map_t *metric_map = NULL;
136 static size_t metric_map_len = 0;
138 static c_avl_tree_t *staging_tree;
139 static pthread_mutex_t staging_lock = PTHREAD_MUTEX_INITIALIZER;
141 static metric_map_t *metric_lookup (const char *key) /* {{{ */
147 /* Search the user-supplied table first.. */
149 map_len = metric_map_len;
150 for (i = 0; i < map_len; i++)
151 if (strcmp (map[i].ganglia_name, key) == 0)
154 /* .. and fall back to the built-in table if nothing is found. */
157 map = metric_map_default;
158 map_len = metric_map_len_default;
160 for (i = 0; i < map_len; i++)
161 if (strcmp (map[i].ganglia_name, key) == 0)
168 /* Look up the DS type and ds_index. */
169 if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) /* {{{ */
171 const data_set_t *ds;
173 ds = plugin_get_ds (map[i].type);
176 WARNING ("gmond plugin: Type not defined: %s", map[i].type);
180 if ((map[i].ds_name == NULL) && (ds->ds_num != 1))
182 WARNING ("gmond plugin: No data source name defined for metric %s, "
183 "but type %s has more than one data source.",
184 map[i].ganglia_name, map[i].type);
188 if (map[i].ds_name == NULL)
196 for (j = 0; j < ds->ds_num; j++)
197 if (strcasecmp (ds->ds[j].name, map[i].ds_name) == 0)
202 WARNING ("gmond plugin: There is no data source "
203 "named `%s' in type `%s'.",
204 map[i].ds_name, ds->type);
210 map[i].ds_type = ds->ds[map[i].ds_index].type;
211 } /* }}} if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) */
214 } /* }}} metric_map_t *metric_lookup */
216 static int create_sockets (socket_entry_t **ret_sockets, /* {{{ */
217 size_t *ret_sockets_num,
218 const char *node, const char *service, int listen)
220 struct addrinfo ai_hints;
221 struct addrinfo *ai_list;
222 struct addrinfo *ai_ptr;
225 socket_entry_t *sockets;
230 sockets = *ret_sockets;
231 sockets_num = *ret_sockets_num;
233 memset (&ai_hints, 0, sizeof (ai_hints));
234 ai_hints.ai_flags = 0;
236 ai_hints.ai_flags |= AI_PASSIVE;
239 ai_hints.ai_flags |= AI_ADDRCONFIG;
241 ai_hints.ai_family = AF_UNSPEC;
242 ai_hints.ai_socktype = SOCK_DGRAM;
243 ai_hints.ai_protocol = IPPROTO_UDP;
245 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
249 ERROR ("gmond plugin: getaddrinfo (%s, %s) failed: %s",
250 (node == NULL) ? "(null)" : node,
251 (service == NULL) ? "(null)" : service,
252 (ai_return == EAI_SYSTEM)
253 ? sstrerror (errno, errbuf, sizeof (errbuf))
254 : gai_strerror (ai_return));
258 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) /* {{{ */
262 tmp = realloc (sockets, (sockets_num + 1) * sizeof (*sockets));
265 ERROR ("gmond plugin: realloc failed.");
270 sockets[sockets_num].fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
271 ai_ptr->ai_protocol);
272 if (sockets[sockets_num].fd < 0)
275 ERROR ("gmond plugin: socket failed: %s",
276 sstrerror (errno, errbuf, sizeof (errbuf)));
280 assert (sizeof (sockets[sockets_num].addr) >= ai_ptr->ai_addrlen);
281 memcpy (&sockets[sockets_num].addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
282 sockets[sockets_num].addrlen = ai_ptr->ai_addrlen;
284 /* Sending socket: Open only one socket and don't bind it. */
294 setsockopt (sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR,
295 (void *) &yes, sizeof (yes));
298 status = bind (sockets[sockets_num].fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
302 ERROR ("gmond plugin: bind failed: %s",
303 sstrerror (errno, errbuf, sizeof (errbuf)));
304 close (sockets[sockets_num].fd);
308 if (ai_ptr->ai_family == AF_INET)
310 struct sockaddr_in *addr;
314 addr = (struct sockaddr_in *) ai_ptr->ai_addr;
316 if (!IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
323 setsockopt (sockets[sockets_num].fd, IPPROTO_IP, IP_MULTICAST_LOOP,
324 (void *) &loop, sizeof (loop));
326 memset (&mreq, 0, sizeof (mreq));
327 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
328 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
329 setsockopt (sockets[sockets_num].fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
330 (void *) &mreq, sizeof (mreq));
331 } /* if (ai_ptr->ai_family == AF_INET) */
332 else if (ai_ptr->ai_family == AF_INET6)
334 struct sockaddr_in6 *addr;
335 struct ipv6_mreq mreq;
338 addr = (struct sockaddr_in6 *) ai_ptr->ai_addr;
340 if (!IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
347 setsockopt (sockets[sockets_num].fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
348 (void *) &loop, sizeof (loop));
350 memset (&mreq, 0, sizeof (mreq));
351 memcpy (&mreq.ipv6mr_multiaddr,
352 &addr->sin6_addr, sizeof (addr->sin6_addr));
353 mreq.ipv6mr_interface = 0; /* any */
354 setsockopt (sockets[sockets_num].fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
355 (void *) &mreq, sizeof (mreq));
356 } /* if (ai_ptr->ai_family == AF_INET6) */
359 } /* }}} for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
361 freeaddrinfo (ai_list);
363 if ((*ret_sockets_num) >= sockets_num)
366 *ret_sockets = sockets;
367 *ret_sockets_num = sockets_num;
369 } /* }}} int create_sockets */
371 static int request_meta_data (const char *host, const char *name) /* {{{ */
373 Ganglia_metadata_msg msg;
374 char buffer[BUFF_SIZE];
375 unsigned int buffer_size;
379 memset (&msg, 0, sizeof (msg));
381 msg.id = gmetadata_request;
382 msg.Ganglia_metadata_msg_u.grequest.metric_id.host = strdup (host);
383 msg.Ganglia_metadata_msg_u.grequest.metric_id.name = strdup (name);
385 if ((msg.Ganglia_metadata_msg_u.grequest.metric_id.host == NULL)
386 || (msg.Ganglia_metadata_msg_u.grequest.metric_id.name == NULL))
388 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
389 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
393 memset (buffer, 0, sizeof (buffer));
394 xdrmem_create (&xdr, buffer, sizeof (buffer), XDR_ENCODE);
396 if (!xdr_Ganglia_metadata_msg (&xdr, &msg))
398 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
399 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
403 buffer_size = xdr_getpos (&xdr);
405 DEBUG ("gmond plugin: Requesting meta data for %s/%s.",
408 pthread_mutex_lock (&mc_send_sockets_lock);
409 for (i = 0; i < mc_send_sockets_num; i++)
410 sendto (mc_send_sockets[i].fd, buffer, (size_t) buffer_size,
412 (struct sockaddr *) &mc_send_sockets[i].addr,
413 mc_send_sockets[i].addrlen);
414 pthread_mutex_unlock (&mc_send_sockets_lock);
416 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
417 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
419 } /* }}} int request_meta_data */
421 static staging_entry_t *staging_entry_get (const char *host, /* {{{ */
423 const char *type, const char *type_instance,
426 char key[2 * DATA_MAX_NAME_LEN];
430 if (staging_tree == NULL)
433 ssnprintf (key, sizeof (key), "%s/%s/%s", host, type,
434 (type_instance != NULL) ? type_instance : "");
437 status = c_avl_get (staging_tree, key, (void *) &se);
441 /* insert new entry */
442 se = (staging_entry_t *) malloc (sizeof (*se));
445 memset (se, 0, sizeof (*se));
447 sstrncpy (se->key, key, sizeof (se->key));
450 se->vl.values = (value_t *) calloc (values_len, sizeof (*se->vl.values));
451 if (se->vl.values == NULL)
456 se->vl.values_len = values_len;
460 sstrncpy (se->vl.host, host, sizeof (se->vl.host));
461 sstrncpy (se->vl.plugin, "gmond", sizeof (se->vl.plugin));
462 sstrncpy (se->vl.type, type, sizeof (se->vl.type));
463 if (type_instance != NULL)
464 sstrncpy (se->vl.type_instance, type_instance,
465 sizeof (se->vl.type_instance));
467 status = c_avl_insert (staging_tree, se->key, se);
470 ERROR ("gmond plugin: c_avl_insert failed.");
471 sfree (se->vl.values);
477 } /* }}} staging_entry_t *staging_entry_get */
479 static int staging_entry_submit (const char *host, const char *name, /* {{{ */
483 value_t values[se->vl.values_len];
485 if (se->vl.interval == 0)
487 /* No meta data has been received for this metric yet. */
489 pthread_mutex_unlock (&staging_lock);
490 request_meta_data (host, name);
496 memcpy (values, se->vl.values, sizeof (values));
497 memcpy (&vl, &se->vl, sizeof (vl));
499 /* Unlock before calling `plugin_dispatch_values'.. */
500 pthread_mutex_unlock (&staging_lock);
504 plugin_dispatch_values (&vl);
507 } /* }}} int staging_entry_submit */
509 static int staging_entry_update (const char *host, const char *name, /* {{{ */
510 const char *type, const char *type_instance,
511 int ds_index, int ds_type, value_t value)
513 const data_set_t *ds;
516 ds = plugin_get_ds (type);
519 ERROR ("gmond plugin: Looking up type %s failed.", type);
523 if (ds->ds_num <= ds_index)
525 ERROR ("gmond plugin: Invalid index %i: %s has only %i data source(s).",
526 ds_index, ds->type, ds->ds_num);
530 pthread_mutex_lock (&staging_lock);
532 se = staging_entry_get (host, name, type, type_instance, ds->ds_num);
535 pthread_mutex_unlock (&staging_lock);
536 ERROR ("gmond plugin: staging_entry_get failed.");
539 if (se->vl.values_len != ds->ds_num)
541 pthread_mutex_unlock (&staging_lock);
545 if (ds_type == DS_TYPE_COUNTER)
546 se->vl.values[ds_index].counter += value.counter;
547 else if (ds_type == DS_TYPE_GAUGE)
548 se->vl.values[ds_index].gauge = value.gauge;
549 else if (ds_type == DS_TYPE_DERIVE)
550 se->vl.values[ds_index].derive += value.derive;
551 else if (ds_type == DS_TYPE_ABSOLUTE)
552 se->vl.values[ds_index].absolute = value.absolute;
556 se->flags |= (0x01 << ds_index);
558 /* Check if all values have been set and submit if so. */
559 if (se->flags == ((0x01 << se->vl.values_len) - 1))
561 /* `staging_lock' is unlocked in `staging_entry_submit'. */
562 staging_entry_submit (host, name, se);
566 pthread_mutex_unlock (&staging_lock);
570 } /* }}} int staging_entry_update */
572 static int mc_handle_value_msg (Ganglia_value_msg *msg) /* {{{ */
578 value_t value_counter;
580 value_t value_derive;
582 /* Fill in `host', `name', `value_counter', and `value_gauge' according to
583 * the value type, or return with an error. */
584 switch (msg->id) /* {{{ */
588 Ganglia_gmetric_uint msg_uint;
590 msg_uint = msg->Ganglia_value_msg_u.gu_int;
592 host = msg_uint.metric_id.host;
593 name = msg_uint.metric_id.name;
594 value_counter.counter = (counter_t) msg_uint.ui;
595 value_gauge.gauge = (gauge_t) msg_uint.ui;
596 value_derive.derive = (derive_t) msg_uint.ui;
602 Ganglia_gmetric_string msg_string;
605 msg_string = msg->Ganglia_value_msg_u.gstr;
607 host = msg_string.metric_id.host;
608 name = msg_string.metric_id.name;
610 status = parse_value (msg_string.str, &value_derive, DS_TYPE_DERIVE);
612 value_derive.derive = -1;
614 status = parse_value (msg_string.str, &value_gauge, DS_TYPE_GAUGE);
616 value_gauge.gauge = NAN;
618 status = parse_value (msg_string.str, &value_counter, DS_TYPE_COUNTER);
620 value_counter.counter = 0;
627 Ganglia_gmetric_float msg_float;
629 msg_float = msg->Ganglia_value_msg_u.gf;
631 host = msg_float.metric_id.host;
632 name = msg_float.metric_id.name;
633 value_counter.counter = (counter_t) msg_float.f;
634 value_gauge.gauge = (gauge_t) msg_float.f;
635 value_derive.derive = (derive_t) msg_float.f;
641 Ganglia_gmetric_double msg_double;
643 msg_double = msg->Ganglia_value_msg_u.gd;
645 host = msg_double.metric_id.host;
646 name = msg_double.metric_id.name;
647 value_counter.counter = (counter_t) msg_double.d;
648 value_gauge.gauge = (gauge_t) msg_double.d;
649 value_derive.derive = (derive_t) msg_double.d;
653 DEBUG ("gmond plugin: Value type not handled: %i", msg->id);
655 } /* }}} switch (msg->id) */
657 assert (host != NULL);
658 assert (name != NULL);
660 map = metric_lookup (name);
665 if ((map->ds_type == DS_TYPE_COUNTER)
666 || (map->ds_type == DS_TYPE_ABSOLUTE))
667 val_copy = value_counter;
668 if (map->ds_type == DS_TYPE_GAUGE)
669 val_copy = value_gauge;
670 else if (map->ds_type == DS_TYPE_DERIVE)
671 val_copy = value_derive;
675 return (staging_entry_update (host, name,
676 map->type, map->type_instance,
677 map->ds_index, map->ds_type,
681 DEBUG ("gmond plugin: Cannot find a translation for %s.", name);
683 } /* }}} int mc_handle_value_msg */
685 static int mc_handle_metadata_msg (Ganglia_metadata_msg *msg) /* {{{ */
691 Ganglia_metadatadef msg_meta;
693 const data_set_t *ds;
696 msg_meta = msg->Ganglia_metadata_msg_u.gfull;
698 if (msg_meta.metric.tmax <= 0)
701 map = metric_lookup (msg_meta.metric_id.name);
704 DEBUG ("gmond plugin: Not handling meta data %s.",
705 msg_meta.metric_id.name);
709 ds = plugin_get_ds (map->type);
712 WARNING ("gmond plugin: Could not find data set %s.", map->type);
716 DEBUG ("gmond plugin: Received meta data for %s/%s.",
717 msg_meta.metric_id.host, msg_meta.metric_id.name);
719 pthread_mutex_lock (&staging_lock);
720 se = staging_entry_get (msg_meta.metric_id.host,
721 msg_meta.metric_id.name,
722 map->type, map->type_instance,
725 se->vl.interval = TIME_T_TO_CDTIME_T (msg_meta.metric.tmax);
726 pthread_mutex_unlock (&staging_lock);
730 ERROR ("gmond plugin: staging_entry_get failed.");
744 } /* }}} int mc_handle_metadata_msg */
746 static int mc_handle_metric (void *buffer, size_t buffer_size) /* {{{ */
749 Ganglia_msg_formats format;
751 xdrmem_create (&xdr, buffer, buffer_size, XDR_DECODE);
753 xdr_Ganglia_msg_formats (&xdr, &format);
754 xdr_setpos (&xdr, 0);
766 Ganglia_value_msg msg;
768 memset (&msg, 0, sizeof (msg));
769 if (xdr_Ganglia_value_msg (&xdr, &msg))
770 mc_handle_value_msg (&msg);
775 case gmetadata_request:
777 Ganglia_metadata_msg msg;
778 memset (&msg, 0, sizeof (msg));
779 if (xdr_Ganglia_metadata_msg (&xdr, &msg))
780 mc_handle_metadata_msg (&msg);
785 DEBUG ("gmond plugin: Unknown format: %i", format);
787 } /* switch (format) */
791 } /* }}} int mc_handle_metric */
793 static int mc_handle_socket (struct pollfd *p) /* {{{ */
795 char buffer[BUFF_SIZE];
798 if ((p->revents & (POLLIN | POLLPRI)) == 0)
804 buffer_size = recv (p->fd, buffer, sizeof (buffer), /* flags = */ 0);
805 if (buffer_size <= 0)
808 ERROR ("gmond plugin: recv failed: %s",
809 sstrerror (errno, errbuf, sizeof (errbuf)));
814 mc_handle_metric (buffer, (size_t) buffer_size);
816 } /* }}} int mc_handle_socket */
818 static void *mc_receive_thread (void *arg) /* {{{ */
820 socket_entry_t *mc_receive_socket_entries;
824 mc_receive_socket_entries = NULL;
825 status = create_sockets (&mc_receive_socket_entries, &mc_receive_sockets_num,
826 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
827 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
831 ERROR ("gmond plugin: create_sockets failed.");
832 return ((void *) -1);
835 mc_receive_sockets = (struct pollfd *) calloc (mc_receive_sockets_num,
836 sizeof (*mc_receive_sockets));
837 if (mc_receive_sockets == NULL)
839 ERROR ("gmond plugin: calloc failed.");
840 for (i = 0; i < mc_receive_sockets_num; i++)
841 close (mc_receive_socket_entries[i].fd);
842 free (mc_receive_socket_entries);
843 mc_receive_socket_entries = NULL;
844 mc_receive_sockets_num = 0;
845 return ((void *) -1);
848 for (i = 0; i < mc_receive_sockets_num; i++)
850 mc_receive_sockets[i].fd = mc_receive_socket_entries[i].fd;
851 mc_receive_sockets[i].events = POLLIN | POLLPRI;
852 mc_receive_sockets[i].revents = 0;
855 while (mc_receive_thread_loop != 0)
857 status = poll (mc_receive_sockets, mc_receive_sockets_num, -1);
863 ERROR ("gmond plugin: poll failed: %s",
864 sstrerror (errno, errbuf, sizeof (errbuf)));
868 for (i = 0; i < mc_receive_sockets_num; i++)
870 if (mc_receive_sockets[i].revents != 0)
871 mc_handle_socket (mc_receive_sockets + i);
873 } /* while (mc_receive_thread_loop != 0) */
876 } /* }}} void *mc_receive_thread */
878 static int mc_receive_thread_start (void) /* {{{ */
882 if (mc_receive_thread_running != 0)
885 mc_receive_thread_loop = 1;
887 status = plugin_thread_create (&mc_receive_thread_id, /* attr = */ NULL,
888 mc_receive_thread, /* args = */ NULL);
891 ERROR ("gmond plugin: Starting receive thread failed.");
892 mc_receive_thread_loop = 0;
896 mc_receive_thread_running = 1;
898 } /* }}} int start_receive_thread */
900 static int mc_receive_thread_stop (void) /* {{{ */
902 if (mc_receive_thread_running == 0)
905 mc_receive_thread_loop = 0;
907 INFO ("gmond plugin: Stopping receive thread.");
908 pthread_kill (mc_receive_thread_id, SIGTERM);
909 pthread_join (mc_receive_thread_id, /* return value = */ NULL);
910 memset (&mc_receive_thread_id, 0, sizeof (mc_receive_thread_id));
912 mc_receive_thread_running = 0;
915 } /* }}} int mc_receive_thread_stop */
921 * MCReceiveFrom "239.2.11.71" "8649"
922 * <Metric "load_one">
924 * [TypeInstance "foo"]
929 static int gmond_config_set_string (oconfig_item_t *ci, char **str) /* {{{ */
933 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
935 WARNING ("gmond plugin: The `%s' option needs "
936 "exactly one string argument.", ci->key);
940 tmp = strdup (ci->values[0].value.string);
943 ERROR ("gmond plugin: strdup failed.");
950 } /* }}} int gmond_config_set_string */
952 static int gmond_config_add_metric (oconfig_item_t *ci) /* {{{ */
957 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
959 WARNING ("gmond plugin: `Metric' blocks need "
960 "exactly one string argument.");
964 map = realloc (metric_map, (metric_map_len + 1) * sizeof (*metric_map));
967 ERROR ("gmond plugin: realloc failed.");
971 map = metric_map + metric_map_len;
973 memset (map, 0, sizeof (*map));
975 map->type_instance = NULL;
980 map->ganglia_name = strdup (ci->values[0].value.string);
981 if (map->ganglia_name == NULL)
983 ERROR ("gmond plugin: strdup failed.");
987 for (i = 0; i < ci->children_num; i++)
989 oconfig_item_t *child = ci->children + i;
990 if (strcasecmp ("Type", child->key) == 0)
991 gmond_config_set_string (child, &map->type);
992 else if (strcasecmp ("TypeInstance", child->key) == 0)
993 gmond_config_set_string (child, &map->type_instance);
994 else if (strcasecmp ("DataSource", child->key) == 0)
995 gmond_config_set_string (child, &map->ds_name);
998 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
1003 if (map->type == NULL)
1005 ERROR ("gmond plugin: No type is set for metric %s.",
1007 sfree (map->ganglia_name);
1008 sfree (map->type_instance);
1014 } /* }}} int gmond_config_add_metric */
1016 static int gmond_config_set_address (oconfig_item_t *ci, /* {{{ */
1017 char **ret_addr, char **ret_port)
1022 if ((ci->values_num != 1) && (ci->values_num != 2))
1024 WARNING ("gmond plugin: The `%s' config option needs "
1025 "one or two string arguments.",
1029 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
1030 || ((ci->values_num == 2)
1031 && (ci->values[1].type != OCONFIG_TYPE_STRING)))
1033 WARNING ("gmond plugin: The `%s' config option needs "
1034 "one or two string arguments.",
1039 addr = strdup (ci->values[0].value.string);
1040 if (ci->values_num == 2)
1041 port = strdup (ci->values[1].value.string);
1045 if ((addr == NULL) || ((ci->values_num == 2) && (port == NULL)))
1047 ERROR ("gmond plugin: strdup failed.");
1060 } /* }}} int gmond_config_set_address */
1062 static int gmond_config (oconfig_item_t *ci) /* {{{ */
1066 for (i = 0; i < ci->children_num; i++)
1068 oconfig_item_t *child = ci->children + i;
1069 if (strcasecmp ("MCReceiveFrom", child->key) == 0)
1070 gmond_config_set_address (child, &mc_receive_group, &mc_receive_port);
1071 else if (strcasecmp ("Metric", child->key) == 0)
1072 gmond_config_add_metric (child);
1075 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
1081 } /* }}} int gmond_config */
1083 static int gmond_init (void) /* {{{ */
1085 create_sockets (&mc_send_sockets, &mc_send_sockets_num,
1086 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
1087 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
1090 staging_tree = c_avl_create ((void *) strcmp);
1091 if (staging_tree == NULL)
1093 ERROR ("gmond plugin: c_avl_create failed.");
1097 mc_receive_thread_start ();
1100 } /* }}} int gmond_init */
1102 static int gmond_shutdown (void) /* {{{ */
1106 mc_receive_thread_stop ();
1108 pthread_mutex_lock (&mc_send_sockets_lock);
1109 for (i = 0; i < mc_send_sockets_num; i++)
1111 close (mc_send_sockets[i].fd);
1112 mc_send_sockets[i].fd = -1;
1114 sfree (mc_send_sockets);
1115 mc_send_sockets_num = 0;
1116 pthread_mutex_unlock (&mc_send_sockets_lock);
1120 } /* }}} int gmond_shutdown */
1122 void module_register (void)
1124 plugin_register_complex_config ("gmond", gmond_config);
1125 plugin_register_init ("gmond", gmond_init);
1126 plugin_register_shutdown ("gmond", gmond_shutdown);
1129 /* vim: set sw=2 sts=2 et fdm=marker : */