2 * collectd - src/gmond.c
3 * Copyright (C) 2009,2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
25 #include "configfile.h"
26 #include "utils_avltree.h"
32 # include <sys/socket.h>
38 # include <netinet/in.h>
41 # include <arpa/inet.h>
47 #include <gm_protocol.h>
49 #ifndef IPV6_ADD_MEMBERSHIP
50 # ifdef IPV6_JOIN_GROUP
51 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
53 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
55 #endif /* !IP_ADD_MEMBERSHIP */
57 #ifdef GANGLIA_MAX_MESSAGE_LEN
58 # define BUFF_SIZE GANGLIA_MAX_MESSAGE_LEN
60 # define BUFF_SIZE 1400
66 struct sockaddr_storage addr;
69 typedef struct socket_entry_s socket_entry_t;
71 struct staging_entry_s
73 char key[2 * DATA_MAX_NAME_LEN];
77 typedef struct staging_entry_s staging_entry_t;
88 typedef struct metric_map_s metric_map_t;
90 #define MC_RECEIVE_GROUP_DEFAULT "239.2.11.71"
91 static char *mc_receive_group = NULL;
92 #define MC_RECEIVE_PORT_DEFAULT "8649"
93 static char *mc_receive_port = NULL;
95 static struct pollfd *mc_receive_sockets = NULL;
96 static size_t mc_receive_sockets_num = 0;
98 static socket_entry_t *mc_send_sockets = NULL;
99 static size_t mc_send_sockets_num = 0;
100 static pthread_mutex_t mc_send_sockets_lock = PTHREAD_MUTEX_INITIALIZER;
102 static int mc_receive_thread_loop = 0;
103 static int mc_receive_thread_running = 0;
104 static pthread_t mc_receive_thread_id;
106 static metric_map_t metric_map_default[] =
107 { /*---------------+-------------+-----------+-------------+------+-----*
108 * ganglia_name ! type ! type_inst ! data_source ! type ! idx *
109 *---------------+-------------+-----------+-------------+------+-----*/
110 { "load_one", "load", "", "shortterm", -1, -1 },
111 { "load_five", "load", "", "midterm", -1, -1 },
112 { "load_fifteen", "load", "", "longterm", -1, -1 },
113 { "cpu_user", "cpu", "user", "value", -1, -1 },
114 { "cpu_system", "cpu", "system", "value", -1, -1 },
115 { "cpu_idle", "cpu", "idle", "value", -1, -1 },
116 { "cpu_nice", "cpu", "nice", "value", -1, -1 },
117 { "cpu_wio", "cpu", "wait", "value", -1, -1 },
118 { "mem_free", "memory", "free", "value", -1, -1 },
119 { "mem_shared", "memory", "shared", "value", -1, -1 },
120 { "mem_buffers", "memory", "buffered", "value", -1, -1 },
121 { "mem_cached", "memory", "cached", "value", -1, -1 },
122 { "mem_total", "memory", "total", "value", -1, -1 },
123 { "bytes_in", "if_octets", "", "rx", -1, -1 },
124 { "bytes_out", "if_octets", "", "tx", -1, -1 },
125 { "pkts_in", "if_packets", "", "rx", -1, -1 },
126 { "pkts_out", "if_packets", "", "tx", -1, -1 }
128 static size_t metric_map_len_default = STATIC_ARRAY_SIZE (metric_map_default);
130 static metric_map_t *metric_map = NULL;
131 static size_t metric_map_len = 0;
133 static c_avl_tree_t *staging_tree;
134 static pthread_mutex_t staging_lock = PTHREAD_MUTEX_INITIALIZER;
136 static metric_map_t *metric_lookup (const char *key) /* {{{ */
142 /* Search the user-supplied table first.. */
144 map_len = metric_map_len;
145 for (i = 0; i < map_len; i++)
146 if (strcmp (map[i].ganglia_name, key) == 0)
149 /* .. and fall back to the built-in table if nothing is found. */
152 map = metric_map_default;
153 map_len = metric_map_len_default;
155 for (i = 0; i < map_len; i++)
156 if (strcmp (map[i].ganglia_name, key) == 0)
163 /* Look up the DS type and ds_index. */
164 if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) /* {{{ */
166 const data_set_t *ds;
168 ds = plugin_get_ds (map[i].type);
171 WARNING ("gmond plugin: Type not defined: %s", map[i].type);
175 if ((map[i].ds_name == NULL) && (ds->ds_num != 1))
177 WARNING ("gmond plugin: No data source name defined for metric %s, "
178 "but type %s has more than one data source.",
179 map[i].ganglia_name, map[i].type);
183 if (map[i].ds_name == NULL)
191 for (j = 0; j < ds->ds_num; j++)
192 if (strcasecmp (ds->ds[j].name, map[i].ds_name) == 0)
197 WARNING ("gmond plugin: There is no data source "
198 "named `%s' in type `%s'.",
199 map[i].ds_name, ds->type);
205 map[i].ds_type = ds->ds[map[i].ds_index].type;
206 } /* }}} if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) */
209 } /* }}} metric_map_t *metric_lookup */
211 static int create_sockets (socket_entry_t **ret_sockets, /* {{{ */
212 size_t *ret_sockets_num,
213 const char *node, const char *service, int listen)
215 struct addrinfo ai_hints;
216 struct addrinfo *ai_list;
217 struct addrinfo *ai_ptr;
220 socket_entry_t *sockets;
225 sockets = *ret_sockets;
226 sockets_num = *ret_sockets_num;
228 memset (&ai_hints, 0, sizeof (ai_hints));
229 ai_hints.ai_flags = 0;
231 ai_hints.ai_flags |= AI_PASSIVE;
234 ai_hints.ai_flags |= AI_ADDRCONFIG;
236 ai_hints.ai_family = AF_UNSPEC;
237 ai_hints.ai_socktype = SOCK_DGRAM;
238 ai_hints.ai_protocol = IPPROTO_UDP;
240 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
244 ERROR ("gmond plugin: getaddrinfo (%s, %s) failed: %s",
245 (node == NULL) ? "(null)" : node,
246 (service == NULL) ? "(null)" : service,
247 (ai_return == EAI_SYSTEM)
248 ? sstrerror (errno, errbuf, sizeof (errbuf))
249 : gai_strerror (ai_return));
253 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) /* {{{ */
257 tmp = realloc (sockets, (sockets_num + 1) * sizeof (*sockets));
260 ERROR ("gmond plugin: realloc failed.");
265 sockets[sockets_num].fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
266 ai_ptr->ai_protocol);
267 if (sockets[sockets_num].fd < 0)
270 ERROR ("gmond plugin: socket failed: %s",
271 sstrerror (errno, errbuf, sizeof (errbuf)));
275 assert (sizeof (sockets[sockets_num].addr) >= ai_ptr->ai_addrlen);
276 memcpy (&sockets[sockets_num].addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
277 sockets[sockets_num].addrlen = ai_ptr->ai_addrlen;
279 /* Sending socket: Open only one socket and don't bind it. */
289 setsockopt (sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR,
290 (void *) &yes, sizeof (yes));
293 status = bind (sockets[sockets_num].fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
297 ERROR ("gmond plugin: bind failed: %s",
298 sstrerror (errno, errbuf, sizeof (errbuf)));
299 close (sockets[sockets_num].fd);
303 if (ai_ptr->ai_family == AF_INET)
305 struct sockaddr_in *addr;
309 addr = (struct sockaddr_in *) ai_ptr->ai_addr;
311 if (!IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
318 setsockopt (sockets[sockets_num].fd, IPPROTO_IP, IP_MULTICAST_LOOP,
319 (void *) &loop, sizeof (loop));
321 memset (&mreq, 0, sizeof (mreq));
322 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
323 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
324 setsockopt (sockets[sockets_num].fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
325 (void *) &mreq, sizeof (mreq));
326 } /* if (ai_ptr->ai_family == AF_INET) */
327 else if (ai_ptr->ai_family == AF_INET6)
329 struct sockaddr_in6 *addr;
330 struct ipv6_mreq mreq;
333 addr = (struct sockaddr_in6 *) ai_ptr->ai_addr;
335 if (!IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
342 setsockopt (sockets[sockets_num].fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
343 (void *) &loop, sizeof (loop));
345 memset (&mreq, 0, sizeof (mreq));
346 memcpy (&mreq.ipv6mr_multiaddr,
347 &addr->sin6_addr, sizeof (addr->sin6_addr));
348 mreq.ipv6mr_interface = 0; /* any */
349 setsockopt (sockets[sockets_num].fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
350 (void *) &mreq, sizeof (mreq));
351 } /* if (ai_ptr->ai_family == AF_INET6) */
354 } /* }}} for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
356 freeaddrinfo (ai_list);
358 if ((*ret_sockets_num) >= sockets_num)
361 *ret_sockets = sockets;
362 *ret_sockets_num = sockets_num;
364 } /* }}} int create_sockets */
366 static int request_meta_data (const char *host, const char *name) /* {{{ */
368 Ganglia_metadata_msg msg;
369 char buffer[BUFF_SIZE];
370 unsigned int buffer_size;
374 memset (&msg, 0, sizeof (msg));
376 msg.id = gmetadata_request;
377 msg.Ganglia_metadata_msg_u.grequest.metric_id.host = strdup (host);
378 msg.Ganglia_metadata_msg_u.grequest.metric_id.name = strdup (name);
380 if ((msg.Ganglia_metadata_msg_u.grequest.metric_id.host == NULL)
381 || (msg.Ganglia_metadata_msg_u.grequest.metric_id.name == NULL))
383 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
384 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
388 memset (buffer, 0, sizeof (buffer));
389 xdrmem_create (&xdr, buffer, sizeof (buffer), XDR_ENCODE);
391 if (!xdr_Ganglia_metadata_msg (&xdr, &msg))
393 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
394 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
398 buffer_size = xdr_getpos (&xdr);
400 DEBUG ("gmond plugin: Requesting meta data for %s/%s.",
403 pthread_mutex_lock (&mc_send_sockets_lock);
404 for (i = 0; i < mc_send_sockets_num; i++)
405 sendto (mc_send_sockets[i].fd, buffer, (size_t) buffer_size,
407 (struct sockaddr *) &mc_send_sockets[i].addr,
408 mc_send_sockets[i].addrlen);
409 pthread_mutex_unlock (&mc_send_sockets_lock);
411 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
412 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
414 } /* }}} int request_meta_data */
416 static staging_entry_t *staging_entry_get (const char *host, /* {{{ */
418 const char *type, const char *type_instance,
421 char key[2 * DATA_MAX_NAME_LEN];
425 if (staging_tree == NULL)
428 ssnprintf (key, sizeof (key), "%s/%s/%s", host, type,
429 (type_instance != NULL) ? type_instance : "");
432 status = c_avl_get (staging_tree, key, (void *) &se);
436 /* insert new entry */
437 se = (staging_entry_t *) malloc (sizeof (*se));
440 memset (se, 0, sizeof (*se));
442 sstrncpy (se->key, key, sizeof (se->key));
445 se->vl.values = (value_t *) calloc (values_len, sizeof (*se->vl.values));
446 if (se->vl.values == NULL)
451 se->vl.values_len = values_len;
455 sstrncpy (se->vl.host, host, sizeof (se->vl.host));
456 sstrncpy (se->vl.plugin, "gmond", sizeof (se->vl.plugin));
457 sstrncpy (se->vl.type, type, sizeof (se->vl.type));
458 if (type_instance != NULL)
459 sstrncpy (se->vl.type_instance, type_instance,
460 sizeof (se->vl.type_instance));
462 status = c_avl_insert (staging_tree, se->key, se);
465 ERROR ("gmond plugin: c_avl_insert failed.");
466 sfree (se->vl.values);
472 } /* }}} staging_entry_t *staging_entry_get */
474 static int staging_entry_submit (const char *host, const char *name, /* {{{ */
478 value_t values[se->vl.values_len];
480 if (se->vl.interval == 0)
482 /* No meta data has been received for this metric yet. */
484 pthread_mutex_unlock (&staging_lock);
485 request_meta_data (host, name);
491 memcpy (values, se->vl.values, sizeof (values));
492 memcpy (&vl, &se->vl, sizeof (vl));
494 /* Unlock before calling `plugin_dispatch_values'.. */
495 pthread_mutex_unlock (&staging_lock);
499 plugin_dispatch_values (&vl);
502 } /* }}} int staging_entry_submit */
504 static int staging_entry_update (const char *host, const char *name, /* {{{ */
505 const char *type, const char *type_instance,
506 int ds_index, int ds_type, value_t value)
508 const data_set_t *ds;
511 ds = plugin_get_ds (type);
514 ERROR ("gmond plugin: Looking up type %s failed.", type);
518 if (ds->ds_num <= ds_index)
520 ERROR ("gmond plugin: Invalid index %i: %s has only %i data source(s).",
521 ds_index, ds->type, ds->ds_num);
525 pthread_mutex_lock (&staging_lock);
527 se = staging_entry_get (host, name, type, type_instance, ds->ds_num);
530 pthread_mutex_unlock (&staging_lock);
531 ERROR ("gmond plugin: staging_entry_get failed.");
534 if (se->vl.values_len != ds->ds_num)
536 pthread_mutex_unlock (&staging_lock);
540 if (ds_type == DS_TYPE_COUNTER)
541 se->vl.values[ds_index].counter += value.counter;
542 else if (ds_type == DS_TYPE_GAUGE)
543 se->vl.values[ds_index].gauge = value.gauge;
544 else if (ds_type == DS_TYPE_DERIVE)
545 se->vl.values[ds_index].derive += value.derive;
546 else if (ds_type == DS_TYPE_ABSOLUTE)
547 se->vl.values[ds_index].absolute = value.absolute;
551 se->flags |= (0x01 << ds_index);
553 /* Check if all values have been set and submit if so. */
554 if (se->flags == ((0x01 << se->vl.values_len) - 1))
556 /* `staging_lock' is unlocked in `staging_entry_submit'. */
557 staging_entry_submit (host, name, se);
561 pthread_mutex_unlock (&staging_lock);
565 } /* }}} int staging_entry_update */
567 static int mc_handle_value_msg (Ganglia_value_msg *msg) /* {{{ */
573 value_t value_counter;
575 value_t value_derive;
577 /* Fill in `host', `name', `value_counter', and `value_gauge' according to
578 * the value type, or return with an error. */
579 switch (msg->id) /* {{{ */
583 Ganglia_gmetric_uint msg_uint;
585 msg_uint = msg->Ganglia_value_msg_u.gu_int;
587 host = msg_uint.metric_id.host;
588 name = msg_uint.metric_id.name;
589 value_counter.counter = (counter_t) msg_uint.ui;
590 value_gauge.gauge = (gauge_t) msg_uint.ui;
591 value_derive.derive = (derive_t) msg_uint.ui;
597 Ganglia_gmetric_string msg_string;
600 msg_string = msg->Ganglia_value_msg_u.gstr;
602 host = msg_string.metric_id.host;
603 name = msg_string.metric_id.name;
605 status = parse_value (msg_string.str, &value_derive, DS_TYPE_DERIVE);
607 value_derive.derive = -1;
609 status = parse_value (msg_string.str, &value_gauge, DS_TYPE_GAUGE);
611 value_gauge.gauge = NAN;
613 status = parse_value (msg_string.str, &value_counter, DS_TYPE_COUNTER);
615 value_counter.counter = 0;
622 Ganglia_gmetric_float msg_float;
624 msg_float = msg->Ganglia_value_msg_u.gf;
626 host = msg_float.metric_id.host;
627 name = msg_float.metric_id.name;
628 value_counter.counter = (counter_t) msg_float.f;
629 value_gauge.gauge = (gauge_t) msg_float.f;
630 value_derive.derive = (derive_t) msg_float.f;
636 Ganglia_gmetric_double msg_double;
638 msg_double = msg->Ganglia_value_msg_u.gd;
640 host = msg_double.metric_id.host;
641 name = msg_double.metric_id.name;
642 value_counter.counter = (counter_t) msg_double.d;
643 value_gauge.gauge = (gauge_t) msg_double.d;
644 value_derive.derive = (derive_t) msg_double.d;
648 DEBUG ("gmond plugin: Value type not handled: %i", msg->id);
650 } /* }}} switch (msg->id) */
652 assert (host != NULL);
653 assert (name != NULL);
655 map = metric_lookup (name);
660 if ((map->ds_type == DS_TYPE_COUNTER)
661 || (map->ds_type == DS_TYPE_ABSOLUTE))
662 val_copy = value_counter;
663 if (map->ds_type == DS_TYPE_GAUGE)
664 val_copy = value_gauge;
665 else if (map->ds_type == DS_TYPE_DERIVE)
666 val_copy = value_derive;
670 return (staging_entry_update (host, name,
671 map->type, map->type_instance,
672 map->ds_index, map->ds_type,
676 DEBUG ("gmond plugin: Cannot find a translation for %s.", name);
678 } /* }}} int mc_handle_value_msg */
680 static int mc_handle_metadata_msg (Ganglia_metadata_msg *msg) /* {{{ */
686 Ganglia_metadatadef msg_meta;
688 const data_set_t *ds;
691 msg_meta = msg->Ganglia_metadata_msg_u.gfull;
693 if (msg_meta.metric.tmax <= 0)
696 map = metric_lookup (msg_meta.metric_id.name);
699 DEBUG ("gmond plugin: Not handling meta data %s.",
700 msg_meta.metric_id.name);
704 ds = plugin_get_ds (map->type);
707 WARNING ("gmond plugin: Could not find data set %s.", map->type);
711 DEBUG ("gmond plugin: Received meta data for %s/%s.",
712 msg_meta.metric_id.host, msg_meta.metric_id.name);
714 pthread_mutex_lock (&staging_lock);
715 se = staging_entry_get (msg_meta.metric_id.host,
716 msg_meta.metric_id.name,
717 map->type, map->type_instance,
720 se->vl.interval = TIME_T_TO_CDTIME_T (msg_meta.metric.tmax);
721 pthread_mutex_unlock (&staging_lock);
725 ERROR ("gmond plugin: staging_entry_get failed.");
739 } /* }}} int mc_handle_metadata_msg */
741 static int mc_handle_metric (void *buffer, size_t buffer_size) /* {{{ */
744 Ganglia_msg_formats format;
746 xdrmem_create (&xdr, buffer, buffer_size, XDR_DECODE);
748 xdr_Ganglia_msg_formats (&xdr, &format);
749 xdr_setpos (&xdr, 0);
761 Ganglia_value_msg msg;
763 memset (&msg, 0, sizeof (msg));
764 if (xdr_Ganglia_value_msg (&xdr, &msg))
765 mc_handle_value_msg (&msg);
770 case gmetadata_request:
772 Ganglia_metadata_msg msg;
773 memset (&msg, 0, sizeof (msg));
774 if (xdr_Ganglia_metadata_msg (&xdr, &msg))
775 mc_handle_metadata_msg (&msg);
780 DEBUG ("gmond plugin: Unknown format: %i", format);
782 } /* switch (format) */
786 } /* }}} int mc_handle_metric */
788 static int mc_handle_socket (struct pollfd *p) /* {{{ */
790 char buffer[BUFF_SIZE];
793 if ((p->revents & (POLLIN | POLLPRI)) == 0)
799 buffer_size = recv (p->fd, buffer, sizeof (buffer), /* flags = */ 0);
800 if (buffer_size <= 0)
803 ERROR ("gmond plugin: recv failed: %s",
804 sstrerror (errno, errbuf, sizeof (errbuf)));
809 mc_handle_metric (buffer, (size_t) buffer_size);
811 } /* }}} int mc_handle_socket */
813 static void *mc_receive_thread (void *arg) /* {{{ */
815 socket_entry_t *mc_receive_socket_entries;
819 mc_receive_socket_entries = NULL;
820 status = create_sockets (&mc_receive_socket_entries, &mc_receive_sockets_num,
821 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
822 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
826 ERROR ("gmond plugin: create_sockets failed.");
827 return ((void *) -1);
830 mc_receive_sockets = (struct pollfd *) calloc (mc_receive_sockets_num,
831 sizeof (*mc_receive_sockets));
832 if (mc_receive_sockets == NULL)
834 ERROR ("gmond plugin: calloc failed.");
835 for (i = 0; i < mc_receive_sockets_num; i++)
836 close (mc_receive_socket_entries[i].fd);
837 free (mc_receive_socket_entries);
838 mc_receive_socket_entries = NULL;
839 mc_receive_sockets_num = 0;
840 return ((void *) -1);
843 for (i = 0; i < mc_receive_sockets_num; i++)
845 mc_receive_sockets[i].fd = mc_receive_socket_entries[i].fd;
846 mc_receive_sockets[i].events = POLLIN | POLLPRI;
847 mc_receive_sockets[i].revents = 0;
850 while (mc_receive_thread_loop != 0)
852 status = poll (mc_receive_sockets, mc_receive_sockets_num, -1);
858 ERROR ("gmond plugin: poll failed: %s",
859 sstrerror (errno, errbuf, sizeof (errbuf)));
863 for (i = 0; i < mc_receive_sockets_num; i++)
865 if (mc_receive_sockets[i].revents != 0)
866 mc_handle_socket (mc_receive_sockets + i);
868 } /* while (mc_receive_thread_loop != 0) */
871 } /* }}} void *mc_receive_thread */
873 static int mc_receive_thread_start (void) /* {{{ */
877 if (mc_receive_thread_running != 0)
880 mc_receive_thread_loop = 1;
882 status = plugin_thread_create (&mc_receive_thread_id, /* attr = */ NULL,
883 mc_receive_thread, /* args = */ NULL);
886 ERROR ("gmond plugin: Starting receive thread failed.");
887 mc_receive_thread_loop = 0;
891 mc_receive_thread_running = 1;
893 } /* }}} int start_receive_thread */
895 static int mc_receive_thread_stop (void) /* {{{ */
897 if (mc_receive_thread_running == 0)
900 mc_receive_thread_loop = 0;
902 INFO ("gmond plugin: Stopping receive thread.");
903 pthread_kill (mc_receive_thread_id, SIGTERM);
904 pthread_join (mc_receive_thread_id, /* return value = */ NULL);
905 memset (&mc_receive_thread_id, 0, sizeof (mc_receive_thread_id));
907 mc_receive_thread_running = 0;
910 } /* }}} int mc_receive_thread_stop */
916 * MCReceiveFrom "239.2.11.71" "8649"
917 * <Metric "load_one">
919 * [TypeInstance "foo"]
924 static int gmond_config_set_string (oconfig_item_t *ci, char **str) /* {{{ */
928 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
930 WARNING ("gmond plugin: The `%s' option needs "
931 "exactly one string argument.", ci->key);
935 tmp = strdup (ci->values[0].value.string);
938 ERROR ("gmond plugin: strdup failed.");
945 } /* }}} int gmond_config_set_string */
947 static int gmond_config_add_metric (oconfig_item_t *ci) /* {{{ */
952 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
954 WARNING ("gmond plugin: `Metric' blocks need "
955 "exactly one string argument.");
959 map = realloc (metric_map, (metric_map_len + 1) * sizeof (*metric_map));
962 ERROR ("gmond plugin: realloc failed.");
966 map = metric_map + metric_map_len;
968 memset (map, 0, sizeof (*map));
970 map->type_instance = NULL;
975 map->ganglia_name = strdup (ci->values[0].value.string);
976 if (map->ganglia_name == NULL)
978 ERROR ("gmond plugin: strdup failed.");
982 for (i = 0; i < ci->children_num; i++)
984 oconfig_item_t *child = ci->children + i;
985 if (strcasecmp ("Type", child->key) == 0)
986 gmond_config_set_string (child, &map->type);
987 else if (strcasecmp ("TypeInstance", child->key) == 0)
988 gmond_config_set_string (child, &map->type_instance);
989 else if (strcasecmp ("DataSource", child->key) == 0)
990 gmond_config_set_string (child, &map->ds_name);
993 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
998 if (map->type == NULL)
1000 ERROR ("gmond plugin: No type is set for metric %s.",
1002 sfree (map->ganglia_name);
1003 sfree (map->type_instance);
1009 } /* }}} int gmond_config_add_metric */
1011 static int gmond_config_set_address (oconfig_item_t *ci, /* {{{ */
1012 char **ret_addr, char **ret_port)
1017 if ((ci->values_num != 1) && (ci->values_num != 2))
1019 WARNING ("gmond plugin: The `%s' config option needs "
1020 "one or two string arguments.",
1024 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
1025 || ((ci->values_num == 2)
1026 && (ci->values[1].type != OCONFIG_TYPE_STRING)))
1028 WARNING ("gmond plugin: The `%s' config option needs "
1029 "one or two string arguments.",
1034 addr = strdup (ci->values[0].value.string);
1035 if (ci->values_num == 2)
1036 port = strdup (ci->values[1].value.string);
1040 if ((addr == NULL) || ((ci->values_num == 2) && (port == NULL)))
1042 ERROR ("gmond plugin: strdup failed.");
1055 } /* }}} int gmond_config_set_address */
1057 static int gmond_config (oconfig_item_t *ci) /* {{{ */
1061 for (i = 0; i < ci->children_num; i++)
1063 oconfig_item_t *child = ci->children + i;
1064 if (strcasecmp ("MCReceiveFrom", child->key) == 0)
1065 gmond_config_set_address (child, &mc_receive_group, &mc_receive_port);
1066 else if (strcasecmp ("Metric", child->key) == 0)
1067 gmond_config_add_metric (child);
1070 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
1076 } /* }}} int gmond_config */
1078 static int gmond_init (void) /* {{{ */
1080 create_sockets (&mc_send_sockets, &mc_send_sockets_num,
1081 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
1082 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
1085 staging_tree = c_avl_create ((void *) strcmp);
1086 if (staging_tree == NULL)
1088 ERROR ("gmond plugin: c_avl_create failed.");
1092 mc_receive_thread_start ();
1095 } /* }}} int gmond_init */
1097 static int gmond_shutdown (void) /* {{{ */
1101 mc_receive_thread_stop ();
1103 pthread_mutex_lock (&mc_send_sockets_lock);
1104 for (i = 0; i < mc_send_sockets_num; i++)
1106 close (mc_send_sockets[i].fd);
1107 mc_send_sockets[i].fd = -1;
1109 sfree (mc_send_sockets);
1110 mc_send_sockets_num = 0;
1111 pthread_mutex_unlock (&mc_send_sockets_lock);
1115 } /* }}} int gmond_shutdown */
1117 void module_register (void)
1119 plugin_register_complex_config ("gmond", gmond_config);
1120 plugin_register_init ("gmond", gmond_init);
1121 plugin_register_shutdown ("gmond", gmond_shutdown);
1124 /* vim: set sw=2 sts=2 et fdm=marker : */