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