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
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * Antony Dovgal <tony at daylessday dot org>
25 * Florian octo Forster <octo at collectd.org>
26 * Doug MacEachern <dougm at hyperic.com>
37 #include <netinet/in.h>
38 #include <netinet/tcp.h>
41 #define MEMCACHED_DEF_HOST "127.0.0.1"
42 #define MEMCACHED_DEF_PORT "11211"
51 typedef struct memcached_s memcached_t;
53 static _Bool memcached_have_instances = 0;
55 static void memcached_free(void *arg) {
56 memcached_t *st = arg;
68 static int memcached_connect_unix(memcached_t *st) {
69 struct sockaddr_un serv_addr = {0};
72 serv_addr.sun_family = AF_UNIX;
73 sstrncpy(serv_addr.sun_path, st->socket, sizeof(serv_addr.sun_path));
75 /* create our socket descriptor */
76 fd = socket(AF_UNIX, SOCK_STREAM, 0);
79 ERROR("memcached plugin: memcached_connect_unix: socket(2) failed: %s",
80 sstrerror(errno, errbuf, sizeof(errbuf)));
84 /* connect to the memcached daemon */
85 int status = connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
87 shutdown(fd, SHUT_RDWR);
93 } /* int memcached_connect_unix */
95 static int memcached_connect_inet(memcached_t *st) {
96 struct addrinfo *ai_list;
100 struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
101 .ai_flags = AI_ADDRCONFIG,
102 .ai_socktype = SOCK_STREAM};
104 status = getaddrinfo(st->connhost, st->connport, &ai_hints, &ai_list);
107 ERROR("memcached plugin: memcached_connect_inet: "
108 "getaddrinfo(%s,%s) failed: %s",
109 st->connhost, st->connport,
110 (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
111 : gai_strerror(status));
115 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
116 ai_ptr = ai_ptr->ai_next) {
117 /* create our socket descriptor */
118 fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
121 WARNING("memcached plugin: memcached_connect_inet: "
122 "socket(2) failed: %s",
123 sstrerror(errno, errbuf, sizeof(errbuf)));
127 /* connect to the memcached daemon */
128 status = (int)connect(fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
130 shutdown(fd, SHUT_RDWR);
136 /* A socket could be opened and connecting succeeded. We're done. */
140 freeaddrinfo(ai_list);
142 } /* int memcached_connect_inet */
144 static int memcached_connect(memcached_t *st) {
145 if (st->socket != NULL)
146 return (memcached_connect_unix(st));
148 return (memcached_connect_inet(st));
151 static int memcached_query_daemon(char *buffer, size_t buffer_size,
156 fd = memcached_connect(st);
158 ERROR("memcached plugin: Instance \"%s\" could not connect to daemon.",
163 status = (int)swrite(fd, "stats\r\n", strlen("stats\r\n"));
166 ERROR("memcached plugin: write(2) failed: %s",
167 sstrerror(errno, errbuf, sizeof(errbuf)));
168 shutdown(fd, SHUT_RDWR);
173 /* receive data from the memcached daemon */
174 memset(buffer, 0, buffer_size);
177 while ((status = (int)recv(fd, buffer + buffer_fill,
178 buffer_size - buffer_fill, /* flags = */ 0)) !=
180 char const end_token[5] = {'E', 'N', 'D', '\r', '\n'};
184 if ((errno == EAGAIN) || (errno == EINTR))
187 ERROR("memcached: Error reading from socket: %s",
188 sstrerror(errno, errbuf, sizeof(errbuf)));
189 shutdown(fd, SHUT_RDWR);
194 buffer_fill += (size_t)status;
195 if (buffer_fill > buffer_size) {
196 buffer_fill = buffer_size;
197 WARNING("memcached plugin: Message was truncated.");
201 /* If buffer ends in end_token, we have all the data. */
202 if (memcmp(buffer + buffer_fill - sizeof(end_token), end_token,
203 sizeof(end_token)) == 0)
208 if (buffer_fill == 0) {
209 WARNING("memcached plugin: No data returned by memcached.");
213 shutdown(fd, SHUT_RDWR);
216 } /* int memcached_query_daemon */
218 static void memcached_init_vl(value_list_t *vl, memcached_t const *st) {
219 sstrncpy(vl->plugin, "memcached", sizeof(vl->plugin));
220 if (st->host != NULL)
221 sstrncpy(vl->host, st->host, sizeof(vl->host));
222 if (st->name != NULL)
223 sstrncpy(vl->plugin_instance, st->name, sizeof(vl->plugin_instance));
226 static void submit_derive(const char *type, const char *type_inst,
227 derive_t value, memcached_t *st) {
228 value_list_t vl = VALUE_LIST_INIT;
230 memcached_init_vl(&vl, st);
231 vl.values = &(value_t){.derive = value};
233 sstrncpy(vl.type, type, sizeof(vl.type));
234 if (type_inst != NULL)
235 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
237 plugin_dispatch_values(&vl);
240 static void submit_derive2(const char *type, const char *type_inst,
241 derive_t value0, derive_t value1, memcached_t *st) {
242 value_list_t vl = VALUE_LIST_INIT;
244 {.derive = value0}, {.derive = value1},
247 memcached_init_vl(&vl, st);
249 vl.values_len = STATIC_ARRAY_SIZE(values);
250 sstrncpy(vl.type, type, sizeof(vl.type));
251 if (type_inst != NULL)
252 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
254 plugin_dispatch_values(&vl);
257 static void submit_gauge(const char *type, const char *type_inst, gauge_t value,
259 value_list_t vl = VALUE_LIST_INIT;
261 memcached_init_vl(&vl, st);
262 vl.values = &(value_t){.gauge = value};
264 sstrncpy(vl.type, type, sizeof(vl.type));
265 if (type_inst != NULL)
266 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
268 plugin_dispatch_values(&vl);
271 static void submit_gauge2(const char *type, const char *type_inst,
272 gauge_t value0, gauge_t value1, memcached_t *st) {
273 value_list_t vl = VALUE_LIST_INIT;
275 {.gauge = value0}, {.gauge = value1},
278 memcached_init_vl(&vl, st);
280 vl.values_len = STATIC_ARRAY_SIZE(values);
281 sstrncpy(vl.type, type, sizeof(vl.type));
282 if (type_inst != NULL)
283 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
285 plugin_dispatch_values(&vl);
288 static int memcached_read(user_data_t *user_data) {
296 gauge_t bytes_used = NAN;
297 gauge_t bytes_total = NAN;
300 gauge_t incr_hits = NAN;
302 gauge_t decr_hits = NAN;
304 derive_t rusage_user = 0;
305 derive_t rusage_syst = 0;
306 derive_t octets_rx = 0;
307 derive_t octets_tx = 0;
310 st = user_data->data;
312 /* get data from daemon */
313 if (memcached_query_daemon(buf, sizeof(buf), st) < 0) {
317 #define FIELD_IS(cnst) \
318 (((sizeof(cnst) - 1) == name_len) && (strcmp(cnst, fields[1]) == 0))
322 while ((line = strtok_r(ptr, "\n\r", &saveptr)) != NULL) {
327 fields_num = strsplit(line, fields, 3);
331 name_len = strlen(fields[1]);
336 * For an explanation on these fields please refer to
337 * <https://github.com/memcached/memcached/blob/master/doc/protocol.txt>
341 * CPU time consumed by the memcached process
343 if (FIELD_IS("rusage_user")) {
344 rusage_user = atoll(fields[2]);
345 } else if (FIELD_IS("rusage_system")) {
346 rusage_syst = atoll(fields[2]);
350 * Number of threads of this instance
352 else if (FIELD_IS("threads")) {
353 submit_gauge2("ps_count", NULL, NAN, atof(fields[2]), st);
357 * Number of items stored
359 else if (FIELD_IS("curr_items")) {
360 submit_gauge("memcached_items", "current", atof(fields[2]), st);
364 * Number of bytes used and available (total - used)
366 else if (FIELD_IS("bytes")) {
367 bytes_used = atof(fields[2]);
368 } else if (FIELD_IS("limit_maxbytes")) {
369 bytes_total = atof(fields[2]);
375 else if (FIELD_IS("curr_connections")) {
376 submit_gauge("memcached_connections", "current", atof(fields[2]), st);
377 } else if (FIELD_IS("listen_disabled_num")) {
378 submit_derive("connections", "listen_disabled", atof(fields[2]), st);
384 else if ((name_len > 4) && (strncmp(fields[1], "cmd_", 4) == 0)) {
385 const char *name = fields[1] + 4;
386 submit_derive("memcached_command", name, atoll(fields[2]), st);
387 if (strcmp(name, "get") == 0)
388 gets = atof(fields[2]);
392 * Increment/Decrement
394 else if (FIELD_IS("incr_misses")) {
395 derive_t incr_count = atoll(fields[2]);
396 submit_derive("memcached_ops", "incr_misses", incr_count, st);
398 } else if (FIELD_IS("incr_hits")) {
399 derive_t incr_count = atoll(fields[2]);
400 submit_derive("memcached_ops", "incr_hits", incr_count, st);
401 incr_hits = atof(fields[2]);
403 } else if (FIELD_IS("decr_misses")) {
404 derive_t decr_count = atoll(fields[2]);
405 submit_derive("memcached_ops", "decr_misses", decr_count, st);
407 } else if (FIELD_IS("decr_hits")) {
408 derive_t decr_count = atoll(fields[2]);
409 submit_derive("memcached_ops", "decr_hits", decr_count, st);
410 decr_hits = atof(fields[2]);
415 * Operations on the cache, i. e. cache hits, cache misses and evictions of
418 else if (FIELD_IS("get_hits")) {
419 submit_derive("memcached_ops", "hits", atoll(fields[2]), st);
420 hits = atof(fields[2]);
421 } else if (FIELD_IS("get_misses")) {
422 submit_derive("memcached_ops", "misses", atoll(fields[2]), st);
423 } else if (FIELD_IS("evictions")) {
424 submit_derive("memcached_ops", "evictions", atoll(fields[2]), st);
430 else if (FIELD_IS("bytes_read")) {
431 octets_rx = atoll(fields[2]);
432 } else if (FIELD_IS("bytes_written")) {
433 octets_tx = atoll(fields[2]);
435 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
437 if (!isnan(bytes_used) && !isnan(bytes_total) && (bytes_used <= bytes_total))
438 submit_gauge2("df", "cache", bytes_used, bytes_total - bytes_used, st);
440 if ((rusage_user != 0) || (rusage_syst != 0))
441 submit_derive2("ps_cputime", NULL, rusage_user, rusage_syst, st);
443 if ((octets_rx != 0) || (octets_tx != 0))
444 submit_derive2("memcached_octets", NULL, octets_rx, octets_tx, st);
446 if (!isnan(gets) && !isnan(hits)) {
450 rate = 100.0 * hits / gets;
452 submit_gauge("percent", "hitratio", rate, st);
455 if (!isnan(incr_hits) && incr != 0) {
456 gauge_t incr_rate = 100.0 * incr_hits / incr;
457 submit_gauge("percent", "incr_hitratio", incr_rate, st);
458 submit_derive("memcached_ops", "incr", incr, st);
461 if (!isnan(decr_hits) && decr != 0) {
462 gauge_t decr_rate = 100.0 * decr_hits / decr;
463 submit_gauge("percent", "decr_hitratio", decr_rate, st);
464 submit_derive("memcached_ops", "decr", decr, st);
468 } /* int memcached_read */
470 static int memcached_add_read_callback(memcached_t *st) {
471 char callback_name[3 * DATA_MAX_NAME_LEN];
474 ssnprintf(callback_name, sizeof(callback_name), "memcached/%s",
475 (st->name != NULL) ? st->name : "__legacy__");
477 /* If no <Address> used then:
478 * - Connect to the destination specified by <Host>, if present.
479 * If not, use the default address.
480 * - Use the default hostname (set st->host to NULL), if
481 * - Legacy mode is used (no configuration options at all), or
482 * - "Host" option is not provided, or
483 * - "Host" option is set to "localhost" or "127.0.0.1".
485 * If <Address> used then host may be set to "localhost" or "127.0.0.1"
488 if (st->connhost == NULL) {
490 st->connhost = strdup(st->host);
491 if (st->connhost == NULL)
494 if ((strcmp("127.0.0.1", st->host) == 0) ||
495 (strcmp("localhost", st->host) == 0))
498 st->connhost = strdup(MEMCACHED_DEF_HOST);
499 if (st->connhost == NULL)
504 if (st->connport == NULL) {
505 st->connport = strdup(MEMCACHED_DEF_PORT);
506 if (st->connport == NULL)
510 assert(st->connhost != NULL);
511 assert(st->connport != NULL);
513 status = plugin_register_complex_read(
514 /* group = */ "memcached",
515 /* name = */ callback_name,
516 /* callback = */ memcached_read,
517 /* interval = */ 0, &(user_data_t){
518 .data = st, .free_func = memcached_free,
522 } /* int memcached_add_read_callback */
524 /* Configuration handling functiions
526 * <Instance "instance_name">
533 static int config_add_instance(oconfig_item_t *ci) {
537 /* Disable automatic generation of default instance in the init callback. */
538 memcached_have_instances = 1;
540 st = calloc(1, sizeof(*st));
542 ERROR("memcached plugin: calloc failed.");
552 if (strcasecmp(ci->key, "Instance") == 0)
553 status = cf_util_get_string(ci, &st->name);
560 for (int i = 0; i < ci->children_num; i++) {
561 oconfig_item_t *child = ci->children + i;
563 if (strcasecmp("Socket", child->key) == 0)
564 status = cf_util_get_string(child, &st->socket);
565 else if (strcasecmp("Host", child->key) == 0)
566 status = cf_util_get_string(child, &st->host);
567 else if (strcasecmp("Address", child->key) == 0)
568 status = cf_util_get_string(child, &st->connhost);
569 else if (strcasecmp("Port", child->key) == 0)
570 status = cf_util_get_service(child, &st->connport);
572 WARNING("memcached plugin: Option `%s' not allowed here.", child->key);
581 status = memcached_add_read_callback(st);
591 static int memcached_config(oconfig_item_t *ci) {
593 _Bool have_instance_block = 0;
595 for (int i = 0; i < ci->children_num; i++) {
596 oconfig_item_t *child = ci->children + i;
598 if (strcasecmp("Instance", child->key) == 0) {
599 config_add_instance(child);
600 have_instance_block = 1;
601 } else if (!have_instance_block) {
602 /* Non-instance option: Assume legacy configuration (without <Instance />
603 * blocks) and call config_add_instance() with the <Plugin /> block. */
604 return (config_add_instance(ci));
606 WARNING("memcached plugin: The configuration option "
607 "\"%s\" is not allowed here. Did you "
608 "forget to add an <Instance /> block "
609 "around the configuration?",
611 } /* for (ci->children) */
616 static int memcached_init(void) {
620 if (memcached_have_instances)
623 /* No instances were configured, lets start a default instance. */
624 st = calloc(1, sizeof(*st));
633 status = memcached_add_read_callback(st);
635 memcached_have_instances = 1;
640 } /* int memcached_init */
642 void module_register(void) {
643 plugin_register_complex_config("memcached", memcached_config);
644 plugin_register_init("memcached", memcached_init);