| 1 |
/** |
| 2 |
* collectd - src/memcached.c, based on src/hddtemp.c |
| 3 |
* Copyright (C) 2007 Antony Dovgal |
| 4 |
* Copyright (C) 2007-2012 Florian Forster |
| 5 |
* Copyright (C) 2009 Doug MacEachern |
| 6 |
* Copyright (C) 2009 Franck Lombardi |
| 7 |
* Copyright (C) 2012 Nicolas Szalay |
| 8 |
* Copyright (C) 2017 Pavel Rochnyak |
| 9 |
* |
| 10 |
* This program is free software; you can redistribute it and/or modify it |
| 11 |
* under the terms of the GNU General Public License as published by the |
| 12 |
* Free Software Foundation; either version 2 of the License, or (at your |
| 13 |
* option) any later version. |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, but |
| 16 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
* General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License along |
| 21 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 22 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 |
* |
| 24 |
* Authors: |
| 25 |
* Antony Dovgal <tony at daylessday dot org> |
| 26 |
* Florian octo Forster <octo at collectd.org> |
| 27 |
* Doug MacEachern <dougm at hyperic.com> |
| 28 |
* Franck Lombardi |
| 29 |
* Nicolas Szalay |
| 30 |
* Pavel Rochnyak <pavel2000 ngs.ru> |
| 31 |
**/ |
| 32 |
|
| 33 |
#include "collectd.h" |
| 34 |
|
| 35 |
#include "plugin.h" |
| 36 |
#include "utils/common/common.h" |
| 37 |
|
| 38 |
#include <netdb.h> |
| 39 |
#include <netinet/in.h> |
| 40 |
#include <netinet/tcp.h> |
| 41 |
#include <sys/un.h> |
| 42 |
|
| 43 |
#include <poll.h> |
| 44 |
|
| 45 |
#define MEMCACHED_DEF_HOST "127.0.0.1" |
| 46 |
#define MEMCACHED_DEF_PORT "11211" |
| 47 |
#define MEMCACHED_CONNECT_TIMEOUT 10000 |
| 48 |
#define MEMCACHED_IO_TIMEOUT 5000 |
| 49 |
|
| 50 |
struct prev_s { |
| 51 |
derive_t hits; |
| 52 |
derive_t gets; |
| 53 |
derive_t incr_hits; |
| 54 |
derive_t incr_misses; |
| 55 |
derive_t decr_hits; |
| 56 |
derive_t decr_misses; |
| 57 |
}; |
| 58 |
|
| 59 |
typedef struct prev_s prev_t; |
| 60 |
|
| 61 |
struct memcached_s { |
| 62 |
char *name; |
| 63 |
char *host; |
| 64 |
char *socket; |
| 65 |
char *connhost; |
| 66 |
char *connport; |
| 67 |
int fd; |
| 68 |
prev_t prev; |
| 69 |
}; |
| 70 |
typedef struct memcached_s memcached_t; |
| 71 |
|
| 72 |
static bool memcached_have_instances; |
| 73 |
|
| 74 |
static void memcached_free(void *arg) { |
| 75 |
memcached_t *st = arg; |
| 76 |
if (st == NULL) |
| 77 |
return; |
| 78 |
|
| 79 |
if (st->fd >= 0) { |
| 80 |
shutdown(st->fd, SHUT_RDWR); |
| 81 |
close(st->fd); |
| 82 |
st->fd = -1; |
| 83 |
} |
| 84 |
|
| 85 |
sfree(st->name); |
| 86 |
sfree(st->host); |
| 87 |
sfree(st->socket); |
| 88 |
sfree(st->connhost); |
| 89 |
sfree(st->connport); |
| 90 |
sfree(st); |
| 91 |
} |
| 92 |
|
| 93 |
static int memcached_connect_unix(memcached_t *st) { |
| 94 |
struct sockaddr_un serv_addr = {0}; |
| 95 |
|
| 96 |
serv_addr.sun_family = AF_UNIX; |
| 97 |
sstrncpy(serv_addr.sun_path, st->socket, sizeof(serv_addr.sun_path)); |
| 98 |
|
| 99 |
/* create our socket descriptor */ |
| 100 |
int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 101 |
if (fd < 0) { |
| 102 |
ERROR("memcached plugin: memcached_connect_unix: socket(2) failed: %s", |
| 103 |
STRERRNO); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
/* connect to the memcached daemon */ |
| 108 |
int status = connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); |
| 109 |
if (status != 0) { |
| 110 |
shutdown(fd, SHUT_RDWR); |
| 111 |
close(fd); |
| 112 |
return -1; |
| 113 |
} |
| 114 |
|
| 115 |
/* switch to non-blocking mode */ |
| 116 |
int flags = fcntl(fd, F_GETFL); |
| 117 |
status = fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 118 |
if (status != 0) { |
| 119 |
close(fd); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
return fd; |
| 124 |
} /* int memcached_connect_unix */ |
| 125 |
|
| 126 |
static int memcached_connect_inet(memcached_t *st) { |
| 127 |
struct addrinfo *ai_list; |
| 128 |
int fd = -1; |
| 129 |
|
| 130 |
struct addrinfo ai_hints = {.ai_family = AF_UNSPEC, |
| 131 |
.ai_flags = AI_ADDRCONFIG, |
| 132 |
.ai_socktype = SOCK_STREAM}; |
| 133 |
|
| 134 |
int status = getaddrinfo(st->connhost, st->connport, &ai_hints, &ai_list); |
| 135 |
if (status != 0) { |
| 136 |
ERROR("memcached plugin: memcached_connect_inet: " |
| 137 |
"getaddrinfo(%s,%s) failed: %s", |
| 138 |
st->connhost, st->connport, |
| 139 |
(status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status)); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; |
| 144 |
ai_ptr = ai_ptr->ai_next) { |
| 145 |
/* create our socket descriptor */ |
| 146 |
fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); |
| 147 |
if (fd < 0) { |
| 148 |
WARNING("memcached plugin: memcached_connect_inet: " |
| 149 |
"socket(2) failed: %s", |
| 150 |
STRERRNO); |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
/* switch socket to non-blocking mode */ |
| 155 |
int flags = fcntl(fd, F_GETFL); |
| 156 |
status = fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 157 |
if (status != 0) { |
| 158 |
close(fd); |
| 159 |
fd = -1; |
| 160 |
continue; |
| 161 |
} |
| 162 |
|
| 163 |
/* connect to the memcached daemon */ |
| 164 |
status = (int)connect(fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen); |
| 165 |
if (status != 0 && errno != EINPROGRESS) { |
| 166 |
shutdown(fd, SHUT_RDWR); |
| 167 |
close(fd); |
| 168 |
fd = -1; |
| 169 |
continue; |
| 170 |
} |
| 171 |
|
| 172 |
/* Wait until connection establishes */ |
| 173 |
struct pollfd pollfd = { |
| 174 |
.fd = fd, |
| 175 |
.events = POLLOUT, |
| 176 |
}; |
| 177 |
do |
| 178 |
status = poll(&pollfd, 1, MEMCACHED_CONNECT_TIMEOUT); |
| 179 |
while (status < 0 && errno == EINTR); |
| 180 |
if (status <= 0) { |
| 181 |
close(fd); |
| 182 |
fd = -1; |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
/* Check if all is good */ |
| 187 |
int socket_error; |
| 188 |
status = getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&socket_error, |
| 189 |
&(socklen_t){sizeof(socket_error)}); |
| 190 |
if (status != 0 || socket_error != 0) { |
| 191 |
close(fd); |
| 192 |
fd = -1; |
| 193 |
continue; |
| 194 |
} |
| 195 |
/* A socket is opened and connection succeeded. We're done. */ |
| 196 |
break; |
| 197 |
} |
| 198 |
|
| 199 |
freeaddrinfo(ai_list); |
| 200 |
return fd; |
| 201 |
} /* int memcached_connect_inet */ |
| 202 |
|
| 203 |
static void memcached_connect(memcached_t *st) { |
| 204 |
if (st->fd >= 0) |
| 205 |
return; |
| 206 |
|
| 207 |
if (st->socket != NULL) |
| 208 |
st->fd = memcached_connect_unix(st); |
| 209 |
else |
| 210 |
st->fd = memcached_connect_inet(st); |
| 211 |
|
| 212 |
if (st->fd >= 0) |
| 213 |
INFO("memcached plugin: Instance \"%s\": connection established.", |
| 214 |
st->name); |
| 215 |
} |
| 216 |
|
| 217 |
static int memcached_query_daemon(char *buffer, size_t buffer_size, |
| 218 |
memcached_t *st) { |
| 219 |
int status; |
| 220 |
size_t buffer_fill; |
| 221 |
|
| 222 |
memcached_connect(st); |
| 223 |
if (st->fd < 0) { |
| 224 |
ERROR("memcached plugin: Instance \"%s\" could not connect to daemon.", |
| 225 |
st->name); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
struct pollfd pollfd = { |
| 230 |
.fd = st->fd, |
| 231 |
.events = POLLOUT, |
| 232 |
}; |
| 233 |
|
| 234 |
do |
| 235 |
status = poll(&pollfd, 1, MEMCACHED_IO_TIMEOUT); |
| 236 |
while (status < 0 && errno == EINTR); |
| 237 |
|
| 238 |
if (status <= 0) { |
| 239 |
ERROR("memcached plugin: poll() failed for write() call."); |
| 240 |
close(st->fd); |
| 241 |
st->fd = -1; |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
status = (int)swrite(st->fd, "stats\r\n", strlen("stats\r\n")); |
| 246 |
if (status != 0) { |
| 247 |
ERROR("memcached plugin: Instance \"%s\": write(2) failed: %s", st->name, |
| 248 |
STRERRNO); |
| 249 |
shutdown(st->fd, SHUT_RDWR); |
| 250 |
close(st->fd); |
| 251 |
st->fd = -1; |
| 252 |
return -1; |
| 253 |
} |
| 254 |
|
| 255 |
/* receive data from the memcached daemon */ |
| 256 |
memset(buffer, 0, buffer_size); |
| 257 |
|
| 258 |
buffer_fill = 0; |
| 259 |
pollfd.events = POLLIN; |
| 260 |
while (1) { |
| 261 |
do |
| 262 |
status = poll(&pollfd, 1, MEMCACHED_IO_TIMEOUT); |
| 263 |
while (status < 0 && errno == EINTR); |
| 264 |
|
| 265 |
if (status <= 0) { |
| 266 |
ERROR("memcached plugin: Instance \"%s\": Timeout reading from socket", |
| 267 |
st->name); |
| 268 |
close(st->fd); |
| 269 |
st->fd = -1; |
| 270 |
return -1; |
| 271 |
} |
| 272 |
|
| 273 |
do |
| 274 |
status = (int)recv(st->fd, buffer + buffer_fill, |
| 275 |
buffer_size - buffer_fill, /* flags = */ 0); |
| 276 |
while (status < 0 && errno == EINTR); |
| 277 |
|
| 278 |
char const end_token[5] = {'E', 'N', 'D', '\r', '\n'}; |
| 279 |
if (status < 0) { |
| 280 |
|
| 281 |
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
| 282 |
continue; |
| 283 |
|
| 284 |
ERROR("memcached plugin: Instance \"%s\": Error reading from socket: %s", |
| 285 |
st->name, STRERRNO); |
| 286 |
shutdown(st->fd, SHUT_RDWR); |
| 287 |
close(st->fd); |
| 288 |
st->fd = -1; |
| 289 |
return -1; |
| 290 |
} else if (status == 0) { |
| 291 |
ERROR("memcached plugin: Instance \"%s\": Connection closed by peer", |
| 292 |
st->name); |
| 293 |
close(st->fd); |
| 294 |
st->fd = -1; |
| 295 |
return -1; |
| 296 |
} |
| 297 |
|
| 298 |
buffer_fill += (size_t)status; |
| 299 |
if (buffer_fill > buffer_size) { |
| 300 |
buffer_fill = buffer_size; |
| 301 |
WARNING("memcached plugin: Instance \"%s\": Message was truncated.", |
| 302 |
st->name); |
| 303 |
shutdown(st->fd, SHUT_RDWR); |
| 304 |
close(st->fd); |
| 305 |
st->fd = -1; |
| 306 |
break; |
| 307 |
} |
| 308 |
|
| 309 |
/* If buffer ends in end_token, we have all the data. */ |
| 310 |
if (memcmp(buffer + buffer_fill - sizeof(end_token), end_token, |
| 311 |
sizeof(end_token)) == 0) |
| 312 |
break; |
| 313 |
} /* while (recv) */ |
| 314 |
|
| 315 |
status = 0; |
| 316 |
if (buffer_fill == 0) { |
| 317 |
WARNING("memcached plugin: Instance \"%s\": No data returned by memcached.", |
| 318 |
st->name); |
| 319 |
status = -1; |
| 320 |
} |
| 321 |
|
| 322 |
return status; |
| 323 |
} /* int memcached_query_daemon */ |
| 324 |
|
| 325 |
static void memcached_init_vl(value_list_t *vl, memcached_t const *st) { |
| 326 |
sstrncpy(vl->plugin, "memcached", sizeof(vl->plugin)); |
| 327 |
if (st->host != NULL) |
| 328 |
sstrncpy(vl->host, st->host, sizeof(vl->host)); |
| 329 |
if (st->name != NULL) |
| 330 |
sstrncpy(vl->plugin_instance, st->name, sizeof(vl->plugin_instance)); |
| 331 |
} |
| 332 |
|
| 333 |
static void submit_derive(const char *type, const char *type_inst, |
| 334 |
derive_t value, memcached_t *st) { |
| 335 |
value_list_t vl = VALUE_LIST_INIT; |
| 336 |
|
| 337 |
memcached_init_vl(&vl, st); |
| 338 |
vl.values = &(value_t){.derive = value}; |
| 339 |
vl.values_len = 1; |
| 340 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 341 |
if (type_inst != NULL) |
| 342 |
sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance)); |
| 343 |
|
| 344 |
plugin_dispatch_values(&vl); |
| 345 |
} |
| 346 |
|
| 347 |
static void submit_derive2(const char *type, const char *type_inst, |
| 348 |
derive_t value0, derive_t value1, memcached_t *st) { |
| 349 |
value_list_t vl = VALUE_LIST_INIT; |
| 350 |
value_t values[] = { |
| 351 |
{.derive = value0}, |
| 352 |
{.derive = value1}, |
| 353 |
}; |
| 354 |
|
| 355 |
memcached_init_vl(&vl, st); |
| 356 |
vl.values = values; |
| 357 |
vl.values_len = STATIC_ARRAY_SIZE(values); |
| 358 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 359 |
if (type_inst != NULL) |
| 360 |
sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance)); |
| 361 |
|
| 362 |
plugin_dispatch_values(&vl); |
| 363 |
} |
| 364 |
|
| 365 |
static void submit_gauge(const char *type, const char *type_inst, gauge_t value, |
| 366 |
memcached_t *st) { |
| 367 |
value_list_t vl = VALUE_LIST_INIT; |
| 368 |
|
| 369 |
memcached_init_vl(&vl, st); |
| 370 |
vl.values = &(value_t){.gauge = value}; |
| 371 |
vl.values_len = 1; |
| 372 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 373 |
if (type_inst != NULL) |
| 374 |
sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance)); |
| 375 |
|
| 376 |
plugin_dispatch_values(&vl); |
| 377 |
} |
| 378 |
|
| 379 |
static void submit_gauge2(const char *type, const char *type_inst, |
| 380 |
gauge_t value0, gauge_t value1, memcached_t *st) { |
| 381 |
value_list_t vl = VALUE_LIST_INIT; |
| 382 |
value_t values[] = { |
| 383 |
{.gauge = value0}, |
| 384 |
{.gauge = value1}, |
| 385 |
}; |
| 386 |
|
| 387 |
memcached_init_vl(&vl, st); |
| 388 |
vl.values = values; |
| 389 |
vl.values_len = STATIC_ARRAY_SIZE(values); |
| 390 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 391 |
if (type_inst != NULL) |
| 392 |
sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance)); |
| 393 |
|
| 394 |
plugin_dispatch_values(&vl); |
| 395 |
} |
| 396 |
|
| 397 |
static gauge_t calculate_ratio_percent(derive_t part, derive_t total, |
| 398 |
derive_t *prev_part, |
| 399 |
derive_t *prev_total) { |
| 400 |
if ((*prev_part == 0) || (*prev_total == 0) || (part < *prev_part) || |
| 401 |
(total < *prev_total)) { |
| 402 |
*prev_part = part; |
| 403 |
*prev_total = total; |
| 404 |
return NAN; |
| 405 |
} |
| 406 |
|
| 407 |
derive_t num = part - *prev_part; |
| 408 |
derive_t denom = total - *prev_total; |
| 409 |
|
| 410 |
*prev_part = part; |
| 411 |
*prev_total = total; |
| 412 |
|
| 413 |
if (denom == 0) |
| 414 |
return NAN; |
| 415 |
|
| 416 |
if (num == 0) |
| 417 |
return 0; |
| 418 |
|
| 419 |
return 100.0 * (gauge_t)num / (gauge_t)denom; |
| 420 |
} |
| 421 |
|
| 422 |
static gauge_t calculate_ratio_percent2(derive_t part1, derive_t part2, |
| 423 |
derive_t *prev1, derive_t *prev2) { |
| 424 |
if ((*prev1 == 0) || (*prev2 == 0) || (part1 < *prev1) || (part2 < *prev2)) { |
| 425 |
*prev1 = part1; |
| 426 |
*prev2 = part2; |
| 427 |
return NAN; |
| 428 |
} |
| 429 |
|
| 430 |
derive_t num = part1 - *prev1; |
| 431 |
derive_t denom = part2 - *prev2 + num; |
| 432 |
|
| 433 |
*prev1 = part1; |
| 434 |
*prev2 = part2; |
| 435 |
|
| 436 |
if (denom == 0) |
| 437 |
return NAN; |
| 438 |
|
| 439 |
if (num == 0) |
| 440 |
return 0; |
| 441 |
|
| 442 |
return 100.0 * (gauge_t)num / (gauge_t)denom; |
| 443 |
} |
| 444 |
|
| 445 |
static int memcached_read(user_data_t *user_data) { |
| 446 |
char buf[4096]; |
| 447 |
char *fields[3]; |
| 448 |
char *line; |
| 449 |
|
| 450 |
derive_t bytes_used = 0; |
| 451 |
derive_t bytes_total = 0; |
| 452 |
derive_t get_hits = 0; |
| 453 |
derive_t cmd_get = 0; |
| 454 |
derive_t incr_hits = 0; |
| 455 |
derive_t incr_misses = 0; |
| 456 |
derive_t decr_hits = 0; |
| 457 |
derive_t decr_misses = 0; |
| 458 |
derive_t rusage_user = 0; |
| 459 |
derive_t rusage_syst = 0; |
| 460 |
derive_t octets_rx = 0; |
| 461 |
derive_t octets_tx = 0; |
| 462 |
|
| 463 |
memcached_t *st = user_data->data; |
| 464 |
prev_t *prev = &st->prev; |
| 465 |
|
| 466 |
/* get data from daemon */ |
| 467 |
if (memcached_query_daemon(buf, sizeof(buf), st) < 0) { |
| 468 |
return -1; |
| 469 |
} |
| 470 |
|
| 471 |
#define FIELD_IS(cnst) \ |
| 472 |
(((sizeof(cnst) - 1) == name_len) && (strcmp(cnst, fields[1]) == 0)) |
| 473 |
|
| 474 |
char *ptr = buf; |
| 475 |
char *saveptr = NULL; |
| 476 |
while ((line = strtok_r(ptr, "\n\r", &saveptr)) != NULL) { |
| 477 |
ptr = NULL; |
| 478 |
|
| 479 |
if (strsplit(line, fields, 3) != 3) |
| 480 |
continue; |
| 481 |
|
| 482 |
size_t name_len = strlen(fields[1]); |
| 483 |
if (name_len == 0) |
| 484 |
continue; |
| 485 |
|
| 486 |
/* |
| 487 |
* For an explanation on these fields please refer to |
| 488 |
* <https://github.com/memcached/memcached/blob/master/doc/protocol.txt> |
| 489 |
*/ |
| 490 |
|
| 491 |
/* |
| 492 |
* CPU time consumed by the memcached process |
| 493 |
*/ |
| 494 |
if (FIELD_IS("rusage_user")) { |
| 495 |
/* Convert to useconds */ |
| 496 |
rusage_user = atof(fields[2]) * 1000000; |
| 497 |
} else if (FIELD_IS("rusage_system")) { |
| 498 |
rusage_syst = atof(fields[2]) * 1000000; |
| 499 |
} |
| 500 |
|
| 501 |
/* |
| 502 |
* Number of threads of this instance |
| 503 |
*/ |
| 504 |
else if (FIELD_IS("threads")) { |
| 505 |
submit_gauge2("ps_count", NULL, NAN, atof(fields[2]), st); |
| 506 |
} |
| 507 |
|
| 508 |
/* |
| 509 |
* Number of items stored |
| 510 |
*/ |
| 511 |
else if (FIELD_IS("curr_items")) { |
| 512 |
submit_gauge("memcached_items", "current", atof(fields[2]), st); |
| 513 |
} |
| 514 |
|
| 515 |
/* |
| 516 |
* Number of secs since the server started |
| 517 |
*/ |
| 518 |
else if (FIELD_IS("uptime")) { |
| 519 |
submit_gauge("uptime", NULL, atof(fields[2]), st); |
| 520 |
} |
| 521 |
|
| 522 |
/* |
| 523 |
* Number of bytes used and available (total - used) |
| 524 |
*/ |
| 525 |
else if (FIELD_IS("bytes")) { |
| 526 |
bytes_used = atoll(fields[2]); |
| 527 |
} else if (FIELD_IS("limit_maxbytes")) { |
| 528 |
bytes_total = atoll(fields[2]); |
| 529 |
} |
| 530 |
|
| 531 |
/* |
| 532 |
* Connections |
| 533 |
*/ |
| 534 |
else if (FIELD_IS("curr_connections")) { |
| 535 |
submit_gauge("memcached_connections", "current", atof(fields[2]), st); |
| 536 |
} else if (FIELD_IS("listen_disabled_num")) { |
| 537 |
submit_derive("total_events", "listen_disabled", atoll(fields[2]), st); |
| 538 |
} |
| 539 |
/* |
| 540 |
* Total number of connections opened since the server started running |
| 541 |
* Report this as connection rate. |
| 542 |
*/ |
| 543 |
else if (FIELD_IS("total_connections")) { |
| 544 |
submit_derive("connections", "opened", atoll(fields[2]), st); |
| 545 |
} |
| 546 |
|
| 547 |
/* |
| 548 |
* Commands |
| 549 |
*/ |
| 550 |
else if ((name_len > 4) && (strncmp(fields[1], "cmd_", 4) == 0)) { |
| 551 |
const char *name = fields[1] + 4; |
| 552 |
submit_derive("memcached_command", name, atoll(fields[2]), st); |
| 553 |
if (strcmp(name, "get") == 0) |
| 554 |
cmd_get = atoll(fields[2]); |
| 555 |
} |
| 556 |
|
| 557 |
/* |
| 558 |
* Increment/Decrement |
| 559 |
*/ |
| 560 |
else if (FIELD_IS("incr_misses")) { |
| 561 |
incr_misses = atoll(fields[2]); |
| 562 |
submit_derive("memcached_ops", "incr_misses", incr_misses, st); |
| 563 |
} else if (FIELD_IS("incr_hits")) { |
| 564 |
incr_hits = atoll(fields[2]); |
| 565 |
submit_derive("memcached_ops", "incr_hits", incr_hits, st); |
| 566 |
} else if (FIELD_IS("decr_misses")) { |
| 567 |
decr_misses = atoll(fields[2]); |
| 568 |
submit_derive("memcached_ops", "decr_misses", decr_misses, st); |
| 569 |
} else if (FIELD_IS("decr_hits")) { |
| 570 |
decr_hits = atoll(fields[2]); |
| 571 |
submit_derive("memcached_ops", "decr_hits", decr_hits, st); |
| 572 |
} |
| 573 |
|
| 574 |
/* |
| 575 |
* Operations on the cache: |
| 576 |
* - get hits/misses |
| 577 |
* - delete hits/misses |
| 578 |
* - evictions |
| 579 |
*/ |
| 580 |
else if (FIELD_IS("get_hits")) { |
| 581 |
get_hits = atoll(fields[2]); |
| 582 |
submit_derive("memcached_ops", "hits", get_hits, st); |
| 583 |
} else if (FIELD_IS("get_misses")) { |
| 584 |
submit_derive("memcached_ops", "misses", atoll(fields[2]), st); |
| 585 |
} else if (FIELD_IS("evictions")) { |
| 586 |
submit_derive("memcached_ops", "evictions", atoll(fields[2]), st); |
| 587 |
} else if (FIELD_IS("delete_hits")) { |
| 588 |
submit_derive("memcached_ops", "delete_hits", atoll(fields[2]), st); |
| 589 |
} else if (FIELD_IS("delete_misses")) { |
| 590 |
submit_derive("memcached_ops", "delete_misses", atoll(fields[2]), st); |
| 591 |
} |
| 592 |
|
| 593 |
/* |
| 594 |
* Network traffic |
| 595 |
*/ |
| 596 |
else if (FIELD_IS("bytes_read")) { |
| 597 |
octets_rx = atoll(fields[2]); |
| 598 |
} else if (FIELD_IS("bytes_written")) { |
| 599 |
octets_tx = atoll(fields[2]); |
| 600 |
} |
| 601 |
} /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */ |
| 602 |
|
| 603 |
if ((bytes_total > 0) && (bytes_used <= bytes_total)) |
| 604 |
submit_gauge2("df", "cache", bytes_used, bytes_total - bytes_used, st); |
| 605 |
|
| 606 |
if ((rusage_user != 0) || (rusage_syst != 0)) |
| 607 |
submit_derive2("ps_cputime", NULL, rusage_user, rusage_syst, st); |
| 608 |
|
| 609 |
if ((octets_rx != 0) || (octets_tx != 0)) |
| 610 |
submit_derive2("memcached_octets", NULL, octets_rx, octets_tx, st); |
| 611 |
|
| 612 |
if ((cmd_get != 0) && (get_hits != 0)) { |
| 613 |
gauge_t ratio = |
| 614 |
calculate_ratio_percent(get_hits, cmd_get, &prev->hits, &prev->gets); |
| 615 |
submit_gauge("percent", "hitratio", ratio, st); |
| 616 |
} |
| 617 |
|
| 618 |
if ((incr_hits != 0) && (incr_misses != 0)) { |
| 619 |
gauge_t ratio = calculate_ratio_percent2( |
| 620 |
incr_hits, incr_misses, &prev->incr_hits, &prev->incr_misses); |
| 621 |
submit_gauge("percent", "incr_hitratio", ratio, st); |
| 622 |
submit_derive("memcached_ops", "incr", incr_hits + incr_misses, st); |
| 623 |
} |
| 624 |
|
| 625 |
if ((decr_hits != 0) && (decr_misses != 0)) { |
| 626 |
gauge_t ratio = calculate_ratio_percent2( |
| 627 |
decr_hits, decr_misses, &prev->decr_hits, &prev->decr_misses); |
| 628 |
submit_gauge("percent", "decr_hitratio", ratio, st); |
| 629 |
submit_derive("memcached_ops", "decr", decr_hits + decr_misses, st); |
| 630 |
} |
| 631 |
|
| 632 |
return 0; |
| 633 |
} /* int memcached_read */ |
| 634 |
|
| 635 |
static int memcached_set_defaults(memcached_t *st) { |
| 636 |
/* If no <Address> used then: |
| 637 |
* - Connect to the destination specified by <Host>, if present. |
| 638 |
* If not, use the default address. |
| 639 |
* - Use the default hostname (set st->host to NULL), if |
| 640 |
* - Legacy mode is used (no configuration options at all), or |
| 641 |
* - "Host" option is not provided, or |
| 642 |
* - "Host" option is set to "localhost" or "127.0.0.1". |
| 643 |
* |
| 644 |
* If <Address> used then host may be set to "localhost" or "127.0.0.1" |
| 645 |
* explicitly. |
| 646 |
*/ |
| 647 |
if (st->connhost == NULL) { |
| 648 |
if (st->host) { |
| 649 |
st->connhost = strdup(st->host); |
| 650 |
if (st->connhost == NULL) |
| 651 |
return ENOMEM; |
| 652 |
|
| 653 |
if ((strcmp("127.0.0.1", st->host) == 0) || |
| 654 |
(strcmp("localhost", st->host) == 0)) |
| 655 |
sfree(st->host); |
| 656 |
} else { |
| 657 |
st->connhost = strdup(MEMCACHED_DEF_HOST); |
| 658 |
if (st->connhost == NULL) |
| 659 |
return ENOMEM; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
if (st->connport == NULL) { |
| 664 |
st->connport = strdup(MEMCACHED_DEF_PORT); |
| 665 |
if (st->connport == NULL) |
| 666 |
return ENOMEM; |
| 667 |
} |
| 668 |
|
| 669 |
assert(st->connhost != NULL); |
| 670 |
assert(st->connport != NULL); |
| 671 |
|
| 672 |
st->prev.hits = 0; |
| 673 |
st->prev.gets = 0; |
| 674 |
st->prev.incr_hits = 0; |
| 675 |
st->prev.incr_misses = 0; |
| 676 |
st->prev.decr_hits = 0; |
| 677 |
st->prev.decr_misses = 0; |
| 678 |
|
| 679 |
return 0; |
| 680 |
} /* int memcached_set_defaults */ |
| 681 |
|
| 682 |
static int memcached_add_read_callback(memcached_t *st) { |
| 683 |
char callback_name[3 * DATA_MAX_NAME_LEN]; |
| 684 |
|
| 685 |
if (memcached_set_defaults(st) != 0) { |
| 686 |
memcached_free(st); |
| 687 |
return -1; |
| 688 |
} |
| 689 |
|
| 690 |
snprintf(callback_name, sizeof(callback_name), "memcached/%s", |
| 691 |
(st->name != NULL) ? st->name : "__legacy__"); |
| 692 |
|
| 693 |
return plugin_register_complex_read( |
| 694 |
/* group = */ "memcached", |
| 695 |
/* name = */ callback_name, |
| 696 |
/* callback = */ memcached_read, |
| 697 |
/* interval = */ 0, |
| 698 |
&(user_data_t){ |
| 699 |
.data = st, |
| 700 |
.free_func = memcached_free, |
| 701 |
}); |
| 702 |
} /* int memcached_add_read_callback */ |
| 703 |
|
| 704 |
/* Configuration handling functiions |
| 705 |
* <Plugin memcached> |
| 706 |
* <Instance "instance_name"> |
| 707 |
* Host foo.zomg.com |
| 708 |
* Address 1.2.3.4 |
| 709 |
* Port "1234" |
| 710 |
* </Instance> |
| 711 |
* </Plugin> |
| 712 |
*/ |
| 713 |
static int config_add_instance(oconfig_item_t *ci) { |
| 714 |
int status = 0; |
| 715 |
|
| 716 |
/* Disable automatic generation of default instance in the init callback. */ |
| 717 |
memcached_have_instances = true; |
| 718 |
|
| 719 |
memcached_t *st = calloc(1, sizeof(*st)); |
| 720 |
if (st == NULL) { |
| 721 |
ERROR("memcached plugin: calloc failed."); |
| 722 |
return ENOMEM; |
| 723 |
} |
| 724 |
|
| 725 |
st->name = NULL; |
| 726 |
st->host = NULL; |
| 727 |
st->socket = NULL; |
| 728 |
st->connhost = NULL; |
| 729 |
st->connport = NULL; |
| 730 |
|
| 731 |
st->fd = -1; |
| 732 |
|
| 733 |
if (strcasecmp(ci->key, "Instance") == 0) |
| 734 |
status = cf_util_get_string(ci, &st->name); |
| 735 |
|
| 736 |
if (status != 0) { |
| 737 |
sfree(st); |
| 738 |
return status; |
| 739 |
} |
| 740 |
|
| 741 |
for (int i = 0; i < ci->children_num; i++) { |
| 742 |
oconfig_item_t *child = ci->children + i; |
| 743 |
|
| 744 |
if (strcasecmp("Socket", child->key) == 0) |
| 745 |
status = cf_util_get_string(child, &st->socket); |
| 746 |
else if (strcasecmp("Host", child->key) == 0) |
| 747 |
status = cf_util_get_string(child, &st->host); |
| 748 |
else if (strcasecmp("Address", child->key) == 0) |
| 749 |
status = cf_util_get_string(child, &st->connhost); |
| 750 |
else if (strcasecmp("Port", child->key) == 0) |
| 751 |
status = cf_util_get_service(child, &st->connport); |
| 752 |
else { |
| 753 |
WARNING("memcached plugin: Option `%s' not allowed here.", child->key); |
| 754 |
status = -1; |
| 755 |
} |
| 756 |
|
| 757 |
if (status != 0) |
| 758 |
break; |
| 759 |
} |
| 760 |
|
| 761 |
if (status != 0) { |
| 762 |
memcached_free(st); |
| 763 |
return -1; |
| 764 |
} |
| 765 |
|
| 766 |
return memcached_add_read_callback(st); |
| 767 |
} /* int config_add_instance */ |
| 768 |
|
| 769 |
static int memcached_config(oconfig_item_t *ci) { |
| 770 |
bool have_instance_block = 0; |
| 771 |
|
| 772 |
for (int i = 0; i < ci->children_num; i++) { |
| 773 |
oconfig_item_t *child = ci->children + i; |
| 774 |
|
| 775 |
if (strcasecmp("Instance", child->key) == 0) { |
| 776 |
config_add_instance(child); |
| 777 |
have_instance_block = 1; |
| 778 |
} else if (!have_instance_block) { |
| 779 |
/* Non-instance option: Assume legacy configuration (without <Instance /> |
| 780 |
* blocks) and call config_add_instance() with the <Plugin /> block. */ |
| 781 |
return config_add_instance(ci); |
| 782 |
} else |
| 783 |
WARNING("memcached plugin: The configuration option " |
| 784 |
"\"%s\" is not allowed here. Did you " |
| 785 |
"forget to add an <Instance /> block " |
| 786 |
"around the configuration?", |
| 787 |
child->key); |
| 788 |
} /* for (ci->children) */ |
| 789 |
|
| 790 |
return 0; |
| 791 |
} /* int memcached_config */ |
| 792 |
|
| 793 |
static int memcached_init(void) { |
| 794 |
|
| 795 |
if (memcached_have_instances) |
| 796 |
return 0; |
| 797 |
|
| 798 |
/* No instances were configured, lets start a default instance. */ |
| 799 |
memcached_t *st = calloc(1, sizeof(*st)); |
| 800 |
if (st == NULL) |
| 801 |
return ENOMEM; |
| 802 |
st->name = NULL; |
| 803 |
st->host = NULL; |
| 804 |
st->socket = NULL; |
| 805 |
st->connhost = NULL; |
| 806 |
st->connport = NULL; |
| 807 |
|
| 808 |
st->fd = -1; |
| 809 |
|
| 810 |
int status = memcached_add_read_callback(st); |
| 811 |
if (status == 0) |
| 812 |
memcached_have_instances = true; |
| 813 |
|
| 814 |
return status; |
| 815 |
} /* int memcached_init */ |
| 816 |
|
| 817 |
void module_register(void) { |
| 818 |
plugin_register_complex_config("memcached", memcached_config); |
| 819 |
plugin_register_init("memcached", memcached_init); |
| 820 |
} |