memcached plugin: List Franck Lombardi as copyright holder.
[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
66         struct sockaddr_un serv_addr;
67
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));
71
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         }
78
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         }
85
86     } else {
87
88         const char *host;
89         const char *port;
90
91         struct addrinfo  ai_hints;
92         struct addrinfo *ai_list, *ai_ptr;
93         int              ai_return = 0;
94
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;
103
104         host = memcached_host;
105         if (host == NULL) {
106             host = MEMCACHED_DEF_HOST;
107         }
108
109         port = memcached_port;
110         if (strlen (port) == 0) {
111             port = MEMCACHED_DEF_PORT;
112         }
113
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         }
123
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             }
132
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             }
140
141             /* A socket could be opened and connecting succeeded. We're
142              * done. */
143             break;
144         }
145
146         freeaddrinfo (ai_list);
147     }
148
149         if (fd < 0) {
150                 ERROR ("memcached: Could not connect to daemon.");
151                 return -1;
152         }
153
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         }
158
159         {
160                 struct pollfd p;
161                 int status;
162
163                 memset (&p, 0, sizeof (p));
164                 p.fd = fd;
165                 p.events = POLLIN | POLLERR | POLLHUP;
166                 p.revents = 0;
167
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         }
186
187         /* receive data from the memcached daemon */
188         memset (buffer, '\0', buffer_size);
189
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++;
197
198                 if (status == -1) {
199                         char errbuf[1024];
200
201                         if (errno == EAGAIN) {
202                                 continue;
203                         }
204
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;
212
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         }
218
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         }
229
230         shutdown(fd, SHUT_RDWR);
231         close(fd);
232         return 0;
233 }
234 /* }}} */
235
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         }
258
259         return 0;
260 }
261 /* }}} */
262
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;
268
269         values[0].counter = value;
270
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));
278
279         plugin_dispatch_values (&vl);
280 } /* void memcached_submit_cmd */
281 /* }}} */
282
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;
288
289         values[0].counter = value0;
290         values[1].counter = value1;
291
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));
300
301         plugin_dispatch_values (&vl);
302 } /* void memcached_submit_cmd */
303 /* }}} */
304
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;
310
311         values[0].gauge = value;
312
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));
321
322         plugin_dispatch_values (&vl);
323 }
324 /* }}} */
325
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;
331
332         values[0].gauge = value0;
333         values[1].gauge = value1;
334
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));
343
344         plugin_dispatch_values (&vl);
345 }
346 /* }}} */
347
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;
356
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;
365
366         /* get data from daemon */
367         if (memcached_query_daemon (buf, sizeof (buf)) < 0) {
368                 return -1;
369         }
370
371 #define FIELD_IS(cnst) \
372         (((sizeof(cnst) - 1) == name_len) && (strcmp (cnst, fields[1]) == 0))
373
374     ptr = buf;
375     saveptr = NULL;
376     while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
377         {
378                 int name_len;
379
380         ptr = NULL;
381
382                 fields_num = strsplit(line, fields, 3);
383                 if (fields_num != 3)
384                         continue;
385
386                 name_len = strlen(fields[1]);
387                 if (name_len == 0)
388                         continue;
389
390                 /*
391                  * For an explanation on these fields please refer to
392                  * <http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
393                  */
394
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                 }
406
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                 }
414
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                 }
422 /*
423                 else if (FIELD_IS ("total_items"))
424                 {
425                         total_items = atoll(fields[2]);
426                 }
427  */
428
429                 /*
430                  * Number of bytes used and available (total - used)
431                  */
432                 else if (FIELD_IS ("bytes"))
433                 {
434                         bytes_used = atof (fields[2]);
435                 }
436                 else if (FIELD_IS ("limit_maxbytes"))
437                 {
438                         bytes_total = atof(fields[2]);
439                 }
440
441                 /*
442                  * Connections
443                  */
444                 else if (FIELD_IS ("curr_connections"))
445                 {
446                         submit_gauge ("memcached_connections", "current", atof (fields[2]));
447                 }
448 /*
449                 else if (FIELD_IS("total_connections"))
450                 {
451                         total_connections = atoll(fields[2]);
452                 }
453 */
454
455 /*
456  * ``Number of connection structures allocated by the server''
457                 else if (FIELD_IS ("connection_structures"))
458                 {
459                         connection_structures = atof(fields[2]);
460                 }
461  */
462
463                 /*
464                  * Commands
465                  */
466                 else if ((name_len > 4) && (strncmp (fields[1], "cmd_", 4) == 0))
467                 {
468                         const char *name = fields[1] + 4;
469                         submit_counter ("memcached_command", name, atoll (fields[2]));
470                         if (strcmp (name, "get") == 0)
471                                 gets = atof (fields[2]);
472                 }
473
474                 /*
475                  * Operations on the cache, i. e. cache hits, cache misses and evictions of items
476                  */
477                 else if (FIELD_IS ("get_hits"))
478                 {
479                         submit_counter ("memcached_ops", "hits", atoll (fields[2]));
480                         hits = atof (fields[2]);
481                 }
482                 else if (FIELD_IS ("get_misses"))
483                 {
484                         submit_counter ("memcached_ops", "misses", atoll (fields[2]));
485                 }
486                 else if (FIELD_IS ("evictions"))
487                 {
488                         submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
489                 }
490
491                 /*
492                  * Network traffic
493                  */
494                 else if (FIELD_IS ("bytes_read"))
495                 {
496                         octets_rx = atoll (fields[2]);
497                 }
498                 else if (FIELD_IS ("bytes_written"))
499                 {
500                         octets_tx = atoll (fields[2]);
501                 }
502         } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
503
504         if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
505                 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
506
507         if ((rusage_user != 0) || (rusage_syst != 0))
508                 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
509
510         if ((octets_rx != 0) || (octets_tx != 0))
511                 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
512
513         if (!isnan (gets) && !isnan (hits))
514         {
515                 gauge_t rate = NAN;
516
517                 if (gets != 0.0)
518                         rate = 100.0 * hits / gets;
519
520                 submit_gauge ("percent", "hitratio", rate);
521         }
522
523         return 0;
524 }
525 /* }}} */
526
527 void module_register (void) /* {{{ */
528 {
529         plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
530         plugin_register_read ("memcached", memcached_read);
531 }
532 /* }}} */
533
534 /*
535  * Local variables:
536  * tab-width: 4
537  * c-basic-offset: 4
538  * End:
539  * vim600: sw=4 ts=4 fdm=marker
540  * vim<600: sw=4 ts=4
541  */
542