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