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