Merge branch 'collectd-5.7'
[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     st->fd = -1;
71   }
72
73   sfree(st->name);
74   sfree(st->host);
75   sfree(st->socket);
76   sfree(st->connhost);
77   sfree(st->connport);
78   sfree(st);
79 }
80
81 static int memcached_connect_unix(memcached_t *st) {
82   struct sockaddr_un serv_addr = {0};
83   int fd;
84
85   serv_addr.sun_family = AF_UNIX;
86   sstrncpy(serv_addr.sun_path, st->socket, sizeof(serv_addr.sun_path));
87
88   /* create our socket descriptor */
89   fd = socket(AF_UNIX, SOCK_STREAM, 0);
90   if (fd < 0) {
91     char errbuf[1024];
92     ERROR("memcached plugin: memcached_connect_unix: socket(2) failed: %s",
93           sstrerror(errno, errbuf, sizeof(errbuf)));
94     return -1;
95   }
96
97   /* connect to the memcached daemon */
98   int status = connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
99   if (status != 0) {
100     shutdown(fd, SHUT_RDWR);
101     close(fd);
102     return -1;
103   }
104
105   /* switch to non-blocking mode */
106   int flags = fcntl(fd, F_GETFL);
107   status = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
108   if (status != 0) {
109     close(fd);
110     return -1;
111   }
112
113   return fd;
114 } /* int memcached_connect_unix */
115
116 static int memcached_connect_inet(memcached_t *st) {
117   struct addrinfo *ai_list;
118   int status;
119   int fd = -1;
120
121   struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
122                               .ai_flags = AI_ADDRCONFIG,
123                               .ai_socktype = SOCK_STREAM};
124
125   status = getaddrinfo(st->connhost, st->connport, &ai_hints, &ai_list);
126   if (status != 0) {
127     char errbuf[1024];
128     ERROR("memcached plugin: memcached_connect_inet: "
129           "getaddrinfo(%s,%s) failed: %s",
130           st->connhost, st->connport,
131           (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
132                                  : gai_strerror(status));
133     return -1;
134   }
135
136   for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
137        ai_ptr = ai_ptr->ai_next) {
138     /* create our socket descriptor */
139     fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
140     if (fd < 0) {
141       char errbuf[1024];
142       WARNING("memcached plugin: memcached_connect_inet: "
143               "socket(2) failed: %s",
144               sstrerror(errno, errbuf, sizeof(errbuf)));
145       continue;
146     }
147
148     /* switch socket to non-blocking mode */
149     int flags = fcntl(fd, F_GETFL);
150     status = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
151     if (status != 0) {
152       close(fd);
153       fd = -1;
154       continue;
155     }
156
157     /* connect to the memcached daemon */
158     status = (int)connect(fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
159     if (status != 0 && errno != EINPROGRESS) {
160       shutdown(fd, SHUT_RDWR);
161       close(fd);
162       fd = -1;
163       continue;
164     }
165
166     /* Wait until connection establishes */
167     struct pollfd pollfd = {
168         .fd = fd, .events = POLLOUT,
169     };
170     do
171       status = poll(&pollfd, 1, MEMCACHED_CONNECT_TIMEOUT);
172     while (status < 0 && errno == EINTR);
173     if (status <= 0) {
174       close(fd);
175       fd = -1;
176       continue;
177     }
178
179     /* Check if all is good */
180     int socket_error;
181     status = getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&socket_error,
182                         &(socklen_t){sizeof(socket_error)});
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       .fd = st->fd, .events = POLLOUT,
224   };
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      * Total number of connections opened since the server started running
477      * Report this as connection rate.
478      */
479     else if (FIELD_IS("total_connections")) {
480       submit_derive("connections", "opened", atof(fields[2]), st);
481     }
482
483     /*
484      * Commands
485      */
486     else if ((name_len > 4) && (strncmp(fields[1], "cmd_", 4) == 0)) {
487       const char *name = fields[1] + 4;
488       submit_derive("memcached_command", name, atoll(fields[2]), st);
489       if (strcmp(name, "get") == 0)
490         gets = atof(fields[2]);
491     }
492
493     /*
494      * Increment/Decrement
495      */
496     else if (FIELD_IS("incr_misses")) {
497       derive_t incr_count = atoll(fields[2]);
498       submit_derive("memcached_ops", "incr_misses", incr_count, st);
499       incr += incr_count;
500     } else if (FIELD_IS("incr_hits")) {
501       derive_t incr_count = atoll(fields[2]);
502       submit_derive("memcached_ops", "incr_hits", incr_count, st);
503       incr_hits = atof(fields[2]);
504       incr += incr_count;
505     } else if (FIELD_IS("decr_misses")) {
506       derive_t decr_count = atoll(fields[2]);
507       submit_derive("memcached_ops", "decr_misses", decr_count, st);
508       decr += decr_count;
509     } else if (FIELD_IS("decr_hits")) {
510       derive_t decr_count = atoll(fields[2]);
511       submit_derive("memcached_ops", "decr_hits", decr_count, st);
512       decr_hits = atof(fields[2]);
513       decr += decr_count;
514     }
515
516     /*
517      * Operations on the cache:
518      * - get hits/misses
519      * - delete hits/misses
520      * - evictions
521      */
522     else if (FIELD_IS("get_hits")) {
523       submit_derive("memcached_ops", "hits", atoll(fields[2]), st);
524       hits = atof(fields[2]);
525     } else if (FIELD_IS("get_misses")) {
526       submit_derive("memcached_ops", "misses", atoll(fields[2]), st);
527     } else if (FIELD_IS("evictions")) {
528       submit_derive("memcached_ops", "evictions", atoll(fields[2]), st);
529     } else if (FIELD_IS("delete_hits")) {
530       submit_derive("memcached_ops", "delete_hits", atoll(fields[2]), st);
531     } else if (FIELD_IS("delete_misses")) {
532       submit_derive("memcached_ops", "delete_misses", atoll(fields[2]), st);
533     }
534
535     /*
536      * Network traffic
537      */
538     else if (FIELD_IS("bytes_read")) {
539       octets_rx = atoll(fields[2]);
540     } else if (FIELD_IS("bytes_written")) {
541       octets_tx = atoll(fields[2]);
542     }
543   } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
544
545   if (!isnan(bytes_used) && !isnan(bytes_total) && (bytes_used <= bytes_total))
546     submit_gauge2("df", "cache", bytes_used, bytes_total - bytes_used, st);
547
548   if ((rusage_user != 0) || (rusage_syst != 0))
549     submit_derive2("ps_cputime", NULL, rusage_user, rusage_syst, st);
550
551   if ((octets_rx != 0) || (octets_tx != 0))
552     submit_derive2("memcached_octets", NULL, octets_rx, octets_tx, st);
553
554   if (!isnan(gets) && !isnan(hits)) {
555     gauge_t rate = NAN;
556
557     if (gets != 0.0)
558       rate = 100.0 * hits / gets;
559
560     submit_gauge("percent", "hitratio", rate, st);
561   }
562
563   if (!isnan(incr_hits) && incr != 0) {
564     gauge_t incr_rate = 100.0 * incr_hits / incr;
565     submit_gauge("percent", "incr_hitratio", incr_rate, st);
566     submit_derive("memcached_ops", "incr", incr, st);
567   }
568
569   if (!isnan(decr_hits) && decr != 0) {
570     gauge_t decr_rate = 100.0 * decr_hits / decr;
571     submit_gauge("percent", "decr_hitratio", decr_rate, st);
572     submit_derive("memcached_ops", "decr", decr, st);
573   }
574
575   return 0;
576 } /* int memcached_read */
577
578 static int memcached_set_defaults(memcached_t *st) {
579   /* If no <Address> used then:
580    * - Connect to the destination specified by <Host>, if present.
581    *   If not, use the default address.
582    * - Use the default hostname (set st->host to NULL), if
583    *    - Legacy mode is used (no configuration options at all), or
584    *    - "Host" option is not provided, or
585    *    - "Host" option is set to "localhost" or "127.0.0.1".
586    *
587    * If <Address> used then host may be set to "localhost" or "127.0.0.1"
588    * explicitly.
589    */
590   if (st->connhost == NULL) {
591     if (st->host) {
592       st->connhost = strdup(st->host);
593       if (st->connhost == NULL)
594         return ENOMEM;
595
596       if ((strcmp("127.0.0.1", st->host) == 0) ||
597           (strcmp("localhost", st->host) == 0))
598         sfree(st->host);
599     } else {
600       st->connhost = strdup(MEMCACHED_DEF_HOST);
601       if (st->connhost == NULL)
602         return ENOMEM;
603     }
604   }
605
606   if (st->connport == NULL) {
607     st->connport = strdup(MEMCACHED_DEF_PORT);
608     if (st->connport == NULL)
609       return ENOMEM;
610   }
611
612   assert(st->connhost != NULL);
613   assert(st->connport != NULL);
614
615   return 0;
616 } /* int memcached_set_defaults */
617
618 static int memcached_add_read_callback(memcached_t *st) {
619   char callback_name[3 * DATA_MAX_NAME_LEN];
620
621   if (memcached_set_defaults(st) != 0) {
622     memcached_free(st);
623     return -1;
624   }
625
626   snprintf(callback_name, sizeof(callback_name), "memcached/%s",
627            (st->name != NULL) ? st->name : "__legacy__");
628
629   return plugin_register_complex_read(
630       /* group = */ "memcached",
631       /* name      = */ callback_name,
632       /* callback  = */ memcached_read,
633       /* interval  = */ 0,
634       &(user_data_t){
635           .data = st, .free_func = memcached_free,
636       });
637 } /* int memcached_add_read_callback */
638
639 /* Configuration handling functiions
640  * <Plugin memcached>
641  *   <Instance "instance_name">
642  *     Host foo.zomg.com
643  *     Address 1.2.3.4
644  *     Port "1234"
645  *   </Instance>
646  * </Plugin>
647  */
648 static int config_add_instance(oconfig_item_t *ci) {
649   memcached_t *st;
650   int status = 0;
651
652   /* Disable automatic generation of default instance in the init callback. */
653   memcached_have_instances = 1;
654
655   st = calloc(1, sizeof(*st));
656   if (st == NULL) {
657     ERROR("memcached plugin: calloc failed.");
658     return ENOMEM;
659   }
660
661   st->name = NULL;
662   st->host = NULL;
663   st->socket = NULL;
664   st->connhost = NULL;
665   st->connport = NULL;
666
667   st->fd = -1;
668
669   if (strcasecmp(ci->key, "Instance") == 0)
670     status = cf_util_get_string(ci, &st->name);
671
672   if (status != 0) {
673     sfree(st);
674     return status;
675   }
676
677   for (int i = 0; i < ci->children_num; i++) {
678     oconfig_item_t *child = ci->children + i;
679
680     if (strcasecmp("Socket", child->key) == 0)
681       status = cf_util_get_string(child, &st->socket);
682     else if (strcasecmp("Host", child->key) == 0)
683       status = cf_util_get_string(child, &st->host);
684     else if (strcasecmp("Address", child->key) == 0)
685       status = cf_util_get_string(child, &st->connhost);
686     else if (strcasecmp("Port", child->key) == 0)
687       status = cf_util_get_service(child, &st->connport);
688     else {
689       WARNING("memcached plugin: Option `%s' not allowed here.", child->key);
690       status = -1;
691     }
692
693     if (status != 0)
694       break;
695   }
696
697   if (status != 0) {
698     memcached_free(st);
699     return -1;
700   }
701
702   return memcached_add_read_callback(st);
703 } /* int config_add_instance */
704
705 static int memcached_config(oconfig_item_t *ci) {
706   _Bool have_instance_block = 0;
707
708   for (int i = 0; i < ci->children_num; i++) {
709     oconfig_item_t *child = ci->children + i;
710
711     if (strcasecmp("Instance", child->key) == 0) {
712       config_add_instance(child);
713       have_instance_block = 1;
714     } else if (!have_instance_block) {
715       /* Non-instance option: Assume legacy configuration (without <Instance />
716        * blocks) and call config_add_instance() with the <Plugin /> block. */
717       return config_add_instance(ci);
718     } else
719       WARNING("memcached plugin: The configuration option "
720               "\"%s\" is not allowed here. Did you "
721               "forget to add an <Instance /> block "
722               "around the configuration?",
723               child->key);
724   } /* for (ci->children) */
725
726   return 0;
727 } /* int memcached_config */
728
729 static int memcached_init(void) {
730   memcached_t *st;
731   int status;
732
733   if (memcached_have_instances)
734     return 0;
735
736   /* No instances were configured, lets start a default instance. */
737   st = calloc(1, sizeof(*st));
738   if (st == NULL)
739     return ENOMEM;
740   st->name = NULL;
741   st->host = NULL;
742   st->socket = NULL;
743   st->connhost = NULL;
744   st->connport = NULL;
745
746   st->fd = -1;
747
748   status = memcached_add_read_callback(st);
749   if (status == 0)
750     memcached_have_instances = 1;
751
752   return status;
753 } /* int memcached_init */
754
755 void module_register(void) {
756   plugin_register_complex_config("memcached", memcached_config);
757   plugin_register_init("memcached", memcached_init);
758 }