1 /**
2 * collectd - src/gmond.c
3 * Copyright (C) 2009-2015 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31 #include "utils_avltree.h"
33 #if HAVE_PTHREAD_H
34 # include <pthread.h>
35 #endif
36 #if HAVE_NETDB_H
37 # include <netdb.h>
38 #endif
39 #if HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42 #if HAVE_ARPA_INET_H
43 # include <arpa/inet.h>
44 #endif
45 #if HAVE_POLL_H
46 # include <poll.h>
47 #endif
49 #include <gm_protocol.h>
51 #ifndef IPV6_ADD_MEMBERSHIP
52 # ifdef IPV6_JOIN_GROUP
53 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
54 # else
55 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
56 # endif
57 #endif /* !IP_ADD_MEMBERSHIP */
59 #ifdef GANGLIA_MAX_MESSAGE_LEN
60 # define BUFF_SIZE GANGLIA_MAX_MESSAGE_LEN
61 #else
62 # define BUFF_SIZE 1400
63 #endif
65 struct socket_entry_s
66 {
67 int fd;
68 struct sockaddr_storage addr;
69 socklen_t addrlen;
70 };
71 typedef struct socket_entry_s socket_entry_t;
73 struct staging_entry_s
74 {
75 char key[2 * DATA_MAX_NAME_LEN];
76 value_list_t vl;
77 int flags;
78 };
79 typedef struct staging_entry_s staging_entry_t;
81 struct metric_map_s
82 {
83 char *ganglia_name;
84 char *type;
85 char *type_instance;
86 char *ds_name;
87 int ds_type;
88 size_t ds_index;
89 };
90 typedef struct metric_map_s metric_map_t;
92 #define MC_RECEIVE_GROUP_DEFAULT "239.2.11.71"
93 static char *mc_receive_group = NULL;
94 #define MC_RECEIVE_PORT_DEFAULT "8649"
95 static char *mc_receive_port = NULL;
97 static struct pollfd *mc_receive_sockets = NULL;
98 static size_t mc_receive_sockets_num = 0;
100 static socket_entry_t *mc_send_sockets = NULL;
101 static size_t mc_send_sockets_num = 0;
102 static pthread_mutex_t mc_send_sockets_lock = PTHREAD_MUTEX_INITIALIZER;
104 static int mc_receive_thread_loop = 0;
105 static int mc_receive_thread_running = 0;
106 static pthread_t mc_receive_thread_id;
108 static metric_map_t metric_map_default[] =
109 { /*---------------+-------------+-----------+-------------+------+-----*
110 * ganglia_name ! type ! type_inst ! data_source ! type ! idx *
111 *---------------+-------------+-----------+-------------+------+-----*/
112 { "load_one", "load", "", "shortterm", -1, -1 },
113 { "load_five", "load", "", "midterm", -1, -1 },
114 { "load_fifteen", "load", "", "longterm", -1, -1 },
115 { "cpu_user", "cpu", "user", "value", -1, -1 },
116 { "cpu_system", "cpu", "system", "value", -1, -1 },
117 { "cpu_idle", "cpu", "idle", "value", -1, -1 },
118 { "cpu_nice", "cpu", "nice", "value", -1, -1 },
119 { "cpu_wio", "cpu", "wait", "value", -1, -1 },
120 { "mem_free", "memory", "free", "value", -1, -1 },
121 { "mem_shared", "memory", "shared", "value", -1, -1 },
122 { "mem_buffers", "memory", "buffered", "value", -1, -1 },
123 { "mem_cached", "memory", "cached", "value", -1, -1 },
124 { "mem_total", "memory", "total", "value", -1, -1 },
125 { "bytes_in", "if_octets", "", "rx", -1, -1 },
126 { "bytes_out", "if_octets", "", "tx", -1, -1 },
127 { "pkts_in", "if_packets", "", "rx", -1, -1 },
128 { "pkts_out", "if_packets", "", "tx", -1, -1 }
129 };
130 static size_t metric_map_len_default = STATIC_ARRAY_SIZE (metric_map_default);
132 static metric_map_t *metric_map = NULL;
133 static size_t metric_map_len = 0;
135 static c_avl_tree_t *staging_tree;
136 static pthread_mutex_t staging_lock = PTHREAD_MUTEX_INITIALIZER;
138 static metric_map_t *metric_lookup (const char *key) /* {{{ */
139 {
140 metric_map_t *map;
141 size_t map_len;
142 size_t i;
144 /* Search the user-supplied table first.. */
145 map = metric_map;
146 map_len = metric_map_len;
147 for (i = 0; i < map_len; i++)
148 if (strcmp (map[i].ganglia_name, key) == 0)
149 break;
151 /* .. and fall back to the built-in table if nothing is found. */
152 if (i >= map_len)
153 {
154 map = metric_map_default;
155 map_len = metric_map_len_default;
157 for (i = 0; i < map_len; i++)
158 if (strcmp (map[i].ganglia_name, key) == 0)
159 break;
160 }
162 if (i >= map_len)
163 return (NULL);
165 /* Look up the DS type and ds_index. */
166 if (map[i].ds_type < 0) /* {{{ */
167 {
168 const data_set_t *ds;
170 ds = plugin_get_ds (map[i].type);
171 if (ds == NULL)
172 {
173 WARNING ("gmond plugin: Type not defined: %s", map[i].type);
174 return (NULL);
175 }
177 if ((map[i].ds_name == NULL) && (ds->ds_num != 1))
178 {
179 WARNING ("gmond plugin: No data source name defined for metric %s, "
180 "but type %s has more than one data source.",
181 map[i].ganglia_name, map[i].type);
182 return (NULL);
183 }
185 if (map[i].ds_name == NULL)
186 {
187 map[i].ds_index = 0;
188 }
189 else
190 {
191 size_t j;
193 for (j = 0; j < ds->ds_num; j++)
194 if (strcasecmp (ds->ds[j].name, map[i].ds_name) == 0)
195 break;
197 if (j >= ds->ds_num)
198 {
199 WARNING ("gmond plugin: There is no data source "
200 "named `%s' in type `%s'.",
201 map[i].ds_name, ds->type);
202 return (NULL);
203 }
204 map[i].ds_index = j;
205 }
207 map[i].ds_type = ds->ds[map[i].ds_index].type;
208 } /* }}} if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) */
210 return (map + i);
211 } /* }}} metric_map_t *metric_lookup */
213 static int create_sockets (socket_entry_t **ret_sockets, /* {{{ */
214 size_t *ret_sockets_num,
215 const char *node, const char *service, int listen)
216 {
217 struct addrinfo ai_hints;
218 struct addrinfo *ai_list;
219 struct addrinfo *ai_ptr;
220 int ai_return;
222 socket_entry_t *sockets = NULL;
223 size_t sockets_num = 0;
225 int status;
227 if (*ret_sockets != NULL)
228 return (EINVAL);
230 memset (&ai_hints, 0, sizeof (ai_hints));
231 ai_hints.ai_flags = 0;
232 #ifdef AI_PASSIVE
233 ai_hints.ai_flags |= AI_PASSIVE;
234 #endif
235 #ifdef AI_ADDRCONFIG
236 ai_hints.ai_flags |= AI_ADDRCONFIG;
237 #endif
238 ai_hints.ai_family = AF_UNSPEC;
239 ai_hints.ai_socktype = SOCK_DGRAM;
240 ai_hints.ai_protocol = IPPROTO_UDP;
242 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
243 if (ai_return != 0)
244 {
245 char errbuf[1024];
246 ERROR ("gmond plugin: getaddrinfo (%s, %s) failed: %s",
247 (node == NULL) ? "(null)" : node,
248 (service == NULL) ? "(null)" : service,
249 (ai_return == EAI_SYSTEM)
250 ? sstrerror (errno, errbuf, sizeof (errbuf))
251 : gai_strerror (ai_return));
252 return (-1);
253 }
255 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) /* {{{ */
256 {
257 socket_entry_t *tmp;
259 tmp = realloc (sockets, (sockets_num + 1) * sizeof (*sockets));
260 if (tmp == NULL)
261 {
262 ERROR ("gmond plugin: realloc failed.");
263 continue;
264 }
265 sockets = tmp;
267 sockets[sockets_num].fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
268 ai_ptr->ai_protocol);
269 if (sockets[sockets_num].fd < 0)
270 {
271 char errbuf[1024];
272 ERROR ("gmond plugin: socket failed: %s",
273 sstrerror (errno, errbuf, sizeof (errbuf)));
274 continue;
275 }
277 assert (sizeof (sockets[sockets_num].addr) >= ai_ptr->ai_addrlen);
278 memcpy (&sockets[sockets_num].addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
279 sockets[sockets_num].addrlen = ai_ptr->ai_addrlen;
281 /* Sending socket: Open only one socket and don't bind it. */
282 if (listen == 0)
283 {
284 sockets_num++;
285 break;
286 }
287 else
288 {
289 int yes = 1;
291 status = setsockopt (sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR,
292 (void *) &yes, sizeof (yes));
293 if (status != 0)
294 {
295 char errbuf[1024];
296 WARNING ("gmond plugin: setsockopt(2) failed: %s",
297 sstrerror (errno, errbuf, sizeof (errbuf)));
298 }
299 }
301 status = bind (sockets[sockets_num].fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
302 if (status != 0)
303 {
304 char errbuf[1024];
305 ERROR ("gmond plugin: bind failed: %s",
306 sstrerror (errno, errbuf, sizeof (errbuf)));
307 close (sockets[sockets_num].fd);
308 continue;
309 }
311 if (ai_ptr->ai_family == AF_INET)
312 {
313 struct sockaddr_in *addr;
314 struct ip_mreq mreq;
315 int loop;
317 addr = (struct sockaddr_in *) ai_ptr->ai_addr;
319 if (!IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
320 {
321 sockets_num++;
322 continue;
323 }
325 loop = 1;
326 status = setsockopt (sockets[sockets_num].fd, IPPROTO_IP, IP_MULTICAST_LOOP,
327 (void *) &loop, sizeof (loop));
328 if (status != 0)
329 {
330 char errbuf[1024];
331 WARNING ("gmond plugin: setsockopt(2) failed: %s",
332 sstrerror (errno, errbuf, sizeof (errbuf)));
333 }
335 memset (&mreq, 0, sizeof (mreq));
336 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
337 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
339 status = setsockopt (sockets[sockets_num].fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
340 (void *) &mreq, sizeof (mreq));
341 if (status != 0)
342 {
343 char errbuf[1024];
344 WARNING ("gmond plugin: setsockopt(2) failed: %s",
345 sstrerror (errno, errbuf, sizeof (errbuf)));
346 }
347 } /* if (ai_ptr->ai_family == AF_INET) */
348 else if (ai_ptr->ai_family == AF_INET6)
349 {
350 struct sockaddr_in6 *addr;
351 struct ipv6_mreq mreq;
352 int loop;
354 addr = (struct sockaddr_in6 *) ai_ptr->ai_addr;
356 if (!IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
357 {
358 sockets_num++;
359 continue;
360 }
362 loop = 1;
363 status = setsockopt (sockets[sockets_num].fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
364 (void *) &loop, sizeof (loop));
365 if (status != 0)
366 {
367 char errbuf[1024];
368 WARNING ("gmond plugin: setsockopt(2) failed: %s",
369 sstrerror (errno, errbuf, sizeof (errbuf)));
370 }
372 memset (&mreq, 0, sizeof (mreq));
373 memcpy (&mreq.ipv6mr_multiaddr,
374 &addr->sin6_addr, sizeof (addr->sin6_addr));
375 mreq.ipv6mr_interface = 0; /* any */
376 status = setsockopt (sockets[sockets_num].fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
377 (void *) &mreq, sizeof (mreq));
378 if (status != 0)
379 {
380 char errbuf[1024];
381 WARNING ("gmond plugin: setsockopt(2) failed: %s",
382 sstrerror (errno, errbuf, sizeof (errbuf)));
383 }
384 } /* if (ai_ptr->ai_family == AF_INET6) */
386 sockets_num++;
387 } /* }}} for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
389 freeaddrinfo (ai_list);
391 if (sockets_num == 0)
392 {
393 sfree (sockets);
394 return (-1);
395 }
397 *ret_sockets = sockets;
398 *ret_sockets_num = sockets_num;
399 return (0);
400 } /* }}} int create_sockets */
402 static int request_meta_data (const char *host, const char *name) /* {{{ */
403 {
404 Ganglia_metadata_msg msg;
405 char buffer[BUFF_SIZE];
406 unsigned int buffer_size;
407 XDR xdr;
408 size_t i;
410 memset (&msg, 0, sizeof (msg));
412 msg.id = gmetadata_request;
413 msg.Ganglia_metadata_msg_u.grequest.metric_id.host = strdup (host);
414 msg.Ganglia_metadata_msg_u.grequest.metric_id.name = strdup (name);
416 if ((msg.Ganglia_metadata_msg_u.grequest.metric_id.host == NULL)
417 || (msg.Ganglia_metadata_msg_u.grequest.metric_id.name == NULL))
418 {
419 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
420 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
421 return (-1);
422 }
424 memset (buffer, 0, sizeof (buffer));
425 xdrmem_create (&xdr, buffer, sizeof (buffer), XDR_ENCODE);
427 if (!xdr_Ganglia_metadata_msg (&xdr, &msg))
428 {
429 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
430 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
431 return (-1);
432 }
434 buffer_size = xdr_getpos (&xdr);
436 DEBUG ("gmond plugin: Requesting meta data for %s/%s.",
437 host, name);
439 pthread_mutex_lock (&mc_send_sockets_lock);
440 for (i = 0; i < mc_send_sockets_num; i++)
441 {
442 ssize_t status = sendto (mc_send_sockets[i].fd, buffer, (size_t) buffer_size,
443 /* flags = */ 0,
444 (struct sockaddr *) &mc_send_sockets[i].addr,
445 mc_send_sockets[i].addrlen);
446 if (status == -1)
447 {
448 char errbuf[1024];
449 ERROR ("gmond plugin: sendto(2) failed: %s",
450 sstrerror (errno, errbuf, sizeof (errbuf)));
451 continue;
452 }
453 }
454 pthread_mutex_unlock (&mc_send_sockets_lock);
456 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
457 sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
458 return (0);
459 } /* }}} int request_meta_data */
461 static staging_entry_t *staging_entry_get (const char *host, /* {{{ */
462 const char *name,
463 const char *type, const char *type_instance,
464 int values_len)
465 {
466 char key[2 * DATA_MAX_NAME_LEN];
467 staging_entry_t *se;
468 int status;
470 if (staging_tree == NULL)
471 return (NULL);
473 ssnprintf (key, sizeof (key), "%s/%s/%s", host, type,
474 (type_instance != NULL) ? type_instance : "");
476 se = NULL;
477 status = c_avl_get (staging_tree, key, (void *) &se);
478 if (status == 0)
479 return (se);
481 /* insert new entry */
482 se = calloc (1, sizeof (*se));
483 if (se == NULL)
484 return (NULL);
486 sstrncpy (se->key, key, sizeof (se->key));
487 se->flags = 0;
489 se->vl.values = (value_t *) calloc (values_len, sizeof (*se->vl.values));
490 if (se->vl.values == NULL)
491 {
492 sfree (se);
493 return (NULL);
494 }
495 se->vl.values_len = values_len;
497 se->vl.time = 0;
498 se->vl.interval = 0;
499 sstrncpy (se->vl.host, host, sizeof (se->vl.host));
500 sstrncpy (se->vl.plugin, "gmond", sizeof (se->vl.plugin));
501 sstrncpy (se->vl.type, type, sizeof (se->vl.type));
502 if (type_instance != NULL)
503 sstrncpy (se->vl.type_instance, type_instance,
504 sizeof (se->vl.type_instance));
506 status = c_avl_insert (staging_tree, se->key, se);
507 if (status != 0)
508 {
509 ERROR ("gmond plugin: c_avl_insert failed.");
510 sfree (se->vl.values);
511 sfree (se);
512 return (NULL);
513 }
515 return (se);
516 } /* }}} staging_entry_t *staging_entry_get */
518 static int staging_entry_update (const char *host, const char *name, /* {{{ */
519 const char *type, const char *type_instance,
520 size_t ds_index, int ds_type, value_t value)
521 {
522 const data_set_t *ds;
523 staging_entry_t *se;
525 ds = plugin_get_ds (type);
526 if (ds == NULL)
527 {
528 ERROR ("gmond plugin: Looking up type %s failed.", type);
529 return (-1);
530 }
532 if (ds->ds_num <= ds_index)
533 {
534 ERROR ("gmond plugin: Invalid index %zu: %s has only %zu data source(s).",
535 ds_index, ds->type, ds->ds_num);
536 return (-1);
537 }
539 pthread_mutex_lock (&staging_lock);
541 se = staging_entry_get (host, name, type, type_instance, ds->ds_num);
542 if (se == NULL)
543 {
544 pthread_mutex_unlock (&staging_lock);
545 ERROR ("gmond plugin: staging_entry_get failed.");
546 return (-1);
547 }
548 if (se->vl.values_len != ds->ds_num)
549 {
550 pthread_mutex_unlock (&staging_lock);
551 return (-1);
552 }
554 if (ds_type == DS_TYPE_COUNTER)
555 se->vl.values[ds_index].counter += value.counter;
556 else if (ds_type == DS_TYPE_GAUGE)
557 se->vl.values[ds_index].gauge = value.gauge;
558 else if (ds_type == DS_TYPE_DERIVE)
559 se->vl.values[ds_index].derive += value.derive;
560 else if (ds_type == DS_TYPE_ABSOLUTE)
561 se->vl.values[ds_index].absolute = value.absolute;
562 else
563 assert (23 == 42);
565 se->flags |= (0x01 << ds_index);
567 /* Check if all data sources have been set. If not, return here. */
568 if (se->flags != ((0x01 << se->vl.values_len) - 1))
569 {
570 pthread_mutex_unlock (&staging_lock);
571 return (0);
572 }
574 /* Check if the interval of this metric is known. If not, request meta data
575 * and return. */
576 if (se->vl.interval == 0)
577 {
578 /* No meta data has been received for this metric yet. */
579 se->flags = 0;
580 pthread_mutex_unlock (&staging_lock);
582 request_meta_data (host, name);
583 return (0);
584 }
586 plugin_dispatch_values (&se->vl);
588 se->flags = 0;
589 pthread_mutex_unlock (&staging_lock);
591 return (0);
592 } /* }}} int staging_entry_update */
594 static int mc_handle_value_msg (Ganglia_value_msg *msg) /* {{{ */
595 {
596 const char *host;
597 const char *name;
598 metric_map_t *map;
600 value_t value_counter;
601 value_t value_gauge;
602 value_t value_derive;
604 /* Fill in `host', `name', `value_counter', and `value_gauge' according to
605 * the value type, or return with an error. */
606 switch (msg->id) /* {{{ */
607 {
608 case gmetric_uint:
609 {
610 Ganglia_gmetric_uint msg_uint;
612 msg_uint = msg->Ganglia_value_msg_u.gu_int;
614 host = msg_uint.metric_id.host;
615 name = msg_uint.metric_id.name;
616 value_counter.counter = (counter_t) msg_uint.ui;
617 value_gauge.gauge = (gauge_t) msg_uint.ui;
618 value_derive.derive = (derive_t) msg_uint.ui;
619 break;
620 }
622 case gmetric_string:
623 {
624 Ganglia_gmetric_string msg_string;
625 int status;
627 msg_string = msg->Ganglia_value_msg_u.gstr;
629 host = msg_string.metric_id.host;
630 name = msg_string.metric_id.name;
632 status = parse_value (msg_string.str, &value_derive, DS_TYPE_DERIVE);
633 if (status != 0)
634 value_derive.derive = -1;
636 status = parse_value (msg_string.str, &value_gauge, DS_TYPE_GAUGE);
637 if (status != 0)
638 value_gauge.gauge = NAN;
640 status = parse_value (msg_string.str, &value_counter, DS_TYPE_COUNTER);
641 if (status != 0)
642 value_counter.counter = 0;
644 break;
645 }
647 case gmetric_float:
648 {
649 Ganglia_gmetric_float msg_float;
651 msg_float = msg->Ganglia_value_msg_u.gf;
653 host = msg_float.metric_id.host;
654 name = msg_float.metric_id.name;
655 value_counter.counter = (counter_t) msg_float.f;
656 value_gauge.gauge = (gauge_t) msg_float.f;
657 value_derive.derive = (derive_t) msg_float.f;
658 break;
659 }
661 case gmetric_double:
662 {
663 Ganglia_gmetric_double msg_double;
665 msg_double = msg->Ganglia_value_msg_u.gd;
667 host = msg_double.metric_id.host;
668 name = msg_double.metric_id.name;
669 value_counter.counter = (counter_t) msg_double.d;
670 value_gauge.gauge = (gauge_t) msg_double.d;
671 value_derive.derive = (derive_t) msg_double.d;
672 break;
673 }
674 default:
675 DEBUG ("gmond plugin: Value type not handled: %i", msg->id);
676 return (-1);
677 } /* }}} switch (msg->id) */
679 assert (host != NULL);
680 assert (name != NULL);
682 map = metric_lookup (name);
683 if (map != NULL)
684 {
685 value_t val_copy;
687 if ((map->ds_type == DS_TYPE_COUNTER)
688 || (map->ds_type == DS_TYPE_ABSOLUTE))
689 val_copy = value_counter;
690 else if (map->ds_type == DS_TYPE_GAUGE)
691 val_copy = value_gauge;
692 else if (map->ds_type == DS_TYPE_DERIVE)
693 val_copy = value_derive;
694 else
695 assert (23 == 42);
697 return (staging_entry_update (host, name,
698 map->type, map->type_instance,
699 map->ds_index, map->ds_type,
700 val_copy));
701 }
703 DEBUG ("gmond plugin: Cannot find a translation for %s.", name);
704 return (-1);
705 } /* }}} int mc_handle_value_msg */
707 static int mc_handle_metadata_msg (Ganglia_metadata_msg *msg) /* {{{ */
708 {
709 switch (msg->id)
710 {
711 case gmetadata_full:
712 {
713 Ganglia_metadatadef msg_meta;
714 staging_entry_t *se;
715 const data_set_t *ds;
716 metric_map_t *map;
718 msg_meta = msg->Ganglia_metadata_msg_u.gfull;
720 if (msg_meta.metric.tmax <= 0)
721 return (-1);
723 map = metric_lookup (msg_meta.metric_id.name);
724 if (map == NULL)
725 {
726 DEBUG ("gmond plugin: Not handling meta data %s.",
727 msg_meta.metric_id.name);
728 return (0);
729 }
731 ds = plugin_get_ds (map->type);
732 if (ds == NULL)
733 {
734 WARNING ("gmond plugin: Could not find data set %s.", map->type);
735 return (-1);
736 }
738 DEBUG ("gmond plugin: Received meta data for %s/%s.",
739 msg_meta.metric_id.host, msg_meta.metric_id.name);
741 pthread_mutex_lock (&staging_lock);
742 se = staging_entry_get (msg_meta.metric_id.host,
743 msg_meta.metric_id.name,
744 map->type, map->type_instance,
745 ds->ds_num);
746 if (se != NULL)
747 se->vl.interval = TIME_T_TO_CDTIME_T (msg_meta.metric.tmax);
748 pthread_mutex_unlock (&staging_lock);
750 if (se == NULL)
751 {
752 ERROR ("gmond plugin: staging_entry_get failed.");
753 return (-1);
754 }
756 break;
757 }
759 default:
760 {
761 return (-1);
762 }
763 }
765 return (0);
766 } /* }}} int mc_handle_metadata_msg */
768 static int mc_handle_metric (void *buffer, size_t buffer_size) /* {{{ */
769 {
770 XDR xdr;
771 Ganglia_msg_formats format;
773 xdrmem_create (&xdr, buffer, buffer_size, XDR_DECODE);
775 xdr_Ganglia_msg_formats (&xdr, &format);
776 xdr_setpos (&xdr, 0);
778 switch (format)
779 {
780 case gmetric_ushort:
781 case gmetric_short:
782 case gmetric_int:
783 case gmetric_uint:
784 case gmetric_string:
785 case gmetric_float:
786 case gmetric_double:
787 {
788 Ganglia_value_msg msg;
790 memset (&msg, 0, sizeof (msg));
791 if (xdr_Ganglia_value_msg (&xdr, &msg))
792 mc_handle_value_msg (&msg);
793 break;
794 }
796 case gmetadata_full:
797 case gmetadata_request:
798 {
799 Ganglia_metadata_msg msg;
800 memset (&msg, 0, sizeof (msg));
801 if (xdr_Ganglia_metadata_msg (&xdr, &msg))
802 mc_handle_metadata_msg (&msg);
803 break;
804 }
806 default:
807 DEBUG ("gmond plugin: Unknown format: %i", format);
808 return (-1);
809 } /* switch (format) */
812 return (0);
813 } /* }}} int mc_handle_metric */
815 static int mc_handle_socket (struct pollfd *p) /* {{{ */
816 {
817 char buffer[BUFF_SIZE];
818 ssize_t buffer_size;
820 if ((p->revents & (POLLIN | POLLPRI)) == 0)
821 {
822 p->revents = 0;
823 return (-1);
824 }
826 buffer_size = recv (p->fd, buffer, sizeof (buffer), /* flags = */ 0);
827 if (buffer_size <= 0)
828 {
829 char errbuf[1024];
830 ERROR ("gmond plugin: recv failed: %s",
831 sstrerror (errno, errbuf, sizeof (errbuf)));
832 p->revents = 0;
833 return (-1);
834 }
836 mc_handle_metric (buffer, (size_t) buffer_size);
837 return (0);
838 } /* }}} int mc_handle_socket */
840 static void *mc_receive_thread (void *arg) /* {{{ */
841 {
842 socket_entry_t *mc_receive_socket_entries;
843 int status;
844 size_t i;
846 mc_receive_socket_entries = NULL;
847 status = create_sockets (&mc_receive_socket_entries, &mc_receive_sockets_num,
848 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
849 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
850 /* listen = */ 1);
851 if (status != 0)
852 {
853 ERROR ("gmond plugin: create_sockets failed.");
854 return ((void *) -1);
855 }
857 mc_receive_sockets = (struct pollfd *) calloc (mc_receive_sockets_num,
858 sizeof (*mc_receive_sockets));
859 if (mc_receive_sockets == NULL)
860 {
861 ERROR ("gmond plugin: calloc failed.");
862 for (i = 0; i < mc_receive_sockets_num; i++)
863 close (mc_receive_socket_entries[i].fd);
864 free (mc_receive_socket_entries);
865 mc_receive_socket_entries = NULL;
866 mc_receive_sockets_num = 0;
867 return ((void *) -1);
868 }
870 for (i = 0; i < mc_receive_sockets_num; i++)
871 {
872 mc_receive_sockets[i].fd = mc_receive_socket_entries[i].fd;
873 mc_receive_sockets[i].events = POLLIN | POLLPRI;
874 mc_receive_sockets[i].revents = 0;
875 }
877 while (mc_receive_thread_loop != 0)
878 {
879 status = poll (mc_receive_sockets, mc_receive_sockets_num, -1);
880 if (status <= 0)
881 {
882 char errbuf[1024];
883 if (errno == EINTR)
884 continue;
885 ERROR ("gmond plugin: poll failed: %s",
886 sstrerror (errno, errbuf, sizeof (errbuf)));
887 break;
888 }
890 for (i = 0; i < mc_receive_sockets_num; i++)
891 {
892 if (mc_receive_sockets[i].revents != 0)
893 mc_handle_socket (mc_receive_sockets + i);
894 }
895 } /* while (mc_receive_thread_loop != 0) */
897 free (mc_receive_socket_entries);
898 return ((void *) 0);
899 } /* }}} void *mc_receive_thread */
901 static int mc_receive_thread_start (void) /* {{{ */
902 {
903 int status;
905 if (mc_receive_thread_running != 0)
906 return (-1);
908 mc_receive_thread_loop = 1;
910 status = plugin_thread_create (&mc_receive_thread_id, /* attr = */ NULL,
911 mc_receive_thread, /* args = */ NULL);
912 if (status != 0)
913 {
914 ERROR ("gmond plugin: Starting receive thread failed.");
915 mc_receive_thread_loop = 0;
916 return (-1);
917 }
919 mc_receive_thread_running = 1;
920 return (0);
921 } /* }}} int start_receive_thread */
923 static int mc_receive_thread_stop (void) /* {{{ */
924 {
925 if (mc_receive_thread_running == 0)
926 return (-1);
928 mc_receive_thread_loop = 0;
930 INFO ("gmond plugin: Stopping receive thread.");
931 pthread_kill (mc_receive_thread_id, SIGTERM);
932 pthread_join (mc_receive_thread_id, /* return value = */ NULL);
933 memset (&mc_receive_thread_id, 0, sizeof (mc_receive_thread_id));
935 mc_receive_thread_running = 0;
937 return (0);
938 } /* }}} int mc_receive_thread_stop */
940 /*
941 * Config:
942 *
943 * <Plugin gmond>
944 * MCReceiveFrom "239.2.11.71" "8649"
945 * <Metric "load_one">
946 * Type "load"
947 * [TypeInstance "foo"]
948 * [DataSource "bar"]
949 * </Metric>
950 * </Plugin>
951 */
952 static int gmond_config_set_string (oconfig_item_t *ci, char **str) /* {{{ */
953 {
954 char *tmp;
956 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
957 {
958 WARNING ("gmond plugin: The `%s' option needs "
959 "exactly one string argument.", ci->key);
960 return (-1);
961 }
963 tmp = strdup (ci->values[0].value.string);
964 if (tmp == NULL)
965 {
966 ERROR ("gmond plugin: strdup failed.");
967 return (-1);
968 }
970 sfree (*str);
971 *str = tmp;
972 return (0);
973 } /* }}} int gmond_config_set_string */
975 static int gmond_config_add_metric (oconfig_item_t *ci) /* {{{ */
976 {
977 metric_map_t *map;
978 int i;
980 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
981 {
982 WARNING ("gmond plugin: `Metric' blocks need "
983 "exactly one string argument.");
984 return (-1);
985 }
987 map = realloc (metric_map, (metric_map_len + 1) * sizeof (*metric_map));
988 if (map == NULL)
989 {
990 ERROR ("gmond plugin: realloc failed.");
991 return (-1);
992 }
993 metric_map = map;
994 map = metric_map + metric_map_len;
996 memset (map, 0, sizeof (*map));
997 map->type = NULL;
998 map->type_instance = NULL;
999 map->ds_name = NULL;
1000 map->ds_type = -1;
1001 map->ds_index = -1;
1003 map->ganglia_name = strdup (ci->values[0].value.string);
1004 if (map->ganglia_name == NULL)
1005 {
1006 ERROR ("gmond plugin: strdup failed.");
1007 return (-1);
1008 }
1010 for (i = 0; i < ci->children_num; i++)
1011 {
1012 oconfig_item_t *child = ci->children + i;
1013 if (strcasecmp ("Type", child->key) == 0)
1014 gmond_config_set_string (child, &map->type);
1015 else if (strcasecmp ("TypeInstance", child->key) == 0)
1016 gmond_config_set_string (child, &map->type_instance);
1017 else if (strcasecmp ("DataSource", child->key) == 0)
1018 gmond_config_set_string (child, &map->ds_name);
1019 else
1020 {
1021 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
1022 child->key);
1023 }
1024 }
1026 if (map->type == NULL)
1027 {
1028 ERROR ("gmond plugin: No type is set for metric %s.",
1029 map->ganglia_name);
1030 sfree (map->ganglia_name);
1031 sfree (map->type_instance);
1032 return (-1);
1033 }
1035 metric_map_len++;
1036 return (0);
1037 } /* }}} int gmond_config_add_metric */
1039 static int gmond_config_set_address (oconfig_item_t *ci, /* {{{ */
1040 char **ret_addr, char **ret_port)
1041 {
1042 char *addr;
1043 char *port;
1045 if ((ci->values_num != 1) && (ci->values_num != 2))
1046 {
1047 WARNING ("gmond plugin: The `%s' config option needs "
1048 "one or two string arguments.",
1049 ci->key);
1050 return (-1);
1051 }
1052 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
1053 || ((ci->values_num == 2)
1054 && (ci->values[1].type != OCONFIG_TYPE_STRING)))
1055 {
1056 WARNING ("gmond plugin: The `%s' config option needs "
1057 "one or two string arguments.",
1058 ci->key);
1059 return (-1);
1060 }
1062 addr = strdup (ci->values[0].value.string);
1063 if (ci->values_num == 2)
1064 port = strdup (ci->values[1].value.string);
1065 else
1066 port = NULL;
1068 if ((addr == NULL) || ((ci->values_num == 2) && (port == NULL)))
1069 {
1070 ERROR ("gmond plugin: strdup failed.");
1071 sfree (addr);
1072 sfree (port);
1073 return (-1);
1074 }
1076 sfree (*ret_addr);
1077 sfree (*ret_port);
1079 *ret_addr = addr;
1080 *ret_port = port;
1082 return (0);
1083 } /* }}} int gmond_config_set_address */
1085 static int gmond_config (oconfig_item_t *ci) /* {{{ */
1086 {
1087 int i;
1089 for (i = 0; i < ci->children_num; i++)
1090 {
1091 oconfig_item_t *child = ci->children + i;
1092 if (strcasecmp ("MCReceiveFrom", child->key) == 0)
1093 gmond_config_set_address (child, &mc_receive_group, &mc_receive_port);
1094 else if (strcasecmp ("Metric", child->key) == 0)
1095 gmond_config_add_metric (child);
1096 else
1097 {
1098 WARNING ("gmond plugin: Unknown configuration option `%s' ignored.",
1099 child->key);
1100 }
1101 }
1103 return (0);
1104 } /* }}} int gmond_config */
1106 static int gmond_init (void) /* {{{ */
1107 {
1108 create_sockets (&mc_send_sockets, &mc_send_sockets_num,
1109 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
1110 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
1111 /* listen = */ 0);
1113 staging_tree = c_avl_create ((void *) strcmp);
1114 if (staging_tree == NULL)
1115 {
1116 ERROR ("gmond plugin: c_avl_create failed.");
1117 return (-1);
1118 }
1120 mc_receive_thread_start ();
1122 return (0);
1123 } /* }}} int gmond_init */
1125 static int gmond_shutdown (void) /* {{{ */
1126 {
1127 size_t i;
1129 mc_receive_thread_stop ();
1131 pthread_mutex_lock (&mc_send_sockets_lock);
1132 for (i = 0; i < mc_send_sockets_num; i++)
1133 {
1134 close (mc_send_sockets[i].fd);
1135 mc_send_sockets[i].fd = -1;
1136 }
1137 sfree (mc_send_sockets);
1138 mc_send_sockets_num = 0;
1139 pthread_mutex_unlock (&mc_send_sockets_lock);
1142 return (0);
1143 } /* }}} int gmond_shutdown */
1145 void module_register (void)
1146 {
1147 plugin_register_complex_config ("gmond", gmond_config);
1148 plugin_register_init ("gmond", gmond_init);
1149 plugin_register_shutdown ("gmond", gmond_shutdown);
1150 }
1152 /* vim: set sw=2 sts=2 et fdm=marker : */