492094b669f1581358f29ab05296a560ada57f06
[collectd.git] / src / swap.c
1 /**
2  * collectd - src/swap.c
3  * Copyright (C) 2005-2009  Florian octo Forster
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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 # undef HAVE_CONFIG_H
25 #endif
26 /* avoid swap.h error "Cannot use swapctl in the large files compilation environment" */
27 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
28 #  undef _FILE_OFFSET_BITS
29 #  undef _LARGEFILE64_SOURCE
30 #endif
31
32 #include "collectd.h"
33 #include "common.h"
34 #include "plugin.h"
35
36 #if HAVE_SYS_SWAP_H
37 # include <sys/swap.h>
38 #endif
39 #if HAVE_VM_ANON_H
40 # include <vm/anon.h>
41 #endif
42 #if HAVE_SYS_PARAM_H
43 #  include <sys/param.h>
44 #endif
45 #if HAVE_SYS_SYSCTL_H
46 #  include <sys/sysctl.h>
47 #endif
48 #if HAVE_SYS_DKSTAT_H
49 #  include <sys/dkstat.h>
50 #endif
51 #if HAVE_KVM_H
52 #  include <kvm.h>
53 #endif
54
55 #if HAVE_STATGRAB_H
56 # include <statgrab.h>
57 #endif
58
59 #undef  MAX
60 #define MAX(x,y) ((x) > (y) ? (x) : (y))
61
62 #if KERNEL_LINUX
63 /* No global variables */
64 /* #endif KERNEL_LINUX */
65
66 #elif HAVE_LIBKSTAT
67 static derive_t pagesize;
68 static kstat_t *ksp;
69 /* #endif HAVE_LIBKSTAT */
70
71 #elif HAVE_SWAPCTL
72 /* No global variables */
73 /* #endif HAVE_SWAPCTL */
74
75 #elif defined(VM_SWAPUSAGE)
76 /* No global variables */
77 /* #endif defined(VM_SWAPUSAGE) */
78
79 #elif HAVE_LIBKVM_GETSWAPINFO
80 static kvm_t *kvm_obj = NULL;
81 int kvm_pagesize;
82 /* #endif HAVE_LIBKVM_GETSWAPINFO */
83
84 #elif HAVE_LIBSTATGRAB
85 /* No global variables */
86 /* #endif HAVE_LIBSTATGRAB */
87
88 #else
89 # error "No applicable input method."
90 #endif /* HAVE_LIBSTATGRAB */
91
92 static int swap_init (void)
93 {
94 #if KERNEL_LINUX
95         /* No init stuff */
96 /* #endif KERNEL_LINUX */
97
98 #elif HAVE_LIBKSTAT
99         /* getpagesize(3C) tells me this does not fail.. */
100         pagesize = (derive_t) getpagesize ();
101         if (get_kstat (&ksp, "unix", 0, "system_pages"))
102                 ksp = NULL;
103 /* #endif HAVE_LIBKSTAT */
104
105 #elif HAVE_SWAPCTL
106         /* No init stuff */
107 /* #endif HAVE_SWAPCTL */
108
109 #elif defined(VM_SWAPUSAGE)
110         /* No init stuff */
111 /* #endif defined(VM_SWAPUSAGE) */
112
113 #elif HAVE_LIBKVM_GETSWAPINFO
114         if (kvm_obj != NULL)
115         {
116                 kvm_close (kvm_obj);
117                 kvm_obj = NULL;
118         }
119
120         kvm_pagesize = getpagesize ();
121
122         if ((kvm_obj = kvm_open (NULL, /* execfile */
123                                         NULL, /* corefile */
124                                         NULL, /* swapfile */
125                                         O_RDONLY, /* flags */
126                                         NULL)) /* errstr */
127                         == NULL)
128         {
129                 ERROR ("swap plugin: kvm_open failed.");
130                 return (-1);
131         }
132 /* #endif HAVE_LIBKVM_GETSWAPINFO */
133
134 #elif HAVE_LIBSTATGRAB
135         /* No init stuff */
136 #endif /* HAVE_LIBSTATGRAB */
137
138         return (0);
139 }
140
141 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
142 {
143         value_t values[1];
144         value_list_t vl = VALUE_LIST_INIT;
145
146         switch (type)
147         {
148                 case DS_TYPE_GAUGE:
149                         values[0].gauge = (gauge_t) value;
150                         sstrncpy (vl.type, "swap", sizeof (vl.type));
151                         break;
152                 case DS_TYPE_DERIVE:
153                         values[0].derive = value;
154                         sstrncpy (vl.type, "swap_io", sizeof (vl.type));
155                         break;
156                 default:
157                         ERROR ("swap plugin: swap_submit called with wrong"
158                                 " type");
159         }
160
161         vl.values = values;
162         vl.values_len = 1;
163         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
164         sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
165         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
166
167         plugin_dispatch_values (&vl);
168 } /* void swap_submit */
169
170 static int swap_read (void)
171 {
172 #if KERNEL_LINUX
173         FILE *fh;
174         char buffer[1024];
175         
176         char *fields[8];
177         int numfields;
178
179         derive_t swap_used   = 0;
180         derive_t swap_cached = 0;
181         derive_t swap_free   = 0;
182         derive_t swap_total  = 0;
183         derive_t swap_in     = 0;
184         derive_t swap_out    = 0;
185
186         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
187         {
188                 char errbuf[1024];
189                 WARNING ("memory: fopen: %s",
190                                 sstrerror (errno, errbuf, sizeof (errbuf)));
191                 return (-1);
192         }
193
194         while (fgets (buffer, 1024, fh) != NULL)
195         {
196                 derive_t *val = NULL;
197
198                 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
199                         val = &swap_total;
200                 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
201                         val = &swap_free;
202                 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
203                         val = &swap_cached;
204                 else
205                         continue;
206
207                 numfields = strsplit (buffer, fields, 8);
208
209                 if (numfields < 2)
210                         continue;
211
212                 *val = (derive_t) atoll (fields[1]) * 1024LL;
213         }
214
215         if (fclose (fh))
216         {
217                 char errbuf[1024];
218                 WARNING ("memory: fclose: %s",
219                                 sstrerror (errno, errbuf, sizeof (errbuf)));
220         }
221
222         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
223                 return (-1);
224
225         swap_used = swap_total - (swap_free + swap_cached);
226
227         if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
228         {
229                 char errbuf[1024];
230                 WARNING ("swap: fopen: %s",
231                                 sstrerror (errno, errbuf, sizeof (errbuf)));
232                 return (-1);
233         }
234
235         while (fgets (buffer, 1024, fh) != NULL)
236         {
237                 derive_t *val = NULL;
238
239                 if (strncasecmp (buffer, "pswpin", 6) == 0)
240                         val = &swap_in;
241                 else if (strncasecmp (buffer, "pswpout", 7) == 0)
242                         val = &swap_out;
243                 else
244                         continue;
245
246                 numfields = strsplit (buffer, fields, 8);
247
248                 if (numfields < 2)
249                         continue;
250
251                 *val = (derive_t) atoll (fields[1]);
252         }
253
254         if (fclose (fh))
255         {
256                 char errbuf[1024];
257                 WARNING ("swap: fclose: %s",
258                                 sstrerror (errno, errbuf, sizeof (errbuf)));
259         }
260
261         swap_submit ("used", swap_used, DS_TYPE_GAUGE);
262         swap_submit ("free", swap_free, DS_TYPE_GAUGE);
263         swap_submit ("cached", swap_cached, DS_TYPE_GAUGE);
264         swap_submit ("in", swap_in, DS_TYPE_DERIVE);
265         swap_submit ("out", swap_out, DS_TYPE_DERIVE);
266
267 /* #endif KERNEL_LINUX */
268
269 #elif HAVE_LIBKSTAT
270         derive_t swap_alloc;
271         derive_t swap_resv;
272         derive_t swap_avail;
273
274         struct anoninfo ai;
275
276         if (swapctl (SC_AINFO, &ai) == -1)
277         {
278                 char errbuf[1024];
279                 ERROR ("swap plugin: swapctl failed: %s",
280                                 sstrerror (errno, errbuf, sizeof (errbuf)));
281                 return (-1);
282         }
283
284         /*
285          * Calculations from:
286          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
287          * Also see:
288          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
289          * /usr/include/vm/anon.h
290          *
291          * In short, swap -s shows: allocated + reserved = used, available
292          *
293          * However, Solaris does not allow to allocated/reserved more than the
294          * available swap (physical memory + disk swap), so the pedant may
295          * prefer: allocated + unallocated = reserved, available
296          * 
297          * We map the above to: used + resv = n/a, free
298          *
299          * Does your brain hurt yet?  - Christophe Kalt
300          *
301          * Oh, and in case you wonder,
302          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
303          * can suffer from a 32bit overflow.
304          */
305         swap_alloc  = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
306         swap_resv   = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
307                         * pagesize);
308         swap_avail  = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
309
310         swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
311         swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
312         swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
313 /* #endif HAVE_LIBKSTAT */
314
315 #elif HAVE_SWAPCTL
316         struct swapent *swap_entries;
317         int swap_num;
318         int status;
319         int i;
320
321         derive_t used  = 0;
322         derive_t total = 0;
323
324         /*
325          * XXX: This is the syntax for the *BSD `swapctl', which has the
326          * following prototype:
327          *   swapctl (int cmd, void *arg, int misc);
328          *
329          * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
330          * too, but with the following prototype:
331          *   swapctl (int cmd, void *arg);
332          *
333          * Solaris is usually handled in the KSTAT case above. For other UNIXes
334          * a separate case for the other version of `swapctl' may be necessary.
335          */
336         swap_num = swapctl (SWAP_NSWAP, NULL, 0);
337         if (swap_num < 0)
338         {
339                 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
340                                 swap_num);
341                 return (-1);
342         }
343         else if (swap_num == 0)
344                 return (0);
345
346         swap_entries = calloc (swap_num, sizeof (*swap_entries));
347         if (swap_entries == NULL)
348         {
349                 ERROR ("swap plugin: calloc failed.");
350                 return (-1);
351         }
352
353         status = swapctl (SWAP_STATS, swap_entries, swap_num);
354         if (status != swap_num)
355         {
356                 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
357                                 status);
358                 sfree (swap_entries);
359                 return (-1);
360         }
361
362 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
363 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
364 #else
365 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
366 #endif
367
368         for (i = 0; i < swap_num; i++)
369         {
370                 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
371                         continue;
372
373                 used  += ((derive_t) swap_entries[i].se_inuse)
374                         * C_SWAP_BLOCK_SIZE;
375                 total += ((derive_t) swap_entries[i].se_nblks)
376                         * C_SWAP_BLOCK_SIZE;
377         }
378
379         if (total < used)
380         {
381                 ERROR ("swap plugin: Total swap space (%"PRIu64") "
382                                 "is less than used swap space (%"PRIu64").",
383                                 total, used);
384                 return (-1);
385         }
386
387         swap_submit ("used", used, DS_TYPE_GAUGE);
388         swap_submit ("free", total - used, DS_TYPE_GAUGE);
389
390         sfree (swap_entries);
391 /* #endif HAVE_SWAPCTL */
392
393 #elif defined(VM_SWAPUSAGE)
394         int              mib[3];
395         size_t           mib_len;
396         struct xsw_usage sw_usage;
397         size_t           sw_usage_len;
398
399         mib_len = 2;
400         mib[0]  = CTL_VM;
401         mib[1]  = VM_SWAPUSAGE;
402
403         sw_usage_len = sizeof (struct xsw_usage);
404
405         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
406                 return (-1);
407
408         /* The returned values are bytes. */
409         swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
410         swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
411 /* #endif VM_SWAPUSAGE */
412
413 #elif HAVE_LIBKVM_GETSWAPINFO
414         struct kvm_swap data_s;
415         int             status;
416
417         derive_t used;
418         derive_t free;
419         derive_t total;
420
421         if (kvm_obj == NULL)
422                 return (-1);
423
424         /* only one structure => only get the grand total, no details */
425         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
426         if (status == -1)
427                 return (-1);
428
429         total = (derive_t) data_s.ksw_total;
430         used  = (derive_t) data_s.ksw_used;
431
432         total *= (derive_t) kvm_pagesize;
433         used  *= (derive_t) kvm_pagesize;
434
435         free = total - used;
436
437         swap_submit ("used", used, DS_TYPE_GAUGE);
438         swap_submit ("free", free, DS_TYPE_GAUGE);
439 /* #endif HAVE_LIBKVM_GETSWAPINFO */
440
441 #elif HAVE_LIBSTATGRAB
442         sg_swap_stats *swap;
443
444         swap = sg_get_swap_stats ();
445
446         if (swap == NULL)
447                 return (-1);
448
449         swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
450         swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
451 #endif /* HAVE_LIBSTATGRAB */
452
453         return (0);
454 } /* int swap_read */
455
456 void module_register (void)
457 {
458         plugin_register_init ("swap", swap_init);
459         plugin_register_read ("swap", swap_read);
460 } /* void module_register */