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
61 typedef struct memcached_s memcached_t;
63 static int memcached_read (user_data_t *user_data);
65 static void memcached_free (memcached_t *st)
76 static int memcached_query_daemon (char *buffer, int buffer_size, user_data_t *user_data)
85 if (st->socket != NULL) {
86 struct sockaddr_un serv_addr;
88 memset (&serv_addr, 0, sizeof (serv_addr));
89 serv_addr.sun_family = AF_UNIX;
90 sstrncpy (serv_addr.sun_path, st->socket,
91 sizeof (serv_addr.sun_path));
93 /* create our socket descriptor */
94 fd = socket (AF_UNIX, SOCK_STREAM, 0);
97 ERROR ("memcached: unix socket: %s", sstrerror (errno, errbuf,
103 if (st->port != NULL) {
107 struct addrinfo ai_hints;
108 struct addrinfo *ai_list, *ai_ptr;
111 memset (&ai_hints, '\0', sizeof (ai_hints));
112 ai_hints.ai_flags = 0;
114 /* ai_hints.ai_flags |= AI_ADDRCONFIG; */
116 ai_hints.ai_family = AF_INET;
117 ai_hints.ai_socktype = SOCK_STREAM;
118 ai_hints.ai_protocol = 0;
122 host = MEMCACHED_DEF_HOST;
126 if (strlen (port) == 0) {
127 port = MEMCACHED_DEF_PORT;
130 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0) {
132 ERROR ("memcached: getaddrinfo (%s, %s): %s",
134 (ai_return == EAI_SYSTEM)
135 ? sstrerror (errno, errbuf, sizeof (errbuf))
136 : gai_strerror (ai_return));
141 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
142 /* create our socket descriptor */
143 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
146 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
150 /* connect to the memcached daemon */
151 status = (ssize_t) connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen);
153 shutdown (fd, SHUT_RDWR);
159 /* A socket could be opened and connecting succeeded. We're
164 freeaddrinfo (ai_list);
169 ERROR ("memcached: Could not connect to daemon.");
173 if (send(fd, "stats\r\n", sizeof("stats\r\n") - 1, MSG_DONTWAIT) != (sizeof("stats\r\n") - 1)) {
174 ERROR ("memcached: Could not send command to the memcached daemon.");
182 memset (&p, 0, sizeof (p));
184 p.events = POLLIN | POLLERR | POLLHUP;
187 status = poll (&p, /* nfds = */ 1,
188 /* timeout = */ CDTIME_T_TO_MS (interval_g));
193 ERROR ("memcached: poll(2) timed out after %.3f seconds.",
194 CDTIME_T_TO_DOUBLE (interval_g));
199 ERROR ("memcached: poll(2) failed: %s",
200 sstrerror (errno, errbuf, sizeof (errbuf)));
202 shutdown (fd, SHUT_RDWR);
208 /* receive data from the memcached daemon */
209 memset (buffer, '\0', buffer_size);
212 while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
213 if (i > MEMCACHED_RETRY_COUNT) {
214 ERROR("recv() timed out");
222 if (errno == EAGAIN) {
226 ERROR ("memcached: Error reading from socket: %s",
227 sstrerror (errno, errbuf, sizeof (errbuf)));
228 shutdown(fd, SHUT_RDWR);
232 buffer_fill += status;
234 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
235 /* we got all the data */
240 if (buffer_fill >= buffer_size) {
241 buffer[buffer_size - 1] = '\0';
242 WARNING ("memcached: Message from memcached has been truncated.");
243 } else if (buffer_fill == 0) {
244 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
245 "Buffer: `%s'", buffer);
246 shutdown(fd, SHUT_RDWR);
251 shutdown(fd, SHUT_RDWR);
256 /* Configuration handling functiions
258 * <Instance "instance_name">
264 static int config_add_instance(oconfig_item_t *ci)
270 if ((ci->values_num != 1)
271 || (ci->values[0].type != OCONFIG_TYPE_STRING))
273 WARNING ("memcached plugin: The `%s' config option "
274 "needs exactly one string argument.", ci->key);
278 st = (memcached_t *) malloc (sizeof (*st));
281 ERROR ("memcached plugin: malloc failed.");
289 memset (st, 0, sizeof (*st));
291 status = cf_util_get_string (ci, &st->name);
297 assert (st->name != NULL);
299 for (i = 0; i < ci->children_num; i++)
301 oconfig_item_t *child = ci->children + i;
303 if (strcasecmp ("Socket", child->key) == 0)
304 status = cf_util_get_string (child, &st->socket);
305 else if (strcasecmp ("Host", child->key) == 0)
306 status = cf_util_get_string (child, &st->host);
307 else if (strcasecmp ("Port", child->key) == 0)
308 status = cf_util_get_service (child, &st->port);
311 WARNING ("memcached plugin: Option `%s' not allowed here.",
323 char callback_name[3*DATA_MAX_NAME_LEN];
325 memset (&ud, 0, sizeof (ud));
327 ud.free_func = (void *) memcached_free;
329 memset (callback_name, 0, sizeof (callback_name));
330 ssnprintf (callback_name, sizeof (callback_name),
332 (st->host != NULL) ? st->host : hostname_g,
333 (st->port != NULL) ? st->port : "default"),
335 status = plugin_register_complex_read (/* group = */ "memcached",
336 /* name = */ callback_name,
337 /* callback = */ memcached_read,
338 /* interval = */ NULL,
339 /* user_data = */ &ud);
351 static int memcached_config (oconfig_item_t *ci)
356 for (i = 0; i < ci->children_num; i++)
358 oconfig_item_t *child = ci->children + i;
360 if (strcasecmp ("Instance", child->key) == 0)
361 config_add_instance (child);
363 WARNING ("memcached plugin: The configuration option "
364 "\"%s\" is not allowed here. Did you "
365 "forget to add an <Instance /> block "
366 "around the configuration?",
368 } /* for (ci->children) */
373 static void submit_derive (const char *type, const char *type_inst,
374 derive_t value, memcached_t *st)
377 value_list_t vl = VALUE_LIST_INIT;
379 values[0].derive = value;
383 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
384 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
385 if (st->name != NULL)
386 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
387 sstrncpy (vl.type, type, sizeof (vl.type));
388 if (type_inst != NULL)
389 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
391 plugin_dispatch_values (&vl);
394 static void submit_derive2 (const char *type, const char *type_inst,
395 derive_t value0, derive_t value1, memcached_t *st)
398 value_list_t vl = VALUE_LIST_INIT;
400 values[0].derive = value0;
401 values[1].derive = value1;
405 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
406 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
407 if (st->name != NULL)
408 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
409 sstrncpy (vl.type, type, sizeof (vl.type));
410 if (type_inst != NULL)
411 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
413 plugin_dispatch_values (&vl);
416 static void submit_gauge (const char *type, const char *type_inst,
417 gauge_t value, memcached_t *st)
420 value_list_t vl = VALUE_LIST_INIT;
422 values[0].gauge = value;
426 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
427 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
428 if (st->name != NULL)
429 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
430 sstrncpy (vl.type, type, sizeof (vl.type));
431 if (type_inst != NULL)
432 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
434 plugin_dispatch_values (&vl);
437 static void submit_gauge2 (const char *type, const char *type_inst,
438 gauge_t value0, gauge_t value1, memcached_t *st)
441 value_list_t vl = VALUE_LIST_INIT;
443 values[0].gauge = value0;
444 values[1].gauge = value1;
448 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
449 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
450 if (st->name != NULL)
451 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
452 sstrncpy (vl.type, type, sizeof (vl.type));
453 if (type_inst != NULL)
454 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
456 plugin_dispatch_values (&vl);
459 static int memcached_read (user_data_t *user_data)
468 gauge_t bytes_used = NAN;
469 gauge_t bytes_total = NAN;
472 derive_t rusage_user = 0;
473 derive_t rusage_syst = 0;
474 derive_t octets_rx = 0;
475 derive_t octets_tx = 0;
478 st = user_data->data;
480 /* get data from daemon */
481 if (memcached_query_daemon (buf, sizeof (buf), user_data) < 0) {
485 #define FIELD_IS(cnst) \
486 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
490 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
496 fields_num = strsplit(line, fields, 3);
500 name_len = strlen(fields[1]);
505 * For an explanation on these fields please refer to
506 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
510 * CPU time consumed by the memcached process
512 if (FIELD_IS ("rusage_user"))
514 rusage_user = atoll (fields[2]);
516 else if (FIELD_IS ("rusage_system"))
518 rusage_syst = atoll(fields[2]);
522 * Number of threads of this instance
524 else if (FIELD_IS ("threads"))
526 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]), st);
530 * Number of items stored
532 else if (FIELD_IS ("curr_items"))
534 submit_gauge ("memcached_items", "current", atof (fields[2]), st);
538 * Number of bytes used and available (total - used)
540 else if (FIELD_IS ("bytes"))
542 bytes_used = atof (fields[2]);
544 else if (FIELD_IS ("limit_maxbytes"))
546 bytes_total = atof(fields[2]);
552 else if (FIELD_IS ("curr_connections"))
554 submit_gauge ("memcached_connections", "current", atof (fields[2]), st);
560 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
562 const char *name = fields[1] + 4;
563 submit_derive ("memcached_command", name, atoll (fields[2]), st);
564 if (strcmp (name, "get") == 0)
565 gets = atof (fields[2]);
569 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
571 else if (FIELD_IS ("get_hits"))
573 submit_derive ("memcached_ops", "hits", atoll (fields[2]), st);
574 hits = atof (fields[2]);
576 else if (FIELD_IS ("get_misses"))
578 submit_derive ("memcached_ops", "misses", atoll (fields[2]), st);
580 else if (FIELD_IS ("evictions"))
582 submit_derive ("memcached_ops", "evictions", atoll (fields[2]), st);
588 else if (FIELD_IS ("bytes_read"))
590 octets_rx = atoll (fields[2]);
592 else if (FIELD_IS ("bytes_written"))
594 octets_tx = atoll (fields[2]);
596 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
598 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
599 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used, st);
601 if ((rusage_user != 0) || (rusage_syst != 0))
602 submit_derive2 ("ps_cputime", NULL, rusage_user, rusage_syst, st);
604 if ((octets_rx != 0) || (octets_tx != 0))
605 submit_derive2 ("memcached_octets", NULL, octets_rx, octets_tx, st);
607 if (!isnan (gets) && !isnan (hits))
612 rate = 100.0 * hits / gets;
614 submit_gauge ("percent", "hitratio", rate, st);
620 void module_register (void)
622 plugin_register_complex_config ("memcached", memcached_config);