2 * collectd - src/memcached.c, based on src/hddtemp.c
3 * Copyright (C) 2007 Antony Dovgal
4 * Copyright (C) 2007-2009 Florian Forster
5 * Copyright (C) 2009 Doug MacEachern
6 * Copyright (C) 2009 Franck Lombardi
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * Antony Dovgal <tony at daylessday dot org>
24 * Florian octo Forster <octo at verplant.org>
25 * Doug MacEachern <dougm at hyperic.com>
32 #include "configfile.h"
36 # include <sys/socket.h>
38 # include <netinet/in.h>
39 # include <netinet/tcp.h>
41 #define MEMCACHED_DEF_HOST "127.0.0.1"
42 #define MEMCACHED_DEF_PORT "11211"
44 #define MEMCACHED_RETRY_COUNT 100
46 static const char *config_keys[] =
52 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
54 static char *memcached_socket = NULL;
55 static char *memcached_host = NULL;
56 static char memcached_port[16];
58 static int memcached_query_daemon (char *buffer, int buffer_size) /* {{{ */
65 if (memcached_socket != NULL) {
66 struct sockaddr_un serv_addr;
68 memset (&serv_addr, 0, sizeof (serv_addr));
69 serv_addr.sun_family = AF_UNIX;
70 sstrncpy (serv_addr.sun_path, memcached_socket, sizeof (serv_addr.sun_path));
72 /* create our socket descriptor */
73 fd = socket (AF_UNIX, SOCK_STREAM, 0);
76 ERROR ("memcached: unix socket: %s", sstrerror (errno, errbuf,
81 /* connect to the memcached daemon */
82 status = (ssize_t) connect (fd, (struct sockaddr *) &serv_addr,
83 SUN_LEN (&serv_addr));
85 shutdown (fd, SHUT_RDWR);
90 else { /* if (memcached_socket == NULL) */
94 struct addrinfo ai_hints;
95 struct addrinfo *ai_list, *ai_ptr;
98 memset (&ai_hints, '\0', sizeof (ai_hints));
99 ai_hints.ai_flags = 0;
101 /* ai_hints.ai_flags |= AI_ADDRCONFIG; */
103 ai_hints.ai_family = AF_INET;
104 ai_hints.ai_socktype = SOCK_STREAM;
105 ai_hints.ai_protocol = 0;
107 host = memcached_host;
109 host = MEMCACHED_DEF_HOST;
112 port = memcached_port;
113 if (strlen (port) == 0) {
114 port = MEMCACHED_DEF_PORT;
117 if ((ai_return = getaddrinfo (host, port, NULL, &ai_list)) != 0) {
119 ERROR ("memcached: getaddrinfo (%s, %s): %s",
121 (ai_return == EAI_SYSTEM)
122 ? sstrerror (errno, errbuf, sizeof (errbuf))
123 : gai_strerror (ai_return));
128 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
129 /* create our socket descriptor */
130 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
133 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
137 /* connect to the memcached daemon */
138 status = (ssize_t) connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen);
140 shutdown (fd, SHUT_RDWR);
146 /* A socket could be opened and connecting succeeded. We're
151 freeaddrinfo (ai_list);
155 ERROR ("memcached: Could not connect to daemon.");
159 if (send(fd, "stats\r\n", sizeof("stats\r\n") - 1, MSG_DONTWAIT) != (sizeof("stats\r\n") - 1)) {
160 ERROR ("memcached: Could not send command to the memcached daemon.");
168 memset (&p, 0, sizeof (p));
170 p.events = POLLIN | POLLERR | POLLHUP;
173 status = poll (&p, /* nfds = */ 1, /* timeout = */ 1000 * interval_g);
178 ERROR ("memcached: poll(2) timed out after %i seconds.", interval_g);
183 ERROR ("memcached: poll(2) failed: %s",
184 sstrerror (errno, errbuf, sizeof (errbuf)));
186 shutdown (fd, SHUT_RDWR);
192 /* receive data from the memcached daemon */
193 memset (buffer, '\0', buffer_size);
196 while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
197 if (i > MEMCACHED_RETRY_COUNT) {
198 ERROR("recv() timed out");
206 if (errno == EAGAIN) {
210 ERROR ("memcached: Error reading from socket: %s",
211 sstrerror (errno, errbuf, sizeof (errbuf)));
212 shutdown(fd, SHUT_RDWR);
216 buffer_fill += status;
218 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
219 /* we got all the data */
224 if (buffer_fill >= buffer_size) {
225 buffer[buffer_size - 1] = '\0';
226 WARNING ("memcached: Message from memcached has been truncated.");
227 } else if (buffer_fill == 0) {
228 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
229 "Buffer: `%s'", buffer);
230 shutdown(fd, SHUT_RDWR);
235 shutdown(fd, SHUT_RDWR);
241 static int memcached_config (const char *key, const char *value) /* {{{ */
243 if (strcasecmp (key, "Socket") == 0) {
244 if (memcached_socket != NULL) {
245 free (memcached_socket);
247 memcached_socket = strdup (value);
248 } else if (strcasecmp (key, "Host") == 0) {
249 if (memcached_host != NULL) {
250 free (memcached_host);
252 memcached_host = strdup (value);
253 } else if (strcasecmp (key, "Port") == 0) {
254 int port = (int) (atof (value));
255 if ((port > 0) && (port <= 65535)) {
256 ssnprintf (memcached_port, sizeof (memcached_port), "%i", port);
258 sstrncpy (memcached_port, value, sizeof (memcached_port));
268 static void submit_counter (const char *type, const char *type_inst,
269 counter_t value) /* {{{ */
272 value_list_t vl = VALUE_LIST_INIT;
274 values[0].counter = value;
278 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
279 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
280 sstrncpy (vl.type, type, sizeof (vl.type));
281 if (type_inst != NULL)
282 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
284 plugin_dispatch_values (&vl);
285 } /* void memcached_submit_cmd */
288 static void submit_counter2 (const char *type, const char *type_inst,
289 counter_t value0, counter_t value1) /* {{{ */
292 value_list_t vl = VALUE_LIST_INIT;
294 values[0].counter = value0;
295 values[1].counter = value1;
299 vl.time = time (NULL);
300 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
301 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
302 sstrncpy (vl.type, type, sizeof (vl.type));
303 if (type_inst != NULL)
304 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
306 plugin_dispatch_values (&vl);
307 } /* void memcached_submit_cmd */
310 static void submit_gauge (const char *type, const char *type_inst,
311 gauge_t value) /* {{{ */
314 value_list_t vl = VALUE_LIST_INIT;
316 values[0].gauge = value;
320 vl.time = time (NULL);
321 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
322 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
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);
331 static void submit_gauge2 (const char *type, const char *type_inst,
332 gauge_t value0, gauge_t value1) /* {{{ */
335 value_list_t vl = VALUE_LIST_INIT;
337 values[0].gauge = value0;
338 values[1].gauge = value1;
342 vl.time = time (NULL);
343 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
344 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
345 sstrncpy (vl.type, type, sizeof (vl.type));
346 if (type_inst != NULL)
347 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
349 plugin_dispatch_values (&vl);
353 static int memcached_read (void) /* {{{ */
362 gauge_t bytes_used = NAN;
363 gauge_t bytes_total = NAN;
366 counter_t rusage_user = 0;
367 counter_t rusage_syst = 0;
368 counter_t octets_rx = 0;
369 counter_t octets_tx = 0;
371 /* get data from daemon */
372 if (memcached_query_daemon (buf, sizeof (buf)) < 0) {
376 #define FIELD_IS(cnst) \
377 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
381 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
387 fields_num = strsplit(line, fields, 3);
391 name_len = strlen(fields[1]);
396 * For an explanation on these fields please refer to
397 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
401 * CPU time consumed by the memcached process
403 if (FIELD_IS ("rusage_user"))
405 rusage_user = atoll (fields[2]);
407 else if (FIELD_IS ("rusage_system"))
409 rusage_syst = atoll(fields[2]);
413 * Number of threads of this instance
415 else if (FIELD_IS ("threads"))
417 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]));
421 * Number of items stored
423 else if (FIELD_IS ("curr_items"))
425 submit_gauge ("memcached_items", "current", atof (fields[2]));
429 * Number of bytes used and available (total - used)
431 else if (FIELD_IS ("bytes"))
433 bytes_used = atof (fields[2]);
435 else if (FIELD_IS ("limit_maxbytes"))
437 bytes_total = atof(fields[2]);
443 else if (FIELD_IS ("curr_connections"))
445 submit_gauge ("memcached_connections", "current", atof (fields[2]));
451 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
453 const char *name = fields[1] + 4;
454 submit_counter ("memcached_command", name, atoll (fields[2]));
455 if (strcmp (name, "get") == 0)
456 gets = atof (fields[2]);
460 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
462 else if (FIELD_IS ("get_hits"))
464 submit_counter ("memcached_ops", "hits", atoll (fields[2]));
465 hits = atof (fields[2]);
467 else if (FIELD_IS ("get_misses"))
469 submit_counter ("memcached_ops", "misses", atoll (fields[2]));
471 else if (FIELD_IS ("evictions"))
473 submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
479 else if (FIELD_IS ("bytes_read"))
481 octets_rx = atoll (fields[2]);
483 else if (FIELD_IS ("bytes_written"))
485 octets_tx = atoll (fields[2]);
487 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
489 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
490 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
492 if ((rusage_user != 0) || (rusage_syst != 0))
493 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
495 if ((octets_rx != 0) || (octets_tx != 0))
496 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
498 if (!isnan (gets) && !isnan (hits))
503 rate = 100.0 * hits / gets;
505 submit_gauge ("percent", "hitratio", rate);
512 void module_register (void) /* {{{ */
514 plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
515 plugin_register_read ("memcached", memcached_read);
524 * vim600: sw=4 ts=4 fdm=marker noexpandtab
525 * vim<600: sw=4 ts=4 noexpandtab