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>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
41 #define MEMCACHED_DEF_HOST "127.0.0.1"
42 #define MEMCACHED_DEF_PORT "11211"
52 typedef struct memcached_s memcached_t;
54 static _Bool memcached_have_instances = 0;
56 static void memcached_free (void *arg)
58 memcached_t *st = arg;
70 static int memcached_connect_unix (memcached_t *st)
72 struct sockaddr_un serv_addr = { 0 };
75 serv_addr.sun_family = AF_UNIX;
76 sstrncpy (serv_addr.sun_path, st->socket,
77 sizeof (serv_addr.sun_path));
79 /* create our socket descriptor */
80 fd = socket (AF_UNIX, SOCK_STREAM, 0);
84 ERROR ("memcached plugin: memcached_connect_unix: socket(2) failed: %s",
85 sstrerror (errno, errbuf, sizeof (errbuf)));
89 /* connect to the memcached daemon */
90 int status = connect (fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
93 shutdown (fd, SHUT_RDWR);
99 } /* int memcached_connect_unix */
101 static int memcached_connect_inet (memcached_t *st)
103 struct addrinfo *ai_list;
107 struct addrinfo ai_hints = {
108 .ai_family = AF_UNSPEC,
109 .ai_flags = AI_ADDRCONFIG,
110 .ai_socktype = SOCK_STREAM
113 status = getaddrinfo (st->connhost, st->connport, &ai_hints, &ai_list);
117 ERROR ("memcached plugin: memcached_connect_inet: "
118 "getaddrinfo(%s,%s) failed: %s",
119 st->connhost, st->connport,
120 (status == EAI_SYSTEM)
121 ? sstrerror (errno, errbuf, sizeof (errbuf))
122 : gai_strerror (status));
126 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
128 /* create our socket descriptor */
129 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
133 WARNING ("memcached plugin: memcached_connect_inet: "
134 "socket(2) failed: %s",
135 sstrerror (errno, errbuf, sizeof (errbuf)));
139 /* connect to the memcached daemon */
140 status = (int) connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
143 shutdown (fd, SHUT_RDWR);
149 /* A socket could be opened and connecting succeeded. We're done. */
153 freeaddrinfo (ai_list);
155 } /* int memcached_connect_inet */
157 static int memcached_connect (memcached_t *st)
159 if (st->socket != NULL)
160 return (memcached_connect_unix (st));
162 return (memcached_connect_inet (st));
165 static int memcached_query_daemon (char *buffer, size_t buffer_size, memcached_t *st)
170 fd = memcached_connect (st);
172 ERROR ("memcached plugin: Instance \"%s\" could not connect to daemon.",
177 status = (int) swrite (fd, "stats\r\n", strlen ("stats\r\n"));
181 ERROR ("memcached plugin: write(2) failed: %s",
182 sstrerror (errno, errbuf, sizeof (errbuf)));
183 shutdown(fd, SHUT_RDWR);
188 /* receive data from the memcached daemon */
189 memset (buffer, 0, buffer_size);
192 while ((status = (int) recv (fd, buffer + buffer_fill,
193 buffer_size - buffer_fill, /* flags = */ 0)) != 0)
195 char const end_token[5] = {'E', 'N', 'D', '\r', '\n'};
200 if ((errno == EAGAIN) || (errno == EINTR))
203 ERROR ("memcached: Error reading from socket: %s",
204 sstrerror (errno, errbuf, sizeof (errbuf)));
205 shutdown(fd, SHUT_RDWR);
210 buffer_fill += (size_t) status;
211 if (buffer_fill > buffer_size)
213 buffer_fill = buffer_size;
214 WARNING ("memcached plugin: Message was truncated.");
218 /* If buffer ends in end_token, we have all the data. */
219 if (memcmp (buffer + buffer_fill - sizeof (end_token),
220 end_token, sizeof (end_token)) == 0)
225 if (buffer_fill == 0)
227 WARNING ("memcached plugin: No data returned by memcached.");
231 shutdown(fd, SHUT_RDWR);
234 } /* int memcached_query_daemon */
236 static void memcached_init_vl (value_list_t *vl, memcached_t const *st)
238 sstrncpy (vl->plugin, "memcached", sizeof (vl->plugin));
239 if (st->host != NULL)
240 sstrncpy (vl->host, st->host, sizeof (vl->host));
241 if (st->name != NULL)
242 sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance));
245 static void submit_derive (const char *type, const char *type_inst,
246 derive_t value, memcached_t *st)
248 value_list_t vl = VALUE_LIST_INIT;
250 memcached_init_vl (&vl, st);
251 vl.values = &(value_t) { .derive = value };
253 sstrncpy (vl.type, type, sizeof (vl.type));
254 if (type_inst != NULL)
255 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
257 plugin_dispatch_values (&vl);
260 static void submit_derive2 (const char *type, const char *type_inst,
261 derive_t value0, derive_t value1, memcached_t *st)
263 value_list_t vl = VALUE_LIST_INIT;
265 { .derive = value0 },
266 { .derive = value1 },
269 memcached_init_vl (&vl, st);
271 vl.values_len = STATIC_ARRAY_SIZE (values);
272 sstrncpy (vl.type, type, sizeof (vl.type));
273 if (type_inst != NULL)
274 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
276 plugin_dispatch_values (&vl);
279 static void submit_gauge (const char *type, const char *type_inst,
280 gauge_t value, memcached_t *st)
282 value_list_t vl = VALUE_LIST_INIT;
284 memcached_init_vl (&vl, st);
285 vl.values = &(value_t) { .gauge = value };
287 sstrncpy (vl.type, type, sizeof (vl.type));
288 if (type_inst != NULL)
289 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
291 plugin_dispatch_values (&vl);
294 static void submit_gauge2 (const char *type, const char *type_inst,
295 gauge_t value0, gauge_t value1, memcached_t *st)
297 value_list_t vl = VALUE_LIST_INIT;
303 memcached_init_vl (&vl, st);
305 vl.values_len = STATIC_ARRAY_SIZE (values);
306 sstrncpy (vl.type, type, sizeof (vl.type));
307 if (type_inst != NULL)
308 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
310 plugin_dispatch_values (&vl);
313 static int memcached_read (user_data_t *user_data)
322 gauge_t bytes_used = NAN;
323 gauge_t bytes_total = NAN;
326 gauge_t incr_hits = NAN;
328 gauge_t decr_hits = NAN;
330 derive_t rusage_user = 0;
331 derive_t rusage_syst = 0;
332 derive_t octets_rx = 0;
333 derive_t octets_tx = 0;
336 st = user_data->data;
338 /* get data from daemon */
339 if (memcached_query_daemon (buf, sizeof (buf), st) < 0) {
343 #define FIELD_IS(cnst) \
344 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
348 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
354 fields_num = strsplit(line, fields, 3);
358 name_len = strlen(fields[1]);
363 * For an explanation on these fields please refer to
364 * <https://github.com/memcached/memcached/blob/master/doc/protocol.txt>
368 * CPU time consumed by the memcached process
370 if (FIELD_IS ("rusage_user"))
372 rusage_user = atoll (fields[2]);
374 else if (FIELD_IS ("rusage_system"))
376 rusage_syst = atoll(fields[2]);
380 * Number of threads of this instance
382 else if (FIELD_IS ("threads"))
384 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]), st);
388 * Number of items stored
390 else if (FIELD_IS ("curr_items"))
392 submit_gauge ("memcached_items", "current", atof (fields[2]), st);
396 * Number of bytes used and available (total - used)
398 else if (FIELD_IS ("bytes"))
400 bytes_used = atof (fields[2]);
402 else if (FIELD_IS ("limit_maxbytes"))
404 bytes_total = atof(fields[2]);
410 else if (FIELD_IS ("curr_connections"))
412 submit_gauge ("memcached_connections", "current", atof (fields[2]), st);
414 else if (FIELD_IS ("listen_disabled_num"))
416 submit_derive ("connections", "listen_disabled", atof (fields[2]), st);
422 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
424 const char *name = fields[1] + 4;
425 submit_derive ("memcached_command", name, atoll (fields[2]), st);
426 if (strcmp (name, "get") == 0)
427 gets = atof (fields[2]);
431 * Increment/Decrement
433 else if (FIELD_IS("incr_misses"))
435 derive_t incr_count = atoll (fields[2]);
436 submit_derive ("memcached_ops", "incr_misses", incr_count, st);
439 else if (FIELD_IS ("incr_hits"))
441 derive_t incr_count = atoll (fields[2]);
442 submit_derive ("memcached_ops", "incr_hits", incr_count, st);
443 incr_hits = atof (fields[2]);
446 else if (FIELD_IS ("decr_misses"))
448 derive_t decr_count = atoll (fields[2]);
449 submit_derive ("memcached_ops", "decr_misses", decr_count, st);
452 else if (FIELD_IS ("decr_hits"))
454 derive_t decr_count = atoll (fields[2]);
455 submit_derive ("memcached_ops", "decr_hits", decr_count, st);
456 decr_hits = atof (fields[2]);
461 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
463 else if (FIELD_IS ("get_hits"))
465 submit_derive ("memcached_ops", "hits", atoll (fields[2]), st);
466 hits = atof (fields[2]);
468 else if (FIELD_IS ("get_misses"))
470 submit_derive ("memcached_ops", "misses", atoll (fields[2]), st);
472 else if (FIELD_IS ("evictions"))
474 submit_derive ("memcached_ops", "evictions", atoll (fields[2]), st);
480 else if (FIELD_IS ("bytes_read"))
482 octets_rx = atoll (fields[2]);
484 else if (FIELD_IS ("bytes_written"))
486 octets_tx = atoll (fields[2]);
488 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
490 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
491 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used, st);
493 if ((rusage_user != 0) || (rusage_syst != 0))
494 submit_derive2 ("ps_cputime", NULL, rusage_user, rusage_syst, st);
496 if ((octets_rx != 0) || (octets_tx != 0))
497 submit_derive2 ("memcached_octets", NULL, octets_rx, octets_tx, st);
499 if (!isnan (gets) && !isnan (hits))
504 rate = 100.0 * hits / gets;
506 submit_gauge ("percent", "hitratio", rate, st);
509 if (!isnan (incr_hits) && incr != 0)
511 gauge_t incr_rate = 100.0 * incr_hits / incr;
512 submit_gauge ("percent", "incr_hitratio", incr_rate, st);
513 submit_derive ("memcached_ops", "incr", incr, st);
516 if (!isnan (decr_hits) && decr != 0)
518 gauge_t decr_rate = 100.0 * decr_hits / decr;
519 submit_gauge ("percent", "decr_hitratio", decr_rate, st);
520 submit_derive ("memcached_ops", "decr", decr, st);
524 } /* int memcached_read */
526 static int memcached_add_read_callback (memcached_t *st)
528 char callback_name[3*DATA_MAX_NAME_LEN];
531 ssnprintf (callback_name, sizeof (callback_name), "memcached/%s",
532 (st->name != NULL) ? st->name : "__legacy__");
534 /* If no <Address> used then:
535 * - Connect to the destination specified by <Host>, if present.
536 * If not, use the default address.
537 * - Use the default hostname (set st->host to NULL), if
538 * - Legacy mode is used (no configuration options at all), or
539 * - "Host" option is not provided, or
540 * - "Host" option is set to "localhost" or "127.0.0.1".
542 * If <Address> used then host may be set to "localhost" or "127.0.0.1"
545 if (st->connhost == NULL)
549 st->connhost = strdup(st->host);
550 if (st->connhost == NULL)
553 if ((strcmp ("127.0.0.1", st->host) == 0)
554 || (strcmp ("localhost", st->host) == 0))
559 st->connhost = strdup(MEMCACHED_DEF_HOST);
560 if (st->connhost == NULL)
565 if (st->connport == NULL)
567 st->connport = strdup(MEMCACHED_DEF_PORT);
568 if (st->connport == NULL)
572 assert (st->connhost != NULL);
573 assert (st->connport != NULL);
575 status = plugin_register_complex_read (/* group = */ "memcached",
576 /* name = */ callback_name,
577 /* callback = */ memcached_read,
581 .free_func = memcached_free,
585 } /* int memcached_add_read_callback */
587 /* Configuration handling functiions
589 * <Instance "instance_name">
596 static int config_add_instance(oconfig_item_t *ci)
601 /* Disable automatic generation of default instance in the init callback. */
602 memcached_have_instances = 1;
604 st = calloc (1, sizeof (*st));
607 ERROR ("memcached plugin: calloc failed.");
617 if (strcasecmp (ci->key, "Instance") == 0)
618 status = cf_util_get_string (ci, &st->name);
626 for (int i = 0; i < ci->children_num; i++)
628 oconfig_item_t *child = ci->children + i;
630 if (strcasecmp ("Socket", child->key) == 0)
631 status = cf_util_get_string (child, &st->socket);
632 else if (strcasecmp ("Host", child->key) == 0)
633 status = cf_util_get_string (child, &st->host);
634 else if (strcasecmp ("Address", child->key) == 0)
635 status = cf_util_get_string (child, &st->connhost);
636 else if (strcasecmp ("Port", child->key) == 0)
637 status = cf_util_get_service (child, &st->connport);
640 WARNING ("memcached plugin: Option `%s' not allowed here.",
650 status = memcached_add_read_callback (st);
661 static int memcached_config (oconfig_item_t *ci)
664 _Bool have_instance_block = 0;
666 for (int i = 0; i < ci->children_num; i++)
668 oconfig_item_t *child = ci->children + i;
670 if (strcasecmp ("Instance", child->key) == 0)
672 config_add_instance (child);
673 have_instance_block = 1;
675 else if (!have_instance_block)
677 /* Non-instance option: Assume legacy configuration (without <Instance />
678 * blocks) and call config_add_instance() with the <Plugin /> block. */
679 return (config_add_instance (ci));
682 WARNING ("memcached plugin: The configuration option "
683 "\"%s\" is not allowed here. Did you "
684 "forget to add an <Instance /> block "
685 "around the configuration?",
687 } /* for (ci->children) */
692 static int memcached_init (void)
697 if (memcached_have_instances)
700 /* No instances were configured, lets start a default instance. */
701 st = calloc (1, sizeof (*st));
710 status = memcached_add_read_callback (st);
712 memcached_have_instances = 1;
717 } /* int memcached_init */
719 void module_register (void)
721 plugin_register_complex_config ("memcached", memcached_config);
722 plugin_register_init ("memcached", memcached_init);