2 * collectd - src/gmond.c
3 * Copyright (C) 2009 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 verplant.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 se->flags |= (0x01 << ds_index);
546 /* Check if all values have been set and submit if so. */
547 if (se->flags == ((0x01 << se->vl.values_len) - 1))
549 /* `staging_lock' is unlocked in `staging_entry_submit'. */
550 staging_entry_submit (host, name, se);
554 pthread_mutex_unlock (&staging_lock);
558 } /* }}} int staging_entry_update */
560 static int mc_handle_value_msg (Ganglia_value_msg *msg) /* {{{ */
566 value_t value_counter;
569 /* Fill in `host', `name', `value_counter', and `value_gauge' according to
570 * the value type, or return with an error. */
571 switch (msg->id) /* {{{ */
575 Ganglia_gmetric_uint msg_uint;
577 msg_uint = msg->Ganglia_value_msg_u.gu_int;
579 host = msg_uint.metric_id.host;
580 name = msg_uint.metric_id.name;
581 value_counter.counter = (counter_t) msg_uint.ui;
582 value_gauge.gauge = (gauge_t) msg_uint.ui;
588 Ganglia_gmetric_string msg_string;
591 msg_string = msg->Ganglia_value_msg_u.gstr;
593 host = msg_string.metric_id.host;
594 name = msg_string.metric_id.name;
598 value_counter.counter = (counter_t) strtoll (msg_string.str,
599 &endptr, /* base = */ 0);
600 if ((endptr == msg_string.str) || (errno != 0))
601 value_counter.counter = -1;
605 value_gauge.gauge = (gauge_t) strtod (msg_string.str, &endptr);
606 if ((endptr == msg_string.str) || (errno != 0))
607 value_gauge.gauge = NAN;
614 Ganglia_gmetric_float msg_float;
616 msg_float = msg->Ganglia_value_msg_u.gf;
618 host = msg_float.metric_id.host;
619 name = msg_float.metric_id.name;
620 value_counter.counter = (counter_t) msg_float.f;
621 value_gauge.gauge = (gauge_t) msg_float.f;
627 Ganglia_gmetric_double msg_double;
629 msg_double = msg->Ganglia_value_msg_u.gd;
631 host = msg_double.metric_id.host;
632 name = msg_double.metric_id.name;
633 value_counter.counter = (counter_t) msg_double.d;
634 value_gauge.gauge = (gauge_t) msg_double.d;
638 DEBUG ("gmond plugin: Value type not handled: %i", msg->id);
640 } /* }}} switch (msg->id) */
642 assert (host != NULL);
643 assert (name != NULL);
645 map = metric_lookup (name);
647 return (staging_entry_update (host, name,
648 map->type, map->type_instance,
649 map->ds_index, map->ds_type,
650 (map->ds_type == DS_TYPE_COUNTER) ? value_counter : value_gauge));
652 DEBUG ("gmond plugin: Cannot find a translation for %s.", name);
654 } /* }}} int mc_handle_value_msg */
656 static int mc_handle_metadata_msg (Ganglia_metadata_msg *msg) /* {{{ */
662 Ganglia_metadatadef msg_meta;
664 const data_set_t *ds;
667 msg_meta = msg->Ganglia_metadata_msg_u.gfull;
669 if (msg_meta.metric.tmax <= 0)
672 map = metric_lookup (msg_meta.metric_id.name);
675 DEBUG ("gmond plugin: Not handling meta data %s.",
676 msg_meta.metric_id.name);
680 ds = plugin_get_ds (map->type);
683 WARNING ("gmond plugin: Could not find data set %s.", map->type);
687 DEBUG ("gmond plugin: Received meta data for %s/%s.",
688 msg_meta.metric_id.host, msg_meta.metric_id.name);
690 pthread_mutex_lock (&staging_lock);
691 se = staging_entry_get (msg_meta.metric_id.host,
692 msg_meta.metric_id.name,
693 map->type, map->type_instance,
696 se->vl.interval = (int) msg_meta.metric.tmax;
697 pthread_mutex_unlock (&staging_lock);
701 ERROR ("gmond plugin: staging_entry_get failed.");
715 } /* }}} int mc_handle_metadata_msg */
717 static int mc_handle_metric (void *buffer, size_t buffer_size) /* {{{ */
720 Ganglia_msg_formats format;
722 xdrmem_create (&xdr, buffer, buffer_size, XDR_DECODE);
724 xdr_Ganglia_msg_formats (&xdr, &format);
725 xdr_setpos (&xdr, 0);
737 Ganglia_value_msg msg;
739 memset (&msg, 0, sizeof (msg));
740 if (xdr_Ganglia_value_msg (&xdr, &msg))
741 mc_handle_value_msg (&msg);
746 case gmetadata_request:
748 Ganglia_metadata_msg msg;
749 memset (&msg, 0, sizeof (msg));
750 if (xdr_Ganglia_metadata_msg (&xdr, &msg))
751 mc_handle_metadata_msg (&msg);
756 DEBUG ("gmond plugin: Unknown format: %i", format);
758 } /* switch (format) */
762 } /* }}} int mc_handle_metric */
764 static int mc_handle_socket (struct pollfd *p) /* {{{ */
766 char buffer[BUFF_SIZE];
769 if ((p->revents & (POLLIN | POLLPRI)) == 0)
775 buffer_size = recv (p->fd, buffer, sizeof (buffer), /* flags = */ 0);
776 if (buffer_size <= 0)
779 ERROR ("gmond plugin: recv failed: %s",
780 sstrerror (errno, errbuf, sizeof (errbuf)));
785 mc_handle_metric (buffer, (size_t) buffer_size);
787 } /* }}} int mc_handle_socket */
789 static void *mc_receive_thread (void *arg) /* {{{ */
791 socket_entry_t *mc_receive_socket_entries;
795 mc_receive_socket_entries = NULL;
796 status = create_sockets (&mc_receive_socket_entries, &mc_receive_sockets_num,
797 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
798 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
802 ERROR ("gmond plugin: create_sockets failed.");
803 return ((void *) -1);
806 mc_receive_sockets = (struct pollfd *) calloc (mc_receive_sockets_num,
807 sizeof (*mc_receive_sockets));
808 if (mc_receive_sockets == NULL)
810 ERROR ("gmond plugin: calloc failed.");
811 for (i = 0; i < mc_receive_sockets_num; i++)
812 close (mc_receive_socket_entries[i].fd);
813 free (mc_receive_socket_entries);
814 mc_receive_socket_entries = NULL;
815 mc_receive_sockets_num = 0;
816 return ((void *) -1);
819 for (i = 0; i < mc_receive_sockets_num; i++)
821 mc_receive_sockets[i].fd = mc_receive_socket_entries[i].fd;
822 mc_receive_sockets[i].events = POLLIN | POLLPRI;
823 mc_receive_sockets[i].revents = 0;
826 while (mc_receive_thread_loop != 0)
828 status = poll (mc_receive_sockets, mc_receive_sockets_num, -1);
834 ERROR ("gmond plugin: poll failed: %s",
835 sstrerror (errno, errbuf, sizeof (errbuf)));
839 for (i = 0; i < mc_receive_sockets_num; i++)
841 if (mc_receive_sockets[i].revents != 0)
842 mc_handle_socket (mc_receive_sockets + i);
844 } /* while (mc_receive_thread_loop != 0) */
847 } /* }}} void *mc_receive_thread */
849 static int mc_receive_thread_start (void) /* {{{ */
853 if (mc_receive_thread_running != 0)
856 mc_receive_thread_loop = 1;
858 status = pthread_create (&mc_receive_thread_id, /* attr = */ NULL,
859 mc_receive_thread, /* args = */ NULL);
862 ERROR ("gmond plugin: Starting receive thread failed.");
863 mc_receive_thread_loop = 0;
867 mc_receive_thread_running = 1;
869 } /* }}} int start_receive_thread */
871 static int mc_receive_thread_stop (void) /* {{{ */
873 if (mc_receive_thread_running == 0)
876 mc_receive_thread_loop = 0;
878 INFO ("gmond plugin: Stopping receive thread.");
879 pthread_kill (mc_receive_thread_id, SIGTERM);
880 pthread_join (mc_receive_thread_id, /* return value = */ NULL);
881 memset (&mc_receive_thread_id, 0, sizeof (mc_receive_thread_id));
883 mc_receive_thread_running = 0;
886 } /* }}} int mc_receive_thread_stop */
892 * MCReceiveFrom "239.2.11.71" "8649"
893 * <Metric "load_one">
895 * [TypeInstance "foo"]
900 static int gmond_config_set_string (oconfig_item_t *ci, char **str) /* {{{ */
904 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
906 WARNING ("gmond plugin: The `%s' option needs "
907 "exactly one string argument.", ci->key);
911 tmp = strdup (ci->values[0].value.string);
914 ERROR ("gmond plugin: strdup failed.");
921 } /* }}} int gmond_config_set_string */
923 static int gmond_config_add_metric (oconfig_item_t *ci) /* {{{ */
928 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
930 WARNING ("gmond plugin: `Metric' blocks need "
931 "exactly one string argument.");
935 map = realloc (metric_map, (metric_map_len + 1) * sizeof (*metric_map));
938 ERROR ("gmond plugin: realloc failed.");
942 map = metric_map + metric_map_len;
944 memset (map, 0, sizeof (*map));
946 map->type_instance = NULL;
951 map->ganglia_name = strdup (ci->values[0].value.string);
952 if (map->ganglia_name == NULL)
954 ERROR ("gmond plugin: strdup failed.");
958 for (i = 0; i < ci->children_num; i++)
960 oconfig_item_t *child = ci->children + i;
961 if (strcasecmp ("Type", child->key) == 0)
962 gmond_config_set_string (child, &map->type);
963 else if (strcasecmp ("TypeInstance", child->key) == 0)
964 gmond_config_set_string (child, &map->type_instance);
965 else if (strcasecmp ("DataSource", child->key) == 0)
966 gmond_config_set_string (child, &map->ds_name);
969 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
974 if (map->type == NULL)
976 ERROR ("gmond plugin: No type is set for metric %s.",
978 sfree (map->ganglia_name);
979 sfree (map->type_instance);
985 } /* }}} int gmond_config_add_metric */
987 static int gmond_config_set_address (oconfig_item_t *ci, /* {{{ */
988 char **ret_addr, char **ret_port)
993 if ((ci->values_num != 1) && (ci->values_num != 2))
995 WARNING ("gmond plugin: The `%s' config option needs "
996 "one or two string arguments.",
1000 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
1001 || ((ci->values_num == 2)
1002 && (ci->values[1].type != OCONFIG_TYPE_STRING)))
1004 WARNING ("gmond plugin: The `%s' config option needs "
1005 "one or two string arguments.",
1010 addr = strdup (ci->values[0].value.string);
1011 if (ci->values_num == 2)
1012 port = strdup (ci->values[1].value.string);
1016 if ((addr == NULL) || ((ci->values_num == 2) && (port == NULL)))
1018 ERROR ("gmond plugin: strdup failed.");
1031 } /* }}} int gmond_config_set_address */
1033 static int gmond_config (oconfig_item_t *ci) /* {{{ */
1037 for (i = 0; i < ci->children_num; i++)
1039 oconfig_item_t *child = ci->children + i;
1040 if (strcasecmp ("MCReceiveFrom", child->key) == 0)
1041 gmond_config_set_address (child, &mc_receive_group, &mc_receive_port);
1042 else if (strcasecmp ("Metric", child->key) == 0)
1043 gmond_config_add_metric (child);
1046 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
1052 } /* }}} int gmond_config */
1054 static int gmond_init (void) /* {{{ */
1056 create_sockets (&mc_send_sockets, &mc_send_sockets_num,
1057 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
1058 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
1061 staging_tree = c_avl_create ((void *) strcmp);
1062 if (staging_tree == NULL)
1064 ERROR ("gmond plugin: c_avl_create failed.");
1068 mc_receive_thread_start ();
1071 } /* }}} int gmond_init */
1073 static int gmond_shutdown (void) /* {{{ */
1077 mc_receive_thread_stop ();
1079 pthread_mutex_lock (&mc_send_sockets_lock);
1080 for (i = 0; i < mc_send_sockets_num; i++)
1082 close (mc_send_sockets[i].fd);
1083 mc_send_sockets[i].fd = -1;
1085 sfree (mc_send_sockets);
1086 mc_send_sockets_num = 0;
1087 pthread_mutex_unlock (&mc_send_sockets_lock);
1091 } /* }}} int gmond_shutdown */
1093 void module_register (void)
1095 plugin_register_complex_config ("gmond", gmond_config);
1096 plugin_register_init ("gmond", gmond_init);
1097 plugin_register_shutdown ("gmond", gmond_shutdown);
1100 /* vim: set sw=2 sts=2 et fdm=marker : */