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 *
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.
10 *
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.
15 *
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
19 *
20 * Authors:
21 * Antony Dovgal <tony at daylessday dot org>
22 * Vincent Stehlé <vincent.stehle at free.fr>
23 * Florian octo Forster <octo at verplant.org>
24 **/
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
31 # include <poll.h>
32 # include <netdb.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[] =
43 {
44 "Host",
45 "Port"
46 };
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) /* {{{ */
53 {
54 int fd;
55 ssize_t status;
56 int buffer_fill;
58 const char *host;
59 const char *port;
61 struct addrinfo ai_hints;
62 struct addrinfo *ai_list, *ai_ptr;
63 int ai_return, i = 0;
65 memset (&ai_hints, '\0', sizeof (ai_hints));
66 ai_hints.ai_flags = 0;
67 #ifdef AI_ADDRCONFIG
68 /* ai_hints.ai_flags |= AI_ADDRCONFIG; */
69 #endif
70 ai_hints.ai_family = AF_INET;
71 ai_hints.ai_socktype = SOCK_STREAM;
72 ai_hints.ai_protocol = 0;
74 host = memcached_host;
75 if (host == NULL) {
76 host = MEMCACHED_DEF_HOST;
77 }
79 port = memcached_port;
80 if (strlen (port) == 0) {
81 port = MEMCACHED_DEF_PORT;
82 }
84 if ((ai_return = getaddrinfo (host, port, NULL, &ai_list)) != 0) {
85 char errbuf[1024];
86 ERROR ("memcached: getaddrinfo (%s, %s): %s",
87 host, port,
88 (ai_return == EAI_SYSTEM)
89 ? sstrerror (errno, errbuf, sizeof (errbuf))
90 : gai_strerror (ai_return));
91 return -1;
92 }
94 fd = -1;
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) {
98 char errbuf[1024];
99 ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
100 continue;
101 }
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);
106 close(fd);
107 fd = -1;
108 continue;
109 }
111 /* A socket could be opened and connecting succeeded. We're
112 * done. */
113 break;
114 }
116 freeaddrinfo (ai_list);
118 if (fd < 0) {
119 ERROR ("memcached: Could not connect to daemon.");
120 return -1;
121 }
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.");
125 return -1;
126 }
128 {
129 struct pollfd p;
130 int n;
132 p.fd = fd;
133 p.events = POLLIN|POLLERR|POLLHUP;
134 p.revents = 0;
136 n = poll(&p, 1, 3);
138 if (n <= 0) {
139 ERROR ("memcached: poll() failed or timed out");
140 return -1;
141 }
142 }
144 /* receive data from the memcached daemon */
145 memset (buffer, '\0', buffer_size);
147 buffer_fill = 0;
148 while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
149 if (i > MEMCACHED_RETRY_COUNT) {
150 ERROR("recv() timed out");
151 break;
152 }
153 i++;
155 if (status == -1) {
156 char errbuf[1024];
158 if (errno == EAGAIN) {
159 continue;
160 }
162 ERROR ("memcached: Error reading from socket: %s",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
164 shutdown(fd, SHUT_RDWR);
165 close (fd);
166 return -1;
167 }
168 buffer_fill += status;
170 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
171 /* we got all the data */
172 break;
173 }
174 }
176 if (buffer_fill >= buffer_size) {
177 buffer[buffer_size - 1] = '\0';
178 WARNING ("memcached: Message from memcached has been truncated.");
179 } else if (buffer_fill == 0) {
180 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
181 "Buffer: `%s'", buffer);
182 shutdown(fd, SHUT_RDWR);
183 close(fd);
184 return -1;
185 }
187 shutdown(fd, SHUT_RDWR);
188 close(fd);
189 return 0;
190 }
191 /* }}} */
193 static int memcached_config (const char *key, const char *value) /* {{{ */
194 {
195 if (strcasecmp (key, "Host") == 0) {
196 if (memcached_host != NULL) {
197 free (memcached_host);
198 }
199 memcached_host = strdup (value);
200 } else if (strcasecmp (key, "Port") == 0) {
201 int port = (int) (atof (value));
202 if ((port > 0) && (port <= 65535)) {
203 ssnprintf (memcached_port, sizeof (memcached_port), "%i", port);
204 } else {
205 sstrncpy (memcached_port, value, sizeof (memcached_port));
206 }
207 } else {
208 return -1;
209 }
211 return 0;
212 }
213 /* }}} */
215 static void submit_counter (const char *type, const char *type_inst,
216 counter_t value) /* {{{ */
217 {
218 value_t values[1];
219 value_list_t vl = VALUE_LIST_INIT;
221 values[0].counter = value;
223 vl.values = values;
224 vl.values_len = 1;
225 vl.time = time (NULL);
226 strcpy (vl.host, hostname_g);
227 strcpy (vl.plugin, "memcached");
228 sstrncpy (vl.type, type, sizeof (vl.type));
229 if (type_inst != NULL)
230 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
232 plugin_dispatch_values (&vl);
233 } /* void memcached_submit_cmd */
234 /* }}} */
236 static void submit_counter2 (const char *type, const char *type_inst,
237 counter_t value0, counter_t value1) /* {{{ */
238 {
239 value_t values[2];
240 value_list_t vl = VALUE_LIST_INIT;
242 values[0].counter = value0;
243 values[1].counter = value1;
245 vl.values = values;
246 vl.values_len = 2;
247 vl.time = time (NULL);
248 strcpy (vl.host, hostname_g);
249 strcpy (vl.plugin, "memcached");
250 sstrncpy (vl.type, type, sizeof (vl.type));
251 if (type_inst != NULL)
252 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
254 plugin_dispatch_values (&vl);
255 } /* void memcached_submit_cmd */
256 /* }}} */
258 static void submit_gauge (const char *type, const char *type_inst,
259 gauge_t value) /* {{{ */
260 {
261 value_t values[1];
262 value_list_t vl = VALUE_LIST_INIT;
264 values[0].gauge = value;
266 vl.values = values;
267 vl.values_len = 1;
268 vl.time = time (NULL);
269 strcpy (vl.host, hostname_g);
270 strcpy (vl.plugin, "memcached");
271 sstrncpy (vl.type, type, sizeof (vl.type));
272 if (type_inst != NULL)
273 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
275 plugin_dispatch_values (&vl);
276 }
277 /* }}} */
279 static void submit_gauge2 (const char *type, const char *type_inst,
280 gauge_t value0, gauge_t value1) /* {{{ */
281 {
282 value_t values[2];
283 value_list_t vl = VALUE_LIST_INIT;
285 values[0].gauge = value0;
286 values[1].gauge = value1;
288 vl.values = values;
289 vl.values_len = 2;
290 vl.time = time (NULL);
291 strcpy (vl.host, hostname_g);
292 strcpy (vl.plugin, "memcached");
293 sstrncpy (vl.type, type, sizeof (vl.type));
294 if (type_inst != NULL)
295 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
297 plugin_dispatch_values (&vl);
298 }
299 /* }}} */
301 static int memcached_read (void) /* {{{ */
302 {
303 char buf[1024];
304 char *fields[3];
305 char *ptr;
306 char *line;
307 char *saveptr;
308 int fields_num;
310 gauge_t bytes_used = NAN;
311 gauge_t bytes_total = NAN;
312 counter_t rusage_user = 0;
313 counter_t rusage_syst = 0;
314 counter_t octets_rx = 0;
315 counter_t octets_tx = 0;
317 /* get data from daemon */
318 if (memcached_query_daemon (buf, sizeof (buf)) < 0) {
319 return -1;
320 }
322 #define FIELD_IS(cnst) \
323 (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
325 ptr = buf;
326 saveptr = NULL;
327 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
328 {
329 int name_len;
331 ptr = NULL;
333 fields_num = strsplit(line, fields, 3);
334 if (fields_num != 3)
335 continue;
337 name_len = strlen(fields[1]);
338 if (name_len == 0)
339 continue;
341 /*
342 * For an explanation on these fields please refer to
343 * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
344 */
346 /*
347 * CPU time consumed by the memcached process
348 */
349 if (FIELD_IS ("rusage_user"))
350 {
351 rusage_user = atoll (fields[2]);
352 }
353 else if (FIELD_IS ("rusage_system"))
354 {
355 rusage_syst = atoll(fields[2]);
356 }
358 /*
359 * Number of threads of this instance
360 */
361 else if (FIELD_IS ("threads"))
362 {
363 submit_gauge2 ("ps_count", NULL, NAN, atof (fields[2]));
364 }
366 /*
367 * Number of items stored
368 */
369 else if (FIELD_IS ("curr_items"))
370 {
371 submit_gauge ("memcached_items", "current", atof (fields[2]));
372 }
373 /*
374 else if (FIELD_IS ("total_items"))
375 {
376 total_items = atoll(fields[2]);
377 }
378 */
380 /*
381 * Number of bytes used and available (total - used)
382 */
383 else if (FIELD_IS ("bytes"))
384 {
385 bytes_used = atof (fields[2]);
386 }
387 else if (FIELD_IS ("limit_maxbytes"))
388 {
389 bytes_total = atof(fields[2]);
390 }
392 /*
393 * Connections
394 */
395 else if (FIELD_IS ("curr_connections"))
396 {
397 submit_gauge ("memcached_connections", "current", atof (fields[2]));
398 }
399 /*
400 else if (FIELD_IS("total_connections"))
401 {
402 total_connections = atoll(fields[2]);
403 }
404 */
406 /*
407 * ``Number of connection structures allocated by the server''
408 else if (FIELD_IS ("connection_structures"))
409 {
410 connection_structures = atof(fields[2]);
411 }
412 */
414 /*
415 * Commands
416 */
417 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
418 {
419 const char *name = fields[1] + 4;
420 submit_counter ("memcached_command", name, atoll (fields[2]));
421 }
423 /*
424 * Operations on the cache, i. e. cache hits, cache misses and evictions of items
425 */
426 else if (FIELD_IS ("get_hits"))
427 {
428 submit_counter ("memcached_ops", "hits", atoll (fields[2]));
429 }
430 else if (FIELD_IS ("get_misses"))
431 {
432 submit_counter ("memcached_ops", "misses", atoll (fields[2]));
433 }
434 else if (FIELD_IS ("evictions"))
435 {
436 submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
437 }
439 /*
440 * Network traffic
441 */
442 else if (FIELD_IS ("bytes_read"))
443 {
444 octets_rx = atoll (fields[2]);
445 }
446 else if (FIELD_IS ("bytes_written"))
447 {
448 octets_tx = atoll (fields[2]);
449 }
450 } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
452 if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
453 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
455 if ((rusage_user != 0) || (rusage_syst != 0))
456 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
458 if ((octets_rx != 0) || (octets_tx != 0))
459 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
461 return 0;
462 }
463 /* }}} */
465 void module_register (void) /* {{{ */
466 {
467 plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
468 plugin_register_read ("memcached", memcached_read);
469 }
470 /* }}} */
472 /*
473 * Local variables:
474 * tab-width: 4
475 * c-basic-offset: 4
476 * End:
477 * vim600: sw=4 ts=4 fdm=marker
478 * vim<600: sw=4 ts=4
479 */