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