2 * collectd - src/memcached.c, based on src/hddtemp.c
3 * Copyright (C) 2007 Antony Dovgal
4 * Copyright (C) 2005,2006 Vincent Stehlé
5 * Copyright (C) 2009 Franck Lombardi
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Antony Dovgal <tony at daylessday dot org>
23 * Vincent Stehlé <vincent.stehle at free.fr>
24 * Florian octo Forster <octo at verplant.org>
31 #include "configfile.h"
35 # include <sys/socket.h>
37 # include <netinet/in.h>
38 # include <netinet/tcp.h>
40 #define MEMCACHED_DEF_HOST "127.0.0.1"
41 #define MEMCACHED_DEF_PORT "11211"
43 #define MEMCACHED_RETRY_COUNT 100
45 static const char *config_keys[] =
51 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
53 static char *memcached_socket = NULL;
54 static char *memcached_host = NULL;
55 static char memcached_port[16];
57 static int memcached_query_daemon (char *buffer, int buffer_size) /* {{{ */
64 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 strncpy(serv_addr.sun_path, memcached_socket, sizeof (serv_addr.sun_path));
72 /* create our socket descriptor */
73 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
75 ERROR ("memcached: unix socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
79 /* connect to the memcached daemon */
80 if (connect (fd, (struct sockaddr *) &serv_addr, SUN_LEN(&serv_addr))) {
81 shutdown(fd, SHUT_RDWR);
91 struct addrinfo ai_hints;
92 struct addrinfo *ai_list, *ai_ptr;
95 memset (&ai_hints, '\0', sizeof (ai_hints));
96 ai_hints.ai_flags = 0;
98 /* ai_hints.ai_flags |= AI_ADDRCONFIG; */
100 ai_hints.ai_family = AF_INET;
101 ai_hints.ai_socktype = SOCK_STREAM;
102 ai_hints.ai_protocol = 0;
104 host = memcached_host;
106 host = MEMCACHED_DEF_HOST;
109 port = memcached_port;
110 if (strlen (port) == 0) {
111 port = MEMCACHED_DEF_PORT;
114 if ((ai_return = getaddrinfo (host, port, NULL, &ai_list)) != 0) {
116 ERROR ("memcached: getaddrinfo (%s, %s): %s",
118 (ai_return == EAI_SYSTEM)
119 ? sstrerror (errno, errbuf, sizeof (errbuf))
120 : gai_strerror (ai_return));
125 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
126 /* create our socket descriptor */
127 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0) {
129 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
133 /* connect to the memcached daemon */
134 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
135 shutdown(fd, SHUT_RDWR);
141 /* A socket could be opened and connecting succeeded. We're
146 freeaddrinfo (ai_list);
150 ERROR ("memcached: Could not connect to daemon.");
154 if (send(fd, "stats\r\n", sizeof("stats\r\n") - 1, MSG_DONTWAIT) != (sizeof("stats\r\n") - 1)) {
155 ERROR ("memcached: Could not send command to the memcached daemon.");
163 memset (&p, 0, sizeof (p));
165 p.events = POLLIN | POLLERR | POLLHUP;
168 status = poll (&p, /* nfds = */ 1, /* timeout = */ 1000 * interval_g);
173 ERROR ("memcached: poll(2) timed out after %i seconds.", interval_g);
178 ERROR ("memcached: poll(2) failed: %s",
179 sstrerror (errno, errbuf, sizeof (errbuf)));
181 shutdown (fd, SHUT_RDWR);
187 /* receive data from the memcached daemon */
188 memset (buffer, '\0', buffer_size);
191 while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
192 if (i > MEMCACHED_RETRY_COUNT) {
193 ERROR("recv() timed out");
201 if (errno == EAGAIN) {
205 ERROR ("memcached: Error reading from socket: %s",
206 sstrerror (errno, errbuf, sizeof (errbuf)));
207 shutdown(fd, SHUT_RDWR);
211 buffer_fill += status;
213 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
214 /* we got all the data */
219 if (buffer_fill >= buffer_size) {
220 buffer[buffer_size - 1] = '\0';
221 WARNING ("memcached: Message from memcached has been truncated.");
222 } else if (buffer_fill == 0) {
223 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
224 "Buffer: `%s'", buffer);
225 shutdown(fd, SHUT_RDWR);
230 shutdown(fd, SHUT_RDWR);
236 static int memcached_config (const char *key, const char *value) /* {{{ */
238 if (strcasecmp (key, "Socket") == 0) {
239 if (memcached_socket != NULL) {
240 free (memcached_socket);
242 memcached_socket = strdup (value);
243 } else if (strcasecmp (key, "Host") == 0) {
244 if (memcached_host != NULL) {
245 free (memcached_host);
247 memcached_host = strdup (value);
248 } else if (strcasecmp (key, "Port") == 0) {
249 int port = (int) (atof (value));
250 if ((port > 0) && (port <= 65535)) {
251 ssnprintf (memcached_port, sizeof (memcached_port), "%i", port);
253 sstrncpy (memcached_port, value, sizeof (memcached_port));
263 static void submit_counter (const char *type, const char *type_inst,
264 counter_t value) /* {{{ */
267 value_list_t vl = VALUE_LIST_INIT;
269 values[0].counter = value;
273 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
274 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
275 sstrncpy (vl.type, type, sizeof (vl.type));
276 if (type_inst != NULL)
277 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
279 plugin_dispatch_values (&vl);
280 } /* void memcached_submit_cmd */
283 static void submit_counter2 (const char *type, const char *type_inst,
284 counter_t value0, counter_t value1) /* {{{ */
287 value_list_t vl = VALUE_LIST_INIT;
289 values[0].counter = value0;
290 values[1].counter = value1;
294 vl.time = time (NULL);
295 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
296 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
297 sstrncpy (vl.type, type, sizeof (vl.type));
298 if (type_inst != NULL)
299 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
301 plugin_dispatch_values (&vl);
302 } /* void memcached_submit_cmd */
305 static void submit_gauge (const char *type, const char *type_inst,
306 gauge_t value) /* {{{ */
309 value_list_t vl = VALUE_LIST_INIT;
311 values[0].gauge = value;
315 vl.time = time (NULL);
316 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
317 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
318 sstrncpy (vl.type, type, sizeof (vl.type));
319 if (type_inst != NULL)
320 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
322 plugin_dispatch_values (&vl);
326 static void submit_gauge2 (const char *type, const char *type_inst,
327 gauge_t value0, gauge_t value1) /* {{{ */
330 value_list_t vl = VALUE_LIST_INIT;
332 values[0].gauge = value0;
333 values[1].gauge = value1;
337 vl.time = time (NULL);
338 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
339 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
340 sstrncpy (vl.type, type, sizeof (vl.type));
341 if (type_inst != NULL)
342 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
344 plugin_dispatch_values (&vl);
348 static int memcached_read (void) /* {{{ */
357 gauge_t bytes_used = NAN;
358 gauge_t bytes_total = NAN;
361 counter_t rusage_user = 0;
362 counter_t rusage_syst = 0;
363 counter_t octets_rx = 0;
364 counter_t octets_tx = 0;
366 /* get data from daemon */
367 if (memcached_query_daemon (buf, sizeof (buf)) < 0) {
371 #define FIELD_IS(cnst) \
372 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
376 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
382 fields_num = strsplit(line, fields, 3);
386 name_len = strlen(fields[1]);
391 * For an explanation on these fields please refer to
392 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
396 * CPU time consumed by the memcached process
398 if (FIELD_IS ("rusage_user"))
400 rusage_user = atoll (fields[2]);
402 else if (FIELD_IS ("rusage_system"))
404 rusage_syst = atoll(fields[2]);
408 * Number of threads of this instance
410 else if (FIELD_IS ("threads"))
412 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]));
416 * Number of items stored
418 else if (FIELD_IS ("curr_items"))
420 submit_gauge ("memcached_items", "current", atof (fields[2]));
423 else if (FIELD_IS ("total_items"))
425 total_items = atoll(fields[2]);
430 * Number of bytes used and available (total - used)
432 else if (FIELD_IS ("bytes"))
434 bytes_used = atof (fields[2]);
436 else if (FIELD_IS ("limit_maxbytes"))
438 bytes_total = atof(fields[2]);
444 else if (FIELD_IS ("curr_connections"))
446 submit_gauge ("memcached_connections", "current", atof (fields[2]));
449 else if (FIELD_IS("total_connections"))
451 total_connections = atoll(fields[2]);
456 * ``Number of connection structures allocated by the server''
457 else if (FIELD_IS ("connection_structures"))
459 connection_structures = atof(fields[2]);
466 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
468 const char *name = fields[1] + 4;
469 submit_counter ("memcached_command", name, atoll (fields[2]));
470 if (strcmp (name, "get") == 0)
471 gets = atof (fields[2]);
475 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
477 else if (FIELD_IS ("get_hits"))
479 submit_counter ("memcached_ops", "hits", atoll (fields[2]));
480 hits = atof (fields[2]);
482 else if (FIELD_IS ("get_misses"))
484 submit_counter ("memcached_ops", "misses", atoll (fields[2]));
486 else if (FIELD_IS ("evictions"))
488 submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
494 else if (FIELD_IS ("bytes_read"))
496 octets_rx = atoll (fields[2]);
498 else if (FIELD_IS ("bytes_written"))
500 octets_tx = atoll (fields[2]);
502 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
504 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
505 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
507 if ((rusage_user != 0) || (rusage_syst != 0))
508 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
510 if ((octets_rx != 0) || (octets_tx != 0))
511 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
513 if (!isnan (gets) && !isnan (hits))
518 rate = 100.0 * hits / gets;
520 submit_gauge ("percent", "hitratio", rate);
527 void module_register (void) /* {{{ */
529 plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
530 plugin_register_read ("memcached", memcached_read);
539 * vim600: sw=4 ts=4 fdm=marker