1 /**
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
6 *
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.
11 *
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.
16 *
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
20 *
21 * Authors:
22 * Antony Dovgal <tony at daylessday dot org>
23 * Vincent Stehlé <vincent.stehle at free.fr>
24 * Florian octo Forster <octo at verplant.org>
25 * Franck Lombardi
26 **/
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31 #include "configfile.h"
33 # include <poll.h>
34 # include <netdb.h>
35 # include <sys/socket.h>
36 # include <sys/un.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[] =
46 {
47 "Socket",
48 "Host",
49 "Port"
50 };
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) /* {{{ */
58 {
59 int fd;
60 ssize_t status;
61 int buffer_fill;
62 int i = 0;
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) {
74 char errbuf[1024];
75 ERROR ("memcached: unix socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
76 return -1;
77 }
79 /* connect to the memcached daemon */
80 if (connect (fd, (struct sockaddr *) &serv_addr, SUN_LEN(&serv_addr))) {
81 shutdown(fd, SHUT_RDWR);
82 close(fd);
83 fd = -1;
84 }
86 } else {
88 const char *host;
89 const char *port;
91 struct addrinfo ai_hints;
92 struct addrinfo *ai_list, *ai_ptr;
93 int ai_return = 0;
95 memset (&ai_hints, '\0', sizeof (ai_hints));
96 ai_hints.ai_flags = 0;
97 #ifdef AI_ADDRCONFIG
98 /* ai_hints.ai_flags |= AI_ADDRCONFIG; */
99 #endif
100 ai_hints.ai_family = AF_INET;
101 ai_hints.ai_socktype = SOCK_STREAM;
102 ai_hints.ai_protocol = 0;
104 host = memcached_host;
105 if (host == NULL) {
106 host = MEMCACHED_DEF_HOST;
107 }
109 port = memcached_port;
110 if (strlen (port) == 0) {
111 port = MEMCACHED_DEF_PORT;
112 }
114 if ((ai_return = getaddrinfo (host, port, NULL, &ai_list)) != 0) {
115 char errbuf[1024];
116 ERROR ("memcached: getaddrinfo (%s, %s): %s",
117 host, port,
118 (ai_return == EAI_SYSTEM)
119 ? sstrerror (errno, errbuf, sizeof (errbuf))
120 : gai_strerror (ai_return));
121 return -1;
122 }
124 fd = -1;
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) {
128 char errbuf[1024];
129 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
130 continue;
131 }
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);
136 close(fd);
137 fd = -1;
138 continue;
139 }
141 /* A socket could be opened and connecting succeeded. We're
142 * done. */
143 break;
144 }
146 freeaddrinfo (ai_list);
147 }
149 if (fd < 0) {
150 ERROR ("memcached: Could not connect to daemon.");
151 return -1;
152 }
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.");
156 return -1;
157 }
159 {
160 struct pollfd p;
161 int status;
163 memset (&p, 0, sizeof (p));
164 p.fd = fd;
165 p.events = POLLIN | POLLERR | POLLHUP;
166 p.revents = 0;
168 status = poll (&p, /* nfds = */ 1, /* timeout = */ 1000 * interval_g);
169 if (status <= 0)
170 {
171 if (status == 0)
172 {
173 ERROR ("memcached: poll(2) timed out after %i seconds.", interval_g);
174 }
175 else
176 {
177 char errbuf[1024];
178 ERROR ("memcached: poll(2) failed: %s",
179 sstrerror (errno, errbuf, sizeof (errbuf)));
180 }
181 shutdown (fd, SHUT_RDWR);
182 close (fd);
183 return (-1);
184 }
185 }
187 /* receive data from the memcached daemon */
188 memset (buffer, '\0', buffer_size);
190 buffer_fill = 0;
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");
194 break;
195 }
196 i++;
198 if (status == -1) {
199 char errbuf[1024];
201 if (errno == EAGAIN) {
202 continue;
203 }
205 ERROR ("memcached: Error reading from socket: %s",
206 sstrerror (errno, errbuf, sizeof (errbuf)));
207 shutdown(fd, SHUT_RDWR);
208 close (fd);
209 return -1;
210 }
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 */
215 break;
216 }
217 }
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);
226 close(fd);
227 return -1;
228 }
230 shutdown(fd, SHUT_RDWR);
231 close(fd);
232 return 0;
233 }
234 /* }}} */
236 static int memcached_config (const char *key, const char *value) /* {{{ */
237 {
238 if (strcasecmp (key, "Socket") == 0) {
239 if (memcached_socket != NULL) {
240 free (memcached_socket);
241 }
242 memcached_socket = strdup (value);
243 } else if (strcasecmp (key, "Host") == 0) {
244 if (memcached_host != NULL) {
245 free (memcached_host);
246 }
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);
252 } else {
253 sstrncpy (memcached_port, value, sizeof (memcached_port));
254 }
255 } else {
256 return -1;
257 }
259 return 0;
260 }
261 /* }}} */
263 static void submit_counter (const char *type, const char *type_inst,
264 counter_t value) /* {{{ */
265 {
266 value_t values[1];
267 value_list_t vl = VALUE_LIST_INIT;
269 values[0].counter = value;
271 vl.values = values;
272 vl.values_len = 1;
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 */
281 /* }}} */
283 static void submit_counter2 (const char *type, const char *type_inst,
284 counter_t value0, counter_t value1) /* {{{ */
285 {
286 value_t values[2];
287 value_list_t vl = VALUE_LIST_INIT;
289 values[0].counter = value0;
290 values[1].counter = value1;
292 vl.values = values;
293 vl.values_len = 2;
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 */
303 /* }}} */
305 static void submit_gauge (const char *type, const char *type_inst,
306 gauge_t value) /* {{{ */
307 {
308 value_t values[1];
309 value_list_t vl = VALUE_LIST_INIT;
311 values[0].gauge = value;
313 vl.values = values;
314 vl.values_len = 1;
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);
323 }
324 /* }}} */
326 static void submit_gauge2 (const char *type, const char *type_inst,
327 gauge_t value0, gauge_t value1) /* {{{ */
328 {
329 value_t values[2];
330 value_list_t vl = VALUE_LIST_INIT;
332 values[0].gauge = value0;
333 values[1].gauge = value1;
335 vl.values = values;
336 vl.values_len = 2;
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);
345 }
346 /* }}} */
348 static int memcached_read (void) /* {{{ */
349 {
350 char buf[1024];
351 char *fields[3];
352 char *ptr;
353 char *line;
354 char *saveptr;
355 int fields_num;
357 gauge_t bytes_used = NAN;
358 gauge_t bytes_total = NAN;
359 gauge_t hits = NAN;
360 gauge_t gets = 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) {
368 return -1;
369 }
371 #define FIELD_IS(cnst) \
372 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
374 ptr = buf;
375 saveptr = NULL;
376 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
377 {
378 int name_len;
380 ptr = NULL;
382 fields_num = strsplit(line, fields, 3);
383 if (fields_num != 3)
384 continue;
386 name_len = strlen(fields[1]);
387 if (name_len == 0)
388 continue;
390 /*
391 * For an explanation on these fields please refer to
392 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
393 */
395 /*
396 * CPU time consumed by the memcached process
397 */
398 if (FIELD_IS ("rusage_user"))
399 {
400 rusage_user = atoll (fields[2]);
401 }
402 else if (FIELD_IS ("rusage_system"))
403 {
404 rusage_syst = atoll(fields[2]);
405 }
407 /*
408 * Number of threads of this instance
409 */
410 else if (FIELD_IS ("threads"))
411 {
412 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]));
413 }
415 /*
416 * Number of items stored
417 */
418 else if (FIELD_IS ("curr_items"))
419 {
420 submit_gauge ("memcached_items", "current", atof (fields[2]));
421 }
423 /*
424 * Number of bytes used and available (total - used)
425 */
426 else if (FIELD_IS ("bytes"))
427 {
428 bytes_used = atof (fields[2]);
429 }
430 else if (FIELD_IS ("limit_maxbytes"))
431 {
432 bytes_total = atof(fields[2]);
433 }
435 /*
436 * Connections
437 */
438 else if (FIELD_IS ("curr_connections"))
439 {
440 submit_gauge ("memcached_connections", "current", atof (fields[2]));
441 }
443 /*
444 * Commands
445 */
446 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
447 {
448 const char *name = fields[1] + 4;
449 submit_counter ("memcached_command", name, atoll (fields[2]));
450 if (strcmp (name, "get") == 0)
451 gets = atof (fields[2]);
452 }
454 /*
455 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
456 */
457 else if (FIELD_IS ("get_hits"))
458 {
459 submit_counter ("memcached_ops", "hits", atoll (fields[2]));
460 hits = atof (fields[2]);
461 }
462 else if (FIELD_IS ("get_misses"))
463 {
464 submit_counter ("memcached_ops", "misses", atoll (fields[2]));
465 }
466 else if (FIELD_IS ("evictions"))
467 {
468 submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
469 }
471 /*
472 * Network traffic
473 */
474 else if (FIELD_IS ("bytes_read"))
475 {
476 octets_rx = atoll (fields[2]);
477 }
478 else if (FIELD_IS ("bytes_written"))
479 {
480 octets_tx = atoll (fields[2]);
481 }
482 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
484 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
485 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
487 if ((rusage_user != 0) || (rusage_syst != 0))
488 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
490 if ((octets_rx != 0) || (octets_tx != 0))
491 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
493 if (!isnan (gets) && !isnan (hits))
494 {
495 gauge_t rate = NAN;
497 if (gets != 0.0)
498 rate = 100.0 * hits / gets;
500 submit_gauge ("percent", "hitratio", rate);
501 }
503 return 0;
504 }
505 /* }}} */
507 void module_register (void) /* {{{ */
508 {
509 plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
510 plugin_register_read ("memcached", memcached_read);
511 }
512 /* }}} */
514 /*
515 * Local variables:
516 * tab-width: 4
517 * c-basic-offset: 4
518 * End:
519 * vim600: sw=4 ts=4 fdm=marker noexpandtab
520 * vim<600: sw=4 ts=4 noexpandtab
521 */