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"
51 typedef struct memcached_s memcached_t;
53 static _Bool memcached_have_instances = 0;
55 static void memcached_free (void *arg)
57 memcached_t *st = arg;
68 static int memcached_connect_unix (memcached_t *st)
70 struct sockaddr_un serv_addr = { 0 };
73 serv_addr.sun_family = AF_UNIX;
74 sstrncpy (serv_addr.sun_path, st->socket,
75 sizeof (serv_addr.sun_path));
77 /* create our socket descriptor */
78 fd = socket (AF_UNIX, SOCK_STREAM, 0);
82 ERROR ("memcached plugin: memcached_connect_unix: socket(2) failed: %s",
83 sstrerror (errno, errbuf, sizeof (errbuf)));
87 /* connect to the memcached daemon */
88 int status = connect (fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
91 shutdown (fd, SHUT_RDWR);
97 } /* int memcached_connect_unix */
99 static int memcached_connect_inet (memcached_t *st)
104 struct addrinfo *ai_list;
108 host = (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST;
109 port = (st->port != NULL) ? st->port : MEMCACHED_DEF_PORT;
111 struct addrinfo ai_hints = {
112 .ai_family = AF_UNSPEC,
113 .ai_flags = AI_ADDRCONFIG,
114 .ai_socktype = SOCK_STREAM
117 status = getaddrinfo (host, port, &ai_hints, &ai_list);
121 ERROR ("memcached plugin: memcached_connect_inet: "
122 "getaddrinfo(%s,%s) failed: %s",
124 (status == EAI_SYSTEM)
125 ? sstrerror (errno, errbuf, sizeof (errbuf))
126 : gai_strerror (status));
130 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
132 /* create our socket descriptor */
133 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
137 WARNING ("memcached plugin: memcached_connect_inet: "
138 "socket(2) failed: %s",
139 sstrerror (errno, errbuf, sizeof (errbuf)));
143 /* connect to the memcached daemon */
144 status = (int) connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
147 shutdown (fd, SHUT_RDWR);
153 /* A socket could be opened and connecting succeeded. We're done. */
157 freeaddrinfo (ai_list);
159 } /* int memcached_connect_inet */
161 static int memcached_connect (memcached_t *st)
163 if (st->socket != NULL)
164 return (memcached_connect_unix (st));
166 return (memcached_connect_inet (st));
169 static int memcached_query_daemon (char *buffer, size_t buffer_size, memcached_t *st)
174 fd = memcached_connect (st);
176 ERROR ("memcached plugin: Instance \"%s\" could not connect to daemon.",
181 status = (int) swrite (fd, "stats\r\n", strlen ("stats\r\n"));
185 ERROR ("memcached plugin: write(2) failed: %s",
186 sstrerror (errno, errbuf, sizeof (errbuf)));
187 shutdown(fd, SHUT_RDWR);
192 /* receive data from the memcached daemon */
193 memset (buffer, 0, buffer_size);
196 while ((status = (int) recv (fd, buffer + buffer_fill,
197 buffer_size - buffer_fill, /* flags = */ 0)) != 0)
199 char const end_token[5] = {'E', 'N', 'D', '\r', '\n'};
204 if ((errno == EAGAIN) || (errno == EINTR))
207 ERROR ("memcached: Error reading from socket: %s",
208 sstrerror (errno, errbuf, sizeof (errbuf)));
209 shutdown(fd, SHUT_RDWR);
214 buffer_fill += (size_t) status;
215 if (buffer_fill > buffer_size)
217 buffer_fill = buffer_size;
218 WARNING ("memcached plugin: Message was truncated.");
222 /* If buffer ends in end_token, we have all the data. */
223 if (memcmp (buffer + buffer_fill - sizeof (end_token),
224 end_token, sizeof (end_token)) == 0)
229 if (buffer_fill == 0)
231 WARNING ("memcached plugin: No data returned by memcached.");
235 shutdown(fd, SHUT_RDWR);
238 } /* int memcached_query_daemon */
240 static void memcached_init_vl (value_list_t *vl, memcached_t const *st)
242 char const *host = st->host;
244 /* Set vl->host to hostname_g, if:
245 * - Legacy mode is used.
246 * - "Socket" option is given (doc: "Host option is ignored").
247 * - "Host" option is not provided.
248 * - "Host" option is set to "localhost" or "127.0.0.1". */
249 if ((strcmp (st->name, "__legacy__") == 0)
250 || (st->socket != NULL)
251 || (st->host == NULL)
252 || (strcmp ("127.0.0.1", st->host) == 0)
253 || (strcmp ("localhost", st->host) == 0))
256 sstrncpy (vl->plugin, "memcached", sizeof (vl->plugin));
257 sstrncpy (vl->host, host, sizeof (vl->host));
258 if (strcmp (st->name, "__legacy__") != 0)
259 sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance));
262 static void submit_derive (const char *type, const char *type_inst,
263 derive_t value, memcached_t *st)
266 value_list_t vl = VALUE_LIST_INIT;
267 memcached_init_vl (&vl, st);
269 values[0].derive = value;
273 sstrncpy (vl.type, type, sizeof (vl.type));
274 if (type_inst != NULL)
275 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
277 plugin_dispatch_values (&vl);
280 static void submit_derive2 (const char *type, const char *type_inst,
281 derive_t value0, derive_t value1, memcached_t *st)
284 value_list_t vl = VALUE_LIST_INIT;
285 memcached_init_vl (&vl, st);
287 values[0].derive = value0;
288 values[1].derive = value1;
292 sstrncpy (vl.type, type, sizeof (vl.type));
293 if (type_inst != NULL)
294 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
296 plugin_dispatch_values (&vl);
299 static void submit_gauge (const char *type, const char *type_inst,
300 gauge_t value, memcached_t *st)
303 value_list_t vl = VALUE_LIST_INIT;
304 memcached_init_vl (&vl, st);
306 values[0].gauge = value;
310 sstrncpy (vl.type, type, sizeof (vl.type));
311 if (type_inst != NULL)
312 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
314 plugin_dispatch_values (&vl);
317 static void submit_gauge2 (const char *type, const char *type_inst,
318 gauge_t value0, gauge_t value1, memcached_t *st)
321 value_list_t vl = VALUE_LIST_INIT;
322 memcached_init_vl (&vl, st);
324 values[0].gauge = value0;
325 values[1].gauge = value1;
329 sstrncpy (vl.type, type, sizeof (vl.type));
330 if (type_inst != NULL)
331 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
333 plugin_dispatch_values (&vl);
336 static int memcached_read (user_data_t *user_data)
345 gauge_t bytes_used = NAN;
346 gauge_t bytes_total = NAN;
349 gauge_t incr_hits = NAN;
351 gauge_t decr_hits = NAN;
353 derive_t rusage_user = 0;
354 derive_t rusage_syst = 0;
355 derive_t octets_rx = 0;
356 derive_t octets_tx = 0;
359 st = user_data->data;
361 /* get data from daemon */
362 if (memcached_query_daemon (buf, sizeof (buf), st) < 0) {
366 #define FIELD_IS(cnst) \
367 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
371 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
377 fields_num = strsplit(line, fields, 3);
381 name_len = strlen(fields[1]);
386 * For an explanation on these fields please refer to
387 * <https://github.com/memcached/memcached/blob/master/doc/protocol.txt>
391 * CPU time consumed by the memcached process
393 if (FIELD_IS ("rusage_user"))
395 rusage_user = atoll (fields[2]);
397 else if (FIELD_IS ("rusage_system"))
399 rusage_syst = atoll(fields[2]);
403 * Number of threads of this instance
405 else if (FIELD_IS ("threads"))
407 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]), st);
411 * Number of items stored
413 else if (FIELD_IS ("curr_items"))
415 submit_gauge ("memcached_items", "current", atof (fields[2]), st);
419 * Number of bytes used and available (total - used)
421 else if (FIELD_IS ("bytes"))
423 bytes_used = atof (fields[2]);
425 else if (FIELD_IS ("limit_maxbytes"))
427 bytes_total = atof(fields[2]);
433 else if (FIELD_IS ("curr_connections"))
435 submit_gauge ("memcached_connections", "current", atof (fields[2]), st);
437 else if (FIELD_IS ("listen_disabled_num"))
439 submit_derive ("connections", "listen_disabled", atof (fields[2]), st);
445 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
447 const char *name = fields[1] + 4;
448 submit_derive ("memcached_command", name, atoll (fields[2]), st);
449 if (strcmp (name, "get") == 0)
450 gets = atof (fields[2]);
454 * Increment/Decrement
456 else if (FIELD_IS("incr_misses"))
458 derive_t incr_count = atoll (fields[2]);
459 submit_derive ("memcached_ops", "incr_misses", incr_count, st);
462 else if (FIELD_IS ("incr_hits"))
464 derive_t incr_count = atoll (fields[2]);
465 submit_derive ("memcached_ops", "incr_hits", incr_count, st);
466 incr_hits = atof (fields[2]);
469 else if (FIELD_IS ("decr_misses"))
471 derive_t decr_count = atoll (fields[2]);
472 submit_derive ("memcached_ops", "decr_misses", decr_count, st);
475 else if (FIELD_IS ("decr_hits"))
477 derive_t decr_count = atoll (fields[2]);
478 submit_derive ("memcached_ops", "decr_hits", decr_count, st);
479 decr_hits = atof (fields[2]);
484 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
486 else if (FIELD_IS ("get_hits"))
488 submit_derive ("memcached_ops", "hits", atoll (fields[2]), st);
489 hits = atof (fields[2]);
491 else if (FIELD_IS ("get_misses"))
493 submit_derive ("memcached_ops", "misses", atoll (fields[2]), st);
495 else if (FIELD_IS ("evictions"))
497 submit_derive ("memcached_ops", "evictions", atoll (fields[2]), st);
503 else if (FIELD_IS ("bytes_read"))
505 octets_rx = atoll (fields[2]);
507 else if (FIELD_IS ("bytes_written"))
509 octets_tx = atoll (fields[2]);
511 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
513 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
514 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used, st);
516 if ((rusage_user != 0) || (rusage_syst != 0))
517 submit_derive2 ("ps_cputime", NULL, rusage_user, rusage_syst, st);
519 if ((octets_rx != 0) || (octets_tx != 0))
520 submit_derive2 ("memcached_octets", NULL, octets_rx, octets_tx, st);
522 if (!isnan (gets) && !isnan (hits))
527 rate = 100.0 * hits / gets;
529 submit_gauge ("percent", "hitratio", rate, st);
532 if (!isnan (incr_hits) && incr != 0)
534 gauge_t incr_rate = 100.0 * incr_hits / incr;
535 submit_gauge ("percent", "incr_hitratio", incr_rate, st);
536 submit_derive ("memcached_ops", "incr", incr, st);
539 if (!isnan (decr_hits) && decr != 0)
541 gauge_t decr_rate = 100.0 * decr_hits / decr;
542 submit_gauge ("percent", "decr_hitratio", decr_rate, st);
543 submit_derive ("memcached_ops", "decr", decr, st);
547 } /* int memcached_read */
549 static int memcached_add_read_callback (memcached_t *st)
551 char callback_name[3*DATA_MAX_NAME_LEN];
554 assert (st->name != NULL);
555 ssnprintf (callback_name, sizeof (callback_name), "memcached/%s", st->name);
557 status = plugin_register_complex_read (/* group = */ "memcached",
558 /* name = */ callback_name,
559 /* callback = */ memcached_read,
563 .free_func = memcached_free,
567 } /* int memcached_add_read_callback */
569 /* Configuration handling functiions
571 * <Instance "instance_name">
577 static int config_add_instance(oconfig_item_t *ci)
582 /* Disable automatic generation of default instance in the init callback. */
583 memcached_have_instances = 1;
585 st = calloc (1, sizeof (*st));
588 ERROR ("memcached plugin: calloc failed.");
597 if (strcasecmp (ci->key, "Plugin") == 0) /* default instance */
598 st->name = sstrdup ("__legacy__");
599 else /* <Instance /> block */
600 status = cf_util_get_string (ci, &st->name);
606 assert (st->name != NULL);
608 for (int i = 0; i < ci->children_num; i++)
610 oconfig_item_t *child = ci->children + i;
612 if (strcasecmp ("Socket", child->key) == 0)
613 status = cf_util_get_string (child, &st->socket);
614 else if (strcasecmp ("Host", child->key) == 0)
615 status = cf_util_get_string (child, &st->host);
616 else if (strcasecmp ("Port", child->key) == 0)
617 status = cf_util_get_service (child, &st->port);
620 WARNING ("memcached plugin: Option `%s' not allowed here.",
630 status = memcached_add_read_callback (st);
641 static int memcached_config (oconfig_item_t *ci)
644 _Bool have_instance_block = 0;
646 for (int i = 0; i < ci->children_num; i++)
648 oconfig_item_t *child = ci->children + i;
650 if (strcasecmp ("Instance", child->key) == 0)
652 config_add_instance (child);
653 have_instance_block = 1;
655 else if (!have_instance_block)
657 /* Non-instance option: Assume legacy configuration (without <Instance />
658 * blocks) and call config_add_instance() with the <Plugin /> block. */
659 return (config_add_instance (ci));
662 WARNING ("memcached plugin: The configuration option "
663 "\"%s\" is not allowed here. Did you "
664 "forget to add an <Instance /> block "
665 "around the configuration?",
667 } /* for (ci->children) */
672 static int memcached_init (void)
677 if (memcached_have_instances)
680 /* No instances were configured, lets start a default instance. */
681 st = calloc (1, sizeof (*st));
684 st->name = sstrdup ("__legacy__");
689 status = memcached_add_read_callback (st);
691 memcached_have_instances = 1;
696 } /* int memcached_init */
698 void module_register (void)
700 plugin_register_complex_config ("memcached", memcached_config);
701 plugin_register_init ("memcached", memcached_init);