1 /**
2 * collectd - src/gmond.c
3 * Copyright (C) 2009,2010 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at collectd.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "configfile.h"
26 #include "utils_avltree.h"
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
31 #if HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
33 #endif
34 #if HAVE_NETDB_H
35 # include <netdb.h>
36 #endif
37 #if HAVE_NETINET_IN_H
38 # include <netinet/in.h>
39 #endif
40 #if HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
42 #endif
43 #if HAVE_POLL_H
44 # include <poll.h>
45 #endif
47 #include <gm_protocol.h>
49 #ifndef IPV6_ADD_MEMBERSHIP
50 # ifdef IPV6_JOIN_GROUP
51 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
52 # else
53 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
54 # endif
55 #endif /* !IP_ADD_MEMBERSHIP */
57 #ifdef GANGLIA_MAX_MESSAGE_LEN
58 # define BUFF_SIZE GANGLIA_MAX_MESSAGE_LEN
59 #else
60 # define BUFF_SIZE 1400
61 #endif
63 struct socket_entry_s
64 {
65 int fd;
66 struct sockaddr_storage addr;
67 socklen_t addrlen;
68 };
69 typedef struct socket_entry_s socket_entry_t;
71 struct staging_entry_s
72 {
73 char key[2 * DATA_MAX_NAME_LEN];
74 value_list_t vl;
75 int flags;
76 };
77 typedef struct staging_entry_s staging_entry_t;
79 struct metric_map_s
80 {
81 char *ganglia_name;
82 char *type;
83 char *type_instance;
84 char *ds_name;
85 int ds_type;
86 int ds_index;
87 };
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 }
127 };
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) /* {{{ */
137 {
138 metric_map_t *map;
139 size_t map_len;
140 size_t i;
142 /* Search the user-supplied table first.. */
143 map = metric_map;
144 map_len = metric_map_len;
145 for (i = 0; i < map_len; i++)
146 if (strcmp (map[i].ganglia_name, key) == 0)
147 break;
149 /* .. and fall back to the built-in table if nothing is found. */
150 if (i >= map_len)
151 {
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)
157 break;
158 }
160 if (i >= map_len)
161 return (NULL);
163 /* Look up the DS type and ds_index. */
164 if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) /* {{{ */
165 {
166 const data_set_t *ds;
168 ds = plugin_get_ds (map[i].type);
169 if (ds == NULL)
170 {
171 WARNING ("gmond plugin: Type not defined: %s", map[i].type);
172 return (NULL);
173 }
175 if ((map[i].ds_name == NULL) && (ds->ds_num != 1))
176 {
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);
180 return (NULL);
181 }
183 if (map[i].ds_name == NULL)
184 {
185 map[i].ds_index = 0;
186 }
187 else
188 {
189 int j;
191 for (j = 0; j < ds->ds_num; j++)
192 if (strcasecmp (ds->ds[j].name, map[i].ds_name) == 0)
193 break;
195 if (j >= ds->ds_num)
196 {
197 WARNING ("gmond plugin: There is no data source "
198 "named `%s' in type `%s'.",
199 map[i].ds_name, ds->type);
200 return (NULL);
201 }
202 map[i].ds_index = j;
203 }
205 map[i].ds_type = ds->ds[map[i].ds_index].type;
206 } /* }}} if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) */
208 return (map + i);
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)
214 {
215 struct addrinfo ai_hints;
216 struct addrinfo *ai_list;
217 struct addrinfo *ai_ptr;
218 int ai_return;
220 socket_entry_t *sockets;
221 size_t sockets_num;
223 int status;
225 sockets = *ret_sockets;
226 sockets_num = *ret_sockets_num;
228 memset (&ai_hints, 0, sizeof (ai_hints));
229 ai_hints.ai_flags = 0;
230 #ifdef AI_PASSIVE
231 ai_hints.ai_flags |= AI_PASSIVE;
232 #endif
233 #ifdef AI_ADDRCONFIG
234 ai_hints.ai_flags |= AI_ADDRCONFIG;
235 #endif
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);
241 if (ai_return != 0)
242 {
243 char errbuf[1024];
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));
250 return (-1);
251 }
253 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) /* {{{ */
254 {
255 socket_entry_t *tmp;
257 tmp = realloc (sockets, (sockets_num + 1) * sizeof (*sockets));
258 if (tmp == NULL)
259 {
260 ERROR ("gmond plugin: realloc failed.");
261 continue;
262 }
263 sockets = tmp;
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)
268 {
269 char errbuf[1024];
270 ERROR ("gmond plugin: socket failed: %s",
271 sstrerror (errno, errbuf, sizeof (errbuf)));
272 continue;
273 }
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. */
280 if (listen == 0)
281 {
282 sockets_num++;
283 break;
284 }
285 else
286 {
287 int yes = 1;
289 setsockopt (sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR,
290 (void *) &yes, sizeof (yes));
291 }
293 status = bind (sockets[sockets_num].fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
294 if (status != 0)
295 {
296 char errbuf[1024];
297 ERROR ("gmond plugin: bind failed: %s",
298 sstrerror (errno, errbuf, sizeof (errbuf)));
299 close (sockets[sockets_num].fd);
300 continue;
301 }
303 if (ai_ptr->ai_family == AF_INET)
304 {
305 struct sockaddr_in *addr;
306 struct ip_mreq mreq;
307 int loop;
309 addr = (struct sockaddr_in *) ai_ptr->ai_addr;
311 if (!IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
312 {
313 sockets_num++;
314 continue;
315 }
317 loop = 1;
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)
328 {
329 struct sockaddr_in6 *addr;
330 struct ipv6_mreq mreq;
331 int loop;
333 addr = (struct sockaddr_in6 *) ai_ptr->ai_addr;
335 if (!IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
336 {
337 sockets_num++;
338 continue;
339 }
341 loop = 1;
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) */
353 sockets_num++;
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)
359 return (-1);
361 *ret_sockets = sockets;
362 *ret_sockets_num = sockets_num;
363 return (0);
364 } /* }}} int create_sockets */
366 static int request_meta_data (const char *host, const char *name) /* {{{ */
367 {
368 Ganglia_metadata_msg msg;
369 char buffer[BUFF_SIZE];
370 unsigned int buffer_size;
371 XDR xdr;
372 size_t i;
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))
382 {
383 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
384 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
385 return (-1);
386 }
388 memset (buffer, 0, sizeof (buffer));
389 xdrmem_create (&xdr, buffer, sizeof (buffer), XDR_ENCODE);
391 if (!xdr_Ganglia_metadata_msg (&xdr, &msg))
392 {
393 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
394 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
395 return (-1);
396 }
398 buffer_size = xdr_getpos (&xdr);
400 DEBUG ("gmond plugin: Requesting meta data for %s/%s.",
401 host, name);
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,
406 /* flags = */ 0,
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);
413 return (0);
414 } /* }}} int request_meta_data */
416 static staging_entry_t *staging_entry_get (const char *host, /* {{{ */
417 const char *name,
418 const char *type, const char *type_instance,
419 int values_len)
420 {
421 char key[2 * DATA_MAX_NAME_LEN];
422 staging_entry_t *se;
423 int status;
425 if (staging_tree == NULL)
426 return (NULL);
428 ssnprintf (key, sizeof (key), "%s/%s/%s", host, type,
429 (type_instance != NULL) ? type_instance : "");
431 se = NULL;
432 status = c_avl_get (staging_tree, key, (void *) &se);
433 if (status == 0)
434 return (se);
436 /* insert new entry */
437 se = (staging_entry_t *) malloc (sizeof (*se));
438 if (se == NULL)
439 return (NULL);
440 memset (se, 0, sizeof (*se));
442 sstrncpy (se->key, key, sizeof (se->key));
443 se->flags = 0;
445 se->vl.values = (value_t *) calloc (values_len, sizeof (*se->vl.values));
446 if (se->vl.values == NULL)
447 {
448 sfree (se);
449 return (NULL);
450 }
451 se->vl.values_len = values_len;
453 se->vl.time = 0;
454 se->vl.interval = 0;
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);
463 if (status != 0)
464 {
465 ERROR ("gmond plugin: c_avl_insert failed.");
466 sfree (se->vl.values);
467 sfree (se);
468 return (NULL);
469 }
471 return (se);
472 } /* }}} staging_entry_t *staging_entry_get */
474 static int staging_entry_submit (const char *host, const char *name, /* {{{ */
475 staging_entry_t *se)
476 {
477 value_list_t vl;
478 value_t values[se->vl.values_len];
480 if (se->vl.interval == 0)
481 {
482 /* No meta data has been received for this metric yet. */
483 se->flags = 0;
484 pthread_mutex_unlock (&staging_lock);
485 request_meta_data (host, name);
486 return (0);
487 }
489 se->flags = 0;
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);
497 vl.values = values;
499 plugin_dispatch_values (&vl);
501 return (0);
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)
507 {
508 const data_set_t *ds;
509 staging_entry_t *se;
511 ds = plugin_get_ds (type);
512 if (ds == NULL)
513 {
514 ERROR ("gmond plugin: Looking up type %s failed.", type);
515 return (-1);
516 }
518 if (ds->ds_num <= ds_index)
519 {
520 ERROR ("gmond plugin: Invalid index %i: %s has only %i data source(s).",
521 ds_index, ds->type, ds->ds_num);
522 return (-1);
523 }
525 pthread_mutex_lock (&staging_lock);
527 se = staging_entry_get (host, name, type, type_instance, ds->ds_num);
528 if (se == NULL)
529 {
530 pthread_mutex_unlock (&staging_lock);
531 ERROR ("gmond plugin: staging_entry_get failed.");
532 return (-1);
533 }
534 if (se->vl.values_len != ds->ds_num)
535 {
536 pthread_mutex_unlock (&staging_lock);
537 return (-1);
538 }
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;
548 else
549 assert (23 == 42);
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))
555 {
556 /* `staging_lock' is unlocked in `staging_entry_submit'. */
557 staging_entry_submit (host, name, se);
558 }
559 else
560 {
561 pthread_mutex_unlock (&staging_lock);
562 }
564 return (0);
565 } /* }}} int staging_entry_update */
567 static int mc_handle_value_msg (Ganglia_value_msg *msg) /* {{{ */
568 {
569 const char *host;
570 const char *name;
571 metric_map_t *map;
573 value_t value_counter;
574 value_t value_gauge;
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) /* {{{ */
580 {
581 case gmetric_uint:
582 {
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;
592 break;
593 }
595 case gmetric_string:
596 {
597 Ganglia_gmetric_string msg_string;
598 int status;
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);
606 if (status != 0)
607 value_derive.derive = -1;
609 status = parse_value (msg_string.str, &value_gauge, DS_TYPE_GAUGE);
610 if (status != 0)
611 value_gauge.gauge = NAN;
613 status = parse_value (msg_string.str, &value_counter, DS_TYPE_COUNTER);
614 if (status != 0)
615 value_counter.counter = 0;
617 break;
618 }
620 case gmetric_float:
621 {
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;
631 break;
632 }
634 case gmetric_double:
635 {
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;
645 break;
646 }
647 default:
648 DEBUG ("gmond plugin: Value type not handled: %i", msg->id);
649 return (-1);
650 } /* }}} switch (msg->id) */
652 assert (host != NULL);
653 assert (name != NULL);
655 map = metric_lookup (name);
656 if (map != NULL)
657 {
658 value_t val_copy;
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;
667 else
668 assert (23 == 42);
670 return (staging_entry_update (host, name,
671 map->type, map->type_instance,
672 map->ds_index, map->ds_type,
673 val_copy));
674 }
676 DEBUG ("gmond plugin: Cannot find a translation for %s.", name);
677 return (-1);
678 } /* }}} int mc_handle_value_msg */
680 static int mc_handle_metadata_msg (Ganglia_metadata_msg *msg) /* {{{ */
681 {
682 switch (msg->id)
683 {
684 case gmetadata_full:
685 {
686 Ganglia_metadatadef msg_meta;
687 staging_entry_t *se;
688 const data_set_t *ds;
689 metric_map_t *map;
691 msg_meta = msg->Ganglia_metadata_msg_u.gfull;
693 if (msg_meta.metric.tmax <= 0)
694 return (-1);
696 map = metric_lookup (msg_meta.metric_id.name);
697 if (map == NULL)
698 {
699 DEBUG ("gmond plugin: Not handling meta data %s.",
700 msg_meta.metric_id.name);
701 return (0);
702 }
704 ds = plugin_get_ds (map->type);
705 if (ds == NULL)
706 {
707 WARNING ("gmond plugin: Could not find data set %s.", map->type);
708 return (-1);
709 }
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,
718 ds->ds_num);
719 if (se != NULL)
720 se->vl.interval = TIME_T_TO_CDTIME_T (msg_meta.metric.tmax);
721 pthread_mutex_unlock (&staging_lock);
723 if (se == NULL)
724 {
725 ERROR ("gmond plugin: staging_entry_get failed.");
726 return (-1);
727 }
729 break;
730 }
732 default:
733 {
734 return (-1);
735 }
736 }
738 return (0);
739 } /* }}} int mc_handle_metadata_msg */
741 static int mc_handle_metric (void *buffer, size_t buffer_size) /* {{{ */
742 {
743 XDR xdr;
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);
751 switch (format)
752 {
753 case gmetric_ushort:
754 case gmetric_short:
755 case gmetric_int:
756 case gmetric_uint:
757 case gmetric_string:
758 case gmetric_float:
759 case gmetric_double:
760 {
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);
766 break;
767 }
769 case gmetadata_full:
770 case gmetadata_request:
771 {
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);
776 break;
777 }
779 default:
780 DEBUG ("gmond plugin: Unknown format: %i", format);
781 return (-1);
782 } /* switch (format) */
785 return (0);
786 } /* }}} int mc_handle_metric */
788 static int mc_handle_socket (struct pollfd *p) /* {{{ */
789 {
790 char buffer[BUFF_SIZE];
791 ssize_t buffer_size;
793 if ((p->revents & (POLLIN | POLLPRI)) == 0)
794 {
795 p->revents = 0;
796 return (-1);
797 }
799 buffer_size = recv (p->fd, buffer, sizeof (buffer), /* flags = */ 0);
800 if (buffer_size <= 0)
801 {
802 char errbuf[1024];
803 ERROR ("gmond plugin: recv failed: %s",
804 sstrerror (errno, errbuf, sizeof (errbuf)));
805 p->revents = 0;
806 return (-1);
807 }
809 mc_handle_metric (buffer, (size_t) buffer_size);
810 return (0);
811 } /* }}} int mc_handle_socket */
813 static void *mc_receive_thread (void *arg) /* {{{ */
814 {
815 socket_entry_t *mc_receive_socket_entries;
816 int status;
817 size_t i;
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,
823 /* listen = */ 1);
824 if (status != 0)
825 {
826 ERROR ("gmond plugin: create_sockets failed.");
827 return ((void *) -1);
828 }
830 mc_receive_sockets = (struct pollfd *) calloc (mc_receive_sockets_num,
831 sizeof (*mc_receive_sockets));
832 if (mc_receive_sockets == NULL)
833 {
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);
841 }
843 for (i = 0; i < mc_receive_sockets_num; i++)
844 {
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;
848 }
850 while (mc_receive_thread_loop != 0)
851 {
852 status = poll (mc_receive_sockets, mc_receive_sockets_num, -1);
853 if (status <= 0)
854 {
855 char errbuf[1024];
856 if (errno == EINTR)
857 continue;
858 ERROR ("gmond plugin: poll failed: %s",
859 sstrerror (errno, errbuf, sizeof (errbuf)));
860 break;
861 }
863 for (i = 0; i < mc_receive_sockets_num; i++)
864 {
865 if (mc_receive_sockets[i].revents != 0)
866 mc_handle_socket (mc_receive_sockets + i);
867 }
868 } /* while (mc_receive_thread_loop != 0) */
870 return ((void *) 0);
871 } /* }}} void *mc_receive_thread */
873 static int mc_receive_thread_start (void) /* {{{ */
874 {
875 int status;
877 if (mc_receive_thread_running != 0)
878 return (-1);
880 mc_receive_thread_loop = 1;
882 status = pthread_create (&mc_receive_thread_id, /* attr = */ NULL,
883 mc_receive_thread, /* args = */ NULL);
884 if (status != 0)
885 {
886 ERROR ("gmond plugin: Starting receive thread failed.");
887 mc_receive_thread_loop = 0;
888 return (-1);
889 }
891 mc_receive_thread_running = 1;
892 return (0);
893 } /* }}} int start_receive_thread */
895 static int mc_receive_thread_stop (void) /* {{{ */
896 {
897 if (mc_receive_thread_running == 0)
898 return (-1);
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;
909 return (0);
910 } /* }}} int mc_receive_thread_stop */
912 /*
913 * Config:
914 *
915 * <Plugin gmond>
916 * MCReceiveFrom "239.2.11.71" "8649"
917 * <Metric "load_one">
918 * Type "load"
919 * [TypeInstance "foo"]
920 * [DataSource "bar"]
921 * </Metric>
922 * </Plugin>
923 */
924 static int gmond_config_set_string (oconfig_item_t *ci, char **str) /* {{{ */
925 {
926 char *tmp;
928 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
929 {
930 WARNING ("gmond plugin: The `%s' option needs "
931 "exactly one string argument.", ci->key);
932 return (-1);
933 }
935 tmp = strdup (ci->values[0].value.string);
936 if (tmp == NULL)
937 {
938 ERROR ("gmond plugin: strdup failed.");
939 return (-1);
940 }
942 sfree (*str);
943 *str = tmp;
944 return (0);
945 } /* }}} int gmond_config_set_string */
947 static int gmond_config_add_metric (oconfig_item_t *ci) /* {{{ */
948 {
949 metric_map_t *map;
950 int i;
952 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
953 {
954 WARNING ("gmond plugin: `Metric' blocks need "
955 "exactly one string argument.");
956 return (-1);
957 }
959 map = realloc (metric_map, (metric_map_len + 1) * sizeof (*metric_map));
960 if (map == NULL)
961 {
962 ERROR ("gmond plugin: realloc failed.");
963 return (-1);
964 }
965 metric_map = map;
966 map = metric_map + metric_map_len;
968 memset (map, 0, sizeof (*map));
969 map->type = NULL;
970 map->type_instance = NULL;
971 map->ds_name = NULL;
972 map->ds_type = -1;
973 map->ds_index = -1;
975 map->ganglia_name = strdup (ci->values[0].value.string);
976 if (map->ganglia_name == NULL)
977 {
978 ERROR ("gmond plugin: strdup failed.");
979 return (-1);
980 }
982 for (i = 0; i < ci->children_num; i++)
983 {
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);
991 else
992 {
993 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
994 child->key);
995 }
996 }
998 if (map->type == NULL)
999 {
1000 ERROR ("gmond plugin: No type is set for metric %s.",
1001 map->ganglia_name);
1002 sfree (map->ganglia_name);
1003 sfree (map->type_instance);
1004 return (-1);
1005 }
1007 metric_map_len++;
1008 return (0);
1009 } /* }}} int gmond_config_add_metric */
1011 static int gmond_config_set_address (oconfig_item_t *ci, /* {{{ */
1012 char **ret_addr, char **ret_port)
1013 {
1014 char *addr;
1015 char *port;
1017 if ((ci->values_num != 1) && (ci->values_num != 2))
1018 {
1019 WARNING ("gmond plugin: The `%s' config option needs "
1020 "one or two string arguments.",
1021 ci->key);
1022 return (-1);
1023 }
1024 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
1025 || ((ci->values_num == 2)
1026 && (ci->values[1].type != OCONFIG_TYPE_STRING)))
1027 {
1028 WARNING ("gmond plugin: The `%s' config option needs "
1029 "one or two string arguments.",
1030 ci->key);
1031 return (-1);
1032 }
1034 addr = strdup (ci->values[0].value.string);
1035 if (ci->values_num == 2)
1036 port = strdup (ci->values[1].value.string);
1037 else
1038 port = NULL;
1040 if ((addr == NULL) || ((ci->values_num == 2) && (port == NULL)))
1041 {
1042 ERROR ("gmond plugin: strdup failed.");
1043 sfree (addr);
1044 sfree (port);
1045 return (-1);
1046 }
1048 sfree (*ret_addr);
1049 sfree (*ret_port);
1051 *ret_addr = addr;
1052 *ret_port = port;
1054 return (0);
1055 } /* }}} int gmond_config_set_address */
1057 static int gmond_config (oconfig_item_t *ci) /* {{{ */
1058 {
1059 int i;
1061 for (i = 0; i < ci->children_num; i++)
1062 {
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);
1068 else
1069 {
1070 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
1071 child->key);
1072 }
1073 }
1075 return (0);
1076 } /* }}} int gmond_config */
1078 static int gmond_init (void) /* {{{ */
1079 {
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,
1083 /* listen = */ 0);
1085 staging_tree = c_avl_create ((void *) strcmp);
1086 if (staging_tree == NULL)
1087 {
1088 ERROR ("gmond plugin: c_avl_create failed.");
1089 return (-1);
1090 }
1092 mc_receive_thread_start ();
1094 return (0);
1095 } /* }}} int gmond_init */
1097 static int gmond_shutdown (void) /* {{{ */
1098 {
1099 size_t i;
1101 mc_receive_thread_stop ();
1103 pthread_mutex_lock (&mc_send_sockets_lock);
1104 for (i = 0; i < mc_send_sockets_num; i++)
1105 {
1106 close (mc_send_sockets[i].fd);
1107 mc_send_sockets[i].fd = -1;
1108 }
1109 sfree (mc_send_sockets);
1110 mc_send_sockets_num = 0;
1111 pthread_mutex_unlock (&mc_send_sockets_lock);
1114 return (0);
1115 } /* }}} int gmond_shutdown */
1117 void module_register (void)
1118 {
1119 plugin_register_complex_config ("gmond", gmond_config);
1120 plugin_register_init ("gmond", gmond_init);
1121 plugin_register_shutdown ("gmond", gmond_shutdown);
1122 }
1124 /* vim: set sw=2 sts=2 et fdm=marker : */