Merge branch 'collectd-5.4'
[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, DS_TYPE_GAUGE,
209                                 "used", used, "free", free,
210                                 other_name, other_value, NULL);
211         if (values_percentage)
212                 plugin_dispatch_multivalue (&vl, 1, DS_TYPE_GAUGE,
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         gauge_t swap_used   = NAN;
299         gauge_t swap_cached = NAN;
300         gauge_t swap_free   = NAN;
301         gauge_t swap_total  = NAN;
302
303         fh = fopen ("/proc/meminfo", "r");
304         if (fh == NULL)
305         {
306                 char errbuf[1024];
307                 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
308                                 sstrerror (errno, errbuf, sizeof (errbuf)));
309                 return (-1);
310         }
311
312         while (fgets (buffer, sizeof (buffer), fh) != NULL)
313         {
314                 char *fields[8];
315                 int numfields;
316
317                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
318                 if (numfields < 2)
319                         continue;
320
321                 if (strcasecmp (fields[0], "SwapTotal:") == 0)
322                         strtogauge (fields[1], &swap_total);
323                 else if (strcasecmp (fields[0], "SwapFree:") == 0)
324                         strtogauge (fields[1], &swap_free);
325                 else if (strcasecmp (fields[0], "SwapCached:") == 0)
326                         strtogauge (fields[1], &swap_cached);
327         }
328
329         fclose (fh);
330
331         if (isnan (swap_total) || isnan (swap_free))
332                 return (ENOENT);
333
334         /* Some systems, OpenVZ for example, don't provide SwapCached. */
335         if (isnan (swap_cached))
336                 swap_used = swap_total - swap_free;
337         else
338                 swap_used = swap_total - (swap_free + swap_cached);
339         assert (!isnan (swap_used));
340
341         if (swap_used < 0.0)
342                 return (EINVAL);
343
344         swap_submit_usage (NULL, swap_used, swap_free,
345                         isnan (swap_cached) ? NULL : "cached", swap_cached);
346         return (0);
347 } /* }}} int swap_read_combined */
348
349 static int swap_read_io (void) /* {{{ */
350 {
351         FILE *fh;
352         char buffer[1024];
353
354         _Bool old_kernel = 0;
355
356         uint8_t have_data = 0;
357         derive_t swap_in  = 0;
358         derive_t swap_out = 0;
359
360         fh = fopen ("/proc/vmstat", "r");
361         if (fh == NULL)
362         {
363                 /* /proc/vmstat does not exist in kernels <2.6 */
364                 fh = fopen ("/proc/stat", "r");
365                 if (fh == NULL)
366                 {
367                         char errbuf[1024];
368                         WARNING ("swap: fopen: %s",
369                                         sstrerror (errno, errbuf, sizeof (errbuf)));
370                         return (-1);
371                 }
372                 else
373                         old_kernel = 1;
374         }
375
376         while (fgets (buffer, sizeof (buffer), fh) != NULL)
377         {
378                 char *fields[8];
379                 int numfields;
380
381                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
382
383                 if (!old_kernel)
384                 {
385                         if (numfields != 2)
386                                 continue;
387
388                         if (strcasecmp ("pswpin", fields[0]) == 0)
389                         {
390                                 strtoderive (fields[1], &swap_in);
391                                 have_data |= 0x01;
392                         }
393                         else if (strcasecmp ("pswpout", fields[0]) == 0)
394                         {
395                                 strtoderive (fields[1], &swap_out);
396                                 have_data |= 0x02;
397                         }
398                 }
399                 else /* if (old_kernel) */
400                 {
401                         if (numfields != 3)
402                                 continue;
403
404                         if (strcasecmp ("page", fields[0]) == 0)
405                         {
406                                 strtoderive (fields[1], &swap_in);
407                                 strtoderive (fields[2], &swap_out);
408                         }
409                 }
410         } /* while (fgets) */
411
412         fclose (fh);
413
414         if (have_data != 0x03)
415                 return (ENOENT);
416
417         if (report_bytes)
418         {
419                 swap_in = swap_in * pagesize;
420                 swap_out = swap_out * pagesize;
421         }
422
423         swap_submit_derive ("in",  swap_in);
424         swap_submit_derive ("out", swap_out);
425
426         return (0);
427 } /* }}} int swap_read_io */
428
429 static int swap_read (void) /* {{{ */
430 {
431         if (report_by_device)
432                 swap_read_separate ();
433         else
434                 swap_read_combined ();
435
436         swap_read_io ();
437
438         return (0);
439 } /* }}} int swap_read */
440 /* #endif KERNEL_LINUX */
441
442 /*
443  * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
444  * and kstat. The former reads physical space used on a device, the latter
445  * reports the view from the virtual memory system. It was decided that the
446  * kstat-based information should be moved to the "vmem" plugin, but nobody
447  * with enough Solaris experience was available at that time to do this. The
448  * code below is still there for your reference but it won't be activated in
449  * *this* plugin again. --octo
450  */
451 #elif 0 && HAVE_LIBKSTAT
452 /* kstat-based read function */
453 static int swap_read_kstat (void) /* {{{ */
454 {
455         gauge_t swap_alloc;
456         gauge_t swap_resv;
457         gauge_t swap_avail;
458
459         struct anoninfo ai;
460
461         if (swapctl (SC_AINFO, &ai) == -1)
462         {
463                 char errbuf[1024];
464                 ERROR ("swap plugin: swapctl failed: %s",
465                                 sstrerror (errno, errbuf, sizeof (errbuf)));
466                 return (-1);
467         }
468
469         /*
470          * Calculations from:
471          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
472          * Also see:
473          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
474          * /usr/include/vm/anon.h
475          *
476          * In short, swap -s shows: allocated + reserved = used, available
477          *
478          * However, Solaris does not allow to allocated/reserved more than the
479          * available swap (physical memory + disk swap), so the pedant may
480          * prefer: allocated + unallocated = reserved, available
481          *
482          * We map the above to: used + resv = n/a, free
483          *
484          * Does your brain hurt yet?  - Christophe Kalt
485          *
486          * Oh, and in case you wonder,
487          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
488          * can suffer from a 32bit overflow.
489          */
490         swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
491         swap_resv  = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
492         swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
493
494         swap_submit_usage (NULL, swap_alloc, swap_avail, "reserved", swap_resv);
495         return (0);
496 } /* }}} int swap_read_kstat */
497 /* #endif 0 && HAVE_LIBKSTAT */
498
499 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
500 /* swapctl-based read function */
501 static int swap_read (void) /* {{{ */
502 {
503         swaptbl_t *s;
504         char *s_paths;
505         int swap_num;
506         int status;
507         int i;
508
509         gauge_t avail = 0;
510         gauge_t total = 0;
511
512         swap_num = swapctl (SC_GETNSWP, NULL);
513         if (swap_num < 0)
514         {
515                 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
516                                 swap_num);
517                 return (-1);
518         }
519         else if (swap_num == 0)
520                 return (0);
521
522         /* Allocate and initialize the swaptbl_t structure */
523         s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
524         if (s == NULL)
525         {
526                 ERROR ("swap plugin: smalloc failed.");
527                 return (-1);
528         }
529
530         /* Memory to store the path names. We only use these paths when the
531          * separate option has been configured, but it's easier to just
532          * allocate enough memory in any case. */
533         s_paths = calloc (swap_num, PATH_MAX);
534         if (s_paths == NULL)
535         {
536                 ERROR ("swap plugin: malloc failed.");
537                 sfree (s);
538                 return (-1);
539         }
540         for (i = 0; i < swap_num; i++)
541                 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
542         s->swt_n = swap_num;
543
544         status = swapctl (SC_LIST, s);
545         if (status < 0)
546         {
547                 char errbuf[1024];
548                 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
549                                 sstrerror (errno, errbuf, sizeof (errbuf)));
550                 sfree (s_paths);
551                 sfree (s);
552                 return (-1);
553         }
554         else if (swap_num < status)
555         {
556                 /* more elements returned than requested */
557                 ERROR ("swap plugin: I allocated memory for %i structure%s, "
558                                 "but swapctl(2) claims to have returned %i. "
559                                 "I'm confused and will give up.",
560                                 swap_num, (swap_num == 1) ? "" : "s",
561                                 status);
562                 sfree (s_paths);
563                 sfree (s);
564                 return (-1);
565         }
566         else if (swap_num > status)
567                 /* less elements returned than requested */
568                 swap_num = status;
569
570         for (i = 0; i < swap_num; i++)
571         {
572                 char path[PATH_MAX];
573                 gauge_t this_total;
574                 gauge_t this_avail;
575
576                 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
577                         continue;
578
579                 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
580                 this_avail = (gauge_t) (s->swt_ent[i].ste_free  * pagesize);
581
582                 /* Shortcut for the "combined" setting (default) */
583                 if (!report_by_device)
584                 {
585                         avail += this_avail;
586                         total += this_total;
587                         continue;
588                 }
589
590                 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
591                 escape_slashes (path, sizeof (path));
592
593                 swap_submit_usage (path, this_total - this_avail, this_avail,
594                                 NULL, NAN);
595         } /* for (swap_num) */
596
597         if (total < avail)
598         {
599                 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
600                                 total, avail);
601                 sfree (s_paths);
602                 sfree (s);
603                 return (-1);
604         }
605
606         /* If the "separate" option was specified (report_by_device == 1), all
607          * values have already been dispatched from within the loop. */
608         if (!report_by_device)
609                 swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
610
611         sfree (s_paths);
612         sfree (s);
613         return (0);
614 } /* }}} int swap_read */
615 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
616
617 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
618 static int swap_read (void) /* {{{ */
619 {
620         struct swapent *swap_entries;
621         int swap_num;
622         int status;
623         int i;
624
625         gauge_t used  = 0;
626         gauge_t total = 0;
627
628         swap_num = swapctl (SWAP_NSWAP, NULL, 0);
629         if (swap_num < 0)
630         {
631                 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
632                                 swap_num);
633                 return (-1);
634         }
635         else if (swap_num == 0)
636                 return (0);
637
638         swap_entries = calloc (swap_num, sizeof (*swap_entries));
639         if (swap_entries == NULL)
640         {
641                 ERROR ("swap plugin: calloc failed.");
642                 return (-1);
643         }
644
645         status = swapctl (SWAP_STATS, swap_entries, swap_num);
646         if (status != swap_num)
647         {
648                 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
649                                 status);
650                 sfree (swap_entries);
651                 return (-1);
652         }
653
654 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
655 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
656 #else
657 # define C_SWAP_BLOCK_SIZE 512.0
658 #endif
659
660         /* TODO: Report per-device stats. The path name is available from
661          * swap_entries[i].se_path */
662         for (i = 0; i < swap_num; i++)
663         {
664                 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
665                         continue;
666
667                 used  += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
668                 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
669         }
670
671         if (total < used)
672         {
673                 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
674                                 total, used);
675                 return (-1);
676         }
677
678         swap_submit_usage (NULL, used, total - used, NULL, NAN);
679
680         sfree (swap_entries);
681         return (0);
682 } /* }}} int swap_read */
683 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
684
685 #elif defined(VM_SWAPUSAGE)
686 static int swap_read (void) /* {{{ */
687 {
688         int              mib[3];
689         size_t           mib_len;
690         struct xsw_usage sw_usage;
691         size_t           sw_usage_len;
692
693         mib_len = 2;
694         mib[0]  = CTL_VM;
695         mib[1]  = VM_SWAPUSAGE;
696
697         sw_usage_len = sizeof (struct xsw_usage);
698
699         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
700                 return (-1);
701
702         /* The returned values are bytes. */
703         swap_submit_usage (NULL,
704                         (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
705                         NULL, NAN);
706
707         return (0);
708 } /* }}} int swap_read */
709 /* #endif VM_SWAPUSAGE */
710
711 #elif HAVE_LIBKVM_GETSWAPINFO
712 static int swap_read (void) /* {{{ */
713 {
714         struct kvm_swap data_s;
715         int             status;
716
717         gauge_t used;
718         gauge_t total;
719
720         if (kvm_obj == NULL)
721                 return (-1);
722
723         /* only one structure => only get the grand total, no details */
724         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
725         if (status == -1)
726                 return (-1);
727
728         total = (gauge_t) data_s.ksw_total;
729         used  = (gauge_t) data_s.ksw_used;
730
731         total *= (gauge_t) kvm_pagesize;
732         used  *= (gauge_t) kvm_pagesize;
733
734         swap_submit_usage (NULL, used, total - used, NULL, NAN);
735
736         return (0);
737 } /* }}} int swap_read */
738 /* #endif HAVE_LIBKVM_GETSWAPINFO */
739
740 #elif HAVE_LIBSTATGRAB
741 static int swap_read (void) /* {{{ */
742 {
743         sg_swap_stats *swap;
744
745         swap = sg_get_swap_stats ();
746         if (swap == NULL)
747                 return (-1);
748
749         swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
750                         NULL, NAN);
751
752         return (0);
753 } /* }}} int swap_read */
754 /* #endif  HAVE_LIBSTATGRAB */
755
756 #elif HAVE_PERFSTAT
757 static int swap_read (void) /* {{{ */
758 {
759         perfstat_memory_total_t pmemory;
760         int status;
761
762         gauge_t total;
763         gauge_t free;
764         gauge_t reserved;
765
766         memset (&pmemory, 0, sizeof (pmemory));
767         status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
768         if (status < 0)
769         {
770                 char errbuf[1024];
771                 WARNING ("swap plugin: perfstat_memory_total failed: %s",
772                         sstrerror (errno, errbuf, sizeof (errbuf)));
773                 return (-1);
774         }
775
776         total    = (gauge_t) (pmemory.pgsp_total * pagesize);
777         free     = (gauge_t) (pmemory.pgsp_free * pagesize);
778         reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
779
780         swap_submit_usage (NULL, total - free, free, "reserved", reserved);
781         swap_submit_derive ("in",  (derive_t) pmemory.pgspins * pagesize);
782         swap_submit_derive ("out", (derive_t) pmemory.pgspouts * pagesize);
783
784         return (0);
785 } /* }}} int swap_read */
786 #endif /* HAVE_PERFSTAT */
787
788 void module_register (void)
789 {
790         plugin_register_complex_config ("swap", swap_config);
791         plugin_register_init ("swap", swap_init);
792         plugin_register_read ("swap", swap_read);
793 } /* void module_register */
794
795 /* vim: set fdm=marker : */