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>
34 #include "configfile.h"
37 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
42 #define MEMCACHED_DEF_HOST "127.0.0.1"
43 #define MEMCACHED_DEF_PORT "11211"
52 typedef struct memcached_s memcached_t;
54 static _Bool memcached_have_instances = 0;
56 static void memcached_free (memcached_t *st)
68 static int memcached_connect_unix (memcached_t *st)
70 struct sockaddr_un serv_addr;
73 memset (&serv_addr, 0, sizeof (serv_addr));
74 serv_addr.sun_family = AF_UNIX;
75 sstrncpy (serv_addr.sun_path, st->socket,
76 sizeof (serv_addr.sun_path));
78 /* create our socket descriptor */
79 fd = socket (AF_UNIX, SOCK_STREAM, 0);
83 ERROR ("memcached plugin: memcached_connect_unix: socket(2) failed: %s",
84 sstrerror (errno, errbuf, sizeof (errbuf)));
88 /* connect to the memcached daemon */
89 int status = connect (fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
92 shutdown (fd, SHUT_RDWR);
98 } /* int memcached_connect_unix */
100 static int memcached_connect_inet (memcached_t *st)
105 struct addrinfo ai_hints;
106 struct addrinfo *ai_list, *ai_ptr;
110 memset (&ai_hints, 0, sizeof (ai_hints));
111 ai_hints.ai_flags = 0;
113 ai_hints.ai_flags |= AI_ADDRCONFIG;
115 ai_hints.ai_family = AF_UNSPEC;
116 ai_hints.ai_socktype = SOCK_STREAM;
117 ai_hints.ai_protocol = 0;
119 host = (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST;
120 port = (st->port != NULL) ? st->port : MEMCACHED_DEF_PORT;
123 status = getaddrinfo (host, port, &ai_hints, &ai_list);
127 ERROR ("memcached plugin: memcached_connect_inet: "
128 "getaddrinfo(%s,%s) failed: %s",
130 (status == EAI_SYSTEM)
131 ? sstrerror (errno, errbuf, sizeof (errbuf))
132 : gai_strerror (status));
136 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
138 /* create our socket descriptor */
139 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
143 WARNING ("memcached plugin: memcached_connect_inet: "
144 "socket(2) failed: %s",
145 sstrerror (errno, errbuf, sizeof (errbuf)));
149 /* connect to the memcached daemon */
150 status = (int) connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
153 shutdown (fd, SHUT_RDWR);
159 /* A socket could be opened and connecting succeeded. We're done. */
163 freeaddrinfo (ai_list);
165 } /* int memcached_connect_inet */
167 static int memcached_connect (memcached_t *st)
169 if (st->socket != NULL)
170 return (memcached_connect_unix (st));
172 return (memcached_connect_inet (st));
175 static int memcached_query_daemon (char *buffer, size_t buffer_size, memcached_t *st)
181 fd = memcached_connect (st);
183 ERROR ("memcached plugin: Instance \"%s\" could not connect to daemon.",
188 status = (int) swrite (fd, "stats\r\n", strlen ("stats\r\n"));
192 ERROR ("memcached plugin: write(2) failed: %s",
193 sstrerror (errno, errbuf, sizeof (errbuf)));
194 shutdown(fd, SHUT_RDWR);
199 /* receive data from the memcached daemon */
200 memset (buffer, 0, buffer_size);
203 while ((status = (int) recv (fd, buffer + buffer_fill,
204 buffer_size - buffer_fill, /* flags = */ 0)) != 0)
206 char const end_token[5] = {'E', 'N', 'D', '\r', '\n'};
211 if ((errno == EAGAIN) || (errno == EINTR))
214 ERROR ("memcached: Error reading from socket: %s",
215 sstrerror (errno, errbuf, sizeof (errbuf)));
216 shutdown(fd, SHUT_RDWR);
221 buffer_fill += (size_t) status;
222 if (buffer_fill > buffer_size)
224 buffer_fill = buffer_size;
225 WARNING ("memcached plugin: Message was truncated.");
229 /* If buffer ends in end_token, we have all the data. */
230 if (memcmp (buffer + buffer_fill - sizeof (end_token),
231 end_token, sizeof (end_token)) == 0)
236 if (buffer_fill == 0)
238 WARNING ("memcached plugin: No data returned by memcached.");
242 shutdown(fd, SHUT_RDWR);
245 } /* int memcached_query_daemon */
247 static void memcached_init_vl (value_list_t *vl, memcached_t const *st)
249 sstrncpy (vl->plugin, "memcached", sizeof (vl->plugin));
250 if (strcmp (st->name, "__legacy__") == 0) /* legacy mode */
252 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
256 if (st->socket != NULL)
257 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
260 (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST,
262 sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance));
266 static void submit_derive (const char *type, const char *type_inst,
267 derive_t value, memcached_t *st)
270 value_list_t vl = VALUE_LIST_INIT;
271 memcached_init_vl (&vl, st);
273 values[0].derive = value;
277 sstrncpy (vl.type, type, sizeof (vl.type));
278 if (type_inst != NULL)
279 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
281 plugin_dispatch_values (&vl);
284 static void submit_derive2 (const char *type, const char *type_inst,
285 derive_t value0, derive_t value1, memcached_t *st)
288 value_list_t vl = VALUE_LIST_INIT;
289 memcached_init_vl (&vl, st);
291 values[0].derive = value0;
292 values[1].derive = value1;
296 sstrncpy (vl.type, type, sizeof (vl.type));
297 if (type_inst != NULL)
298 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
300 plugin_dispatch_values (&vl);
303 static void submit_gauge (const char *type, const char *type_inst,
304 gauge_t value, memcached_t *st)
307 value_list_t vl = VALUE_LIST_INIT;
308 memcached_init_vl (&vl, st);
310 values[0].gauge = value;
314 sstrncpy (vl.type, type, sizeof (vl.type));
315 if (type_inst != NULL)
316 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
318 plugin_dispatch_values (&vl);
321 static void submit_gauge2 (const char *type, const char *type_inst,
322 gauge_t value0, gauge_t value1, memcached_t *st)
325 value_list_t vl = VALUE_LIST_INIT;
326 memcached_init_vl (&vl, st);
328 values[0].gauge = value0;
329 values[1].gauge = value1;
333 sstrncpy (vl.type, type, sizeof (vl.type));
334 if (type_inst != NULL)
335 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
337 plugin_dispatch_values (&vl);
340 static int memcached_read (user_data_t *user_data)
349 gauge_t bytes_used = NAN;
350 gauge_t bytes_total = NAN;
353 gauge_t incr_hits = NAN;
355 gauge_t decr_hits = NAN;
357 derive_t rusage_user = 0;
358 derive_t rusage_syst = 0;
359 derive_t octets_rx = 0;
360 derive_t octets_tx = 0;
363 st = user_data->data;
365 /* get data from daemon */
366 if (memcached_query_daemon (buf, sizeof (buf), st) < 0) {
370 #define FIELD_IS(cnst) \
371 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
375 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
381 fields_num = strsplit(line, fields, 3);
385 name_len = strlen(fields[1]);
390 * For an explanation on these fields please refer to
391 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
395 * CPU time consumed by the memcached process
397 if (FIELD_IS ("rusage_user"))
399 rusage_user = atoll (fields[2]);
401 else if (FIELD_IS ("rusage_system"))
403 rusage_syst = atoll(fields[2]);
407 * Number of threads of this instance
409 else if (FIELD_IS ("threads"))
411 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]), st);
415 * Number of items stored
417 else if (FIELD_IS ("curr_items"))
419 submit_gauge ("memcached_items", "current", atof (fields[2]), st);
423 * Number of bytes used and available (total - used)
425 else if (FIELD_IS ("bytes"))
427 bytes_used = atof (fields[2]);
429 else if (FIELD_IS ("limit_maxbytes"))
431 bytes_total = atof(fields[2]);
437 else if (FIELD_IS ("curr_connections"))
439 submit_gauge ("memcached_connections", "current", 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)
552 char callback_name[3*DATA_MAX_NAME_LEN];
555 memset (&ud, 0, sizeof (ud));
557 ud.free_func = (void *) memcached_free;
559 assert (st->name != NULL);
560 ssnprintf (callback_name, sizeof (callback_name), "memcached/%s", st->name);
562 status = plugin_register_complex_read (/* group = */ "memcached",
563 /* name = */ callback_name,
564 /* callback = */ memcached_read,
565 /* interval = */ NULL,
566 /* user_data = */ &ud);
568 } /* int memcached_add_read_callback */
570 /* Configuration handling functiions
572 * <Instance "instance_name">
578 static int config_add_instance(oconfig_item_t *ci)
584 /* Disable automatic generation of default instance in the init callback. */
585 memcached_have_instances = 1;
587 st = malloc (sizeof (*st));
590 ERROR ("memcached plugin: malloc failed.");
594 memset (st, 0, sizeof (*st));
600 if (strcasecmp (ci->key, "Plugin") == 0) /* default instance */
601 st->name = sstrdup ("__legacy__");
602 else /* <Instance /> block */
603 status = cf_util_get_string (ci, &st->name);
609 assert (st->name != NULL);
611 for (i = 0; i < ci->children_num; i++)
613 oconfig_item_t *child = ci->children + i;
615 if (strcasecmp ("Socket", child->key) == 0)
616 status = cf_util_get_string (child, &st->socket);
617 else if (strcasecmp ("Host", child->key) == 0)
618 status = cf_util_get_string (child, &st->host);
619 else if (strcasecmp ("Port", child->key) == 0)
620 status = cf_util_get_service (child, &st->port);
623 WARNING ("memcached plugin: Option `%s' not allowed here.",
633 status = memcached_add_read_callback (st);
644 static int memcached_config (oconfig_item_t *ci)
647 _Bool have_instance_block = 0;
650 for (i = 0; i < ci->children_num; i++)
652 oconfig_item_t *child = ci->children + i;
654 if (strcasecmp ("Instance", child->key) == 0)
656 config_add_instance (child);
657 have_instance_block = 1;
659 else if (!have_instance_block)
661 /* Non-instance option: Assume legacy configuration (without <Instance />
662 * blocks) and call config_add_instance() with the <Plugin /> block. */
663 return (config_add_instance (ci));
666 WARNING ("memcached plugin: The configuration option "
667 "\"%s\" is not allowed here. Did you "
668 "forget to add an <Instance /> block "
669 "around the configuration?",
671 } /* for (ci->children) */
676 static int memcached_init (void)
681 if (memcached_have_instances)
684 /* No instances were configured, lets start a default instance. */
685 st = malloc (sizeof (*st));
688 memset (st, 0, sizeof (*st));
689 st->name = sstrdup ("__legacy__");
694 status = memcached_add_read_callback (st);
696 memcached_have_instances = 1;
701 } /* int memcached_init */
703 void module_register (void)
705 plugin_register_complex_config ("memcached", memcached_config);
706 plugin_register_init ("memcached", memcached_init);