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) {
65 struct sockaddr_un serv_addr;
67 memset (&serv_addr, 0, sizeof (serv_addr));
68 serv_addr.sun_family = AF_UNIX;
69 sstrncpy (serv_addr.sun_path, memcached_socket, sizeof (serv_addr.sun_path));
71 /* create our socket descriptor */
72 fd = socket (AF_UNIX, SOCK_STREAM, 0);
75 ERROR ("memcached: unix socket: %s", sstrerror (errno, errbuf,
80 /* connect to the memcached daemon */
81 status = (ssize_t) connect (fd, (struct sockaddr *) &serv_addr,
82 SUN_LEN (&serv_addr));
84 shutdown (fd, SHUT_RDWR);
89 else { /* if (memcached_socket == NULL) */
93 struct addrinfo ai_hints;
94 struct addrinfo *ai_list, *ai_ptr;
97 memset (&ai_hints, '\0', sizeof (ai_hints));
98 ai_hints.ai_flags = 0;
100 /* ai_hints.ai_flags |= AI_ADDRCONFIG; */
102 ai_hints.ai_family = AF_INET;
103 ai_hints.ai_socktype = SOCK_STREAM;
104 ai_hints.ai_protocol = 0;
106 host = memcached_host;
108 host = MEMCACHED_DEF_HOST;
111 port = memcached_port;
112 if (strlen (port) == 0) {
113 port = MEMCACHED_DEF_PORT;
116 if ((ai_return = getaddrinfo (host, port, NULL, &ai_list)) != 0) {
118 ERROR ("memcached: getaddrinfo (%s, %s): %s",
120 (ai_return == EAI_SYSTEM)
121 ? sstrerror (errno, errbuf, sizeof (errbuf))
122 : gai_strerror (ai_return));
127 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
128 /* create our socket descriptor */
129 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
132 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
136 /* connect to the memcached daemon */
137 status = (ssize_t) connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen);
139 shutdown (fd, SHUT_RDWR);
145 /* A socket could be opened and connecting succeeded. We're
150 freeaddrinfo (ai_list);
154 ERROR ("memcached: Could not connect to daemon.");
158 if (send(fd, "stats\r\n", sizeof("stats\r\n") - 1, MSG_DONTWAIT) != (sizeof("stats\r\n") - 1)) {
159 ERROR ("memcached: Could not send command to the memcached daemon.");
167 memset (&p, 0, sizeof (p));
169 p.events = POLLIN | POLLERR | POLLHUP;
172 status = poll (&p, /* nfds = */ 1, /* timeout = */ 1000 * interval_g);
177 ERROR ("memcached: poll(2) timed out after %i seconds.", interval_g);
182 ERROR ("memcached: poll(2) failed: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
185 shutdown (fd, SHUT_RDWR);
191 /* receive data from the memcached daemon */
192 memset (buffer, '\0', buffer_size);
195 while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
196 if (i > MEMCACHED_RETRY_COUNT) {
197 ERROR("recv() timed out");
205 if (errno == EAGAIN) {
209 ERROR ("memcached: Error reading from socket: %s",
210 sstrerror (errno, errbuf, sizeof (errbuf)));
211 shutdown(fd, SHUT_RDWR);
215 buffer_fill += status;
217 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
218 /* we got all the data */
223 if (buffer_fill >= buffer_size) {
224 buffer[buffer_size - 1] = '\0';
225 WARNING ("memcached: Message from memcached has been truncated.");
226 } else if (buffer_fill == 0) {
227 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
228 "Buffer: `%s'", buffer);
229 shutdown(fd, SHUT_RDWR);
234 shutdown(fd, SHUT_RDWR);
240 static int memcached_config (const char *key, const char *value) /* {{{ */
242 if (strcasecmp (key, "Socket") == 0) {
243 if (memcached_socket != NULL) {
244 free (memcached_socket);
246 memcached_socket = strdup (value);
247 } else if (strcasecmp (key, "Host") == 0) {
248 if (memcached_host != NULL) {
249 free (memcached_host);
251 memcached_host = strdup (value);
252 } else if (strcasecmp (key, "Port") == 0) {
253 int port = (int) (atof (value));
254 if ((port > 0) && (port <= 65535)) {
255 ssnprintf (memcached_port, sizeof (memcached_port), "%i", port);
257 sstrncpy (memcached_port, value, sizeof (memcached_port));
267 static void submit_counter (const char *type, const char *type_inst,
268 counter_t value) /* {{{ */
271 value_list_t vl = VALUE_LIST_INIT;
273 values[0].counter = value;
277 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
278 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
279 sstrncpy (vl.type, type, sizeof (vl.type));
280 if (type_inst != NULL)
281 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
283 plugin_dispatch_values (&vl);
284 } /* void memcached_submit_cmd */
287 static void submit_counter2 (const char *type, const char *type_inst,
288 counter_t value0, counter_t value1) /* {{{ */
291 value_list_t vl = VALUE_LIST_INIT;
293 values[0].counter = value0;
294 values[1].counter = value1;
298 vl.time = time (NULL);
299 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
300 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
301 sstrncpy (vl.type, type, sizeof (vl.type));
302 if (type_inst != NULL)
303 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
305 plugin_dispatch_values (&vl);
306 } /* void memcached_submit_cmd */
309 static void submit_gauge (const char *type, const char *type_inst,
310 gauge_t value) /* {{{ */
313 value_list_t vl = VALUE_LIST_INIT;
315 values[0].gauge = value;
319 vl.time = time (NULL);
320 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
321 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
322 sstrncpy (vl.type, type, sizeof (vl.type));
323 if (type_inst != NULL)
324 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
326 plugin_dispatch_values (&vl);
330 static void submit_gauge2 (const char *type, const char *type_inst,
331 gauge_t value0, gauge_t value1) /* {{{ */
334 value_list_t vl = VALUE_LIST_INIT;
336 values[0].gauge = value0;
337 values[1].gauge = value1;
341 vl.time = time (NULL);
342 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
343 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
344 sstrncpy (vl.type, type, sizeof (vl.type));
345 if (type_inst != NULL)
346 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
348 plugin_dispatch_values (&vl);
352 static int memcached_read (void) /* {{{ */
361 gauge_t bytes_used = NAN;
362 gauge_t bytes_total = NAN;
365 counter_t rusage_user = 0;
366 counter_t rusage_syst = 0;
367 counter_t octets_rx = 0;
368 counter_t octets_tx = 0;
370 /* get data from daemon */
371 if (memcached_query_daemon (buf, sizeof (buf)) < 0) {
375 #define FIELD_IS(cnst) \
376 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
380 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
386 fields_num = strsplit(line, fields, 3);
390 name_len = strlen(fields[1]);
395 * For an explanation on these fields please refer to
396 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
400 * CPU time consumed by the memcached process
402 if (FIELD_IS ("rusage_user"))
404 rusage_user = atoll (fields[2]);
406 else if (FIELD_IS ("rusage_system"))
408 rusage_syst = atoll(fields[2]);
412 * Number of threads of this instance
414 else if (FIELD_IS ("threads"))
416 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]));
420 * Number of items stored
422 else if (FIELD_IS ("curr_items"))
424 submit_gauge ("memcached_items", "current", atof (fields[2]));
428 * Number of bytes used and available (total - used)
430 else if (FIELD_IS ("bytes"))
432 bytes_used = atof (fields[2]);
434 else if (FIELD_IS ("limit_maxbytes"))
436 bytes_total = atof(fields[2]);
442 else if (FIELD_IS ("curr_connections"))
444 submit_gauge ("memcached_connections", "current", atof (fields[2]));
450 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
452 const char *name = fields[1] + 4;
453 submit_counter ("memcached_command", name, atoll (fields[2]));
454 if (strcmp (name, "get") == 0)
455 gets = atof (fields[2]);
459 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
461 else if (FIELD_IS ("get_hits"))
463 submit_counter ("memcached_ops", "hits", atoll (fields[2]));
464 hits = atof (fields[2]);
466 else if (FIELD_IS ("get_misses"))
468 submit_counter ("memcached_ops", "misses", atoll (fields[2]));
470 else if (FIELD_IS ("evictions"))
472 submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
478 else if (FIELD_IS ("bytes_read"))
480 octets_rx = atoll (fields[2]);
482 else if (FIELD_IS ("bytes_written"))
484 octets_tx = atoll (fields[2]);
486 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
488 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
489 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
491 if ((rusage_user != 0) || (rusage_syst != 0))
492 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
494 if ((octets_rx != 0) || (octets_tx != 0))
495 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
497 if (!isnan (gets) && !isnan (hits))
502 rate = 100.0 * hits / gets;
504 submit_gauge ("percent", "hitratio", rate);
511 void module_register (void) /* {{{ */
513 plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
514 plugin_register_read ("memcached", memcached_read);
523 * vim600: sw=4 ts=4 fdm=marker noexpandtab
524 * vim<600: sw=4 ts=4 noexpandtab