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)));
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_hints;
105 struct addrinfo *ai_list, *ai_ptr;
109 memset (&ai_hints, 0, sizeof (ai_hints));
110 ai_hints.ai_flags = 0;
112 ai_hints.ai_flags |= AI_ADDRCONFIG;
114 ai_hints.ai_family = AF_UNSPEC;
115 ai_hints.ai_socktype = SOCK_STREAM;
116 ai_hints.ai_protocol = 0;
118 host = (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST;
119 port = (st->port != NULL) ? st->port : MEMCACHED_DEF_PORT;
122 status = getaddrinfo (host, port, &ai_hints, &ai_list);
126 ERROR ("memcached plugin: memcached_connect_inet: "
127 "getaddrinfo(%s,%s) failed: %s",
129 (status == EAI_SYSTEM)
130 ? sstrerror (errno, errbuf, sizeof (errbuf))
131 : gai_strerror (status));
135 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
137 /* create our socket descriptor */
138 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
142 WARNING ("memcached plugin: memcached_connect_inet: "
143 "socket(2) failed: %s",
144 sstrerror (errno, errbuf, sizeof (errbuf)));
148 /* connect to the memcached daemon */
149 status = (int) connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
152 shutdown (fd, SHUT_RDWR);
158 /* A socket could be opened and connecting succeeded. We're done. */
162 freeaddrinfo (ai_list);
164 } /* int memcached_connect_inet */
166 static int memcached_connect (memcached_t *st)
168 if (st->socket != NULL)
169 return (memcached_connect_unix (st));
171 return (memcached_connect_inet (st));
174 static int memcached_query_daemon (char *buffer, size_t buffer_size, memcached_t *st)
180 fd = memcached_connect (st);
182 ERROR ("memcached plugin: Instance \"%s\" could not connect to daemon.",
187 status = (int) swrite (fd, "stats\r\n", strlen ("stats\r\n"));
191 ERROR ("memcached plugin: write(2) failed: %s",
192 sstrerror (errno, errbuf, sizeof (errbuf)));
193 shutdown(fd, SHUT_RDWR);
198 /* receive data from the memcached daemon */
199 memset (buffer, 0, buffer_size);
202 while ((status = (int) recv (fd, buffer + buffer_fill,
203 buffer_size - buffer_fill, /* flags = */ 0)) != 0)
205 char const end_token[5] = {'E', 'N', 'D', '\r', '\n'};
210 if ((errno == EAGAIN) || (errno == EINTR))
213 ERROR ("memcached: Error reading from socket: %s",
214 sstrerror (errno, errbuf, sizeof (errbuf)));
215 shutdown(fd, SHUT_RDWR);
220 buffer_fill += (size_t) status;
221 if (buffer_fill > buffer_size)
223 buffer_fill = buffer_size;
224 WARNING ("memcached plugin: Message was truncated.");
228 /* If buffer ends in end_token, we have all the data. */
229 if (memcmp (buffer + buffer_fill - sizeof (end_token),
230 end_token, sizeof (end_token)) == 0)
235 if (buffer_fill == 0)
237 WARNING ("memcached plugin: No data returned by memcached.");
241 shutdown(fd, SHUT_RDWR);
244 } /* int memcached_query_daemon */
246 static void memcached_init_vl (value_list_t *vl, memcached_t const *st)
248 sstrncpy (vl->plugin, "memcached", sizeof (vl->plugin));
249 if (strcmp (st->name, "__legacy__") == 0) /* legacy mode */
251 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
255 if (st->socket != NULL)
256 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
259 (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST,
261 sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance));
265 static void submit_derive (const char *type, const char *type_inst,
266 derive_t value, memcached_t *st)
269 value_list_t vl = VALUE_LIST_INIT;
270 memcached_init_vl (&vl, st);
272 values[0].derive = value;
276 sstrncpy (vl.type, type, sizeof (vl.type));
277 if (type_inst != NULL)
278 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
280 plugin_dispatch_values (&vl);
283 static void submit_derive2 (const char *type, const char *type_inst,
284 derive_t value0, derive_t value1, memcached_t *st)
287 value_list_t vl = VALUE_LIST_INIT;
288 memcached_init_vl (&vl, st);
290 values[0].derive = value0;
291 values[1].derive = value1;
295 sstrncpy (vl.type, type, sizeof (vl.type));
296 if (type_inst != NULL)
297 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
299 plugin_dispatch_values (&vl);
302 static void submit_gauge (const char *type, const char *type_inst,
303 gauge_t value, memcached_t *st)
306 value_list_t vl = VALUE_LIST_INIT;
307 memcached_init_vl (&vl, st);
309 values[0].gauge = value;
313 sstrncpy (vl.type, type, sizeof (vl.type));
314 if (type_inst != NULL)
315 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
317 plugin_dispatch_values (&vl);
320 static void submit_gauge2 (const char *type, const char *type_inst,
321 gauge_t value0, gauge_t value1, memcached_t *st)
324 value_list_t vl = VALUE_LIST_INIT;
325 memcached_init_vl (&vl, st);
327 values[0].gauge = value0;
328 values[1].gauge = value1;
332 sstrncpy (vl.type, type, sizeof (vl.type));
333 if (type_inst != NULL)
334 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
336 plugin_dispatch_values (&vl);
339 static int memcached_read (user_data_t *user_data)
348 gauge_t bytes_used = NAN;
349 gauge_t bytes_total = NAN;
352 derive_t rusage_user = 0;
353 derive_t rusage_syst = 0;
354 derive_t octets_rx = 0;
355 derive_t octets_tx = 0;
358 st = user_data->data;
360 /* get data from daemon */
361 if (memcached_query_daemon (buf, sizeof (buf), st) < 0) {
365 #define FIELD_IS(cnst) \
366 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
370 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
376 fields_num = strsplit(line, fields, 3);
380 name_len = strlen(fields[1]);
385 * For an explanation on these fields please refer to
386 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
390 * CPU time consumed by the memcached process
392 if (FIELD_IS ("rusage_user"))
394 rusage_user = atoll (fields[2]);
396 else if (FIELD_IS ("rusage_system"))
398 rusage_syst = atoll(fields[2]);
402 * Number of threads of this instance
404 else if (FIELD_IS ("threads"))
406 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]), st);
410 * Number of items stored
412 else if (FIELD_IS ("curr_items"))
414 submit_gauge ("memcached_items", "current", atof (fields[2]), st);
418 * Number of bytes used and available (total - used)
420 else if (FIELD_IS ("bytes"))
422 bytes_used = atof (fields[2]);
424 else if (FIELD_IS ("limit_maxbytes"))
426 bytes_total = atof(fields[2]);
432 else if (FIELD_IS ("curr_connections"))
434 submit_gauge ("memcached_connections", "current", atof (fields[2]), st);
440 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
442 const char *name = fields[1] + 4;
443 submit_derive ("memcached_command", name, atoll (fields[2]), st);
444 if (strcmp (name, "get") == 0)
445 gets = atof (fields[2]);
449 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
451 else if (FIELD_IS ("get_hits"))
453 submit_derive ("memcached_ops", "hits", atoll (fields[2]), st);
454 hits = atof (fields[2]);
456 else if (FIELD_IS ("get_misses"))
458 submit_derive ("memcached_ops", "misses", atoll (fields[2]), st);
460 else if (FIELD_IS ("evictions"))
462 submit_derive ("memcached_ops", "evictions", atoll (fields[2]), st);
468 else if (FIELD_IS ("bytes_read"))
470 octets_rx = atoll (fields[2]);
472 else if (FIELD_IS ("bytes_written"))
474 octets_tx = atoll (fields[2]);
476 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
478 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
479 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used, st);
481 if ((rusage_user != 0) || (rusage_syst != 0))
482 submit_derive2 ("ps_cputime", NULL, rusage_user, rusage_syst, st);
484 if ((octets_rx != 0) || (octets_tx != 0))
485 submit_derive2 ("memcached_octets", NULL, octets_rx, octets_tx, st);
487 if (!isnan (gets) && !isnan (hits))
492 rate = 100.0 * hits / gets;
494 submit_gauge ("percent", "hitratio", rate, st);
498 } /* int memcached_read */
500 static int memcached_add_read_callback (memcached_t *st)
503 char callback_name[3*DATA_MAX_NAME_LEN];
506 memset (&ud, 0, sizeof (ud));
508 ud.free_func = (void *) memcached_free;
510 assert (st->name != NULL);
511 ssnprintf (callback_name, sizeof (callback_name), "memcached/%s", st->name);
513 status = plugin_register_complex_read (/* group = */ "memcached",
514 /* name = */ callback_name,
515 /* callback = */ memcached_read,
516 /* interval = */ NULL,
517 /* user_data = */ &ud);
519 } /* int memcached_add_read_callback */
521 /* Configuration handling functiions
523 * <Instance "instance_name">
529 static int config_add_instance(oconfig_item_t *ci)
535 /* Disable automatic generation of default instance in the init callback. */
536 memcached_have_instances = 1;
538 st = malloc (sizeof (*st));
541 ERROR ("memcached plugin: malloc failed.");
545 memset (st, 0, sizeof (*st));
551 if (strcasecmp (ci->key, "Plugin") == 0) /* default instance */
552 st->name = sstrdup ("__legacy__");
553 else /* <Instance /> block */
554 status = cf_util_get_string (ci, &st->name);
560 assert (st->name != NULL);
562 for (i = 0; i < ci->children_num; i++)
564 oconfig_item_t *child = ci->children + i;
566 if (strcasecmp ("Socket", child->key) == 0)
567 status = cf_util_get_string (child, &st->socket);
568 else if (strcasecmp ("Host", child->key) == 0)
569 status = cf_util_get_string (child, &st->host);
570 else if (strcasecmp ("Port", child->key) == 0)
571 status = cf_util_get_service (child, &st->port);
574 WARNING ("memcached plugin: Option `%s' not allowed here.",
584 status = memcached_add_read_callback (st);
595 static int memcached_config (oconfig_item_t *ci)
598 _Bool have_instance_block = 0;
601 for (i = 0; i < ci->children_num; i++)
603 oconfig_item_t *child = ci->children + i;
605 if (strcasecmp ("Instance", child->key) == 0)
607 config_add_instance (child);
608 have_instance_block = 1;
610 else if (!have_instance_block)
612 /* Non-instance option: Assume legacy configuration (without <Instance />
613 * blocks) and call config_add_instance() with the <Plugin /> block. */
614 return (config_add_instance (ci));
617 WARNING ("memcached plugin: The configuration option "
618 "\"%s\" is not allowed here. Did you "
619 "forget to add an <Instance /> block "
620 "around the configuration?",
622 } /* for (ci->children) */
627 static int memcached_init (void)
632 if (memcached_have_instances)
635 /* No instances were configured, lets start a default instance. */
636 st = malloc (sizeof (*st));
639 memset (st, 0, sizeof (*st));
640 st->name = sstrdup ("__legacy__");
645 status = memcached_add_read_callback (st);
647 memcached_have_instances = 1;
652 } /* int memcached_init */
654 void module_register (void)
656 plugin_register_complex_config ("memcached", memcached_config);
657 plugin_register_init ("memcached", memcached_init);