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)
67 static int memcached_connect_unix (memcached_t *st)
69 struct sockaddr_un serv_addr;
72 memset (&serv_addr, 0, sizeof (serv_addr));
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)));
88 } /* int memcached_connect_unix */
90 static int memcached_connect_inet (memcached_t *st)
95 struct addrinfo ai_hints;
96 struct addrinfo *ai_list, *ai_ptr;
100 memset (&ai_hints, 0, sizeof (ai_hints));
101 ai_hints.ai_flags = 0;
103 ai_hints.ai_flags |= AI_ADDRCONFIG;
105 ai_hints.ai_family = AF_UNSPEC;
106 ai_hints.ai_socktype = SOCK_STREAM;
107 ai_hints.ai_protocol = 0;
109 host = (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST;
110 port = (st->port != NULL) ? st->port : MEMCACHED_DEF_PORT;
113 status = getaddrinfo (host, port, &ai_hints, &ai_list);
117 ERROR ("memcached plugin: memcached_connect_inet: "
118 "getaddrinfo(%s,%s) failed: %s",
120 (status == EAI_SYSTEM)
121 ? sstrerror (errno, errbuf, sizeof (errbuf))
122 : gai_strerror (status));
126 for (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)
171 fd = memcached_connect (st);
173 ERROR ("memcached plugin: Instance \"%s\" could not connect to daemon.",
178 status = (int) swrite (fd, "stats\r\n", strlen ("stats\r\n"));
182 ERROR ("memcached plugin: write(2) failed: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
184 shutdown(fd, SHUT_RDWR);
189 /* receive data from the memcached daemon */
190 memset (buffer, 0, buffer_size);
193 while ((status = (int) recv (fd, buffer + buffer_fill,
194 buffer_size - buffer_fill, /* flags = */ 0)) != 0)
196 char const end_token[5] = {'E', 'N', 'D', '\r', '\n'};
201 if ((errno == EAGAIN) || (errno == EINTR))
204 ERROR ("memcached: Error reading from socket: %s",
205 sstrerror (errno, errbuf, sizeof (errbuf)));
206 shutdown(fd, SHUT_RDWR);
211 buffer_fill += (size_t) status;
212 if (buffer_fill > buffer_size)
214 buffer_fill = buffer_size;
215 WARNING ("memcached plugin: Message was truncated.");
219 /* If buffer ends in end_token, we have all the data. */
220 if (memcmp (buffer + buffer_fill - sizeof (end_token),
221 end_token, sizeof (end_token)) == 0)
226 if (buffer_fill == 0)
228 WARNING ("memcached plugin: No data returned by memcached.");
232 shutdown(fd, SHUT_RDWR);
235 } /* int memcached_query_daemon */
237 static void memcached_init_vl (value_list_t *vl, memcached_t const *st)
239 sstrncpy (vl->plugin, "memcached", sizeof (vl->plugin));
240 if (strcmp (st->name, "__legacy__") == 0) /* legacy mode */
242 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
246 if (st->socket != NULL)
247 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
250 (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST,
252 sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance));
256 static void submit_derive (const char *type, const char *type_inst,
257 derive_t value, memcached_t *st)
260 value_list_t vl = VALUE_LIST_INIT;
261 memcached_init_vl (&vl, st);
263 values[0].derive = value;
267 sstrncpy (vl.type, type, sizeof (vl.type));
268 if (type_inst != NULL)
269 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
271 plugin_dispatch_values (&vl);
274 static void submit_derive2 (const char *type, const char *type_inst,
275 derive_t value0, derive_t value1, memcached_t *st)
278 value_list_t vl = VALUE_LIST_INIT;
279 memcached_init_vl (&vl, st);
281 values[0].derive = value0;
282 values[1].derive = value1;
286 sstrncpy (vl.type, type, sizeof (vl.type));
287 if (type_inst != NULL)
288 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
290 plugin_dispatch_values (&vl);
293 static void submit_gauge (const char *type, const char *type_inst,
294 gauge_t value, memcached_t *st)
297 value_list_t vl = VALUE_LIST_INIT;
298 memcached_init_vl (&vl, st);
300 values[0].gauge = value;
304 sstrncpy (vl.type, type, sizeof (vl.type));
305 if (type_inst != NULL)
306 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
308 plugin_dispatch_values (&vl);
311 static void submit_gauge2 (const char *type, const char *type_inst,
312 gauge_t value0, gauge_t value1, memcached_t *st)
315 value_list_t vl = VALUE_LIST_INIT;
316 memcached_init_vl (&vl, st);
318 values[0].gauge = value0;
319 values[1].gauge = value1;
323 sstrncpy (vl.type, type, sizeof (vl.type));
324 if (type_inst != NULL)
325 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
327 plugin_dispatch_values (&vl);
330 static int memcached_read (user_data_t *user_data)
339 gauge_t bytes_used = NAN;
340 gauge_t bytes_total = NAN;
343 derive_t rusage_user = 0;
344 derive_t rusage_syst = 0;
345 derive_t octets_rx = 0;
346 derive_t octets_tx = 0;
349 st = user_data->data;
351 /* get data from daemon */
352 if (memcached_query_daemon (buf, sizeof (buf), st) < 0) {
356 #define FIELD_IS(cnst) \
357 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
361 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
367 fields_num = strsplit(line, fields, 3);
371 name_len = strlen(fields[1]);
376 * For an explanation on these fields please refer to
377 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
381 * CPU time consumed by the memcached process
383 if (FIELD_IS ("rusage_user"))
385 rusage_user = atoll (fields[2]);
387 else if (FIELD_IS ("rusage_system"))
389 rusage_syst = atoll(fields[2]);
393 * Number of threads of this instance
395 else if (FIELD_IS ("threads"))
397 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]), st);
401 * Number of items stored
403 else if (FIELD_IS ("curr_items"))
405 submit_gauge ("memcached_items", "current", atof (fields[2]), st);
409 * Number of bytes used and available (total - used)
411 else if (FIELD_IS ("bytes"))
413 bytes_used = atof (fields[2]);
415 else if (FIELD_IS ("limit_maxbytes"))
417 bytes_total = atof(fields[2]);
423 else if (FIELD_IS ("curr_connections"))
425 submit_gauge ("memcached_connections", "current", atof (fields[2]), st);
431 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
433 const char *name = fields[1] + 4;
434 submit_derive ("memcached_command", name, atoll (fields[2]), st);
435 if (strcmp (name, "get") == 0)
436 gets = atof (fields[2]);
440 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
442 else if (FIELD_IS ("get_hits"))
444 submit_derive ("memcached_ops", "hits", atoll (fields[2]), st);
445 hits = atof (fields[2]);
447 else if (FIELD_IS ("get_misses"))
449 submit_derive ("memcached_ops", "misses", atoll (fields[2]), st);
451 else if (FIELD_IS ("evictions"))
453 submit_derive ("memcached_ops", "evictions", atoll (fields[2]), st);
459 else if (FIELD_IS ("bytes_read"))
461 octets_rx = atoll (fields[2]);
463 else if (FIELD_IS ("bytes_written"))
465 octets_tx = atoll (fields[2]);
467 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
469 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
470 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used, st);
472 if ((rusage_user != 0) || (rusage_syst != 0))
473 submit_derive2 ("ps_cputime", NULL, rusage_user, rusage_syst, st);
475 if ((octets_rx != 0) || (octets_tx != 0))
476 submit_derive2 ("memcached_octets", NULL, octets_rx, octets_tx, st);
478 if (!isnan (gets) && !isnan (hits))
483 rate = 100.0 * hits / gets;
485 submit_gauge ("percent", "hitratio", rate, st);
489 } /* int memcached_read */
491 static int memcached_add_read_callback (memcached_t *st)
494 char callback_name[3*DATA_MAX_NAME_LEN];
497 memset (&ud, 0, sizeof (ud));
499 ud.free_func = (void *) memcached_free;
501 assert (st->name != NULL);
502 ssnprintf (callback_name, sizeof (callback_name), "memcached/%s", st->name);
504 status = plugin_register_complex_read (/* group = */ "memcached",
505 /* name = */ callback_name,
506 /* callback = */ memcached_read,
507 /* interval = */ NULL,
508 /* user_data = */ &ud);
510 } /* int memcached_add_read_callback */
512 /* Configuration handling functiions
514 * <Instance "instance_name">
520 static int config_add_instance(oconfig_item_t *ci)
526 /* Disable automatic generation of default instance in the init callback. */
527 memcached_have_instances = 1;
529 st = malloc (sizeof (*st));
532 ERROR ("memcached plugin: malloc failed.");
536 memset (st, 0, sizeof (*st));
542 if (strcasecmp (ci->key, "Plugin") == 0) /* default instance */
543 st->name = sstrdup ("__legacy__");
544 else /* <Instance /> block */
545 status = cf_util_get_string (ci, &st->name);
551 assert (st->name != NULL);
553 for (i = 0; i < ci->children_num; i++)
555 oconfig_item_t *child = ci->children + i;
557 if (strcasecmp ("Socket", child->key) == 0)
558 status = cf_util_get_string (child, &st->socket);
559 else if (strcasecmp ("Host", child->key) == 0)
560 status = cf_util_get_string (child, &st->host);
561 else if (strcasecmp ("Port", child->key) == 0)
562 status = cf_util_get_service (child, &st->port);
565 WARNING ("memcached plugin: Option `%s' not allowed here.",
575 status = memcached_add_read_callback (st);
586 static int memcached_config (oconfig_item_t *ci)
589 _Bool have_instance_block = 0;
592 for (i = 0; i < ci->children_num; i++)
594 oconfig_item_t *child = ci->children + i;
596 if (strcasecmp ("Instance", child->key) == 0)
598 config_add_instance (child);
599 have_instance_block = 1;
601 else if (!have_instance_block)
603 /* Non-instance option: Assume legacy configuration (without <Instance />
604 * blocks) and call config_add_instance() with the <Plugin /> block. */
605 return (config_add_instance (ci));
608 WARNING ("memcached plugin: The configuration option "
609 "\"%s\" is not allowed here. Did you "
610 "forget to add an <Instance /> block "
611 "around the configuration?",
613 } /* for (ci->children) */
618 static int memcached_init (void)
623 if (memcached_have_instances)
626 /* No instances were configured, lets start a default instance. */
627 st = malloc (sizeof (*st));
630 memset (st, 0, sizeof (*st));
631 st->name = sstrdup ("__legacy__");
636 status = memcached_add_read_callback (st);
638 memcached_have_instances = 1;
643 } /* int memcached_init */
645 void module_register (void)
647 plugin_register_complex_config ("memcached", memcached_config);
648 plugin_register_init ("memcached", memcached_init);