2 * collectd - src/memcached.c, based on src/hddtemp.c
3 * Copyright (C) 2007 Antony Dovgal
4 * Copyright (C) 2007-2010 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"
38 # include <sys/socket.h>
40 # include <netinet/in.h>
41 # include <netinet/tcp.h>
43 /* Hack to work around the missing define in AIX */
45 # define MSG_DONTWAIT MSG_NONBLOCK
48 #define MEMCACHED_DEF_HOST "127.0.0.1"
49 #define MEMCACHED_DEF_PORT "11211"
51 #define MEMCACHED_RETRY_COUNT 100
60 typedef struct memcached_s memcached_t;
62 static _Bool memcached_have_instances = 0;
64 static int memcached_read (user_data_t *user_data);
66 static void memcached_free (memcached_t *st)
77 static int memcached_query_daemon (char *buffer, int buffer_size, user_data_t *user_data)
86 if (st->socket != NULL)
88 struct sockaddr_un serv_addr;
90 memset (&serv_addr, 0, sizeof (serv_addr));
91 serv_addr.sun_family = AF_UNIX;
92 sstrncpy (serv_addr.sun_path, st->socket,
93 sizeof (serv_addr.sun_path));
95 /* create our socket descriptor */
96 fd = socket (AF_UNIX, SOCK_STREAM, 0);
99 ERROR ("memcached: unix socket: %s", sstrerror (errno, errbuf,
109 struct addrinfo ai_hints;
110 struct addrinfo *ai_list, *ai_ptr;
113 memset (&ai_hints, '\0', sizeof (ai_hints));
114 ai_hints.ai_flags = 0;
116 ai_hints.ai_flags |= AI_ADDRCONFIG;
118 ai_hints.ai_family = AF_UNSPEC;
119 ai_hints.ai_socktype = SOCK_STREAM;
120 ai_hints.ai_protocol = 0;
122 host = (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST;
123 port = (st->port != NULL) ? st->port : MEMCACHED_DEF_PORT;
125 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0) {
127 ERROR ("memcached: getaddrinfo (%s, %s): %s",
129 (ai_return == EAI_SYSTEM)
130 ? sstrerror (errno, errbuf, sizeof (errbuf))
131 : gai_strerror (ai_return));
135 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
136 /* create our socket descriptor */
137 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
140 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
144 /* connect to the memcached daemon */
145 status = (ssize_t) 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
158 freeaddrinfo (ai_list);
162 ERROR ("memcached: Could not connect to daemon.");
166 if (send(fd, "stats\r\n", sizeof("stats\r\n") - 1, MSG_DONTWAIT) != (sizeof("stats\r\n") - 1)) {
167 ERROR ("memcached: Could not send command to the memcached daemon.");
175 memset (&p, 0, sizeof (p));
177 p.events = POLLIN | POLLERR | POLLHUP;
180 status = poll (&p, /* nfds = */ 1,
181 /* timeout = */ CDTIME_T_TO_MS (interval_g));
186 ERROR ("memcached: poll(2) timed out after %.3f seconds.",
187 CDTIME_T_TO_DOUBLE (interval_g));
192 ERROR ("memcached: poll(2) failed: %s",
193 sstrerror (errno, errbuf, sizeof (errbuf)));
195 shutdown (fd, SHUT_RDWR);
201 /* receive data from the memcached daemon */
202 memset (buffer, '\0', buffer_size);
205 while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
206 if (i > MEMCACHED_RETRY_COUNT) {
207 ERROR("recv() timed out");
215 if (errno == EAGAIN) {
219 ERROR ("memcached: Error reading from socket: %s",
220 sstrerror (errno, errbuf, sizeof (errbuf)));
221 shutdown(fd, SHUT_RDWR);
225 buffer_fill += status;
227 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
228 /* we got all the data */
233 if (buffer_fill >= buffer_size) {
234 buffer[buffer_size - 1] = '\0';
235 WARNING ("memcached: Message from memcached has been truncated.");
236 } else if (buffer_fill == 0) {
237 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
238 "Buffer: `%s'", buffer);
239 shutdown(fd, SHUT_RDWR);
244 shutdown(fd, SHUT_RDWR);
249 static int memcached_add_read_callback (memcached_t *st)
252 char callback_name[3*DATA_MAX_NAME_LEN];
255 memset (&ud, 0, sizeof (ud));
257 ud.free_func = (void *) memcached_free;
259 assert (st->name != NULL);
260 ssnprintf (callback_name, sizeof (callback_name), "memcached/%s", st->name);
262 status = plugin_register_complex_read (/* group = */ "memcached",
263 /* name = */ callback_name,
264 /* callback = */ memcached_read,
265 /* interval = */ NULL,
266 /* user_data = */ &ud);
268 } /* int memcached_add_read_callback */
270 /* Configuration handling functiions
272 * <Instance "instance_name">
278 static int config_add_instance(oconfig_item_t *ci)
284 /* Disable automatic generation of default instance in the init callback. */
285 memcached_have_instances = 1;
287 st = malloc (sizeof (*st));
290 ERROR ("memcached plugin: malloc failed.");
294 memset (st, 0, sizeof (*st));
300 if (strcasecmp (ci->key, "Plugin") == 0) /* default instance */
301 st->name = sstrdup ("default");
302 else /* <Instance /> block */
303 status = cf_util_get_string (ci, &st->name);
309 assert (st->name != NULL);
311 for (i = 0; i < ci->children_num; i++)
313 oconfig_item_t *child = ci->children + i;
315 if (strcasecmp ("Socket", child->key) == 0)
316 status = cf_util_get_string (child, &st->socket);
317 else if (strcasecmp ("Host", child->key) == 0)
318 status = cf_util_get_string (child, &st->host);
319 else if (strcasecmp ("Port", child->key) == 0)
320 status = cf_util_get_service (child, &st->port);
323 WARNING ("memcached plugin: Option `%s' not allowed here.",
333 status = memcached_add_read_callback (st);
344 static int memcached_config (oconfig_item_t *ci)
347 _Bool have_instance_block = 0;
350 for (i = 0; i < ci->children_num; i++)
352 oconfig_item_t *child = ci->children + i;
354 if (strcasecmp ("Instance", child->key) == 0)
356 config_add_instance (child);
357 have_instance_block = 1;
359 else if (!have_instance_block)
361 /* Non-instance option: Assume legacy configuration (without <Instance />
362 * blocks) and call config_add_instance() with the <Plugin /> block. */
363 return (config_add_instance (ci));
366 WARNING ("memcached plugin: The configuration option "
367 "\"%s\" is not allowed here. Did you "
368 "forget to add an <Instance /> block "
369 "around the configuration?",
371 } /* for (ci->children) */
376 static int memcached_init (void)
381 if (memcached_have_instances)
384 /* No instances were configured, lets start a default instance. */
385 st = malloc (sizeof (*st));
388 memset (st, 0, sizeof (*st));
389 st->name = sstrdup ("default");
394 status = memcached_add_read_callback (st);
396 memcached_have_instances = 1;
401 } /* int memcached_init */
403 static void submit_derive (const char *type, const char *type_inst,
404 derive_t value, memcached_t *st)
407 value_list_t vl = VALUE_LIST_INIT;
409 values[0].derive = value;
413 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
414 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
415 if (st->name != NULL)
416 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
417 sstrncpy (vl.type, type, sizeof (vl.type));
418 if (type_inst != NULL)
419 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
421 plugin_dispatch_values (&vl);
424 static void submit_derive2 (const char *type, const char *type_inst,
425 derive_t value0, derive_t value1, memcached_t *st)
428 value_list_t vl = VALUE_LIST_INIT;
430 values[0].derive = value0;
431 values[1].derive = value1;
435 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
436 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
437 if (st->name != NULL)
438 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
439 sstrncpy (vl.type, type, sizeof (vl.type));
440 if (type_inst != NULL)
441 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
443 plugin_dispatch_values (&vl);
446 static void submit_gauge (const char *type, const char *type_inst,
447 gauge_t value, memcached_t *st)
450 value_list_t vl = VALUE_LIST_INIT;
452 values[0].gauge = value;
456 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
457 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
458 if (st->name != NULL)
459 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
460 sstrncpy (vl.type, type, sizeof (vl.type));
461 if (type_inst != NULL)
462 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
464 plugin_dispatch_values (&vl);
467 static void submit_gauge2 (const char *type, const char *type_inst,
468 gauge_t value0, gauge_t value1, memcached_t *st)
471 value_list_t vl = VALUE_LIST_INIT;
473 values[0].gauge = value0;
474 values[1].gauge = value1;
478 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
479 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
480 if (st->name != NULL)
481 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
482 sstrncpy (vl.type, type, sizeof (vl.type));
483 if (type_inst != NULL)
484 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
486 plugin_dispatch_values (&vl);
489 static int memcached_read (user_data_t *user_data)
498 gauge_t bytes_used = NAN;
499 gauge_t bytes_total = NAN;
502 derive_t rusage_user = 0;
503 derive_t rusage_syst = 0;
504 derive_t octets_rx = 0;
505 derive_t octets_tx = 0;
508 st = user_data->data;
510 /* get data from daemon */
511 if (memcached_query_daemon (buf, sizeof (buf), user_data) < 0) {
515 #define FIELD_IS(cnst) \
516 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
520 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
526 fields_num = strsplit(line, fields, 3);
530 name_len = strlen(fields[1]);
535 * For an explanation on these fields please refer to
536 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
540 * CPU time consumed by the memcached process
542 if (FIELD_IS ("rusage_user"))
544 rusage_user = atoll (fields[2]);
546 else if (FIELD_IS ("rusage_system"))
548 rusage_syst = atoll(fields[2]);
552 * Number of threads of this instance
554 else if (FIELD_IS ("threads"))
556 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]), st);
560 * Number of items stored
562 else if (FIELD_IS ("curr_items"))
564 submit_gauge ("memcached_items", "current", atof (fields[2]), st);
568 * Number of bytes used and available (total - used)
570 else if (FIELD_IS ("bytes"))
572 bytes_used = atof (fields[2]);
574 else if (FIELD_IS ("limit_maxbytes"))
576 bytes_total = atof(fields[2]);
582 else if (FIELD_IS ("curr_connections"))
584 submit_gauge ("memcached_connections", "current", atof (fields[2]), st);
590 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
592 const char *name = fields[1] + 4;
593 submit_derive ("memcached_command", name, atoll (fields[2]), st);
594 if (strcmp (name, "get") == 0)
595 gets = atof (fields[2]);
599 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
601 else if (FIELD_IS ("get_hits"))
603 submit_derive ("memcached_ops", "hits", atoll (fields[2]), st);
604 hits = atof (fields[2]);
606 else if (FIELD_IS ("get_misses"))
608 submit_derive ("memcached_ops", "misses", atoll (fields[2]), st);
610 else if (FIELD_IS ("evictions"))
612 submit_derive ("memcached_ops", "evictions", atoll (fields[2]), st);
618 else if (FIELD_IS ("bytes_read"))
620 octets_rx = atoll (fields[2]);
622 else if (FIELD_IS ("bytes_written"))
624 octets_tx = atoll (fields[2]);
626 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
628 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
629 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used, st);
631 if ((rusage_user != 0) || (rusage_syst != 0))
632 submit_derive2 ("ps_cputime", NULL, rusage_user, rusage_syst, st);
634 if ((octets_rx != 0) || (octets_tx != 0))
635 submit_derive2 ("memcached_octets", NULL, octets_rx, octets_tx, st);
637 if (!isnan (gets) && !isnan (hits))
642 rate = 100.0 * hits / gets;
644 submit_gauge ("percent", "hitratio", rate, st);
650 void module_register (void)
652 plugin_register_complex_config ("memcached", memcached_config);
653 plugin_register_init ("memcached", memcached_init);