2 * collectd - src/memcached.c, based on src/hddtemp.c
3 * Copyright (C) 2007 Antony Dovgal
4 * Copyright (C) 2005,2006 Vincent Stehlé
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Antony Dovgal <tony at daylessday dot org>
22 * Vincent Stehlé <vincent.stehle at free.fr>
23 * Florian octo Forster <octo at verplant.org>
29 #include "configfile.h"
33 # include <sys/socket.h>
35 # include <netinet/in.h>
36 # include <netinet/tcp.h>
38 #define MEMCACHED_DEF_HOST "127.0.0.1"
39 #define MEMCACHED_DEF_PORT "11211"
41 #define MEMCACHED_RETRY_COUNT 100
43 static const char *config_keys[] =
49 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
51 static char *memcached_socket = NULL;
52 static char *memcached_host = NULL;
53 static char memcached_port[16];
55 static int memcached_query_daemon (char *buffer, int buffer_size) /* {{{ */
62 if (memcached_socket != NULL) {
64 struct sockaddr_un serv_addr;
66 memset(&serv_addr, '\0', sizeof (serv_addr));
67 serv_addr.sun_family = AF_UNIX;
68 strncpy(serv_addr.sun_path, memcached_socket, sizeof (serv_addr.sun_path));
70 /* create our socket descriptor */
71 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
73 ERROR ("memcached: unix socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
77 /* connect to the memcached daemon */
78 if (connect (fd, (struct sockaddr *) &serv_addr, SUN_LEN(&serv_addr))) {
79 shutdown(fd, SHUT_RDWR);
89 struct addrinfo ai_hints;
90 struct addrinfo *ai_list, *ai_ptr;
93 memset (&ai_hints, '\0', sizeof (ai_hints));
94 ai_hints.ai_flags = 0;
96 /* ai_hints.ai_flags |= AI_ADDRCONFIG; */
98 ai_hints.ai_family = AF_INET;
99 ai_hints.ai_socktype = SOCK_STREAM;
100 ai_hints.ai_protocol = 0;
102 host = memcached_host;
104 host = MEMCACHED_DEF_HOST;
107 port = memcached_port;
108 if (strlen (port) == 0) {
109 port = MEMCACHED_DEF_PORT;
112 if ((ai_return = getaddrinfo (host, port, NULL, &ai_list)) != 0) {
114 ERROR ("memcached: getaddrinfo (%s, %s): %s",
116 (ai_return == EAI_SYSTEM)
117 ? sstrerror (errno, errbuf, sizeof (errbuf))
118 : gai_strerror (ai_return));
123 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
124 /* create our socket descriptor */
125 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0) {
127 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
131 /* connect to the memcached daemon */
132 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
133 shutdown(fd, SHUT_RDWR);
139 /* A socket could be opened and connecting succeeded. We're
144 freeaddrinfo (ai_list);
148 ERROR ("memcached: Could not connect to daemon.");
152 if (send(fd, "stats\r\n", sizeof("stats\r\n") - 1, MSG_DONTWAIT) != (sizeof("stats\r\n") - 1)) {
153 ERROR ("memcached: Could not send command to the memcached daemon.");
161 memset (&p, 0, sizeof (p));
163 p.events = POLLIN | POLLERR | POLLHUP;
166 status = poll (&p, /* nfds = */ 1, /* timeout = */ 1000 * interval_g);
171 ERROR ("memcached: poll(2) timed out after %i seconds.", interval_g);
176 ERROR ("memcached: poll(2) failed: %s",
177 sstrerror (errno, errbuf, sizeof (errbuf)));
179 shutdown (fd, SHUT_RDWR);
185 /* receive data from the memcached daemon */
186 memset (buffer, '\0', buffer_size);
189 while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
190 if (i > MEMCACHED_RETRY_COUNT) {
191 ERROR("recv() timed out");
199 if (errno == EAGAIN) {
203 ERROR ("memcached: Error reading from socket: %s",
204 sstrerror (errno, errbuf, sizeof (errbuf)));
205 shutdown(fd, SHUT_RDWR);
209 buffer_fill += status;
211 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
212 /* we got all the data */
217 if (buffer_fill >= buffer_size) {
218 buffer[buffer_size - 1] = '\0';
219 WARNING ("memcached: Message from memcached has been truncated.");
220 } else if (buffer_fill == 0) {
221 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
222 "Buffer: `%s'", buffer);
223 shutdown(fd, SHUT_RDWR);
228 shutdown(fd, SHUT_RDWR);
234 static int memcached_config (const char *key, const char *value) /* {{{ */
236 if (strcasecmp (key, "Socket") == 0) {
237 if (memcached_socket != NULL) {
238 free (memcached_socket);
240 memcached_socket = strdup (value);
241 } else if (strcasecmp (key, "Host") == 0) {
242 if (memcached_host != NULL) {
243 free (memcached_host);
245 memcached_host = strdup (value);
246 } else if (strcasecmp (key, "Port") == 0) {
247 int port = (int) (atof (value));
248 if ((port > 0) && (port <= 65535)) {
249 ssnprintf (memcached_port, sizeof (memcached_port), "%i", port);
251 sstrncpy (memcached_port, value, sizeof (memcached_port));
261 static void submit_counter (const char *type, const char *type_inst,
262 counter_t value) /* {{{ */
265 value_list_t vl = VALUE_LIST_INIT;
267 values[0].counter = value;
271 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
272 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
273 sstrncpy (vl.type, type, sizeof (vl.type));
274 if (type_inst != NULL)
275 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
277 plugin_dispatch_values (&vl);
278 } /* void memcached_submit_cmd */
281 static void submit_counter2 (const char *type, const char *type_inst,
282 counter_t value0, counter_t value1) /* {{{ */
285 value_list_t vl = VALUE_LIST_INIT;
287 values[0].counter = value0;
288 values[1].counter = value1;
292 vl.time = time (NULL);
293 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
294 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
295 sstrncpy (vl.type, type, sizeof (vl.type));
296 if (type_inst != NULL)
297 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
299 plugin_dispatch_values (&vl);
300 } /* void memcached_submit_cmd */
303 static void submit_gauge (const char *type, const char *type_inst,
304 gauge_t value) /* {{{ */
307 value_list_t vl = VALUE_LIST_INIT;
309 values[0].gauge = value;
313 vl.time = time (NULL);
314 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
315 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
316 sstrncpy (vl.type, type, sizeof (vl.type));
317 if (type_inst != NULL)
318 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
320 plugin_dispatch_values (&vl);
324 static void submit_gauge2 (const char *type, const char *type_inst,
325 gauge_t value0, gauge_t value1) /* {{{ */
328 value_list_t vl = VALUE_LIST_INIT;
330 values[0].gauge = value0;
331 values[1].gauge = value1;
335 vl.time = time (NULL);
336 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
337 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
338 sstrncpy (vl.type, type, sizeof (vl.type));
339 if (type_inst != NULL)
340 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
342 plugin_dispatch_values (&vl);
346 static int memcached_read (void) /* {{{ */
355 gauge_t bytes_used = NAN;
356 gauge_t bytes_total = NAN;
359 counter_t rusage_user = 0;
360 counter_t rusage_syst = 0;
361 counter_t octets_rx = 0;
362 counter_t octets_tx = 0;
364 /* get data from daemon */
365 if (memcached_query_daemon (buf, sizeof (buf)) < 0) {
369 #define FIELD_IS(cnst) \
370 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
374 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
380 fields_num = strsplit(line, fields, 3);
384 name_len = strlen(fields[1]);
389 * For an explanation on these fields please refer to
390 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
394 * CPU time consumed by the memcached process
396 if (FIELD_IS ("rusage_user"))
398 rusage_user = atoll (fields[2]);
400 else if (FIELD_IS ("rusage_system"))
402 rusage_syst = atoll(fields[2]);
406 * Number of threads of this instance
408 else if (FIELD_IS ("threads"))
410 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]));
414 * Number of items stored
416 else if (FIELD_IS ("curr_items"))
418 submit_gauge ("memcached_items", "current", atof (fields[2]));
421 else if (FIELD_IS ("total_items"))
423 total_items = atoll(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]));
447 else if (FIELD_IS("total_connections"))
449 total_connections = atoll(fields[2]);
454 * ``Number of connection structures allocated by the server''
455 else if (FIELD_IS ("connection_structures"))
457 connection_structures = atof(fields[2]);
464 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
466 const char *name = fields[1] + 4;
467 submit_counter ("memcached_command", name, atoll (fields[2]));
468 if (strcmp (name, "get") == 0)
469 gets = atof (fields[2]);
473 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
475 else if (FIELD_IS ("get_hits"))
477 submit_counter ("memcached_ops", "hits", atoll (fields[2]));
478 hits = atof (fields[2]);
480 else if (FIELD_IS ("get_misses"))
482 submit_counter ("memcached_ops", "misses", atoll (fields[2]));
484 else if (FIELD_IS ("evictions"))
486 submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
492 else if (FIELD_IS ("bytes_read"))
494 octets_rx = atoll (fields[2]);
496 else if (FIELD_IS ("bytes_written"))
498 octets_tx = atoll (fields[2]);
500 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
502 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
503 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
505 if ((rusage_user != 0) || (rusage_syst != 0))
506 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
508 if ((octets_rx != 0) || (octets_tx != 0))
509 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
511 if (!isnan (gets) && !isnan (hits))
516 rate = 100.0 * hits / gets;
518 submit_gauge ("percent", "hitratio", rate);
525 void module_register (void) /* {{{ */
527 plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
528 plugin_register_read ("memcached", memcached_read);
537 * vim600: sw=4 ts=4 fdm=marker