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_set_string (char **ret_string, oconfig_item_t *ci)
268 if ((ci->values_num != 1)
269 || (ci->values[0].type != OCONFIG_TYPE_STRING))
271 WARNING ("memcached plugin: The `%s' config option "
272 "needs exactly one string argument.", ci->key);
276 string = strdup (ci->values[0].value.string);
279 ERROR ("memcached plugin: strdup failed.");
283 if (*ret_string != NULL)
285 *ret_string = string;
290 static int config_add_instance(oconfig_item_t *ci)
296 if ((ci->values_num != 1)
297 || (ci->values[0].type != OCONFIG_TYPE_STRING))
299 WARNING ("memcached plugin: The `%s' config option "
300 "needs exactly one string argument.", ci->key);
304 st = (memcached_t *) malloc (sizeof (*st));
307 ERROR ("memcached plugin: malloc failed.");
315 memset (st, 0, sizeof (*st));
317 status = config_set_string (&st->name, ci);
323 assert (st->name != NULL);
325 for (i = 0; i < ci->children_num; i++)
327 oconfig_item_t *child = ci->children + i;
329 if (strcasecmp ("Socket", child->key) == 0)
330 status = config_set_string (&st->socket, child);
331 else if (strcasecmp ("Host", child->key) == 0)
332 status = config_set_string (&st->host, child);
333 else if (strcasecmp ("Port", child->key) == 0)
334 status = config_set_string (&st->port, child);
337 WARNING ("memcached plugin: Option `%s' not allowed here.",
349 char callback_name[3*DATA_MAX_NAME_LEN];
351 memset (&ud, 0, sizeof (ud));
353 ud.free_func = (void *) memcached_free;
355 memset (callback_name, 0, sizeof (callback_name));
356 ssnprintf (callback_name, sizeof (callback_name),
358 (st->host != NULL) ? st->host : hostname_g,
359 (st->port != NULL) ? st->port : "default"),
361 status = plugin_register_complex_read (/* group = */ NULL,
362 /* name = */ callback_name,
363 /* callback = */ memcached_read,
364 /* interval = */ NULL,
365 /* user_data = */ &ud);
377 static int config (oconfig_item_t *ci)
382 for (i = 0; i < ci->children_num; i++)
384 oconfig_item_t *child = ci->children + i;
386 if (strcasecmp ("Instance", child->key) == 0)
387 config_add_instance (child);
389 WARNING ("memcached plugin: The configuration option "
390 "\"%s\" is not allowed here. Did you "
391 "forget to add an <Instance /> block "
392 "around the configuration?",
394 } /* for (ci->children) */
399 static void submit_derive (const char *type, const char *type_inst,
400 derive_t value, memcached_t *st)
403 value_list_t vl = VALUE_LIST_INIT;
405 values[0].derive = value;
409 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
410 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
411 if (st->name != NULL)
412 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
413 sstrncpy (vl.type, type, sizeof (vl.type));
414 if (type_inst != NULL)
415 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
417 plugin_dispatch_values (&vl);
420 static void submit_derive2 (const char *type, const char *type_inst,
421 derive_t value0, derive_t value1, memcached_t *st)
424 value_list_t vl = VALUE_LIST_INIT;
426 values[0].derive = value0;
427 values[1].derive = value1;
431 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
432 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
433 if (st->name != NULL)
434 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
435 sstrncpy (vl.type, type, sizeof (vl.type));
436 if (type_inst != NULL)
437 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
439 plugin_dispatch_values (&vl);
442 static void submit_gauge (const char *type, const char *type_inst,
443 gauge_t value, memcached_t *st)
446 value_list_t vl = VALUE_LIST_INIT;
448 values[0].gauge = value;
452 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
453 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
454 if (st->name != NULL)
455 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
456 sstrncpy (vl.type, type, sizeof (vl.type));
457 if (type_inst != NULL)
458 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
460 plugin_dispatch_values (&vl);
463 static void submit_gauge2 (const char *type, const char *type_inst,
464 gauge_t value0, gauge_t value1, memcached_t *st)
467 value_list_t vl = VALUE_LIST_INIT;
469 values[0].gauge = value0;
470 values[1].gauge = value1;
474 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
475 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
476 if (st->name != NULL)
477 sstrncpy (vl.plugin_instance, st->name, sizeof (vl.plugin_instance));
478 sstrncpy (vl.type, type, sizeof (vl.type));
479 if (type_inst != NULL)
480 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
482 plugin_dispatch_values (&vl);
485 static int memcached_read (user_data_t *user_data)
494 gauge_t bytes_used = NAN;
495 gauge_t bytes_total = NAN;
498 derive_t rusage_user = 0;
499 derive_t rusage_syst = 0;
500 derive_t octets_rx = 0;
501 derive_t octets_tx = 0;
504 st = user_data->data;
506 /* get data from daemon */
507 if (memcached_query_daemon (buf, sizeof (buf), user_data) < 0) {
511 #define FIELD_IS(cnst) \
512 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
516 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
522 fields_num = strsplit(line, fields, 3);
526 name_len = strlen(fields[1]);
531 * For an explanation on these fields please refer to
532 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
536 * CPU time consumed by the memcached process
538 if (FIELD_IS ("rusage_user"))
540 rusage_user = atoll (fields[2]);
542 else if (FIELD_IS ("rusage_system"))
544 rusage_syst = atoll(fields[2]);
548 * Number of threads of this instance
550 else if (FIELD_IS ("threads"))
552 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]), st);
556 * Number of items stored
558 else if (FIELD_IS ("curr_items"))
560 submit_gauge ("memcached_items", "current", atof (fields[2]), st);
564 * Number of bytes used and available (total - used)
566 else if (FIELD_IS ("bytes"))
568 bytes_used = atof (fields[2]);
570 else if (FIELD_IS ("limit_maxbytes"))
572 bytes_total = atof(fields[2]);
578 else if (FIELD_IS ("curr_connections"))
580 submit_gauge ("memcached_connections", "current", atof (fields[2]), st);
586 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
588 const char *name = fields[1] + 4;
589 submit_derive ("memcached_command", name, atoll (fields[2]), st);
590 if (strcmp (name, "get") == 0)
591 gets = atof (fields[2]);
595 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
597 else if (FIELD_IS ("get_hits"))
599 submit_derive ("memcached_ops", "hits", atoll (fields[2]), st);
600 hits = atof (fields[2]);
602 else if (FIELD_IS ("get_misses"))
604 submit_derive ("memcached_ops", "misses", atoll (fields[2]), st);
606 else if (FIELD_IS ("evictions"))
608 submit_derive ("memcached_ops", "evictions", atoll (fields[2]), st);
614 else if (FIELD_IS ("bytes_read"))
616 octets_rx = atoll (fields[2]);
618 else if (FIELD_IS ("bytes_written"))
620 octets_tx = atoll (fields[2]);
622 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
624 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
625 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used, st);
627 if ((rusage_user != 0) || (rusage_syst != 0))
628 submit_derive2 ("ps_cputime", NULL, rusage_user, rusage_syst, st);
630 if ((octets_rx != 0) || (octets_tx != 0))
631 submit_derive2 ("memcached_octets", NULL, octets_rx, octets_tx, st);
633 if (!isnan (gets) && !isnan (hits))
638 rate = 100.0 * hits / gets;
640 submit_gauge ("percent", "hitratio", rate, st);
646 void module_register (void)
648 plugin_register_complex_config ("memcached", config);