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"
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_avltree.h"
33 #if HAVE_NETDB_H
34 #include <netdb.h>
35 #endif
36 #if HAVE_NETINET_IN_H
37 #include <netinet/in.h>
38 #endif
39 #if HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
41 #endif
42 #if HAVE_POLL_H
43 #include <poll.h>
44 #endif
46 #include <gm_protocol.h>
48 #ifndef IPV6_ADD_MEMBERSHIP
49 #ifdef IPV6_JOIN_GROUP
50 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
51 #else
52 #error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
53 #endif
54 #endif /* !IP_ADD_MEMBERSHIP */
56 #ifdef GANGLIA_MAX_MESSAGE_LEN
57 #define BUFF_SIZE GANGLIA_MAX_MESSAGE_LEN
58 #else
59 #define BUFF_SIZE 1400
60 #endif
62 struct socket_entry_s {
63 int fd;
64 struct sockaddr_storage addr;
65 socklen_t addrlen;
66 };
67 typedef struct socket_entry_s socket_entry_t;
69 struct staging_entry_s {
70 char key[2 * DATA_MAX_NAME_LEN];
71 value_list_t vl;
72 int flags;
73 };
74 typedef struct staging_entry_s staging_entry_t;
76 struct metric_map_s {
77 char *ganglia_name;
78 char *type;
79 char *type_instance;
80 char *ds_name;
81 int ds_type;
82 size_t ds_index;
83 };
84 typedef struct metric_map_s metric_map_t;
86 #define MC_RECEIVE_GROUP_DEFAULT "239.2.11.71"
87 static char *mc_receive_group = NULL;
88 #define MC_RECEIVE_PORT_DEFAULT "8649"
89 static char *mc_receive_port = NULL;
91 static struct pollfd *mc_receive_sockets = NULL;
92 static size_t mc_receive_sockets_num = 0;
94 static socket_entry_t *mc_send_sockets = NULL;
95 static size_t mc_send_sockets_num = 0;
96 static pthread_mutex_t mc_send_sockets_lock = PTHREAD_MUTEX_INITIALIZER;
98 static int mc_receive_thread_loop = 0;
99 static int mc_receive_thread_running = 0;
100 static pthread_t mc_receive_thread_id;
102 static metric_map_t metric_map_default[] =
103 {/*---------------+-------------+-----------+-------------+------+-----*
104 * ganglia_name ! type ! type_inst ! data_source ! type ! idx *
105 *---------------+-------------+-----------+-------------+------+-----*/
106 {"load_one", "load", "", "shortterm", -1, -1},
107 {"load_five", "load", "", "midterm", -1, -1},
108 {"load_fifteen", "load", "", "longterm", -1, -1},
109 {"cpu_user", "cpu", "user", "value", -1, -1},
110 {"cpu_system", "cpu", "system", "value", -1, -1},
111 {"cpu_idle", "cpu", "idle", "value", -1, -1},
112 {"cpu_nice", "cpu", "nice", "value", -1, -1},
113 {"cpu_wio", "cpu", "wait", "value", -1, -1},
114 {"mem_free", "memory", "free", "value", -1, -1},
115 {"mem_shared", "memory", "shared", "value", -1, -1},
116 {"mem_buffers", "memory", "buffered", "value", -1, -1},
117 {"mem_cached", "memory", "cached", "value", -1, -1},
118 {"mem_total", "memory", "total", "value", -1, -1},
119 {"bytes_in", "if_octets", "", "rx", -1, -1},
120 {"bytes_out", "if_octets", "", "tx", -1, -1},
121 {"pkts_in", "if_packets", "", "rx", -1, -1},
122 {"pkts_out", "if_packets", "", "tx", -1, -1}};
123 static size_t metric_map_len_default = STATIC_ARRAY_SIZE(metric_map_default);
125 static metric_map_t *metric_map = NULL;
126 static size_t metric_map_len = 0;
128 static c_avl_tree_t *staging_tree;
129 static pthread_mutex_t staging_lock = PTHREAD_MUTEX_INITIALIZER;
131 static metric_map_t *metric_lookup(const char *key) /* {{{ */
132 {
133 metric_map_t *map;
134 size_t map_len;
135 size_t i;
137 /* Search the user-supplied table first.. */
138 map = metric_map;
139 map_len = metric_map_len;
140 for (i = 0; i < map_len; i++)
141 if (strcmp(map[i].ganglia_name, key) == 0)
142 break;
144 /* .. and fall back to the built-in table if nothing is found. */
145 if (i >= map_len) {
146 map = metric_map_default;
147 map_len = metric_map_len_default;
149 for (i = 0; i < map_len; i++)
150 if (strcmp(map[i].ganglia_name, key) == 0)
151 break;
152 }
154 if (i >= map_len)
155 return (NULL);
157 /* Look up the DS type and ds_index. */
158 if (map[i].ds_type < 0) /* {{{ */
159 {
160 const data_set_t *ds;
162 ds = plugin_get_ds(map[i].type);
163 if (ds == NULL) {
164 WARNING("gmond plugin: Type not defined: %s", map[i].type);
165 return (NULL);
166 }
168 if ((map[i].ds_name == NULL) && (ds->ds_num != 1)) {
169 WARNING("gmond plugin: No data source name defined for metric %s, "
170 "but type %s has more than one data source.",
171 map[i].ganglia_name, map[i].type);
172 return (NULL);
173 }
175 if (map[i].ds_name == NULL) {
176 map[i].ds_index = 0;
177 } else {
178 size_t j;
180 for (j = 0; j < ds->ds_num; j++)
181 if (strcasecmp(ds->ds[j].name, map[i].ds_name) == 0)
182 break;
184 if (j >= ds->ds_num) {
185 WARNING("gmond plugin: There is no data source "
186 "named `%s' in type `%s'.",
187 map[i].ds_name, ds->type);
188 return (NULL);
189 }
190 map[i].ds_index = j;
191 }
193 map[i].ds_type = ds->ds[map[i].ds_index].type;
194 } /* }}} if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) */
196 return (map + i);
197 } /* }}} metric_map_t *metric_lookup */
199 static int create_sockets(socket_entry_t **ret_sockets, /* {{{ */
200 size_t *ret_sockets_num, const char *node,
201 const char *service, int listen) {
202 struct addrinfo *ai_list;
203 int ai_return;
205 socket_entry_t *sockets = NULL;
206 size_t sockets_num = 0;
208 int status;
210 if (*ret_sockets != NULL)
211 return (EINVAL);
213 struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
214 .ai_flags = AI_ADDRCONFIG | AI_PASSIVE,
215 .ai_protocol = IPPROTO_UDP,
216 .ai_socktype = SOCK_DGRAM};
218 ai_return = getaddrinfo(node, service, &ai_hints, &ai_list);
219 if (ai_return != 0) {
220 char errbuf[1024];
221 ERROR("gmond plugin: getaddrinfo (%s, %s) failed: %s",
222 (node == NULL) ? "(null)" : node,
223 (service == NULL) ? "(null)" : service,
224 (ai_return == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
225 : gai_strerror(ai_return));
226 return (-1);
227 }
229 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
230 ai_ptr = ai_ptr->ai_next) /* {{{ */
231 {
232 socket_entry_t *tmp;
234 tmp = realloc(sockets, (sockets_num + 1) * sizeof(*sockets));
235 if (tmp == NULL) {
236 ERROR("gmond plugin: realloc failed.");
237 continue;
238 }
239 sockets = tmp;
241 sockets[sockets_num].fd =
242 socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
243 if (sockets[sockets_num].fd < 0) {
244 char errbuf[1024];
245 ERROR("gmond plugin: socket failed: %s",
246 sstrerror(errno, errbuf, sizeof(errbuf)));
247 continue;
248 }
250 assert(sizeof(sockets[sockets_num].addr) >= ai_ptr->ai_addrlen);
251 memcpy(&sockets[sockets_num].addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
252 sockets[sockets_num].addrlen = ai_ptr->ai_addrlen;
254 /* Sending socket: Open only one socket and don't bind it. */
255 if (listen == 0) {
256 sockets_num++;
257 break;
258 } else {
259 int yes = 1;
261 status = setsockopt(sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR,
262 (void *)&yes, sizeof(yes));
263 if (status != 0) {
264 char errbuf[1024];
265 WARNING("gmond plugin: setsockopt(2) failed: %s",
266 sstrerror(errno, errbuf, sizeof(errbuf)));
267 }
268 }
270 status = bind(sockets[sockets_num].fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
271 if (status != 0) {
272 char errbuf[1024];
273 ERROR("gmond plugin: bind failed: %s",
274 sstrerror(errno, errbuf, sizeof(errbuf)));
275 close(sockets[sockets_num].fd);
276 continue;
277 }
279 if (ai_ptr->ai_family == AF_INET) {
280 struct sockaddr_in *addr;
281 int loop;
283 addr = (struct sockaddr_in *)ai_ptr->ai_addr;
285 if (!IN_MULTICAST(ntohl(addr->sin_addr.s_addr))) {
286 sockets_num++;
287 continue;
288 }
290 loop = 1;
291 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IP,
292 IP_MULTICAST_LOOP, (void *)&loop, sizeof(loop));
293 if (status != 0) {
294 char errbuf[1024];
295 WARNING("gmond plugin: setsockopt(2) failed: %s",
296 sstrerror(errno, errbuf, sizeof(errbuf)));
297 }
299 struct ip_mreq mreq = {.imr_multiaddr.s_addr = addr->sin_addr.s_addr,
300 .imr_interface.s_addr = htonl(INADDR_ANY)};
302 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IP,
303 IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq));
304 if (status != 0) {
305 char errbuf[1024];
306 WARNING("gmond plugin: setsockopt(2) failed: %s",
307 sstrerror(errno, errbuf, sizeof(errbuf)));
308 }
309 } /* if (ai_ptr->ai_family == AF_INET) */
310 else if (ai_ptr->ai_family == AF_INET6) {
311 struct sockaddr_in6 *addr;
312 int loop;
314 addr = (struct sockaddr_in6 *)ai_ptr->ai_addr;
316 if (!IN6_IS_ADDR_MULTICAST(&addr->sin6_addr)) {
317 sockets_num++;
318 continue;
319 }
321 loop = 1;
322 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IPV6,
323 IPV6_MULTICAST_LOOP, (void *)&loop, sizeof(loop));
324 if (status != 0) {
325 char errbuf[1024];
326 WARNING("gmond plugin: setsockopt(2) failed: %s",
327 sstrerror(errno, errbuf, sizeof(errbuf)));
328 }
330 struct ipv6_mreq mreq = {
331 .ipv6mr_interface = 0 /* any */
332 };
334 memcpy(&mreq.ipv6mr_multiaddr, &addr->sin6_addr, sizeof(addr->sin6_addr));
335 status = setsockopt(sockets[sockets_num].fd, IPPROTO_IPV6,
336 IPV6_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq));
337 if (status != 0) {
338 char errbuf[1024];
339 WARNING("gmond plugin: setsockopt(2) failed: %s",
340 sstrerror(errno, errbuf, sizeof(errbuf)));
341 }
342 } /* if (ai_ptr->ai_family == AF_INET6) */
344 sockets_num++;
345 } /* }}} for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
347 freeaddrinfo(ai_list);
349 if (sockets_num == 0) {
350 sfree(sockets);
351 return (-1);
352 }
354 *ret_sockets = sockets;
355 *ret_sockets_num = sockets_num;
356 return (0);
357 } /* }}} int create_sockets */
359 static int request_meta_data(const char *host, const char *name) /* {{{ */
360 {
361 Ganglia_metadata_msg msg = {0};
362 char buffer[BUFF_SIZE] = {0};
363 unsigned int buffer_size;
364 XDR xdr;
366 msg.id = gmetadata_request;
367 msg.Ganglia_metadata_msg_u.grequest.metric_id.host = strdup(host);
368 msg.Ganglia_metadata_msg_u.grequest.metric_id.name = strdup(name);
370 if ((msg.Ganglia_metadata_msg_u.grequest.metric_id.host == NULL) ||
371 (msg.Ganglia_metadata_msg_u.grequest.metric_id.name == NULL)) {
372 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
373 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
374 return (-1);
375 }
377 xdrmem_create(&xdr, buffer, sizeof(buffer), XDR_ENCODE);
379 if (!xdr_Ganglia_metadata_msg(&xdr, &msg)) {
380 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
381 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
382 return (-1);
383 }
385 buffer_size = xdr_getpos(&xdr);
387 DEBUG("gmond plugin: Requesting meta data for %s/%s.", host, name);
389 pthread_mutex_lock(&mc_send_sockets_lock);
390 for (size_t i = 0; i < mc_send_sockets_num; i++) {
391 ssize_t status =
392 sendto(mc_send_sockets[i].fd, buffer, (size_t)buffer_size,
393 /* flags = */ 0, (struct sockaddr *)&mc_send_sockets[i].addr,
394 mc_send_sockets[i].addrlen);
395 if (status == -1) {
396 char errbuf[1024];
397 ERROR("gmond plugin: sendto(2) failed: %s",
398 sstrerror(errno, errbuf, sizeof(errbuf)));
399 continue;
400 }
401 }
402 pthread_mutex_unlock(&mc_send_sockets_lock);
404 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
405 sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name);
406 return (0);
407 } /* }}} int request_meta_data */
409 static staging_entry_t *staging_entry_get(const char *host, /* {{{ */
410 const char *name, const char *type,
411 const char *type_instance,
412 int values_len) {
413 char key[2 * DATA_MAX_NAME_LEN];
414 staging_entry_t *se;
415 int status;
417 if (staging_tree == NULL)
418 return (NULL);
420 ssnprintf(key, sizeof(key), "%s/%s/%s", host, type,
421 (type_instance != NULL) ? type_instance : "");
423 se = NULL;
424 status = c_avl_get(staging_tree, key, (void *)&se);
425 if (status == 0)
426 return (se);
428 /* insert new entry */
429 se = calloc(1, sizeof(*se));
430 if (se == NULL)
431 return (NULL);
433 sstrncpy(se->key, key, sizeof(se->key));
434 se->flags = 0;
436 se->vl.values = (value_t *)calloc(values_len, sizeof(*se->vl.values));
437 if (se->vl.values == NULL) {
438 sfree(se);
439 return (NULL);
440 }
441 se->vl.values_len = values_len;
443 se->vl.time = 0;
444 se->vl.interval = 0;
445 sstrncpy(se->vl.host, host, sizeof(se->vl.host));
446 sstrncpy(se->vl.plugin, "gmond", sizeof(se->vl.plugin));
447 sstrncpy(se->vl.type, type, sizeof(se->vl.type));
448 if (type_instance != NULL)
449 sstrncpy(se->vl.type_instance, type_instance, sizeof(se->vl.type_instance));
451 status = c_avl_insert(staging_tree, se->key, se);
452 if (status != 0) {
453 ERROR("gmond plugin: c_avl_insert failed.");
454 sfree(se->vl.values);
455 sfree(se);
456 return (NULL);
457 }
459 return (se);
460 } /* }}} staging_entry_t *staging_entry_get */
462 static int staging_entry_update(const char *host, const char *name, /* {{{ */
463 const char *type, const char *type_instance,
464 size_t ds_index, int ds_type, value_t value) {
465 const data_set_t *ds;
466 staging_entry_t *se;
468 ds = plugin_get_ds(type);
469 if (ds == NULL) {
470 ERROR("gmond plugin: Looking up type %s failed.", type);
471 return (-1);
472 }
474 if (ds->ds_num <= ds_index) {
475 ERROR("gmond plugin: Invalid index %zu: %s has only %zu data source(s).",
476 ds_index, ds->type, ds->ds_num);
477 return (-1);
478 }
480 pthread_mutex_lock(&staging_lock);
482 se = staging_entry_get(host, name, type, type_instance, ds->ds_num);
483 if (se == NULL) {
484 pthread_mutex_unlock(&staging_lock);
485 ERROR("gmond plugin: staging_entry_get failed.");
486 return (-1);
487 }
488 if (se->vl.values_len != ds->ds_num) {
489 pthread_mutex_unlock(&staging_lock);
490 return (-1);
491 }
493 if (ds_type == DS_TYPE_COUNTER)
494 se->vl.values[ds_index].counter += value.counter;
495 else if (ds_type == DS_TYPE_GAUGE)
496 se->vl.values[ds_index].gauge = value.gauge;
497 else if (ds_type == DS_TYPE_DERIVE)
498 se->vl.values[ds_index].derive += value.derive;
499 else if (ds_type == DS_TYPE_ABSOLUTE)
500 se->vl.values[ds_index].absolute = value.absolute;
501 else
502 assert(23 == 42);
504 se->flags |= (0x01 << ds_index);
506 /* Check if all data sources have been set. If not, return here. */
507 if (se->flags != ((0x01 << se->vl.values_len) - 1)) {
508 pthread_mutex_unlock(&staging_lock);
509 return (0);
510 }
512 /* Check if the interval of this metric is known. If not, request meta data
513 * and return. */
514 if (se->vl.interval == 0) {
515 /* No meta data has been received for this metric yet. */
516 se->flags = 0;
517 pthread_mutex_unlock(&staging_lock);
519 request_meta_data(host, name);
520 return (0);
521 }
523 plugin_dispatch_values(&se->vl);
525 se->flags = 0;
526 pthread_mutex_unlock(&staging_lock);
528 return (0);
529 } /* }}} int staging_entry_update */
531 static int mc_handle_value_msg(Ganglia_value_msg *msg) /* {{{ */
532 {
533 const char *host;
534 const char *name;
535 metric_map_t *map;
537 value_t value_counter;
538 value_t value_gauge;
539 value_t value_derive;
541 /* Fill in `host', `name', `value_counter', and `value_gauge' according to
542 * the value type, or return with an error. */
543 switch (msg->id) /* {{{ */
544 {
545 case gmetric_uint: {
546 Ganglia_gmetric_uint msg_uint;
548 msg_uint = msg->Ganglia_value_msg_u.gu_int;
550 host = msg_uint.metric_id.host;
551 name = msg_uint.metric_id.name;
552 value_counter.counter = (counter_t)msg_uint.ui;
553 value_gauge.gauge = (gauge_t)msg_uint.ui;
554 value_derive.derive = (derive_t)msg_uint.ui;
555 break;
556 }
558 case gmetric_string: {
559 Ganglia_gmetric_string msg_string;
560 int status;
562 msg_string = msg->Ganglia_value_msg_u.gstr;
564 host = msg_string.metric_id.host;
565 name = msg_string.metric_id.name;
567 status = parse_value(msg_string.str, &value_derive, DS_TYPE_DERIVE);
568 if (status != 0)
569 value_derive.derive = -1;
571 status = parse_value(msg_string.str, &value_gauge, DS_TYPE_GAUGE);
572 if (status != 0)
573 value_gauge.gauge = NAN;
575 status = parse_value(msg_string.str, &value_counter, DS_TYPE_COUNTER);
576 if (status != 0)
577 value_counter.counter = 0;
579 break;
580 }
582 case gmetric_float: {
583 Ganglia_gmetric_float msg_float;
585 msg_float = msg->Ganglia_value_msg_u.gf;
587 host = msg_float.metric_id.host;
588 name = msg_float.metric_id.name;
589 value_counter.counter = (counter_t)msg_float.f;
590 value_gauge.gauge = (gauge_t)msg_float.f;
591 value_derive.derive = (derive_t)msg_float.f;
592 break;
593 }
595 case gmetric_double: {
596 Ganglia_gmetric_double msg_double;
598 msg_double = msg->Ganglia_value_msg_u.gd;
600 host = msg_double.metric_id.host;
601 name = msg_double.metric_id.name;
602 value_counter.counter = (counter_t)msg_double.d;
603 value_gauge.gauge = (gauge_t)msg_double.d;
604 value_derive.derive = (derive_t)msg_double.d;
605 break;
606 }
607 default:
608 DEBUG("gmond plugin: Value type not handled: %i", msg->id);
609 return (-1);
610 } /* }}} switch (msg->id) */
612 assert(host != NULL);
613 assert(name != NULL);
615 map = metric_lookup(name);
616 if (map != NULL) {
617 value_t val_copy;
619 if ((map->ds_type == DS_TYPE_COUNTER) || (map->ds_type == DS_TYPE_ABSOLUTE))
620 val_copy = value_counter;
621 else if (map->ds_type == DS_TYPE_GAUGE)
622 val_copy = value_gauge;
623 else if (map->ds_type == DS_TYPE_DERIVE)
624 val_copy = value_derive;
625 else
626 assert(23 == 42);
628 return (staging_entry_update(host, name, map->type, map->type_instance,
629 map->ds_index, map->ds_type, val_copy));
630 }
632 DEBUG("gmond plugin: Cannot find a translation for %s.", name);
633 return (-1);
634 } /* }}} int mc_handle_value_msg */
636 static int mc_handle_metadata_msg(Ganglia_metadata_msg *msg) /* {{{ */
637 {
638 switch (msg->id) {
639 case gmetadata_full: {
640 Ganglia_metadatadef msg_meta;
641 staging_entry_t *se;
642 const data_set_t *ds;
643 metric_map_t *map;
645 msg_meta = msg->Ganglia_metadata_msg_u.gfull;
647 if (msg_meta.metric.tmax == 0)
648 return (-1);
650 map = metric_lookup(msg_meta.metric_id.name);
651 if (map == NULL) {
652 DEBUG("gmond plugin: Not handling meta data %s.",
653 msg_meta.metric_id.name);
654 return (0);
655 }
657 ds = plugin_get_ds(map->type);
658 if (ds == NULL) {
659 WARNING("gmond plugin: Could not find data set %s.", map->type);
660 return (-1);
661 }
663 DEBUG("gmond plugin: Received meta data for %s/%s.",
664 msg_meta.metric_id.host, msg_meta.metric_id.name);
666 pthread_mutex_lock(&staging_lock);
667 se = staging_entry_get(msg_meta.metric_id.host, msg_meta.metric_id.name,
668 map->type, map->type_instance, ds->ds_num);
669 if (se != NULL)
670 se->vl.interval = TIME_T_TO_CDTIME_T(msg_meta.metric.tmax);
671 pthread_mutex_unlock(&staging_lock);
673 if (se == NULL) {
674 ERROR("gmond plugin: staging_entry_get failed.");
675 return (-1);
676 }
678 break;
679 }
681 default: { return (-1); }
682 }
684 return (0);
685 } /* }}} int mc_handle_metadata_msg */
687 static int mc_handle_metric(void *buffer, size_t buffer_size) /* {{{ */
688 {
689 XDR xdr;
690 Ganglia_msg_formats format;
692 xdrmem_create(&xdr, buffer, buffer_size, XDR_DECODE);
694 xdr_Ganglia_msg_formats(&xdr, &format);
695 xdr_setpos(&xdr, 0);
697 switch (format) {
698 case gmetric_ushort:
699 case gmetric_short:
700 case gmetric_int:
701 case gmetric_uint:
702 case gmetric_string:
703 case gmetric_float:
704 case gmetric_double: {
705 Ganglia_value_msg msg = {0};
707 if (xdr_Ganglia_value_msg(&xdr, &msg))
708 mc_handle_value_msg(&msg);
709 break;
710 }
712 case gmetadata_full:
713 case gmetadata_request: {
714 Ganglia_metadata_msg msg = {0};
715 if (xdr_Ganglia_metadata_msg(&xdr, &msg))
716 mc_handle_metadata_msg(&msg);
717 break;
718 }
720 default:
721 DEBUG("gmond plugin: Unknown format: %i", format);
722 return (-1);
723 } /* switch (format) */
725 return (0);
726 } /* }}} int mc_handle_metric */
728 static int mc_handle_socket(struct pollfd *p) /* {{{ */
729 {
730 char buffer[BUFF_SIZE];
731 ssize_t buffer_size;
733 if ((p->revents & (POLLIN | POLLPRI)) == 0) {
734 p->revents = 0;
735 return (-1);
736 }
738 buffer_size = recv(p->fd, buffer, sizeof(buffer), /* flags = */ 0);
739 if (buffer_size <= 0) {
740 char errbuf[1024];
741 ERROR("gmond plugin: recv failed: %s",
742 sstrerror(errno, errbuf, sizeof(errbuf)));
743 p->revents = 0;
744 return (-1);
745 }
747 mc_handle_metric(buffer, (size_t)buffer_size);
748 return (0);
749 } /* }}} int mc_handle_socket */
751 static void *mc_receive_thread(void *arg) /* {{{ */
752 {
753 socket_entry_t *mc_receive_socket_entries;
754 int status;
756 mc_receive_socket_entries = NULL;
757 status = create_sockets(
758 &mc_receive_socket_entries, &mc_receive_sockets_num,
759 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
760 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
761 /* listen = */ 1);
762 if (status != 0) {
763 ERROR("gmond plugin: create_sockets failed.");
764 return ((void *)-1);
765 }
767 mc_receive_sockets = (struct pollfd *)calloc(mc_receive_sockets_num,
768 sizeof(*mc_receive_sockets));
769 if (mc_receive_sockets == NULL) {
770 ERROR("gmond plugin: calloc failed.");
771 for (size_t i = 0; i < mc_receive_sockets_num; i++)
772 close(mc_receive_socket_entries[i].fd);
773 free(mc_receive_socket_entries);
774 mc_receive_socket_entries = NULL;
775 mc_receive_sockets_num = 0;
776 return ((void *)-1);
777 }
779 for (size_t i = 0; i < mc_receive_sockets_num; i++) {
780 mc_receive_sockets[i].fd = mc_receive_socket_entries[i].fd;
781 mc_receive_sockets[i].events = POLLIN | POLLPRI;
782 mc_receive_sockets[i].revents = 0;
783 }
785 while (mc_receive_thread_loop != 0) {
786 status = poll(mc_receive_sockets, mc_receive_sockets_num, -1);
787 if (status <= 0) {
788 char errbuf[1024];
789 if (errno == EINTR)
790 continue;
791 ERROR("gmond plugin: poll failed: %s",
792 sstrerror(errno, errbuf, sizeof(errbuf)));
793 break;
794 }
796 for (size_t i = 0; i < mc_receive_sockets_num; i++) {
797 if (mc_receive_sockets[i].revents != 0)
798 mc_handle_socket(mc_receive_sockets + i);
799 }
800 } /* while (mc_receive_thread_loop != 0) */
802 free(mc_receive_socket_entries);
803 return ((void *)0);
804 } /* }}} void *mc_receive_thread */
806 static int mc_receive_thread_start(void) /* {{{ */
807 {
808 int status;
810 if (mc_receive_thread_running != 0)
811 return (-1);
813 mc_receive_thread_loop = 1;
815 status =
816 plugin_thread_create(&mc_receive_thread_id, /* attr = */ NULL,
817 mc_receive_thread, /* args = */ NULL, "gmond recv");
818 if (status != 0) {
819 ERROR("gmond plugin: Starting receive thread failed.");
820 mc_receive_thread_loop = 0;
821 return (-1);
822 }
824 mc_receive_thread_running = 1;
825 return (0);
826 } /* }}} int start_receive_thread */
828 static int mc_receive_thread_stop(void) /* {{{ */
829 {
830 if (mc_receive_thread_running == 0)
831 return (-1);
833 mc_receive_thread_loop = 0;
835 INFO("gmond plugin: Stopping receive thread.");
836 pthread_kill(mc_receive_thread_id, SIGTERM);
837 pthread_join(mc_receive_thread_id, /* return value = */ NULL);
838 memset(&mc_receive_thread_id, 0, sizeof(mc_receive_thread_id));
840 mc_receive_thread_running = 0;
842 return (0);
843 } /* }}} int mc_receive_thread_stop */
845 /*
846 * Config:
847 *
848 * <Plugin gmond>
849 * MCReceiveFrom "239.2.11.71" "8649"
850 * <Metric "load_one">
851 * Type "load"
852 * [TypeInstance "foo"]
853 * [DataSource "bar"]
854 * </Metric>
855 * </Plugin>
856 */
857 static int gmond_config_set_string(oconfig_item_t *ci, char **str) /* {{{ */
858 {
859 char *tmp;
861 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
862 WARNING("gmond plugin: The `%s' option needs "
863 "exactly one string argument.",
864 ci->key);
865 return (-1);
866 }
868 tmp = strdup(ci->values[0].value.string);
869 if (tmp == NULL) {
870 ERROR("gmond plugin: strdup failed.");
871 return (-1);
872 }
874 sfree(*str);
875 *str = tmp;
876 return (0);
877 } /* }}} int gmond_config_set_string */
879 static int gmond_config_add_metric(oconfig_item_t *ci) /* {{{ */
880 {
881 metric_map_t *map;
883 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
884 WARNING("gmond plugin: `Metric' blocks need "
885 "exactly one string argument.");
886 return (-1);
887 }
889 map = realloc(metric_map, (metric_map_len + 1) * sizeof(*metric_map));
890 if (map == NULL) {
891 ERROR("gmond plugin: realloc failed.");
892 return (-1);
893 }
894 metric_map = map;
895 map = metric_map + metric_map_len;
897 memset(map, 0, sizeof(*map));
898 map->type = NULL;
899 map->type_instance = NULL;
900 map->ds_name = NULL;
901 map->ds_type = -1;
902 map->ds_index = -1;
904 map->ganglia_name = strdup(ci->values[0].value.string);
905 if (map->ganglia_name == NULL) {
906 ERROR("gmond plugin: strdup failed.");
907 return (-1);
908 }
910 for (int i = 0; i < ci->children_num; i++) {
911 oconfig_item_t *child = ci->children + i;
912 if (strcasecmp("Type", child->key) == 0)
913 gmond_config_set_string(child, &map->type);
914 else if (strcasecmp("TypeInstance", child->key) == 0)
915 gmond_config_set_string(child, &map->type_instance);
916 else if (strcasecmp("DataSource", child->key) == 0)
917 gmond_config_set_string(child, &map->ds_name);
918 else {
919 WARNING("gmond plugin: Unknown configuration option `%s' ignored.",
920 child->key);
921 }
922 }
924 if (map->type == NULL) {
925 ERROR("gmond plugin: No type is set for metric %s.", map->ganglia_name);
926 sfree(map->ganglia_name);
927 sfree(map->type_instance);
928 return (-1);
929 }
931 metric_map_len++;
932 return (0);
933 } /* }}} int gmond_config_add_metric */
935 static int gmond_config_set_address(oconfig_item_t *ci, /* {{{ */
936 char **ret_addr, char **ret_port) {
937 char *addr;
938 char *port;
940 if ((ci->values_num != 1) && (ci->values_num != 2)) {
941 WARNING("gmond plugin: The `%s' config option needs "
942 "one or two string arguments.",
943 ci->key);
944 return (-1);
945 }
946 if ((ci->values[0].type != OCONFIG_TYPE_STRING) ||
947 ((ci->values_num == 2) && (ci->values[1].type != OCONFIG_TYPE_STRING))) {
948 WARNING("gmond plugin: The `%s' config option needs "
949 "one or two string arguments.",
950 ci->key);
951 return (-1);
952 }
954 addr = strdup(ci->values[0].value.string);
955 if (ci->values_num == 2)
956 port = strdup(ci->values[1].value.string);
957 else
958 port = NULL;
960 if ((addr == NULL) || ((ci->values_num == 2) && (port == NULL))) {
961 ERROR("gmond plugin: strdup failed.");
962 sfree(addr);
963 sfree(port);
964 return (-1);
965 }
967 sfree(*ret_addr);
968 sfree(*ret_port);
970 *ret_addr = addr;
971 *ret_port = port;
973 return (0);
974 } /* }}} int gmond_config_set_address */
976 static int gmond_config(oconfig_item_t *ci) /* {{{ */
977 {
978 for (int i = 0; i < ci->children_num; i++) {
979 oconfig_item_t *child = ci->children + i;
980 if (strcasecmp("MCReceiveFrom", child->key) == 0)
981 gmond_config_set_address(child, &mc_receive_group, &mc_receive_port);
982 else if (strcasecmp("Metric", child->key) == 0)
983 gmond_config_add_metric(child);
984 else {
985 WARNING("gmond plugin: Unknown configuration option `%s' ignored.",
986 child->key);
987 }
988 }
990 return (0);
991 } /* }}} int gmond_config */
993 static int gmond_init(void) /* {{{ */
994 {
995 create_sockets(
996 &mc_send_sockets, &mc_send_sockets_num,
997 (mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT,
998 (mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT,
999 /* listen = */ 0);
1001 staging_tree = c_avl_create((int (*)(const void *, const void *))strcmp);
1002 if (staging_tree == NULL) {
1003 ERROR("gmond plugin: c_avl_create failed.");
1004 return (-1);
1005 }
1007 mc_receive_thread_start();
1009 return (0);
1010 } /* }}} int gmond_init */
1012 static int gmond_shutdown(void) /* {{{ */
1013 {
1014 mc_receive_thread_stop();
1016 pthread_mutex_lock(&mc_send_sockets_lock);
1017 for (size_t i = 0; i < mc_send_sockets_num; i++) {
1018 close(mc_send_sockets[i].fd);
1019 mc_send_sockets[i].fd = -1;
1020 }
1021 sfree(mc_send_sockets);
1022 mc_send_sockets_num = 0;
1023 pthread_mutex_unlock(&mc_send_sockets_lock);
1025 return (0);
1026 } /* }}} int gmond_shutdown */
1028 void module_register(void) {
1029 plugin_register_complex_config("gmond", gmond_config);
1030 plugin_register_init("gmond", gmond_init);
1031 plugin_register_shutdown("gmond", gmond_shutdown);
1032 }
1034 /* vim: set sw=2 sts=2 et fdm=marker : */