memcached: Persistent connections with IO timeouts
[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-2012  Florian Forster
5  * Copyright (C) 2009       Doug MacEachern
6  * Copyright (C) 2009       Franck Lombardi
7  * Copyright (C) 2012       Nicolas Szalay
8  * Copyright (C) 2017       Pavel Rochnyak
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the
12  * Free Software Foundation; either version 2 of the License, or (at your
13  * option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
23  *
24  * Authors:
25  *   Antony Dovgal <tony at daylessday dot org>
26  *   Florian octo Forster <octo at collectd.org>
27  *   Doug MacEachern <dougm at hyperic.com>
28  *   Franck Lombardi
29  *   Nicolas Szalay
30  *   Pavel Rochnyak <pavel2000 ngs.ru>
31  **/
32
33 #include "collectd.h"
34
35 #include "common.h"
36 #include "plugin.h"
37
38 #include <netdb.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
41 #include <sys/un.h>
42
43 #include <poll.h>
44
45 #define MEMCACHED_DEF_HOST "127.0.0.1"
46 #define MEMCACHED_DEF_PORT "11211"
47 #define MEMCACHED_CONNECT_TIMEOUT 10000
48 #define MEMCACHED_IO_TIMEOUT 5000
49
50 struct memcached_s {
51   char *name;
52   char *host;
53   char *socket;
54   char *connhost;
55   char *connport;
56   int fd;
57 };
58 typedef struct memcached_s memcached_t;
59
60 static _Bool memcached_have_instances = 0;
61
62 static void memcached_free(void *arg) {
63   memcached_t *st = arg;
64   if (st == NULL)
65     return;
66
67   if (st->fd >= 0) {
68     shutdown(st->fd, SHUT_RDWR);
69     close(st->fd);
70   }
71
72   sfree(st->name);
73   sfree(st->host);
74   sfree(st->socket);
75   sfree(st->connhost);
76   sfree(st->connport);
77   sfree(st);
78 }
79
80 static int memcached_connect_unix(memcached_t *st) {
81   struct sockaddr_un serv_addr = {0};
82   int fd;
83
84   serv_addr.sun_family = AF_UNIX;
85   sstrncpy(serv_addr.sun_path, st->socket, sizeof(serv_addr.sun_path));
86
87   /* create our socket descriptor */
88   fd = socket(AF_UNIX, SOCK_STREAM, 0);
89   if (fd < 0) {
90     char errbuf[1024];
91     ERROR("memcached plugin: memcached_connect_unix: socket(2) failed: %s",
92           sstrerror(errno, errbuf, sizeof(errbuf)));
93     return -1;
94   }
95
96   /* connect to the memcached daemon */
97   int status = connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
98   if (status != 0) {
99     shutdown(fd, SHUT_RDWR);
100     close(fd);
101     return -1;
102   }
103
104   /* switch to non-blocking mode */
105   int flags = fcntl(fd, F_GETFL);
106   status = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
107   if (status != 0) {
108     close(fd);
109     return -1;
110   }
111
112   return fd;
113 } /* int memcached_connect_unix */
114
115 static int memcached_connect_inet(memcached_t *st) {
116   struct addrinfo *ai_list;
117   int status;
118   int fd = -1;
119
120   struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
121                               .ai_flags = AI_ADDRCONFIG,
122                               .ai_socktype = SOCK_STREAM};
123
124   status = getaddrinfo(st->connhost, st->connport, &ai_hints, &ai_list);
125   if (status != 0) {
126     char errbuf[1024];
127     ERROR("memcached plugin: memcached_connect_inet: "
128           "getaddrinfo(%s,%s) failed: %s",
129           st->connhost, st->connport,
130           (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
131                                  : gai_strerror(status));
132     return -1;
133   }
134
135   for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
136        ai_ptr = ai_ptr->ai_next) {
137     /* create our socket descriptor */
138     fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
139     if (fd < 0) {
140       char errbuf[1024];
141       WARNING("memcached plugin: memcached_connect_inet: "
142               "socket(2) failed: %s",
143               sstrerror(errno, errbuf, sizeof(errbuf)));
144       continue;
145     }
146
147     /* switch socket to non-blocking mode */
148     int flags = fcntl(fd, F_GETFL);
149     status = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
150     if (status != 0) {
151       close(fd);
152       fd = -1;
153       continue;
154     }
155
156     /* connect to the memcached daemon */
157     status = (int)connect(fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
158     if (status != 0 && errno != EINPROGRESS) {
159       shutdown(fd, SHUT_RDWR);
160       close(fd);
161       fd = -1;
162       continue;
163     }
164
165     /* Wait until connection establishes */
166     struct pollfd pollfd;
167     pollfd.fd = fd;
168     pollfd.events = POLLOUT;
169     do
170       status = poll(&pollfd, 1, MEMCACHED_CONNECT_TIMEOUT);
171     while (status < 0 && errno == EINTR);
172     if (status <= 0) {
173       close(fd);
174       fd = -1;
175       continue;
176     }
177
178     /* Check if all is good */
179     int socket_error;
180     socklen_t socket_error_len = sizeof(socket_error);
181     status = getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&socket_error,
182                         &socket_error_len);
183     if (status != 0 || socket_error != 0) {
184       close(fd);
185       fd = -1;
186       continue;
187     }
188     /* A socket is opened and connection succeeded. We're done. */
189     break;
190   }
191
192   freeaddrinfo(ai_list);
193   return fd;
194 } /* int memcached_connect_inet */
195
196 static void memcached_connect(memcached_t *st) {
197   if (st->fd >= 0)
198     return;
199
200   if (st->socket != NULL)
201     st->fd = memcached_connect_unix(st);
202   else
203     st->fd = memcached_connect_inet(st);
204
205   if (st->fd >= 0)
206     INFO("memcached plugin: Instance \"%s\": connection established.",
207          st->name);
208 }
209
210 static int memcached_query_daemon(char *buffer, size_t buffer_size,
211                                   memcached_t *st) {
212   int status;
213   size_t buffer_fill;
214
215   memcached_connect(st);
216   if (st->fd < 0) {
217     ERROR("memcached plugin: Instance \"%s\" could not connect to daemon.",
218           st->name);
219     return -1;
220   }
221
222   struct pollfd pollfd;
223   pollfd.fd = st->fd;
224   pollfd.events = POLLOUT;
225
226   do
227     status = poll(&pollfd, 1, MEMCACHED_IO_TIMEOUT);
228   while (status < 0 && errno == EINTR);
229
230   if (status <= 0) {
231     ERROR("memcached plugin: poll() failed for write() call.");
232     close(st->fd);
233     st->fd = -1;
234     return -1;
235   }
236
237   status = (int)swrite(st->fd, "stats\r\n", strlen("stats\r\n"));
238   if (status != 0) {
239     char errbuf[1024];
240     ERROR("memcached plugin: Instance \"%s\": write(2) failed: %s", st->name,
241           sstrerror(errno, errbuf, sizeof(errbuf)));
242     shutdown(st->fd, SHUT_RDWR);
243     close(st->fd);
244     st->fd = -1;
245     return -1;
246   }
247
248   /* receive data from the memcached daemon */
249   memset(buffer, 0, buffer_size);
250
251   buffer_fill = 0;
252   pollfd.events = POLLIN;
253   while (1) {
254     do
255       status = poll(&pollfd, 1, MEMCACHED_IO_TIMEOUT);
256     while (status < 0 && errno == EINTR);
257
258     if (status <= 0) {
259       ERROR("memcached plugin: Instance \"%s\": Timeout reading from socket",
260             st->name);
261       close(st->fd);
262       st->fd = -1;
263       return -1;
264     }
265
266     do
267       status = (int)recv(st->fd, buffer + buffer_fill,
268                          buffer_size - buffer_fill, /* flags = */ 0);
269     while (status < 0 && errno == EINTR);
270
271     char const end_token[5] = {'E', 'N', 'D', '\r', '\n'};
272     if (status < 0) {
273       char errbuf[1024];
274
275       if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
276         continue;
277
278       ERROR("memcached plugin: Instance \"%s\": Error reading from socket: %s",
279             st->name, sstrerror(errno, errbuf, sizeof(errbuf)));
280       shutdown(st->fd, SHUT_RDWR);
281       close(st->fd);
282       st->fd = -1;
283       return -1;
284     }
285
286     buffer_fill += (size_t)status;
287     if (buffer_fill > buffer_size) {
288       buffer_fill = buffer_size;
289       WARNING("memcached plugin: Instance \"%s\": Message was truncated.",
290               st->name);
291       shutdown(st->fd, SHUT_RDWR);
292       close(st->fd);
293       st->fd = -1;
294       break;
295     }
296
297     /* If buffer ends in end_token, we have all the data. */
298     if (memcmp(buffer + buffer_fill - sizeof(end_token), end_token,
299                sizeof(end_token)) == 0)
300       break;
301   } /* while (recv) */
302
303   status = 0;
304   if (buffer_fill == 0) {
305     WARNING("memcached plugin: Instance \"%s\": No data returned by memcached.",
306             st->name);
307     status = -1;
308   }
309
310   return status;
311 } /* int memcached_query_daemon */
312
313 static void memcached_init_vl(value_list_t *vl, memcached_t const *st) {
314   sstrncpy(vl->plugin, "memcached", sizeof(vl->plugin));
315   if (st->host != NULL)
316     sstrncpy(vl->host, st->host, sizeof(vl->host));
317   if (st->name != NULL)
318     sstrncpy(vl->plugin_instance, st->name, sizeof(vl->plugin_instance));
319 }
320
321 static void submit_derive(const char *type, const char *type_inst,
322                           derive_t value, memcached_t *st) {
323   value_list_t vl = VALUE_LIST_INIT;
324
325   memcached_init_vl(&vl, st);
326   vl.values = &(value_t){.derive = value};
327   vl.values_len = 1;
328   sstrncpy(vl.type, type, sizeof(vl.type));
329   if (type_inst != NULL)
330     sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
331
332   plugin_dispatch_values(&vl);
333 }
334
335 static void submit_derive2(const char *type, const char *type_inst,
336                            derive_t value0, derive_t value1, memcached_t *st) {
337   value_list_t vl = VALUE_LIST_INIT;
338   value_t values[] = {
339       {.derive = value0}, {.derive = value1},
340   };
341
342   memcached_init_vl(&vl, st);
343   vl.values = values;
344   vl.values_len = STATIC_ARRAY_SIZE(values);
345   sstrncpy(vl.type, type, sizeof(vl.type));
346   if (type_inst != NULL)
347     sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
348
349   plugin_dispatch_values(&vl);
350 }
351
352 static void submit_gauge(const char *type, const char *type_inst, gauge_t value,
353                          memcached_t *st) {
354   value_list_t vl = VALUE_LIST_INIT;
355
356   memcached_init_vl(&vl, st);
357   vl.values = &(value_t){.gauge = value};
358   vl.values_len = 1;
359   sstrncpy(vl.type, type, sizeof(vl.type));
360   if (type_inst != NULL)
361     sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
362
363   plugin_dispatch_values(&vl);
364 }
365
366 static void submit_gauge2(const char *type, const char *type_inst,
367                           gauge_t value0, gauge_t value1, memcached_t *st) {
368   value_list_t vl = VALUE_LIST_INIT;
369   value_t values[] = {
370       {.gauge = value0}, {.gauge = value1},
371   };
372
373   memcached_init_vl(&vl, st);
374   vl.values = values;
375   vl.values_len = STATIC_ARRAY_SIZE(values);
376   sstrncpy(vl.type, type, sizeof(vl.type));
377   if (type_inst != NULL)
378     sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
379
380   plugin_dispatch_values(&vl);
381 }
382
383 static int memcached_read(user_data_t *user_data) {
384   char buf[4096];
385   char *fields[3];
386   char *ptr;
387   char *line;
388   char *saveptr;
389   int fields_num;
390
391   gauge_t bytes_used = NAN;
392   gauge_t bytes_total = NAN;
393   gauge_t hits = NAN;
394   gauge_t gets = NAN;
395   gauge_t incr_hits = NAN;
396   derive_t incr = 0;
397   gauge_t decr_hits = NAN;
398   derive_t decr = 0;
399   derive_t rusage_user = 0;
400   derive_t rusage_syst = 0;
401   derive_t octets_rx = 0;
402   derive_t octets_tx = 0;
403
404   memcached_t *st;
405   st = user_data->data;
406
407   /* get data from daemon */
408   if (memcached_query_daemon(buf, sizeof(buf), st) < 0) {
409     return -1;
410   }
411
412 #define FIELD_IS(cnst)                                                         \
413   (((sizeof(cnst) - 1) == name_len) && (strcmp(cnst, fields[1]) == 0))
414
415   ptr = buf;
416   saveptr = NULL;
417   while ((line = strtok_r(ptr, "\n\r", &saveptr)) != NULL) {
418     int name_len;
419
420     ptr = NULL;
421
422     fields_num = strsplit(line, fields, 3);
423     if (fields_num != 3)
424       continue;
425
426     name_len = strlen(fields[1]);
427     if (name_len == 0)
428       continue;
429
430     /*
431      * For an explanation on these fields please refer to
432      * <https://github.com/memcached/memcached/blob/master/doc/protocol.txt>
433      */
434
435     /*
436      * CPU time consumed by the memcached process
437      */
438     if (FIELD_IS("rusage_user")) {
439       rusage_user = atoll(fields[2]);
440     } else if (FIELD_IS("rusage_system")) {
441       rusage_syst = atoll(fields[2]);
442     }
443
444     /*
445      * Number of threads of this instance
446      */
447     else if (FIELD_IS("threads")) {
448       submit_gauge2("ps_count", NULL, NAN, atof(fields[2]), st);
449     }
450
451     /*
452      * Number of items stored
453      */
454     else if (FIELD_IS("curr_items")) {
455       submit_gauge("memcached_items", "current", atof(fields[2]), st);
456     }
457
458     /*
459      * Number of bytes used and available (total - used)
460      */
461     else if (FIELD_IS("bytes")) {
462       bytes_used = atof(fields[2]);
463     } else if (FIELD_IS("limit_maxbytes")) {
464       bytes_total = atof(fields[2]);
465     }
466
467     /*
468      * Connections
469      */
470     else if (FIELD_IS("curr_connections")) {
471       submit_gauge("memcached_connections", "current", atof(fields[2]), st);
472     } else if (FIELD_IS("listen_disabled_num")) {
473       submit_derive("connections", "listen_disabled", atof(fields[2]), st);
474     }
475
476     /*
477      * Commands
478      */
479     else if ((name_len > 4) && (strncmp(fields[1], "cmd_", 4) == 0)) {
480       const char *name = fields[1] + 4;
481       submit_derive("memcached_command", name, atoll(fields[2]), st);
482       if (strcmp(name, "get") == 0)
483         gets = atof(fields[2]);
484     }
485
486     /*
487      * Increment/Decrement
488      */
489     else if (FIELD_IS("incr_misses")) {
490       derive_t incr_count = atoll(fields[2]);
491       submit_derive("memcached_ops", "incr_misses", incr_count, st);
492       incr += incr_count;
493     } else if (FIELD_IS("incr_hits")) {
494       derive_t incr_count = atoll(fields[2]);
495       submit_derive("memcached_ops", "incr_hits", incr_count, st);
496       incr_hits = atof(fields[2]);
497       incr += incr_count;
498     } else if (FIELD_IS("decr_misses")) {
499       derive_t decr_count = atoll(fields[2]);
500       submit_derive("memcached_ops", "decr_misses", decr_count, st);
501       decr += decr_count;
502     } else if (FIELD_IS("decr_hits")) {
503       derive_t decr_count = atoll(fields[2]);
504       submit_derive("memcached_ops", "decr_hits", decr_count, st);
505       decr_hits = atof(fields[2]);
506       decr += decr_count;
507     }
508
509     /*
510      * Operations on the cache:
511      * - get hits/misses
512      * - delete hits/misses
513      * - evictions
514      */
515     else if (FIELD_IS("get_hits")) {
516       submit_derive("memcached_ops", "hits", atoll(fields[2]), st);
517       hits = atof(fields[2]);
518     } else if (FIELD_IS("get_misses")) {
519       submit_derive("memcached_ops", "misses", atoll(fields[2]), st);
520     } else if (FIELD_IS("evictions")) {
521       submit_derive("memcached_ops", "evictions", atoll(fields[2]), st);
522     } else if (FIELD_IS("delete_hits")) {
523       submit_derive("memcached_ops", "delete_hits", atoll(fields[2]), st);
524     } else if (FIELD_IS("delete_misses")) {
525       submit_derive("memcached_ops", "delete_misses", atoll(fields[2]), st);
526     }
527
528     /*
529      * Network traffic
530      */
531     else if (FIELD_IS("bytes_read")) {
532       octets_rx = atoll(fields[2]);
533     } else if (FIELD_IS("bytes_written")) {
534       octets_tx = atoll(fields[2]);
535     }
536   } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
537
538   if (!isnan(bytes_used) && !isnan(bytes_total) && (bytes_used <= bytes_total))
539     submit_gauge2("df", "cache", bytes_used, bytes_total - bytes_used, st);
540
541   if ((rusage_user != 0) || (rusage_syst != 0))
542     submit_derive2("ps_cputime", NULL, rusage_user, rusage_syst, st);
543
544   if ((octets_rx != 0) || (octets_tx != 0))
545     submit_derive2("memcached_octets", NULL, octets_rx, octets_tx, st);
546
547   if (!isnan(gets) && !isnan(hits)) {
548     gauge_t rate = NAN;
549
550     if (gets != 0.0)
551       rate = 100.0 * hits / gets;
552
553     submit_gauge("percent", "hitratio", rate, st);
554   }
555
556   if (!isnan(incr_hits) && incr != 0) {
557     gauge_t incr_rate = 100.0 * incr_hits / incr;
558     submit_gauge("percent", "incr_hitratio", incr_rate, st);
559     submit_derive("memcached_ops", "incr", incr, st);
560   }
561
562   if (!isnan(decr_hits) && decr != 0) {
563     gauge_t decr_rate = 100.0 * decr_hits / decr;
564     submit_gauge("percent", "decr_hitratio", decr_rate, st);
565     submit_derive("memcached_ops", "decr", decr, st);
566   }
567
568   return 0;
569 } /* int memcached_read */
570
571 static int memcached_set_defaults(memcached_t *st) {
572   /* If no <Address> used then:
573    * - Connect to the destination specified by <Host>, if present.
574    *   If not, use the default address.
575    * - Use the default hostname (set st->host to NULL), if
576    *    - Legacy mode is used (no configuration options at all), or
577    *    - "Host" option is not provided, or
578    *    - "Host" option is set to "localhost" or "127.0.0.1".
579    *
580    * If <Address> used then host may be set to "localhost" or "127.0.0.1"
581    * explicitly.
582    */
583   if (st->connhost == NULL) {
584     if (st->host) {
585       st->connhost = strdup(st->host);
586       if (st->connhost == NULL)
587         return ENOMEM;
588
589       if ((strcmp("127.0.0.1", st->host) == 0) ||
590           (strcmp("localhost", st->host) == 0))
591         sfree(st->host);
592     } else {
593       st->connhost = strdup(MEMCACHED_DEF_HOST);
594       if (st->connhost == NULL)
595         return ENOMEM;
596     }
597   }
598
599   if (st->connport == NULL) {
600     st->connport = strdup(MEMCACHED_DEF_PORT);
601     if (st->connport == NULL)
602       return ENOMEM;
603   }
604
605   assert(st->connhost != NULL);
606   assert(st->connport != NULL);
607
608   return 0;
609 } /* int memcached_set_defaults */
610
611 static int memcached_add_read_callback(memcached_t *st) {
612   char callback_name[3 * DATA_MAX_NAME_LEN];
613
614   if (memcached_set_defaults(st) != 0) {
615     memcached_free(st);
616     return -1;
617   }
618
619   snprintf(callback_name, sizeof(callback_name), "memcached/%s",
620            (st->name != NULL) ? st->name : "__legacy__");
621
622   return plugin_register_complex_read(
623       /* group = */ "memcached",
624       /* name      = */ callback_name,
625       /* callback  = */ memcached_read,
626       /* interval  = */ 0,
627       &(user_data_t){
628           .data = st, .free_func = memcached_free,
629       });
630 } /* int memcached_add_read_callback */
631
632 /* Configuration handling functiions
633  * <Plugin memcached>
634  *   <Instance "instance_name">
635  *     Host foo.zomg.com
636  *     Address 1.2.3.4
637  *     Port "1234"
638  *   </Instance>
639  * </Plugin>
640  */
641 static int config_add_instance(oconfig_item_t *ci) {
642   memcached_t *st;
643   int status = 0;
644
645   /* Disable automatic generation of default instance in the init callback. */
646   memcached_have_instances = 1;
647
648   st = calloc(1, sizeof(*st));
649   if (st == NULL) {
650     ERROR("memcached plugin: calloc failed.");
651     return ENOMEM;
652   }
653
654   st->name = NULL;
655   st->host = NULL;
656   st->socket = NULL;
657   st->connhost = NULL;
658   st->connport = NULL;
659
660   st->fd = -1;
661
662   if (strcasecmp(ci->key, "Instance") == 0)
663     status = cf_util_get_string(ci, &st->name);
664
665   if (status != 0) {
666     sfree(st);
667     return status;
668   }
669
670   for (int i = 0; i < ci->children_num; i++) {
671     oconfig_item_t *child = ci->children + i;
672
673     if (strcasecmp("Socket", child->key) == 0)
674       status = cf_util_get_string(child, &st->socket);
675     else if (strcasecmp("Host", child->key) == 0)
676       status = cf_util_get_string(child, &st->host);
677     else if (strcasecmp("Address", child->key) == 0)
678       status = cf_util_get_string(child, &st->connhost);
679     else if (strcasecmp("Port", child->key) == 0)
680       status = cf_util_get_service(child, &st->connport);
681     else {
682       WARNING("memcached plugin: Option `%s' not allowed here.", child->key);
683       status = -1;
684     }
685
686     if (status != 0)
687       break;
688   }
689
690   if (status != 0) {
691     memcached_free(st);
692     return -1;
693   }
694
695   return memcached_add_read_callback(st);
696 } /* int config_add_instance */
697
698 static int memcached_config(oconfig_item_t *ci) {
699   _Bool have_instance_block = 0;
700
701   for (int i = 0; i < ci->children_num; i++) {
702     oconfig_item_t *child = ci->children + i;
703
704     if (strcasecmp("Instance", child->key) == 0) {
705       config_add_instance(child);
706       have_instance_block = 1;
707     } else if (!have_instance_block) {
708       /* Non-instance option: Assume legacy configuration (without <Instance />
709        * blocks) and call config_add_instance() with the <Plugin /> block. */
710       return config_add_instance(ci);
711     } else
712       WARNING("memcached plugin: The configuration option "
713               "\"%s\" is not allowed here. Did you "
714               "forget to add an <Instance /> block "
715               "around the configuration?",
716               child->key);
717   } /* for (ci->children) */
718
719   return 0;
720 } /* int memcached_config */
721
722 static int memcached_init(void) {
723   memcached_t *st;
724   int status;
725
726   if (memcached_have_instances)
727     return 0;
728
729   /* No instances were configured, lets start a default instance. */
730   st = calloc(1, sizeof(*st));
731   if (st == NULL)
732     return ENOMEM;
733   st->name = NULL;
734   st->host = NULL;
735   st->socket = NULL;
736   st->connhost = NULL;
737   st->connport = NULL;
738
739   st->fd = -1;
740
741   status = memcached_add_read_callback(st);
742   if (status == 0)
743     memcached_have_instances = 1;
744
745   return status;
746 } /* int memcached_init */
747
748 void module_register(void) {
749   plugin_register_complex_config("memcached", memcached_config);
750   plugin_register_init("memcached", memcached_init);
751 }