| 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 |
**/ |
| 26 |
|
| 27 |
#include "collectd.h" |
| 28 |
|
| 29 |
#include "plugin.h" |
| 30 |
#include "utils/avltree/avltree.h" |
| 31 |
#include "utils/common/common.h" |
| 32 |
|
| 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 |
| 45 |
|
| 46 |
#include <gm_protocol.h> |
| 47 |
|
| 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 */ |
| 55 |
|
| 56 |
#ifdef GANGLIA_MAX_MESSAGE_LEN |
| 57 |
#define BUFF_SIZE GANGLIA_MAX_MESSAGE_LEN |
| 58 |
#else |
| 59 |
#define BUFF_SIZE 1400 |
| 60 |
#endif |
| 61 |
|
| 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; |
| 68 |
|
| 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; |
| 75 |
|
| 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; |
| 85 |
|
| 86 |
#define MC_RECEIVE_GROUP_DEFAULT "239.2.11.71" |
| 87 |
static char *mc_receive_group; |
| 88 |
#define MC_RECEIVE_PORT_DEFAULT "8649" |
| 89 |
static char *mc_receive_port; |
| 90 |
|
| 91 |
static struct pollfd *mc_receive_sockets; |
| 92 |
static size_t mc_receive_sockets_num; |
| 93 |
|
| 94 |
static socket_entry_t *mc_send_sockets; |
| 95 |
static size_t mc_send_sockets_num; |
| 96 |
static pthread_mutex_t mc_send_sockets_lock = PTHREAD_MUTEX_INITIALIZER; |
| 97 |
|
| 98 |
static int mc_receive_thread_loop; |
| 99 |
static int mc_receive_thread_running; |
| 100 |
static pthread_t mc_receive_thread_id; |
| 101 |
|
| 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); |
| 124 |
|
| 125 |
static metric_map_t *metric_map; |
| 126 |
static size_t metric_map_len; |
| 127 |
|
| 128 |
static c_avl_tree_t *staging_tree; |
| 129 |
static pthread_mutex_t staging_lock = PTHREAD_MUTEX_INITIALIZER; |
| 130 |
|
| 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; |
| 136 |
|
| 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; |
| 143 |
|
| 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; |
| 148 |
|
| 149 |
for (i = 0; i < map_len; i++) |
| 150 |
if (strcmp(map[i].ganglia_name, key) == 0) |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
if (i >= map_len) |
| 155 |
return NULL; |
| 156 |
|
| 157 |
/* Look up the DS type and ds_index. */ |
| 158 |
if (map[i].ds_type < 0) /* {{{ */ |
| 159 |
{ |
| 160 |
const data_set_t *ds; |
| 161 |
|
| 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 |
} |
| 167 |
|
| 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 |
} |
| 174 |
|
| 175 |
if (map[i].ds_name == NULL) { |
| 176 |
map[i].ds_index = 0; |
| 177 |
} else { |
| 178 |
size_t j; |
| 179 |
|
| 180 |
for (j = 0; j < ds->ds_num; j++) |
| 181 |
if (strcasecmp(ds->ds[j].name, map[i].ds_name) == 0) |
| 182 |
break; |
| 183 |
|
| 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 |
} |
| 192 |
|
| 193 |
map[i].ds_type = ds->ds[map[i].ds_index].type; |
| 194 |
} /* }}} if ((map[i].ds_type < 0) || (map[i].ds_index < 0)) */ |
| 195 |
|
| 196 |
return map + i; |
| 197 |
} /* }}} metric_map_t *metric_lookup */ |
| 198 |
|
| 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; |
| 204 |
|
| 205 |
socket_entry_t *sockets = NULL; |
| 206 |
size_t sockets_num = 0; |
| 207 |
|
| 208 |
int status; |
| 209 |
|
| 210 |
if (*ret_sockets != NULL) |
| 211 |
return EINVAL; |
| 212 |
|
| 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}; |
| 217 |
|
| 218 |
ai_return = getaddrinfo(node, service, &ai_hints, &ai_list); |
| 219 |
if (ai_return != 0) { |
| 220 |
ERROR("gmond plugin: getaddrinfo (%s, %s) failed: %s", |
| 221 |
(node == NULL) ? "(null)" : node, |
| 222 |
(service == NULL) ? "(null)" : service, |
| 223 |
(ai_return == EAI_SYSTEM) ? STRERRNO : gai_strerror(ai_return)); |
| 224 |
return -1; |
| 225 |
} |
| 226 |
|
| 227 |
for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; |
| 228 |
ai_ptr = ai_ptr->ai_next) /* {{{ */ |
| 229 |
{ |
| 230 |
socket_entry_t *tmp; |
| 231 |
|
| 232 |
tmp = realloc(sockets, (sockets_num + 1) * sizeof(*sockets)); |
| 233 |
if (tmp == NULL) { |
| 234 |
ERROR("gmond plugin: realloc failed."); |
| 235 |
continue; |
| 236 |
} |
| 237 |
sockets = tmp; |
| 238 |
|
| 239 |
sockets[sockets_num].fd = |
| 240 |
socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); |
| 241 |
if (sockets[sockets_num].fd < 0) { |
| 242 |
ERROR("gmond plugin: socket failed: %s", STRERRNO); |
| 243 |
continue; |
| 244 |
} |
| 245 |
|
| 246 |
assert(sizeof(sockets[sockets_num].addr) >= ai_ptr->ai_addrlen); |
| 247 |
memcpy(&sockets[sockets_num].addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen); |
| 248 |
sockets[sockets_num].addrlen = ai_ptr->ai_addrlen; |
| 249 |
|
| 250 |
/* Sending socket: Open only one socket and don't bind it. */ |
| 251 |
if (listen == 0) { |
| 252 |
sockets_num++; |
| 253 |
break; |
| 254 |
} else { |
| 255 |
status = setsockopt(sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR, |
| 256 |
&(int){1}, sizeof(int)); |
| 257 |
if (status != 0) { |
| 258 |
WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
status = bind(sockets[sockets_num].fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen); |
| 263 |
if (status != 0) { |
| 264 |
ERROR("gmond plugin: bind failed: %s", STRERRNO); |
| 265 |
close(sockets[sockets_num].fd); |
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
if (ai_ptr->ai_family == AF_INET) { |
| 270 |
struct sockaddr_in *addr; |
| 271 |
int loop; |
| 272 |
|
| 273 |
addr = (struct sockaddr_in *)ai_ptr->ai_addr; |
| 274 |
|
| 275 |
if (!IN_MULTICAST(ntohl(addr->sin_addr.s_addr))) { |
| 276 |
sockets_num++; |
| 277 |
continue; |
| 278 |
} |
| 279 |
|
| 280 |
loop = 1; |
| 281 |
status = setsockopt(sockets[sockets_num].fd, IPPROTO_IP, |
| 282 |
IP_MULTICAST_LOOP, (void *)&loop, sizeof(loop)); |
| 283 |
if (status != 0) { |
| 284 |
WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO); |
| 285 |
} |
| 286 |
|
| 287 |
struct ip_mreq mreq = {.imr_multiaddr.s_addr = addr->sin_addr.s_addr, |
| 288 |
.imr_interface.s_addr = htonl(INADDR_ANY)}; |
| 289 |
|
| 290 |
status = setsockopt(sockets[sockets_num].fd, IPPROTO_IP, |
| 291 |
IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)); |
| 292 |
if (status != 0) { |
| 293 |
WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO); |
| 294 |
} |
| 295 |
} /* if (ai_ptr->ai_family == AF_INET) */ |
| 296 |
else if (ai_ptr->ai_family == AF_INET6) { |
| 297 |
struct sockaddr_in6 *addr; |
| 298 |
int loop; |
| 299 |
|
| 300 |
addr = (struct sockaddr_in6 *)ai_ptr->ai_addr; |
| 301 |
|
| 302 |
if (!IN6_IS_ADDR_MULTICAST(&addr->sin6_addr)) { |
| 303 |
sockets_num++; |
| 304 |
continue; |
| 305 |
} |
| 306 |
|
| 307 |
loop = 1; |
| 308 |
status = setsockopt(sockets[sockets_num].fd, IPPROTO_IPV6, |
| 309 |
IPV6_MULTICAST_LOOP, (void *)&loop, sizeof(loop)); |
| 310 |
if (status != 0) { |
| 311 |
WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO); |
| 312 |
} |
| 313 |
|
| 314 |
struct ipv6_mreq mreq = { |
| 315 |
.ipv6mr_interface = 0 /* any */ |
| 316 |
}; |
| 317 |
|
| 318 |
memcpy(&mreq.ipv6mr_multiaddr, &addr->sin6_addr, sizeof(addr->sin6_addr)); |
| 319 |
status = setsockopt(sockets[sockets_num].fd, IPPROTO_IPV6, |
| 320 |
IPV6_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)); |
| 321 |
if (status != 0) { |
| 322 |
WARNING("gmond plugin: setsockopt(2) failed: %s", STRERRNO); |
| 323 |
} |
| 324 |
} /* if (ai_ptr->ai_family == AF_INET6) */ |
| 325 |
|
| 326 |
sockets_num++; |
| 327 |
} /* }}} for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */ |
| 328 |
|
| 329 |
freeaddrinfo(ai_list); |
| 330 |
|
| 331 |
if (sockets_num == 0) { |
| 332 |
sfree(sockets); |
| 333 |
return -1; |
| 334 |
} |
| 335 |
|
| 336 |
*ret_sockets = sockets; |
| 337 |
*ret_sockets_num = sockets_num; |
| 338 |
return 0; |
| 339 |
} /* }}} int create_sockets */ |
| 340 |
|
| 341 |
static int request_meta_data(const char *host, const char *name) /* {{{ */ |
| 342 |
{ |
| 343 |
Ganglia_metadata_msg msg = {0}; |
| 344 |
char buffer[BUFF_SIZE] = {0}; |
| 345 |
unsigned int buffer_size; |
| 346 |
XDR xdr; |
| 347 |
|
| 348 |
msg.id = gmetadata_request; |
| 349 |
msg.Ganglia_metadata_msg_u.grequest.metric_id.host = strdup(host); |
| 350 |
msg.Ganglia_metadata_msg_u.grequest.metric_id.name = strdup(name); |
| 351 |
|
| 352 |
if ((msg.Ganglia_metadata_msg_u.grequest.metric_id.host == NULL) || |
| 353 |
(msg.Ganglia_metadata_msg_u.grequest.metric_id.name == NULL)) { |
| 354 |
sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host); |
| 355 |
sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name); |
| 356 |
return -1; |
| 357 |
} |
| 358 |
|
| 359 |
xdrmem_create(&xdr, buffer, sizeof(buffer), XDR_ENCODE); |
| 360 |
|
| 361 |
if (!xdr_Ganglia_metadata_msg(&xdr, &msg)) { |
| 362 |
sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host); |
| 363 |
sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name); |
| 364 |
return -1; |
| 365 |
} |
| 366 |
|
| 367 |
buffer_size = xdr_getpos(&xdr); |
| 368 |
|
| 369 |
DEBUG("gmond plugin: Requesting meta data for %s/%s.", host, name); |
| 370 |
|
| 371 |
pthread_mutex_lock(&mc_send_sockets_lock); |
| 372 |
for (size_t i = 0; i < mc_send_sockets_num; i++) { |
| 373 |
ssize_t status = |
| 374 |
sendto(mc_send_sockets[i].fd, buffer, (size_t)buffer_size, |
| 375 |
/* flags = */ 0, (struct sockaddr *)&mc_send_sockets[i].addr, |
| 376 |
mc_send_sockets[i].addrlen); |
| 377 |
if (status == -1) { |
| 378 |
ERROR("gmond plugin: sendto(2) failed: %s", STRERRNO); |
| 379 |
continue; |
| 380 |
} |
| 381 |
} |
| 382 |
pthread_mutex_unlock(&mc_send_sockets_lock); |
| 383 |
|
| 384 |
sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.host); |
| 385 |
sfree(msg.Ganglia_metadata_msg_u.grequest.metric_id.name); |
| 386 |
return 0; |
| 387 |
} /* }}} int request_meta_data */ |
| 388 |
|
| 389 |
static staging_entry_t *staging_entry_get(const char *host, /* {{{ */ |
| 390 |
const char *name, const char *type, |
| 391 |
const char *type_instance, |
| 392 |
int values_len) { |
| 393 |
char key[2 * DATA_MAX_NAME_LEN]; |
| 394 |
staging_entry_t *se; |
| 395 |
int status; |
| 396 |
|
| 397 |
if (staging_tree == NULL) |
| 398 |
return NULL; |
| 399 |
|
| 400 |
ssnprintf(key, sizeof(key), "%s/%s/%s", host, type, |
| 401 |
(type_instance != NULL) ? type_instance : ""); |
| 402 |
|
| 403 |
se = NULL; |
| 404 |
status = c_avl_get(staging_tree, key, (void *)&se); |
| 405 |
if (status == 0) |
| 406 |
return se; |
| 407 |
|
| 408 |
/* insert new entry */ |
| 409 |
se = calloc(1, sizeof(*se)); |
| 410 |
if (se == NULL) |
| 411 |
return NULL; |
| 412 |
|
| 413 |
sstrncpy(se->key, key, sizeof(se->key)); |
| 414 |
se->flags = 0; |
| 415 |
|
| 416 |
se->vl.values = calloc(values_len, sizeof(*se->vl.values)); |
| 417 |
if (se->vl.values == NULL) { |
| 418 |
sfree(se); |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
se->vl.values_len = values_len; |
| 422 |
|
| 423 |
se->vl.time = 0; |
| 424 |
se->vl.interval = 0; |
| 425 |
sstrncpy(se->vl.host, host, sizeof(se->vl.host)); |
| 426 |
sstrncpy(se->vl.plugin, "gmond", sizeof(se->vl.plugin)); |
| 427 |
sstrncpy(se->vl.type, type, sizeof(se->vl.type)); |
| 428 |
if (type_instance != NULL) |
| 429 |
sstrncpy(se->vl.type_instance, type_instance, sizeof(se->vl.type_instance)); |
| 430 |
|
| 431 |
status = c_avl_insert(staging_tree, se->key, se); |
| 432 |
if (status != 0) { |
| 433 |
ERROR("gmond plugin: c_avl_insert failed."); |
| 434 |
sfree(se->vl.values); |
| 435 |
sfree(se); |
| 436 |
return NULL; |
| 437 |
} |
| 438 |
|
| 439 |
return se; |
| 440 |
} /* }}} staging_entry_t *staging_entry_get */ |
| 441 |
|
| 442 |
static int staging_entry_update(const char *host, const char *name, /* {{{ */ |
| 443 |
const char *type, const char *type_instance, |
| 444 |
size_t ds_index, int ds_type, value_t value) { |
| 445 |
const data_set_t *ds; |
| 446 |
staging_entry_t *se; |
| 447 |
|
| 448 |
ds = plugin_get_ds(type); |
| 449 |
if (ds == NULL) { |
| 450 |
ERROR("gmond plugin: Looking up type %s failed.", type); |
| 451 |
return -1; |
| 452 |
} |
| 453 |
|
| 454 |
if (ds->ds_num <= ds_index) { |
| 455 |
ERROR("gmond plugin: Invalid index %" PRIsz ": %s has only %" PRIsz |
| 456 |
" data source(s).", |
| 457 |
ds_index, ds->type, ds->ds_num); |
| 458 |
return -1; |
| 459 |
} |
| 460 |
|
| 461 |
pthread_mutex_lock(&staging_lock); |
| 462 |
|
| 463 |
se = staging_entry_get(host, name, type, type_instance, ds->ds_num); |
| 464 |
if (se == NULL) { |
| 465 |
pthread_mutex_unlock(&staging_lock); |
| 466 |
ERROR("gmond plugin: staging_entry_get failed."); |
| 467 |
return -1; |
| 468 |
} |
| 469 |
if (se->vl.values_len != ds->ds_num) { |
| 470 |
pthread_mutex_unlock(&staging_lock); |
| 471 |
return -1; |
| 472 |
} |
| 473 |
|
| 474 |
if (ds_type == DS_TYPE_COUNTER) |
| 475 |
se->vl.values[ds_index].counter += value.counter; |
| 476 |
else if (ds_type == DS_TYPE_GAUGE) |
| 477 |
se->vl.values[ds_index].gauge = value.gauge; |
| 478 |
else if (ds_type == DS_TYPE_DERIVE) |
| 479 |
se->vl.values[ds_index].derive += value.derive; |
| 480 |
else if (ds_type == DS_TYPE_ABSOLUTE) |
| 481 |
se->vl.values[ds_index].absolute = value.absolute; |
| 482 |
else |
| 483 |
assert(23 == 42); |
| 484 |
|
| 485 |
se->flags |= (0x01 << ds_index); |
| 486 |
|
| 487 |
/* Check if all data sources have been set. If not, return here. */ |
| 488 |
if (se->flags != ((0x01 << se->vl.values_len) - 1)) { |
| 489 |
pthread_mutex_unlock(&staging_lock); |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
/* Check if the interval of this metric is known. If not, request meta data |
| 494 |
* and return. */ |
| 495 |
if (se->vl.interval == 0) { |
| 496 |
/* No meta data has been received for this metric yet. */ |
| 497 |
se->flags = 0; |
| 498 |
pthread_mutex_unlock(&staging_lock); |
| 499 |
|
| 500 |
request_meta_data(host, name); |
| 501 |
return 0; |
| 502 |
} |
| 503 |
|
| 504 |
plugin_dispatch_values(&se->vl); |
| 505 |
|
| 506 |
se->flags = 0; |
| 507 |
pthread_mutex_unlock(&staging_lock); |
| 508 |
|
| 509 |
return 0; |
| 510 |
} /* }}} int staging_entry_update */ |
| 511 |
|
| 512 |
static int mc_handle_value_msg(Ganglia_value_msg *msg) /* {{{ */ |
| 513 |
{ |
| 514 |
const char *host; |
| 515 |
const char *name; |
| 516 |
metric_map_t *map; |
| 517 |
|
| 518 |
value_t value_counter; |
| 519 |
value_t value_gauge; |
| 520 |
value_t value_derive; |
| 521 |
|
| 522 |
/* Fill in `host', `name', `value_counter', and `value_gauge' according to |
| 523 |
* the value type, or return with an error. */ |
| 524 |
switch (msg->id) /* {{{ */ |
| 525 |
{ |
| 526 |
case gmetric_uint: { |
| 527 |
Ganglia_gmetric_uint msg_uint; |
| 528 |
|
| 529 |
msg_uint = msg->Ganglia_value_msg_u.gu_int; |
| 530 |
|
| 531 |
host = msg_uint.metric_id.host; |
| 532 |
name = msg_uint.metric_id.name; |
| 533 |
value_counter.counter = (counter_t)msg_uint.ui; |
| 534 |
value_gauge.gauge = (gauge_t)msg_uint.ui; |
| 535 |
value_derive.derive = (derive_t)msg_uint.ui; |
| 536 |
break; |
| 537 |
} |
| 538 |
|
| 539 |
case gmetric_string: { |
| 540 |
Ganglia_gmetric_string msg_string; |
| 541 |
int status; |
| 542 |
|
| 543 |
msg_string = msg->Ganglia_value_msg_u.gstr; |
| 544 |
|
| 545 |
host = msg_string.metric_id.host; |
| 546 |
name = msg_string.metric_id.name; |
| 547 |
|
| 548 |
status = parse_value(msg_string.str, &value_derive, DS_TYPE_DERIVE); |
| 549 |
if (status != 0) |
| 550 |
value_derive.derive = -1; |
| 551 |
|
| 552 |
status = parse_value(msg_string.str, &value_gauge, DS_TYPE_GAUGE); |
| 553 |
if (status != 0) |
| 554 |
value_gauge.gauge = NAN; |
| 555 |
|
| 556 |
status = parse_value(msg_string.str, &value_counter, DS_TYPE_COUNTER); |
| 557 |
if (status != 0) |
| 558 |
value_counter.counter = 0; |
| 559 |
|
| 560 |
break; |
| 561 |
} |
| 562 |
|
| 563 |
case gmetric_float: { |
| 564 |
Ganglia_gmetric_float msg_float; |
| 565 |
|
| 566 |
msg_float = msg->Ganglia_value_msg_u.gf; |
| 567 |
|
| 568 |
host = msg_float.metric_id.host; |
| 569 |
name = msg_float.metric_id.name; |
| 570 |
value_counter.counter = (counter_t)msg_float.f; |
| 571 |
value_gauge.gauge = (gauge_t)msg_float.f; |
| 572 |
value_derive.derive = (derive_t)msg_float.f; |
| 573 |
break; |
| 574 |
} |
| 575 |
|
| 576 |
case gmetric_double: { |
| 577 |
Ganglia_gmetric_double msg_double; |
| 578 |
|
| 579 |
msg_double = msg->Ganglia_value_msg_u.gd; |
| 580 |
|
| 581 |
host = msg_double.metric_id.host; |
| 582 |
name = msg_double.metric_id.name; |
| 583 |
value_counter.counter = (counter_t)msg_double.d; |
| 584 |
value_gauge.gauge = (gauge_t)msg_double.d; |
| 585 |
value_derive.derive = (derive_t)msg_double.d; |
| 586 |
break; |
| 587 |
} |
| 588 |
default: |
| 589 |
DEBUG("gmond plugin: Value type not handled: %i", msg->id); |
| 590 |
return -1; |
| 591 |
} /* }}} switch (msg->id) */ |
| 592 |
|
| 593 |
assert(host != NULL); |
| 594 |
assert(name != NULL); |
| 595 |
|
| 596 |
map = metric_lookup(name); |
| 597 |
if (map != NULL) { |
| 598 |
value_t val_copy; |
| 599 |
|
| 600 |
if ((map->ds_type == DS_TYPE_COUNTER) || (map->ds_type == DS_TYPE_ABSOLUTE)) |
| 601 |
val_copy = value_counter; |
| 602 |
else if (map->ds_type == DS_TYPE_GAUGE) |
| 603 |
val_copy = value_gauge; |
| 604 |
else if (map->ds_type == DS_TYPE_DERIVE) |
| 605 |
val_copy = value_derive; |
| 606 |
else |
| 607 |
assert(23 == 42); |
| 608 |
|
| 609 |
return staging_entry_update(host, name, map->type, map->type_instance, |
| 610 |
map->ds_index, map->ds_type, val_copy); |
| 611 |
} |
| 612 |
|
| 613 |
DEBUG("gmond plugin: Cannot find a translation for %s.", name); |
| 614 |
return -1; |
| 615 |
} /* }}} int mc_handle_value_msg */ |
| 616 |
|
| 617 |
static int mc_handle_metadata_msg(Ganglia_metadata_msg *msg) /* {{{ */ |
| 618 |
{ |
| 619 |
switch (msg->id) { |
| 620 |
case gmetadata_full: { |
| 621 |
Ganglia_metadatadef msg_meta; |
| 622 |
staging_entry_t *se; |
| 623 |
const data_set_t *ds; |
| 624 |
metric_map_t *map; |
| 625 |
|
| 626 |
msg_meta = msg->Ganglia_metadata_msg_u.gfull; |
| 627 |
|
| 628 |
if (msg_meta.metric.tmax == 0) |
| 629 |
return -1; |
| 630 |
|
| 631 |
map = metric_lookup(msg_meta.metric_id.name); |
| 632 |
if (map == NULL) { |
| 633 |
DEBUG("gmond plugin: Not handling meta data %s.", |
| 634 |
msg_meta.metric_id.name); |
| 635 |
return 0; |
| 636 |
} |
| 637 |
|
| 638 |
ds = plugin_get_ds(map->type); |
| 639 |
if (ds == NULL) { |
| 640 |
WARNING("gmond plugin: Could not find data set %s.", map->type); |
| 641 |
return -1; |
| 642 |
} |
| 643 |
|
| 644 |
DEBUG("gmond plugin: Received meta data for %s/%s.", |
| 645 |
msg_meta.metric_id.host, msg_meta.metric_id.name); |
| 646 |
|
| 647 |
pthread_mutex_lock(&staging_lock); |
| 648 |
se = staging_entry_get(msg_meta.metric_id.host, msg_meta.metric_id.name, |
| 649 |
map->type, map->type_instance, ds->ds_num); |
| 650 |
if (se != NULL) |
| 651 |
se->vl.interval = TIME_T_TO_CDTIME_T(msg_meta.metric.tmax); |
| 652 |
pthread_mutex_unlock(&staging_lock); |
| 653 |
|
| 654 |
if (se == NULL) { |
| 655 |
ERROR("gmond plugin: staging_entry_get failed."); |
| 656 |
return -1; |
| 657 |
} |
| 658 |
|
| 659 |
break; |
| 660 |
} |
| 661 |
|
| 662 |
default: { return -1; } |
| 663 |
} |
| 664 |
|
| 665 |
return 0; |
| 666 |
} /* }}} int mc_handle_metadata_msg */ |
| 667 |
|
| 668 |
static int mc_handle_metric(void *buffer, size_t buffer_size) /* {{{ */ |
| 669 |
{ |
| 670 |
XDR xdr; |
| 671 |
Ganglia_msg_formats format; |
| 672 |
|
| 673 |
xdrmem_create(&xdr, buffer, buffer_size, XDR_DECODE); |
| 674 |
|
| 675 |
xdr_Ganglia_msg_formats(&xdr, &format); |
| 676 |
xdr_setpos(&xdr, 0); |
| 677 |
|
| 678 |
switch (format) { |
| 679 |
case gmetric_ushort: |
| 680 |
case gmetric_short: |
| 681 |
case gmetric_int: |
| 682 |
case gmetric_uint: |
| 683 |
case gmetric_string: |
| 684 |
case gmetric_float: |
| 685 |
case gmetric_double: { |
| 686 |
Ganglia_value_msg msg = {0}; |
| 687 |
|
| 688 |
if (xdr_Ganglia_value_msg(&xdr, &msg)) |
| 689 |
mc_handle_value_msg(&msg); |
| 690 |
break; |
| 691 |
} |
| 692 |
|
| 693 |
case gmetadata_full: |
| 694 |
case gmetadata_request: { |
| 695 |
Ganglia_metadata_msg msg = {0}; |
| 696 |
if (xdr_Ganglia_metadata_msg(&xdr, &msg)) |
| 697 |
mc_handle_metadata_msg(&msg); |
| 698 |
break; |
| 699 |
} |
| 700 |
|
| 701 |
default: |
| 702 |
DEBUG("gmond plugin: Unknown format: %i", format); |
| 703 |
return -1; |
| 704 |
} /* switch (format) */ |
| 705 |
|
| 706 |
return 0; |
| 707 |
} /* }}} int mc_handle_metric */ |
| 708 |
|
| 709 |
static int mc_handle_socket(struct pollfd *p) /* {{{ */ |
| 710 |
{ |
| 711 |
char buffer[BUFF_SIZE]; |
| 712 |
ssize_t buffer_size; |
| 713 |
|
| 714 |
if ((p->revents & (POLLIN | POLLPRI)) == 0) { |
| 715 |
p->revents = 0; |
| 716 |
return -1; |
| 717 |
} |
| 718 |
|
| 719 |
buffer_size = recv(p->fd, buffer, sizeof(buffer), /* flags = */ 0); |
| 720 |
if (buffer_size <= 0) { |
| 721 |
ERROR("gmond plugin: recv failed: %s", STRERRNO); |
| 722 |
p->revents = 0; |
| 723 |
return -1; |
| 724 |
} |
| 725 |
|
| 726 |
mc_handle_metric(buffer, (size_t)buffer_size); |
| 727 |
return 0; |
| 728 |
} /* }}} int mc_handle_socket */ |
| 729 |
|
| 730 |
static void *mc_receive_thread(void *arg) /* {{{ */ |
| 731 |
{ |
| 732 |
socket_entry_t *mc_receive_socket_entries; |
| 733 |
int status; |
| 734 |
|
| 735 |
mc_receive_socket_entries = NULL; |
| 736 |
status = create_sockets( |
| 737 |
&mc_receive_socket_entries, &mc_receive_sockets_num, |
| 738 |
(mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT, |
| 739 |
(mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT, |
| 740 |
/* listen = */ 1); |
| 741 |
if (status != 0) { |
| 742 |
ERROR("gmond plugin: create_sockets failed."); |
| 743 |
return (void *)-1; |
| 744 |
} |
| 745 |
|
| 746 |
mc_receive_sockets = |
| 747 |
calloc(mc_receive_sockets_num, sizeof(*mc_receive_sockets)); |
| 748 |
if (mc_receive_sockets == NULL) { |
| 749 |
ERROR("gmond plugin: calloc failed."); |
| 750 |
for (size_t i = 0; i < mc_receive_sockets_num; i++) |
| 751 |
close(mc_receive_socket_entries[i].fd); |
| 752 |
free(mc_receive_socket_entries); |
| 753 |
mc_receive_socket_entries = NULL; |
| 754 |
mc_receive_sockets_num = 0; |
| 755 |
return (void *)-1; |
| 756 |
} |
| 757 |
|
| 758 |
for (size_t i = 0; i < mc_receive_sockets_num; i++) { |
| 759 |
mc_receive_sockets[i].fd = mc_receive_socket_entries[i].fd; |
| 760 |
mc_receive_sockets[i].events = POLLIN | POLLPRI; |
| 761 |
mc_receive_sockets[i].revents = 0; |
| 762 |
} |
| 763 |
|
| 764 |
while (mc_receive_thread_loop != 0) { |
| 765 |
status = poll(mc_receive_sockets, mc_receive_sockets_num, -1); |
| 766 |
if (status <= 0) { |
| 767 |
if (errno == EINTR) |
| 768 |
continue; |
| 769 |
ERROR("gmond plugin: poll failed: %s", STRERRNO); |
| 770 |
break; |
| 771 |
} |
| 772 |
|
| 773 |
for (size_t i = 0; i < mc_receive_sockets_num; i++) { |
| 774 |
if (mc_receive_sockets[i].revents != 0) |
| 775 |
mc_handle_socket(mc_receive_sockets + i); |
| 776 |
} |
| 777 |
} /* while (mc_receive_thread_loop != 0) */ |
| 778 |
|
| 779 |
free(mc_receive_socket_entries); |
| 780 |
return (void *)0; |
| 781 |
} /* }}} void *mc_receive_thread */ |
| 782 |
|
| 783 |
static int mc_receive_thread_start(void) /* {{{ */ |
| 784 |
{ |
| 785 |
int status; |
| 786 |
|
| 787 |
if (mc_receive_thread_running != 0) |
| 788 |
return -1; |
| 789 |
|
| 790 |
mc_receive_thread_loop = 1; |
| 791 |
|
| 792 |
status = plugin_thread_create(&mc_receive_thread_id, mc_receive_thread, |
| 793 |
/* args = */ NULL, "gmond recv"); |
| 794 |
if (status != 0) { |
| 795 |
ERROR("gmond plugin: Starting receive thread failed."); |
| 796 |
mc_receive_thread_loop = 0; |
| 797 |
return -1; |
| 798 |
} |
| 799 |
|
| 800 |
mc_receive_thread_running = 1; |
| 801 |
return 0; |
| 802 |
} /* }}} int start_receive_thread */ |
| 803 |
|
| 804 |
static int mc_receive_thread_stop(void) /* {{{ */ |
| 805 |
{ |
| 806 |
if (mc_receive_thread_running == 0) |
| 807 |
return -1; |
| 808 |
|
| 809 |
mc_receive_thread_loop = 0; |
| 810 |
|
| 811 |
INFO("gmond plugin: Stopping receive thread."); |
| 812 |
pthread_kill(mc_receive_thread_id, SIGTERM); |
| 813 |
pthread_join(mc_receive_thread_id, /* return value = */ NULL); |
| 814 |
memset(&mc_receive_thread_id, 0, sizeof(mc_receive_thread_id)); |
| 815 |
|
| 816 |
mc_receive_thread_running = 0; |
| 817 |
|
| 818 |
return 0; |
| 819 |
} /* }}} int mc_receive_thread_stop */ |
| 820 |
|
| 821 |
/* |
| 822 |
* Config: |
| 823 |
* |
| 824 |
* <Plugin gmond> |
| 825 |
* MCReceiveFrom "239.2.11.71" "8649" |
| 826 |
* <Metric "load_one"> |
| 827 |
* Type "load" |
| 828 |
* [TypeInstance "foo"] |
| 829 |
* [DataSource "bar"] |
| 830 |
* </Metric> |
| 831 |
* </Plugin> |
| 832 |
*/ |
| 833 |
static int gmond_config_add_metric(oconfig_item_t *ci) /* {{{ */ |
| 834 |
{ |
| 835 |
metric_map_t *map; |
| 836 |
|
| 837 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 838 |
WARNING("gmond plugin: `Metric' blocks need " |
| 839 |
"exactly one string argument."); |
| 840 |
return -1; |
| 841 |
} |
| 842 |
|
| 843 |
map = realloc(metric_map, (metric_map_len + 1) * sizeof(*metric_map)); |
| 844 |
if (map == NULL) { |
| 845 |
ERROR("gmond plugin: realloc failed."); |
| 846 |
return -1; |
| 847 |
} |
| 848 |
metric_map = map; |
| 849 |
map = metric_map + metric_map_len; |
| 850 |
|
| 851 |
memset(map, 0, sizeof(*map)); |
| 852 |
map->type = NULL; |
| 853 |
map->type_instance = NULL; |
| 854 |
map->ds_name = NULL; |
| 855 |
map->ds_type = -1; |
| 856 |
map->ds_index = -1; |
| 857 |
|
| 858 |
map->ganglia_name = strdup(ci->values[0].value.string); |
| 859 |
if (map->ganglia_name == NULL) { |
| 860 |
ERROR("gmond plugin: strdup failed."); |
| 861 |
return -1; |
| 862 |
} |
| 863 |
|
| 864 |
for (int i = 0; i < ci->children_num; i++) { |
| 865 |
oconfig_item_t *child = ci->children + i; |
| 866 |
if (strcasecmp("Type", child->key) == 0) |
| 867 |
cf_util_get_string(child, &map->type); |
| 868 |
else if (strcasecmp("TypeInstance", child->key) == 0) |
| 869 |
cf_util_get_string(child, &map->type_instance); |
| 870 |
else if (strcasecmp("DataSource", child->key) == 0) |
| 871 |
cf_util_get_string(child, &map->ds_name); |
| 872 |
else { |
| 873 |
WARNING("gmond plugin: Unknown configuration option `%s' ignored.", |
| 874 |
child->key); |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
if (map->type == NULL) { |
| 879 |
ERROR("gmond plugin: No type is set for metric %s.", map->ganglia_name); |
| 880 |
sfree(map->ganglia_name); |
| 881 |
sfree(map->type_instance); |
| 882 |
return -1; |
| 883 |
} |
| 884 |
|
| 885 |
metric_map_len++; |
| 886 |
return 0; |
| 887 |
} /* }}} int gmond_config_add_metric */ |
| 888 |
|
| 889 |
static int gmond_config_set_address(oconfig_item_t *ci, /* {{{ */ |
| 890 |
char **ret_addr, char **ret_port) { |
| 891 |
char *addr; |
| 892 |
char *port; |
| 893 |
|
| 894 |
if ((ci->values_num != 1) && (ci->values_num != 2)) { |
| 895 |
WARNING("gmond plugin: The `%s' config option needs " |
| 896 |
"one or two string arguments.", |
| 897 |
ci->key); |
| 898 |
return -1; |
| 899 |
} |
| 900 |
if ((ci->values[0].type != OCONFIG_TYPE_STRING) || |
| 901 |
((ci->values_num == 2) && (ci->values[1].type != OCONFIG_TYPE_STRING))) { |
| 902 |
WARNING("gmond plugin: The `%s' config option needs " |
| 903 |
"one or two string arguments.", |
| 904 |
ci->key); |
| 905 |
return -1; |
| 906 |
} |
| 907 |
|
| 908 |
addr = strdup(ci->values[0].value.string); |
| 909 |
if (ci->values_num == 2) |
| 910 |
port = strdup(ci->values[1].value.string); |
| 911 |
else |
| 912 |
port = NULL; |
| 913 |
|
| 914 |
if ((addr == NULL) || ((ci->values_num == 2) && (port == NULL))) { |
| 915 |
ERROR("gmond plugin: strdup failed."); |
| 916 |
sfree(addr); |
| 917 |
sfree(port); |
| 918 |
return -1; |
| 919 |
} |
| 920 |
|
| 921 |
sfree(*ret_addr); |
| 922 |
sfree(*ret_port); |
| 923 |
|
| 924 |
*ret_addr = addr; |
| 925 |
*ret_port = port; |
| 926 |
|
| 927 |
return 0; |
| 928 |
} /* }}} int gmond_config_set_address */ |
| 929 |
|
| 930 |
static int gmond_config(oconfig_item_t *ci) /* {{{ */ |
| 931 |
{ |
| 932 |
for (int i = 0; i < ci->children_num; i++) { |
| 933 |
oconfig_item_t *child = ci->children + i; |
| 934 |
if (strcasecmp("MCReceiveFrom", child->key) == 0) |
| 935 |
gmond_config_set_address(child, &mc_receive_group, &mc_receive_port); |
| 936 |
else if (strcasecmp("Metric", child->key) == 0) |
| 937 |
gmond_config_add_metric(child); |
| 938 |
else { |
| 939 |
WARNING("gmond plugin: Unknown configuration option `%s' ignored.", |
| 940 |
child->key); |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
return 0; |
| 945 |
} /* }}} int gmond_config */ |
| 946 |
|
| 947 |
static int gmond_init(void) /* {{{ */ |
| 948 |
{ |
| 949 |
create_sockets( |
| 950 |
&mc_send_sockets, &mc_send_sockets_num, |
| 951 |
(mc_receive_group != NULL) ? mc_receive_group : MC_RECEIVE_GROUP_DEFAULT, |
| 952 |
(mc_receive_port != NULL) ? mc_receive_port : MC_RECEIVE_PORT_DEFAULT, |
| 953 |
/* listen = */ 0); |
| 954 |
|
| 955 |
staging_tree = c_avl_create((int (*)(const void *, const void *))strcmp); |
| 956 |
if (staging_tree == NULL) { |
| 957 |
ERROR("gmond plugin: c_avl_create failed."); |
| 958 |
return -1; |
| 959 |
} |
| 960 |
|
| 961 |
mc_receive_thread_start(); |
| 962 |
|
| 963 |
return 0; |
| 964 |
} /* }}} int gmond_init */ |
| 965 |
|
| 966 |
static int gmond_shutdown(void) /* {{{ */ |
| 967 |
{ |
| 968 |
mc_receive_thread_stop(); |
| 969 |
|
| 970 |
pthread_mutex_lock(&mc_send_sockets_lock); |
| 971 |
for (size_t i = 0; i < mc_send_sockets_num; i++) { |
| 972 |
close(mc_send_sockets[i].fd); |
| 973 |
mc_send_sockets[i].fd = -1; |
| 974 |
} |
| 975 |
sfree(mc_send_sockets); |
| 976 |
mc_send_sockets_num = 0; |
| 977 |
pthread_mutex_unlock(&mc_send_sockets_lock); |
| 978 |
|
| 979 |
return 0; |
| 980 |
} /* }}} int gmond_shutdown */ |
| 981 |
|
| 982 |
void module_register(void) { |
| 983 |
plugin_register_complex_config("gmond", gmond_config); |
| 984 |
plugin_register_init("gmond", gmond_init); |
| 985 |
plugin_register_shutdown("gmond", gmond_shutdown); |
| 986 |
} |