1 /**
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
7 *
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.
12 *
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.
17 *
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
21 *
22 * Authors:
23 * Antony Dovgal <tony at daylessday dot org>
24 * Florian octo Forster <octo at verplant.org>
25 * Doug MacEachern <dougm at hyperic.com>
26 * Franck Lombardi
27 **/
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
34 # include <poll.h>
35 # include <netdb.h>
36 # include <sys/socket.h>
37 # include <sys/un.h>
38 # include <netinet/in.h>
39 # include <netinet/tcp.h>
41 /* Hack to work around the missing define in AIX */
42 #ifndef MSG_DONTWAIT
43 # define MSG_DONTWAIT MSG_NONBLOCK
44 #endif
46 #define MEMCACHED_DEF_HOST "127.0.0.1"
47 #define MEMCACHED_DEF_PORT "11211"
49 #define MEMCACHED_RETRY_COUNT 100
51 static const char *config_keys[] =
52 {
53 "Socket",
54 "Host",
55 "Port"
56 };
57 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
59 static char *memcached_socket = NULL;
60 static char *memcached_host = NULL;
61 static char memcached_port[16];
63 static int memcached_query_daemon (char *buffer, int buffer_size) /* {{{ */
64 {
65 int fd;
66 ssize_t status;
67 int buffer_fill;
68 int i = 0;
70 if (memcached_socket != NULL) {
71 struct sockaddr_un serv_addr;
73 memset (&serv_addr, 0, sizeof (serv_addr));
74 serv_addr.sun_family = AF_UNIX;
75 sstrncpy (serv_addr.sun_path, memcached_socket,
76 sizeof (serv_addr.sun_path));
78 /* create our socket descriptor */
79 fd = socket (AF_UNIX, SOCK_STREAM, 0);
80 if (fd < 0) {
81 char errbuf[1024];
82 ERROR ("memcached: unix socket: %s", sstrerror (errno, errbuf,
83 sizeof (errbuf)));
84 return -1;
85 }
87 /* connect to the memcached daemon */
88 status = (ssize_t) connect (fd, (struct sockaddr *) &serv_addr,
89 sizeof (serv_addr));
90 if (status != 0) {
91 shutdown (fd, SHUT_RDWR);
92 close (fd);
93 fd = -1;
94 }
95 }
96 else { /* if (memcached_socket == NULL) */
97 const char *host;
98 const char *port;
100 struct addrinfo ai_hints;
101 struct addrinfo *ai_list, *ai_ptr;
102 int ai_return = 0;
104 memset (&ai_hints, '\0', sizeof (ai_hints));
105 ai_hints.ai_flags = 0;
106 #ifdef AI_ADDRCONFIG
107 /* ai_hints.ai_flags |= AI_ADDRCONFIG; */
108 #endif
109 ai_hints.ai_family = AF_INET;
110 ai_hints.ai_socktype = SOCK_STREAM;
111 ai_hints.ai_protocol = 0;
113 host = memcached_host;
114 if (host == NULL) {
115 host = MEMCACHED_DEF_HOST;
116 }
118 port = memcached_port;
119 if (strlen (port) == 0) {
120 port = MEMCACHED_DEF_PORT;
121 }
123 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0) {
124 char errbuf[1024];
125 ERROR ("memcached: getaddrinfo (%s, %s): %s",
126 host, port,
127 (ai_return == EAI_SYSTEM)
128 ? sstrerror (errno, errbuf, sizeof (errbuf))
129 : gai_strerror (ai_return));
130 return -1;
131 }
133 fd = -1;
134 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
135 /* create our socket descriptor */
136 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
137 if (fd < 0) {
138 char errbuf[1024];
139 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
140 continue;
141 }
143 /* connect to the memcached daemon */
144 status = (ssize_t) connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen);
145 if (status != 0) {
146 shutdown (fd, SHUT_RDWR);
147 close (fd);
148 fd = -1;
149 continue;
150 }
152 /* A socket could be opened and connecting succeeded. We're
153 * done. */
154 break;
155 }
157 freeaddrinfo (ai_list);
158 }
160 if (fd < 0) {
161 ERROR ("memcached: Could not connect to daemon.");
162 return -1;
163 }
165 if (send(fd, "stats\r\n", sizeof("stats\r\n") - 1, MSG_DONTWAIT) != (sizeof("stats\r\n") - 1)) {
166 ERROR ("memcached: Could not send command to the memcached daemon.");
167 return -1;
168 }
170 {
171 struct pollfd p;
172 int status;
174 memset (&p, 0, sizeof (p));
175 p.fd = fd;
176 p.events = POLLIN | POLLERR | POLLHUP;
177 p.revents = 0;
179 status = poll (&p, /* nfds = */ 1, /* timeout = */ 1000 * interval_g);
180 if (status <= 0)
181 {
182 if (status == 0)
183 {
184 ERROR ("memcached: poll(2) timed out after %i seconds.", interval_g);
185 }
186 else
187 {
188 char errbuf[1024];
189 ERROR ("memcached: poll(2) failed: %s",
190 sstrerror (errno, errbuf, sizeof (errbuf)));
191 }
192 shutdown (fd, SHUT_RDWR);
193 close (fd);
194 return (-1);
195 }
196 }
198 /* receive data from the memcached daemon */
199 memset (buffer, '\0', buffer_size);
201 buffer_fill = 0;
202 while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
203 if (i > MEMCACHED_RETRY_COUNT) {
204 ERROR("recv() timed out");
205 break;
206 }
207 i++;
209 if (status == -1) {
210 char errbuf[1024];
212 if (errno == EAGAIN) {
213 continue;
214 }
216 ERROR ("memcached: Error reading from socket: %s",
217 sstrerror (errno, errbuf, sizeof (errbuf)));
218 shutdown(fd, SHUT_RDWR);
219 close (fd);
220 return -1;
221 }
222 buffer_fill += status;
224 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
225 /* we got all the data */
226 break;
227 }
228 }
230 if (buffer_fill >= buffer_size) {
231 buffer[buffer_size - 1] = '\0';
232 WARNING ("memcached: Message from memcached has been truncated.");
233 } else if (buffer_fill == 0) {
234 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
235 "Buffer: `%s'", buffer);
236 shutdown(fd, SHUT_RDWR);
237 close(fd);
238 return -1;
239 }
241 shutdown(fd, SHUT_RDWR);
242 close(fd);
243 return 0;
244 }
245 /* }}} */
247 static int memcached_config (const char *key, const char *value) /* {{{ */
248 {
249 if (strcasecmp (key, "Socket") == 0) {
250 if (memcached_socket != NULL) {
251 free (memcached_socket);
252 }
253 memcached_socket = strdup (value);
254 } else if (strcasecmp (key, "Host") == 0) {
255 if (memcached_host != NULL) {
256 free (memcached_host);
257 }
258 memcached_host = strdup (value);
259 } else if (strcasecmp (key, "Port") == 0) {
260 int port = (int) (atof (value));
261 if ((port > 0) && (port <= 65535)) {
262 ssnprintf (memcached_port, sizeof (memcached_port), "%i", port);
263 } else {
264 sstrncpy (memcached_port, value, sizeof (memcached_port));
265 }
266 } else {
267 return -1;
268 }
270 return 0;
271 }
272 /* }}} */
274 static void submit_counter (const char *type, const char *type_inst,
275 counter_t value) /* {{{ */
276 {
277 value_t values[1];
278 value_list_t vl = VALUE_LIST_INIT;
280 values[0].counter = value;
282 vl.values = values;
283 vl.values_len = 1;
284 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
285 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
286 sstrncpy (vl.type, type, sizeof (vl.type));
287 if (type_inst != NULL)
288 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
290 plugin_dispatch_values (&vl);
291 } /* void memcached_submit_cmd */
292 /* }}} */
294 static void submit_counter2 (const char *type, const char *type_inst,
295 counter_t value0, counter_t value1) /* {{{ */
296 {
297 value_t values[2];
298 value_list_t vl = VALUE_LIST_INIT;
300 values[0].counter = value0;
301 values[1].counter = value1;
303 vl.values = values;
304 vl.values_len = 2;
305 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
306 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
307 sstrncpy (vl.type, type, sizeof (vl.type));
308 if (type_inst != NULL)
309 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
311 plugin_dispatch_values (&vl);
312 } /* void memcached_submit_cmd */
313 /* }}} */
315 static void submit_gauge (const char *type, const char *type_inst,
316 gauge_t value) /* {{{ */
317 {
318 value_t values[1];
319 value_list_t vl = VALUE_LIST_INIT;
321 values[0].gauge = value;
323 vl.values = values;
324 vl.values_len = 1;
325 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
326 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
327 sstrncpy (vl.type, type, sizeof (vl.type));
328 if (type_inst != NULL)
329 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
331 plugin_dispatch_values (&vl);
332 }
333 /* }}} */
335 static void submit_gauge2 (const char *type, const char *type_inst,
336 gauge_t value0, gauge_t value1) /* {{{ */
337 {
338 value_t values[2];
339 value_list_t vl = VALUE_LIST_INIT;
341 values[0].gauge = value0;
342 values[1].gauge = value1;
344 vl.values = values;
345 vl.values_len = 2;
346 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
347 sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
348 sstrncpy (vl.type, type, sizeof (vl.type));
349 if (type_inst != NULL)
350 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
352 plugin_dispatch_values (&vl);
353 }
354 /* }}} */
356 static int memcached_read (void) /* {{{ */
357 {
358 char buf[1024];
359 char *fields[3];
360 char *ptr;
361 char *line;
362 char *saveptr;
363 int fields_num;
365 gauge_t bytes_used = NAN;
366 gauge_t bytes_total = NAN;
367 gauge_t hits = NAN;
368 gauge_t gets = NAN;
369 counter_t rusage_user = 0;
370 counter_t rusage_syst = 0;
371 counter_t octets_rx = 0;
372 counter_t octets_tx = 0;
374 /* get data from daemon */
375 if (memcached_query_daemon (buf, sizeof (buf)) < 0) {
376 return -1;
377 }
379 #define FIELD_IS(cnst) \
380 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
382 ptr = buf;
383 saveptr = NULL;
384 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
385 {
386 int name_len;
388 ptr = NULL;
390 fields_num = strsplit(line, fields, 3);
391 if (fields_num != 3)
392 continue;
394 name_len = strlen(fields[1]);
395 if (name_len == 0)
396 continue;
398 /*
399 * For an explanation on these fields please refer to
400 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
401 */
403 /*
404 * CPU time consumed by the memcached process
405 */
406 if (FIELD_IS ("rusage_user"))
407 {
408 rusage_user = atoll (fields[2]);
409 }
410 else if (FIELD_IS ("rusage_system"))
411 {
412 rusage_syst = atoll(fields[2]);
413 }
415 /*
416 * Number of threads of this instance
417 */
418 else if (FIELD_IS ("threads"))
419 {
420 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]));
421 }
423 /*
424 * Number of items stored
425 */
426 else if (FIELD_IS ("curr_items"))
427 {
428 submit_gauge ("memcached_items", "current", atof (fields[2]));
429 }
431 /*
432 * Number of bytes used and available (total - used)
433 */
434 else if (FIELD_IS ("bytes"))
435 {
436 bytes_used = atof (fields[2]);
437 }
438 else if (FIELD_IS ("limit_maxbytes"))
439 {
440 bytes_total = atof(fields[2]);
441 }
443 /*
444 * Connections
445 */
446 else if (FIELD_IS ("curr_connections"))
447 {
448 submit_gauge ("memcached_connections", "current", atof (fields[2]));
449 }
451 /*
452 * Commands
453 */
454 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
455 {
456 const char *name = fields[1] + 4;
457 submit_counter ("memcached_command", name, atoll (fields[2]));
458 if (strcmp (name, "get") == 0)
459 gets = atof (fields[2]);
460 }
462 /*
463 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
464 */
465 else if (FIELD_IS ("get_hits"))
466 {
467 submit_counter ("memcached_ops", "hits", atoll (fields[2]));
468 hits = atof (fields[2]);
469 }
470 else if (FIELD_IS ("get_misses"))
471 {
472 submit_counter ("memcached_ops", "misses", atoll (fields[2]));
473 }
474 else if (FIELD_IS ("evictions"))
475 {
476 submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
477 }
479 /*
480 * Network traffic
481 */
482 else if (FIELD_IS ("bytes_read"))
483 {
484 octets_rx = atoll (fields[2]);
485 }
486 else if (FIELD_IS ("bytes_written"))
487 {
488 octets_tx = atoll (fields[2]);
489 }
490 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
492 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
493 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
495 if ((rusage_user != 0) || (rusage_syst != 0))
496 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
498 if ((octets_rx != 0) || (octets_tx != 0))
499 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
501 if (!isnan (gets) && !isnan (hits))
502 {
503 gauge_t rate = NAN;
505 if (gets != 0.0)
506 rate = 100.0 * hits / gets;
508 submit_gauge ("percent", "hitratio", rate);
509 }
511 return 0;
512 }
513 /* }}} */
515 void module_register (void) /* {{{ */
516 {
517 plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
518 plugin_register_read ("memcached", memcached_read);
519 }
520 /* }}} */
522 /*
523 * Local variables:
524 * tab-width: 4
525 * c-basic-offset: 4
526 * End:
527 * vim600: sw=4 ts=4 fdm=marker noexpandtab
528 * vim<600: sw=4 ts=4 noexpandtab
529 */