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>
34 # include <netinet/in.h>
35 # include <netinet/tcp.h>
37 #define MEMCACHED_DEF_HOST "127.0.0.1"
38 #define MEMCACHED_DEF_PORT "11211"
40 #define MEMCACHED_RETRY_COUNT 100
42 static const char *config_keys[] =
47 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
49 static char *memcached_host = NULL;
50 static char memcached_port[16];
52 static int memcached_query_daemon (char *buffer, int buffer_size) /* {{{ */
61 struct addrinfo ai_hints;
62 struct addrinfo *ai_list, *ai_ptr;
65 memset (&ai_hints, '\0', sizeof (ai_hints));
66 ai_hints.ai_flags = 0;
68 /* ai_hints.ai_flags |= AI_ADDRCONFIG; */
70 ai_hints.ai_family = AF_INET;
71 ai_hints.ai_socktype = SOCK_STREAM;
72 ai_hints.ai_protocol = 0;
74 host = memcached_host;
76 host = MEMCACHED_DEF_HOST;
79 port = memcached_port;
80 if (strlen (port) == 0) {
81 port = MEMCACHED_DEF_PORT;
84 if ((ai_return = getaddrinfo (host, port, NULL, &ai_list)) != 0) {
86 ERROR ("memcached: getaddrinfo (%s, %s): %s",
88 (ai_return == EAI_SYSTEM)
89 ? sstrerror (errno, errbuf, sizeof (errbuf))
90 : gai_strerror (ai_return));
95 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
96 /* create our socket descriptor */
97 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0) {
99 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
103 /* connect to the memcached daemon */
104 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
105 shutdown(fd, SHUT_RDWR);
111 /* A socket could be opened and connecting succeeded. We're
116 freeaddrinfo (ai_list);
119 ERROR ("memcached: Could not connect to daemon.");
123 if (send(fd, "stats\r\n", sizeof("stats\r\n") - 1, MSG_DONTWAIT) != (sizeof("stats\r\n") - 1)) {
124 ERROR ("memcached: Could not send command to the memcached daemon.");
132 memset (&p, 0, sizeof (p));
134 p.events = POLLIN | POLLERR | POLLHUP;
137 status = poll (&p, /* nfds = */ 1, /* timeout = */ 1000 * interval_g);
142 ERROR ("memcached: poll(2) timed out after %i seconds.", interval_g);
147 ERROR ("memcached: poll(2) failed: %s",
148 sstrerror (errno, errbuf, sizeof (errbuf)));
150 shutdown (fd, SHUT_RDWR);
156 /* receive data from the memcached daemon */
157 memset (buffer, '\0', buffer_size);
160 while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
161 if (i > MEMCACHED_RETRY_COUNT) {
162 ERROR("recv() timed out");
170 if (errno == EAGAIN) {
174 ERROR ("memcached: Error reading from socket: %s",
175 sstrerror (errno, errbuf, sizeof (errbuf)));
176 shutdown(fd, SHUT_RDWR);
180 buffer_fill += status;
182 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
183 /* we got all the data */
188 if (buffer_fill >= buffer_size) {
189 buffer[buffer_size - 1] = '\0';
190 WARNING ("memcached: Message from memcached has been truncated.");
191 } else if (buffer_fill == 0) {
192 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
193 "Buffer: `%s'", buffer);
194 shutdown(fd, SHUT_RDWR);
199 shutdown(fd, SHUT_RDWR);
205 static int memcached_config (const char *key, const char *value) /* {{{ */
207 if (strcasecmp (key, "Host") == 0) {
208 if (memcached_host != NULL) {
209 free (memcached_host);
211 memcached_host = strdup (value);
212 } else if (strcasecmp (key, "Port") == 0) {
213 int port = (int) (atof (value));
214 if ((port > 0) && (port <= 65535)) {
215 snprintf (memcached_port, sizeof (memcached_port), "%i", port);
217 strncpy (memcached_port, value, sizeof (memcached_port));
219 memcached_port[sizeof (memcached_port) - 1] = '\0';
228 static void submit_counter (const char *type, const char *type_inst,
229 counter_t value) /* {{{ */
232 value_list_t vl = VALUE_LIST_INIT;
234 values[0].counter = value;
238 vl.time = time (NULL);
239 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
240 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
241 if (type_inst != NULL)
243 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
244 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
247 plugin_dispatch_values (type, &vl);
248 } /* void memcached_submit_cmd */
251 static void submit_counter2 (const char *type, const char *type_inst,
252 counter_t value0, counter_t value1) /* {{{ */
255 value_list_t vl = VALUE_LIST_INIT;
257 values[0].counter = value0;
258 values[1].counter = value1;
262 vl.time = time (NULL);
263 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
264 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
265 if (type_inst != NULL)
267 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
268 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
271 plugin_dispatch_values (type, &vl);
272 } /* void memcached_submit_cmd */
275 static void submit_gauge (const char *type, const char *type_inst,
276 gauge_t value) /* {{{ */
279 value_list_t vl = VALUE_LIST_INIT;
281 values[0].gauge = value;
285 vl.time = time (NULL);
286 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
287 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
288 if (type_inst != NULL)
290 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
291 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
294 plugin_dispatch_values (type, &vl);
298 static void submit_gauge2 (const char *type, const char *type_inst,
299 gauge_t value0, gauge_t value1) /* {{{ */
302 value_list_t vl = VALUE_LIST_INIT;
304 values[0].gauge = value0;
305 values[1].gauge = value1;
309 vl.time = time (NULL);
310 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
311 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
312 if (type_inst != NULL)
314 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
315 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
318 plugin_dispatch_values (type, &vl);
322 static int memcached_read (void) /* {{{ */
331 gauge_t bytes_used = NAN;
332 gauge_t bytes_total = NAN;
333 counter_t rusage_user = 0;
334 counter_t rusage_syst = 0;
335 counter_t octets_rx = 0;
336 counter_t octets_tx = 0;
338 /* get data from daemon */
339 if (memcached_query_daemon (buf, sizeof (buf)) < 0) {
343 #define FIELD_IS(cnst) \
344 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
348 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
354 fields_num = strsplit(line, fields, 3);
358 name_len = strlen(fields[1]);
363 * For an explanation on these fields please refer to
364 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
368 * CPU time consumed by the memcached process
370 if (FIELD_IS ("rusage_user"))
372 rusage_user = atoll (fields[2]);
374 else if (FIELD_IS ("rusage_system"))
376 rusage_syst = atoll(fields[2]);
380 * Number of threads of this instance
382 else if (FIELD_IS ("threads"))
384 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]));
388 * Number of items stored
390 else if (FIELD_IS ("curr_items"))
392 submit_gauge ("memcached_items", "current", atof (fields[2]));
395 else if (FIELD_IS ("total_items"))
397 total_items = atoll(fields[2]);
402 * Number of bytes used and available (total - used)
404 else if (FIELD_IS ("bytes"))
406 bytes_used = atof (fields[2]);
408 else if (FIELD_IS ("limit_maxbytes"))
410 bytes_total = atof(fields[2]);
416 else if (FIELD_IS ("curr_connections"))
418 submit_gauge ("memcached_connections", "current", atof (fields[2]));
421 else if (FIELD_IS("total_connections"))
423 total_connections = atoll(fields[2]);
428 * ``Number of connection structures allocated by the server''
429 else if (FIELD_IS ("connection_structures"))
431 connection_structures = atof(fields[2]);
438 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
440 const char *name = fields[1] + 4;
441 submit_counter ("memcached_command", name, atoll (fields[2]));
445 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
447 else if (FIELD_IS ("get_hits"))
449 submit_counter ("memcached_ops", "hits", atoll (fields[2]));
451 else if (FIELD_IS ("get_misses"))
453 submit_counter ("memcached_ops", "misses", atoll (fields[2]));
455 else if (FIELD_IS ("evictions"))
457 submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
463 else if (FIELD_IS ("bytes_read"))
465 octets_rx = atoll (fields[2]);
467 else if (FIELD_IS ("bytes_written"))
469 octets_tx = atoll (fields[2]);
471 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
473 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
474 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
476 if ((rusage_user != 0) || (rusage_syst != 0))
477 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
479 if ((octets_rx != 0) || (octets_tx != 0))
480 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
486 void module_register (void) /* {{{ */
488 plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
489 plugin_register_read ("memcached", memcached_read);
498 * vim600: sw=4 ts=4 fdm=marker