Merge pull request #3339 from jkohen/patch-1
[collectd.git] / src / swap.c
1 /**
2  * collectd - src/swap.c
3  * Copyright (C) 2005-2014  Florian octo Forster
4  * Copyright (C) 2009       Stefan Völkel
5  * Copyright (C) 2009       Manuel Sanmartin
6  * Copyright (C) 2010       Aurélien Reynaud
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; only version 2 of the License is applicable.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Florian octo Forster <octo at collectd.org>
23  *   Manuel Sanmartin
24  *   Aurélien Reynaud <collectd at wattapower.net>
25  **/
26
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 # undef HAVE_CONFIG_H
30 #endif
31 /* avoid swap.h error "Cannot use swapctl in the large files compilation environment" */
32 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
33 #  undef _FILE_OFFSET_BITS
34 #  undef _LARGEFILE64_SOURCE
35 #endif
36
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40
41 #if HAVE_SYS_SWAP_H
42 # include <sys/swap.h>
43 #endif
44 #if HAVE_VM_ANON_H
45 # include <vm/anon.h>
46 #endif
47 #if HAVE_SYS_PARAM_H
48 #  include <sys/param.h>
49 #endif
50 #if HAVE_SYS_SYSCTL_H
51 #  include <sys/sysctl.h>
52 #endif
53 #if HAVE_SYS_DKSTAT_H
54 #  include <sys/dkstat.h>
55 #endif
56 #if HAVE_KVM_H
57 #  include <kvm.h>
58 #endif
59
60 #if HAVE_STATGRAB_H
61 # include <statgrab.h>
62 #endif
63
64 #if HAVE_PERFSTAT
65 # include <sys/protosw.h>
66 # include <libperfstat.h>
67 #endif
68
69 #undef  MAX
70 #define MAX(x,y) ((x) > (y) ? (x) : (y))
71
72 #if KERNEL_LINUX
73 # define SWAP_HAVE_REPORT_BY_DEVICE 1
74 static derive_t pagesize;
75 static _Bool report_bytes = 0;
76 static _Bool report_by_device = 0;
77 /* #endif KERNEL_LINUX */
78
79 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
80 # define SWAP_HAVE_REPORT_BY_DEVICE 1
81 static derive_t pagesize;
82 static _Bool report_by_device = 0;
83 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
84
85 #elif defined(VM_SWAPUSAGE)
86 /* No global variables */
87 /* #endif defined(VM_SWAPUSAGE) */
88
89 #elif HAVE_LIBKVM_GETSWAPINFO
90 static kvm_t *kvm_obj = NULL;
91 int kvm_pagesize;
92 /* #endif HAVE_LIBKVM_GETSWAPINFO */
93
94 #elif HAVE_LIBSTATGRAB
95 /* No global variables */
96 /* #endif HAVE_LIBSTATGRAB */
97
98 #elif HAVE_PERFSTAT
99 static int pagesize;
100 /*# endif HAVE_PERFSTAT */
101
102 #else
103 # error "No applicable input method."
104 #endif /* HAVE_LIBSTATGRAB */
105
106 static _Bool values_absolute = 1;
107 static _Bool values_percentage = 0;
108
109 static int swap_config (oconfig_item_t *ci) /* {{{ */
110 {
111         int i;
112
113         for (i = 0; i < ci->children_num; i++)
114         {
115                 oconfig_item_t *child = ci->children + i;
116                 if (strcasecmp ("ReportBytes", child->key) == 0)
117 #if KERNEL_LINUX
118                         cf_util_get_boolean (child, &report_bytes);
119 #else
120                         WARNING ("swap plugin: The \"ReportBytes\" option "
121                                         "is only valid under Linux. "
122                                         "The option is going to be ignored.");
123 #endif
124                 else if (strcasecmp ("ReportByDevice", child->key) == 0)
125 #if SWAP_HAVE_REPORT_BY_DEVICE
126                         cf_util_get_boolean (child, &report_by_device);
127 #else
128                         WARNING ("swap plugin: The \"ReportByDevice\" option "
129                                         "is not supported on this platform. "
130                                         "The option is going to be ignored.");
131 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
132                 else if (strcasecmp ("ValuesAbsolute", child->key) == 0)
133                         cf_util_get_boolean (child, &values_absolute);
134                 else if (strcasecmp ("ValuesPercentage", child->key) == 0)
135                         cf_util_get_boolean (child, &values_percentage);
136                 else
137                         WARNING ("swap plugin: Unknown config option: \"%s\"",
138                                         child->key);
139         }
140
141         return (0);
142 } /* }}} int swap_config */
143
144 static int swap_init (void) /* {{{ */
145 {
146 #if KERNEL_LINUX
147         pagesize = (derive_t) sysconf (_SC_PAGESIZE);
148 /* #endif KERNEL_LINUX */
149
150 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
151         /* getpagesize(3C) tells me this does not fail.. */
152         pagesize = (derive_t) getpagesize ();
153 /* #endif HAVE_SWAPCTL */
154
155 #elif defined(VM_SWAPUSAGE)
156         /* No init stuff */
157 /* #endif defined(VM_SWAPUSAGE) */
158
159 #elif HAVE_LIBKVM_GETSWAPINFO
160         if (kvm_obj != NULL)
161         {
162                 kvm_close (kvm_obj);
163                 kvm_obj = NULL;
164         }
165
166         kvm_pagesize = getpagesize ();
167
168         if ((kvm_obj = kvm_open (NULL, /* execfile */
169                                         NULL, /* corefile */
170                                         NULL, /* swapfile */
171                                         O_RDONLY, /* flags */
172                                         NULL)) /* errstr */
173                         == NULL)
174         {
175                 ERROR ("swap plugin: kvm_open failed.");
176                 return (-1);
177         }
178 /* #endif HAVE_LIBKVM_GETSWAPINFO */
179
180 #elif HAVE_LIBSTATGRAB
181         /* No init stuff */
182 /* #endif HAVE_LIBSTATGRAB */
183
184 #elif HAVE_PERFSTAT
185         pagesize = getpagesize();
186 #endif /* HAVE_PERFSTAT */
187
188         return (0);
189 } /* }}} int swap_init */
190
191 static void swap_submit_usage (char const *plugin_instance, /* {{{ */
192                 gauge_t used, gauge_t free,
193                 char const *other_name, gauge_t other_value)
194 {
195         value_t v[1];
196         value_list_t vl = VALUE_LIST_INIT;
197
198         vl.values = v;
199         vl.values_len = STATIC_ARRAY_SIZE (v);
200         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
201         sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
202         if (plugin_instance != NULL)
203                 sstrncpy (vl.plugin_instance, plugin_instance,
204                                 sizeof (vl.plugin_instance));
205         sstrncpy (vl.type, "swap", sizeof (vl.type));
206
207         if (values_absolute)
208                 plugin_dispatch_multivalue (&vl, 0,
209                                 "used", used, "free", free,
210                                 other_name, other_value, NULL);
211         if (values_percentage)
212                 plugin_dispatch_multivalue (&vl, 1,
213                                 "used", used, "free", free,
214                                 other_name, other_value, NULL);
215 } /* }}} void swap_submit_usage */
216
217 #if KERNEL_LINUX || HAVE_PERFSTAT
218 __attribute__((nonnull(1)))
219 static void swap_submit_derive (char const *type_instance, /* {{{ */
220                 derive_t value)
221 {
222         value_list_t vl = VALUE_LIST_INIT;
223         value_t v[1];
224
225         v[0].derive = value;
226
227         vl.values = v;
228         vl.values_len = STATIC_ARRAY_SIZE (v);
229         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
230         sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
231         sstrncpy (vl.type, "swap_io", sizeof (vl.type));
232         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
233
234         plugin_dispatch_values (&vl);
235 } /* }}} void swap_submit_derive */
236 #endif
237
238 #if KERNEL_LINUX
239 static int swap_read_separate (void) /* {{{ */
240 {
241         FILE *fh;
242         char buffer[1024];
243
244         fh = fopen ("/proc/swaps", "r");
245         if (fh == NULL)
246         {
247                 char errbuf[1024];
248                 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
249                                 sstrerror (errno, errbuf, sizeof (errbuf)));
250                 return (-1);
251         }
252
253         while (fgets (buffer, sizeof (buffer), fh) != NULL)
254         {
255                 char *fields[8];
256                 int numfields;
257                 char *endptr;
258
259                 char path[PATH_MAX];
260                 gauge_t total;
261                 gauge_t used;
262
263                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
264                 if (numfields != 5)
265                         continue;
266
267                 sstrncpy (path, fields[0], sizeof (path));
268                 escape_slashes (path, sizeof (path));
269
270                 errno = 0;
271                 endptr = NULL;
272                 total = strtod (fields[2], &endptr);
273                 if ((endptr == fields[2]) || (errno != 0))
274                         continue;
275
276                 errno = 0;
277                 endptr = NULL;
278                 used = strtod (fields[3], &endptr);
279                 if ((endptr == fields[3]) || (errno != 0))
280                         continue;
281
282                 if (total < used)
283                         continue;
284
285                 swap_submit_usage (path, used, total - used, NULL, NAN);
286         }
287
288         fclose (fh);
289
290         return (0);
291 } /* }}} int swap_read_separate */
292
293 static int swap_read_combined (void) /* {{{ */
294 {
295         FILE *fh;
296         char buffer[1024];
297
298         uint8_t have_data = 0;
299         gauge_t swap_used   = 0.0;
300         gauge_t swap_cached = 0.0;
301         gauge_t swap_free   = 0.0;
302         gauge_t swap_total  = 0.0;
303
304         fh = fopen ("/proc/meminfo", "r");
305         if (fh == NULL)
306         {
307                 char errbuf[1024];
308                 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
309                                 sstrerror (errno, errbuf, sizeof (errbuf)));
310                 return (-1);
311         }
312
313         while (fgets (buffer, sizeof (buffer), fh) != NULL)
314         {
315                 char *fields[8];
316                 int numfields;
317
318                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
319                 if (numfields < 2)
320                         continue;
321
322                 if (strcasecmp (fields[0], "SwapTotal:") == 0)
323                 {
324                         swap_total = strtod (fields[1], /* endptr = */ NULL);
325                         have_data |= 0x01;
326                 }
327                 else if (strcasecmp (fields[0], "SwapFree:") == 0)
328                 {
329                         swap_free = strtod (fields[1], /* endptr = */ NULL);
330                         have_data |= 0x02;
331                 }
332                 else if (strcasecmp (fields[0], "SwapCached:") == 0)
333                 {
334                         swap_cached = strtod (fields[1], /* endptr = */ NULL);
335                         have_data |= 0x04;
336                 }
337         }
338
339         fclose (fh);
340
341         if (have_data != 0x07)
342                 return (ENOENT);
343
344         if (isnan (swap_total)
345                         || (swap_total <= 0.0)
346                         || ((swap_free + swap_cached) > swap_total))
347                 return (EINVAL);
348
349         swap_used = swap_total - (swap_free + swap_cached);
350
351         swap_submit_usage (NULL, swap_used, swap_free, "cached", swap_cached);
352         return (0);
353 } /* }}} int swap_read_combined */
354
355 static int swap_read_io (void) /* {{{ */
356 {
357         FILE *fh;
358         char buffer[1024];
359
360         _Bool old_kernel = 0;
361
362         uint8_t have_data = 0;
363         derive_t swap_in  = 0;
364         derive_t swap_out = 0;
365
366         fh = fopen ("/proc/vmstat", "r");
367         if (fh == NULL)
368         {
369                 /* /proc/vmstat does not exist in kernels <2.6 */
370                 fh = fopen ("/proc/stat", "r");
371                 if (fh == NULL)
372                 {
373                         char errbuf[1024];
374                         WARNING ("swap: fopen: %s",
375                                         sstrerror (errno, errbuf, sizeof (errbuf)));
376                         return (-1);
377                 }
378                 else
379                         old_kernel = 1;
380         }
381
382         while (fgets (buffer, sizeof (buffer), fh) != NULL)
383         {
384                 char *fields[8];
385                 int numfields;
386
387                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
388
389                 if (!old_kernel)
390                 {
391                         if (numfields != 2)
392                                 continue;
393
394                         if (strcasecmp ("pswpin", fields[0]) == 0)
395                         {
396                                 strtoderive (fields[1], &swap_in);
397                                 have_data |= 0x01;
398                         }
399                         else if (strcasecmp ("pswpout", fields[0]) == 0)
400                         {
401                                 strtoderive (fields[1], &swap_out);
402                                 have_data |= 0x02;
403                         }
404                 }
405                 else /* if (old_kernel) */
406                 {
407                         if (numfields != 3)
408                                 continue;
409
410                         if (strcasecmp ("page", fields[0]) == 0)
411                         {
412                                 strtoderive (fields[1], &swap_in);
413                                 strtoderive (fields[2], &swap_out);
414                         }
415                 }
416         } /* while (fgets) */
417
418         fclose (fh);
419
420         if (have_data != 0x03)
421                 return (ENOENT);
422
423         if (report_bytes)
424         {
425                 swap_in = swap_in * pagesize;
426                 swap_out = swap_out * pagesize;
427         }
428
429         swap_submit_derive ("in",  swap_in);
430         swap_submit_derive ("out", swap_out);
431
432         return (0);
433 } /* }}} int swap_read_io */
434
435 static int swap_read (void) /* {{{ */
436 {
437         if (report_by_device)
438                 swap_read_separate ();
439         else
440                 swap_read_combined ();
441
442         swap_read_io ();
443
444         return (0);
445 } /* }}} int swap_read */
446 /* #endif KERNEL_LINUX */
447
448 /*
449  * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
450  * and kstat. The former reads physical space used on a device, the latter
451  * reports the view from the virtual memory system. It was decided that the
452  * kstat-based information should be moved to the "vmem" plugin, but nobody
453  * with enough Solaris experience was available at that time to do this. The
454  * code below is still there for your reference but it won't be activated in
455  * *this* plugin again. --octo
456  */
457 #elif 0 && HAVE_LIBKSTAT
458 /* kstat-based read function */
459 static int swap_read_kstat (void) /* {{{ */
460 {
461         gauge_t swap_alloc;
462         gauge_t swap_resv;
463         gauge_t swap_avail;
464
465         struct anoninfo ai;
466
467         if (swapctl (SC_AINFO, &ai) == -1)
468         {
469                 char errbuf[1024];
470                 ERROR ("swap plugin: swapctl failed: %s",
471                                 sstrerror (errno, errbuf, sizeof (errbuf)));
472                 return (-1);
473         }
474
475         /*
476          * Calculations from:
477          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
478          * Also see:
479          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
480          * /usr/include/vm/anon.h
481          *
482          * In short, swap -s shows: allocated + reserved = used, available
483          *
484          * However, Solaris does not allow to allocated/reserved more than the
485          * available swap (physical memory + disk swap), so the pedant may
486          * prefer: allocated + unallocated = reserved, available
487          *
488          * We map the above to: used + resv = n/a, free
489          *
490          * Does your brain hurt yet?  - Christophe Kalt
491          *
492          * Oh, and in case you wonder,
493          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
494          * can suffer from a 32bit overflow.
495          */
496         swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
497         swap_resv  = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
498         swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
499
500         swap_submit_usage (NULL, swap_alloc, swap_avail, "reserved", swap_resv);
501         return (0);
502 } /* }}} int swap_read_kstat */
503 /* #endif 0 && HAVE_LIBKSTAT */
504
505 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
506 /* swapctl-based read function */
507 static int swap_read (void) /* {{{ */
508 {
509         swaptbl_t *s;
510         char *s_paths;
511         int swap_num;
512         int status;
513         int i;
514
515         gauge_t avail = 0;
516         gauge_t total = 0;
517
518         swap_num = swapctl (SC_GETNSWP, NULL);
519         if (swap_num < 0)
520         {
521                 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
522                                 swap_num);
523                 return (-1);
524         }
525         else if (swap_num == 0)
526                 return (0);
527
528         /* Allocate and initialize the swaptbl_t structure */
529         s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
530         if (s == NULL)
531         {
532                 ERROR ("swap plugin: smalloc failed.");
533                 return (-1);
534         }
535
536         /* Memory to store the path names. We only use these paths when the
537          * separate option has been configured, but it's easier to just
538          * allocate enough memory in any case. */
539         s_paths = calloc (swap_num, PATH_MAX);
540         if (s_paths == NULL)
541         {
542                 ERROR ("swap plugin: malloc failed.");
543                 sfree (s);
544                 return (-1);
545         }
546         for (i = 0; i < swap_num; i++)
547                 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
548         s->swt_n = swap_num;
549
550         status = swapctl (SC_LIST, s);
551         if (status < 0)
552         {
553                 char errbuf[1024];
554                 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
555                                 sstrerror (errno, errbuf, sizeof (errbuf)));
556                 sfree (s_paths);
557                 sfree (s);
558                 return (-1);
559         }
560         else if (swap_num < status)
561         {
562                 /* more elements returned than requested */
563                 ERROR ("swap plugin: I allocated memory for %i structure%s, "
564                                 "but swapctl(2) claims to have returned %i. "
565                                 "I'm confused and will give up.",
566                                 swap_num, (swap_num == 1) ? "" : "s",
567                                 status);
568                 sfree (s_paths);
569                 sfree (s);
570                 return (-1);
571         }
572         else if (swap_num > status)
573                 /* less elements returned than requested */
574                 swap_num = status;
575
576         for (i = 0; i < swap_num; i++)
577         {
578                 char path[PATH_MAX];
579                 gauge_t this_total;
580                 gauge_t this_avail;
581
582                 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
583                         continue;
584
585                 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
586                 this_avail = (gauge_t) (s->swt_ent[i].ste_free  * pagesize);
587
588                 /* Shortcut for the "combined" setting (default) */
589                 if (!report_by_device)
590                 {
591                         avail += this_avail;
592                         total += this_total;
593                         continue;
594                 }
595
596                 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
597                 escape_slashes (path, sizeof (path));
598
599                 swap_submit_usage (path, this_total - this_avail, this_avail,
600                                 NULL, NAN);
601         } /* for (swap_num) */
602
603         if (total < avail)
604         {
605                 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
606                                 total, avail);
607                 sfree (s_paths);
608                 sfree (s);
609                 return (-1);
610         }
611
612         /* If the "separate" option was specified (report_by_device == 1), all
613          * values have already been dispatched from within the loop. */
614         if (!report_by_device)
615                 swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
616
617         sfree (s_paths);
618         sfree (s);
619         return (0);
620 } /* }}} int swap_read */
621 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
622
623 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
624 static int swap_read (void) /* {{{ */
625 {
626         struct swapent *swap_entries;
627         int swap_num;
628         int status;
629         int i;
630
631         gauge_t used  = 0;
632         gauge_t total = 0;
633
634         swap_num = swapctl (SWAP_NSWAP, NULL, 0);
635         if (swap_num < 0)
636         {
637                 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
638                                 swap_num);
639                 return (-1);
640         }
641         else if (swap_num == 0)
642                 return (0);
643
644         swap_entries = calloc (swap_num, sizeof (*swap_entries));
645         if (swap_entries == NULL)
646         {
647                 ERROR ("swap plugin: calloc failed.");
648                 return (-1);
649         }
650
651         status = swapctl (SWAP_STATS, swap_entries, swap_num);
652         if (status != swap_num)
653         {
654                 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
655                                 status);
656                 sfree (swap_entries);
657                 return (-1);
658         }
659
660 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
661 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
662 #else
663 # define C_SWAP_BLOCK_SIZE 512.0
664 #endif
665
666         /* TODO: Report per-device stats. The path name is available from
667          * swap_entries[i].se_path */
668         for (i = 0; i < swap_num; i++)
669         {
670                 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
671                         continue;
672
673                 used  += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
674                 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
675         }
676
677         if (total < used)
678         {
679                 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
680                                 total, used);
681                 return (-1);
682         }
683
684         swap_submit_usage (NULL, used, total - used, NULL, NAN);
685
686         sfree (swap_entries);
687         return (0);
688 } /* }}} int swap_read */
689 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
690
691 #elif defined(VM_SWAPUSAGE)
692 static int swap_read (void) /* {{{ */
693 {
694         int              mib[3];
695         size_t           mib_len;
696         struct xsw_usage sw_usage;
697         size_t           sw_usage_len;
698
699         mib_len = 2;
700         mib[0]  = CTL_VM;
701         mib[1]  = VM_SWAPUSAGE;
702
703         sw_usage_len = sizeof (struct xsw_usage);
704
705         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
706                 return (-1);
707
708         /* The returned values are bytes. */
709         swap_submit_usage (NULL,
710                         (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
711                         NULL, NAN);
712
713         return (0);
714 } /* }}} int swap_read */
715 /* #endif VM_SWAPUSAGE */
716
717 #elif HAVE_LIBKVM_GETSWAPINFO
718 static int swap_read (void) /* {{{ */
719 {
720         struct kvm_swap data_s;
721         int             status;
722
723         gauge_t used;
724         gauge_t total;
725
726         if (kvm_obj == NULL)
727                 return (-1);
728
729         /* only one structure => only get the grand total, no details */
730         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
731         if (status == -1)
732                 return (-1);
733
734         total = (gauge_t) data_s.ksw_total;
735         used  = (gauge_t) data_s.ksw_used;
736
737         total *= (gauge_t) kvm_pagesize;
738         used  *= (gauge_t) kvm_pagesize;
739
740         swap_submit_usage (NULL, used, total - used, NULL, NAN);
741
742         return (0);
743 } /* }}} int swap_read */
744 /* #endif HAVE_LIBKVM_GETSWAPINFO */
745
746 #elif HAVE_LIBSTATGRAB
747 static int swap_read (void) /* {{{ */
748 {
749         sg_swap_stats *swap;
750
751         swap = sg_get_swap_stats ();
752         if (swap == NULL)
753                 return (-1);
754
755         swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
756                         NULL, NAN);
757
758         return (0);
759 } /* }}} int swap_read */
760 /* #endif  HAVE_LIBSTATGRAB */
761
762 #elif HAVE_PERFSTAT
763 static int swap_read (void) /* {{{ */
764 {
765         perfstat_memory_total_t pmemory;
766         int status;
767
768         gauge_t total;
769         gauge_t free;
770         gauge_t reserved;
771
772         memset (&pmemory, 0, sizeof (pmemory));
773         status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
774         if (status < 0)
775         {
776                 char errbuf[1024];
777                 WARNING ("swap plugin: perfstat_memory_total failed: %s",
778                         sstrerror (errno, errbuf, sizeof (errbuf)));
779                 return (-1);
780         }
781
782         total    = (gauge_t) (pmemory.pgsp_total * pagesize);
783         free     = (gauge_t) (pmemory.pgsp_free * pagesize);
784         reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
785
786         swap_submit_usage (NULL, total - free, free, "reserved", reserved);
787         swap_submit_derive ("in",  (derive_t) pmemory.pgspins * pagesize);
788         swap_submit_derive ("out", (derive_t) pmemory.pgspouts * pagesize);
789
790         return (0);
791 } /* }}} int swap_read */
792 #endif /* HAVE_PERFSTAT */
793
794 void module_register (void)
795 {
796         plugin_register_complex_config ("swap", swap_config);
797         plugin_register_init ("swap", swap_init);
798         plugin_register_read ("swap", swap_read);
799 } /* void module_register */
800
801 /* vim: set fdm=marker : */