39c599c2cc4ddd9cbb98b865696ecc3d430c12c3
[collectd.git] / src / memcached.c
1 /**
2  * collectd - src/memcached.c
3  * Copyright (C) 2007  Antony Dovgal, heavily based on hddtemp.c
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Antony Dovgal <tony at daylessday dot org>
21  *
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28
29 # include <poll.h>
30 # include <netdb.h>
31 # include <sys/socket.h>
32 # include <netinet/in.h>
33 # include <netinet/tcp.h>
34 # include <libgen.h> /* for basename */
35
36 #if HAVE_LINUX_MAJOR_H
37 # include <linux/major.h>
38 #endif
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         "Host",
48         "Port",
49         NULL
50 };
51 static int config_keys_num = 2;
52
53 static char *memcached_host = NULL;
54 static char memcached_port[16];
55
56 static int memcached_query_daemon (char *buffer, int buffer_size) /* {{{ */
57 {
58         int fd;
59         ssize_t status;
60         int buffer_fill;
61
62         const char *host;
63         const char *port;
64
65         struct addrinfo  ai_hints;
66         struct addrinfo *ai_list, *ai_ptr;
67         int              ai_return, i = 0;
68
69         memset (&ai_hints, '\0', sizeof (ai_hints));
70         ai_hints.ai_flags    = 0;
71 #ifdef AI_ADDRCONFIG
72 /*      ai_hints.ai_flags   |= AI_ADDRCONFIG; */
73 #endif
74         ai_hints.ai_family   = AF_INET;
75         ai_hints.ai_socktype = SOCK_STREAM;
76         ai_hints.ai_protocol = 0;
77
78         host = memcached_host;
79         if (host == NULL) {
80                 host = MEMCACHED_DEF_HOST;
81         }
82
83         port = memcached_port;
84         if (strlen (port) == 0) {
85                 port = MEMCACHED_DEF_PORT;
86         }
87
88         if ((ai_return = getaddrinfo (host, port, NULL, &ai_list)) != 0) {
89                 char errbuf[1024];
90                 ERROR ("memcached: getaddrinfo (%s, %s): %s",
91                                 host, port,
92                                 (ai_return == EAI_SYSTEM)
93                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
94                                 : gai_strerror (ai_return));
95                 return -1;
96         }
97
98         fd = -1;
99         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
100                 /* create our socket descriptor */
101                 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0) {
102                         char errbuf[1024];
103                         ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
104                         continue;
105                 }
106
107                 /* connect to the memcached daemon */
108                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
109                         shutdown(fd, SHUT_RDWR);
110                         close(fd);
111                         fd = -1;
112                         continue;
113                 }
114
115                 /* A socket could be opened and connecting succeeded. We're
116                  * done. */
117                 break;
118         }
119
120         freeaddrinfo (ai_list);
121
122         if (fd < 0) {
123                 ERROR ("memcached: Could not connect to daemon.");
124                 return -1;
125         }
126
127         if (send(fd, "stats\r\n", sizeof("stats\r\n") - 1, MSG_DONTWAIT) != (sizeof("stats\r\n") - 1)) {
128                 ERROR ("memcached: Could not send command to the memcached daemon.");
129                 return -1;
130         }
131
132         {
133                 struct pollfd p;
134                 int n;
135
136                 p.fd = fd;
137                 p.events = POLLIN|POLLERR|POLLHUP;
138                 p.revents = 0;
139
140                 n = poll(&p, 1, 3);
141
142                 if (n <= 0) {
143                         ERROR ("memcached: poll() failed or timed out");
144                         return -1;
145                 }
146         }
147
148         /* receive data from the memcached daemon */
149         memset (buffer, '\0', buffer_size);
150
151         buffer_fill = 0;
152         while ((status = recv (fd, buffer + buffer_fill, buffer_size - buffer_fill, MSG_DONTWAIT)) != 0) {
153                 if (i > MEMCACHED_RETRY_COUNT) {
154                         ERROR("recv() timed out");
155                         break;
156                 }
157                 i++;
158
159                 if (status == -1) {
160                         char errbuf[1024];
161
162                         if (errno == EAGAIN) {
163                                 continue;
164                         }
165
166                         ERROR ("memcached: Error reading from socket: %s",
167                                         sstrerror (errno, errbuf, sizeof (errbuf)));
168                         shutdown(fd, SHUT_RDWR);
169                         close (fd);
170                         return -1;
171                 }
172                 buffer_fill += status;
173
174                 if (buffer_fill > 3 && buffer[buffer_fill-5] == 'E' && buffer[buffer_fill-4] == 'N' && buffer[buffer_fill-3] == 'D') {
175                         /* we got all the data */
176                         break;
177                 }
178         }
179
180         if (buffer_fill >= buffer_size) {
181                 buffer[buffer_size - 1] = '\0';
182                 WARNING ("memcached: Message from memcached has been truncated.");
183         } else if (buffer_fill == 0) {
184                 WARNING ("memcached: Peer has unexpectedly shut down the socket. "
185                                 "Buffer: `%s'", buffer);
186                 shutdown(fd, SHUT_RDWR);
187                 close(fd);
188                 return -1;
189         }
190
191         shutdown(fd, SHUT_RDWR);
192         close(fd);
193         return 0;
194 }
195 /* }}} */
196
197 static int memcached_config (const char *key, const char *value) /* {{{ */
198 {
199         if (strcasecmp (key, "Host") == 0) {
200                 if (memcached_host != NULL) {
201                         free (memcached_host);
202                 }
203                 memcached_host = strdup (value);
204         } else if (strcasecmp (key, "Port") == 0) {
205                 int port = (int) (atof (value));
206                 if ((port > 0) && (port <= 65535)) {
207                         snprintf (memcached_port, sizeof (memcached_port), "%i", port);
208                 } else {
209                         strncpy (memcached_port, value, sizeof (memcached_port));
210                 }
211                 memcached_port[sizeof (memcached_port) - 1] = '\0';
212         } else {
213                 return -1;
214         }
215
216         return 0;
217 }
218 /* }}} */
219
220 static void submit_counter (const char *type, const char *type_inst,
221                 counter_t value) /* {{{ */
222 {
223         value_t values[1];
224         value_list_t vl = VALUE_LIST_INIT;
225
226         values[0].counter = value;
227
228         vl.values = values;
229         vl.values_len = 1;
230         vl.time = time (NULL);
231         strcpy (vl.host, hostname_g);
232         strcpy (vl.plugin, "memcached");
233         if (type_inst != NULL)
234         {
235                 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
236                 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
237         }
238
239         plugin_dispatch_values (type, &vl);
240 } /* void memcached_submit_cmd */
241 /* }}} */
242
243 static void submit_counter2 (const char *type, const char *type_inst,
244                 counter_t value0, counter_t value1) /* {{{ */
245 {
246         value_t values[2];
247         value_list_t vl = VALUE_LIST_INIT;
248
249         values[0].counter = value0;
250         values[1].counter = value1;
251
252         vl.values = values;
253         vl.values_len = 2;
254         vl.time = time (NULL);
255         strcpy (vl.host, hostname_g);
256         strcpy (vl.plugin, "memcached");
257         if (type_inst != NULL)
258         {
259                 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
260                 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
261         }
262
263         plugin_dispatch_values (type, &vl);
264 } /* void memcached_submit_cmd */
265 /* }}} */
266
267 static void submit_gauge (const char *type, const char *type_inst,
268                 gauge_t value) /* {{{ */
269 {
270         value_t values[1];
271         value_list_t vl = VALUE_LIST_INIT;
272
273         values[0].gauge = value;
274
275         vl.values = values;
276         vl.values_len = 1;
277         vl.time = time (NULL);
278         strcpy (vl.host, hostname_g);
279         strcpy (vl.plugin, "memcached");
280         if (type_inst != NULL)
281         {
282                 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
283                 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
284         }
285
286         plugin_dispatch_values (type, &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         strcpy (vl.host, hostname_g);
303         strcpy (vl.plugin, "memcached");
304         if (type_inst != NULL)
305         {
306                 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
307                 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
308         }
309
310         plugin_dispatch_values (type, &vl);
311 }
312 /* }}} */
313
314 static int memcached_read (void) /* {{{ */
315 {
316         char buf[1024];
317         char *fields[3];
318         char *ptr;
319         char *line;
320         char *saveptr;
321         int fields_num;
322
323         gauge_t bytes_used = NAN;
324         gauge_t bytes_total = 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                 }
435
436                 /*
437                  * Operations on the cache, i. e. cache hits, cache misses and evictions of items
438                  */
439                 else if (FIELD_IS ("get_hits"))
440                 {
441                         submit_counter ("memcached_ops", "hits", atoll (fields[2]));
442                 }
443                 else if (FIELD_IS ("get_misses"))
444                 {
445                         submit_counter ("memcached_ops", "misses", atoll (fields[2]));
446                 }
447                 else if (FIELD_IS ("evictions"))
448                 {
449                         submit_counter ("memcached_ops", "evictions", atoll (fields[2]));
450                 }
451
452                 /*
453                  * Network traffic
454                  */
455                 else if (FIELD_IS ("bytes_read"))
456                 {
457                         octets_rx = atoll (fields[2]);
458                 }
459                 else if (FIELD_IS ("bytes_written"))
460                 {
461                         octets_tx = atoll (fields[2]);
462                 }
463         } /* while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) */
464
465         if (!isnan (bytes_used) && !isnan (bytes_total) && (bytes_used <= bytes_total))
466                 submit_gauge2 ("df", "cache", bytes_used, bytes_total - bytes_used);
467         
468         if ((rusage_user != 0) || (rusage_syst != 0))
469                 submit_counter2 ("ps_cputime", NULL, rusage_user, rusage_syst);
470
471         if ((octets_rx != 0) || (octets_tx != 0))
472                 submit_counter2 ("memcached_octets", NULL, octets_rx, octets_tx);
473         
474         return 0;
475 }
476 /* }}} */
477
478 void module_register (void) /* {{{ */
479 {
480         plugin_register_config ("memcached", memcached_config, config_keys, config_keys_num);
481         plugin_register_read ("memcached", memcached_read);
482 }
483 /* }}} */
484
485 /*
486  * Local variables:
487  * tab-width: 4
488  * c-basic-offset: 4
489  * End:
490  * vim600: sw=4 ts=4 fdm=marker
491  * vim<600: sw=4 ts=4
492  */
493