+# define C_SWAP_BLOCK_SIZE ((derive_t) 512)
+#endif
+
+ for (i = 0; i < swap_num; i++)
+ {
+ if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
+ continue;
+
+ used += ((derive_t) swap_entries[i].se_inuse)
+ * C_SWAP_BLOCK_SIZE;
+ total += ((derive_t) swap_entries[i].se_nblks)
+ * C_SWAP_BLOCK_SIZE;
+ }
+
+ if (total < used)
+ {
+ ERROR ("swap plugin: Total swap space (%"PRIu64") "
+ "is less than used swap space (%"PRIu64").",
+ total, used);
+ return (-1);
+ }
+
+ swap_submit ("used", used, DS_TYPE_GAUGE);
+ swap_submit ("free", total - used, DS_TYPE_GAUGE);
+
+ sfree (swap_entries);
+
+ return (0);
+} /* }}} int swap_read */
+/* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
+
+#elif defined(VM_SWAPUSAGE)
+static int swap_read (void) /* {{{ */
+{
+ int mib[3];
+ size_t mib_len;
+ struct xsw_usage sw_usage;
+ size_t sw_usage_len;
+
+ mib_len = 2;
+ mib[0] = CTL_VM;
+ mib[1] = VM_SWAPUSAGE;
+
+ sw_usage_len = sizeof (struct xsw_usage);
+
+ if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
+ return (-1);
+
+ /* The returned values are bytes. */
+ swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
+ swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
+
+ return (0);
+} /* }}} int swap_read */
+/* #endif VM_SWAPUSAGE */
+
+#elif HAVE_LIBKVM_GETSWAPINFO
+static int swap_read (void) /* {{{ */
+{
+ struct kvm_swap data_s;
+ int status;
+
+ derive_t used;
+ derive_t free;
+ derive_t total;
+
+ if (kvm_obj == NULL)
+ return (-1);
+
+ /* only one structure => only get the grand total, no details */
+ status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
+ if (status == -1)
+ return (-1);
+
+ total = (derive_t) data_s.ksw_total;
+ used = (derive_t) data_s.ksw_used;
+
+ total *= (derive_t) kvm_pagesize;
+ used *= (derive_t) kvm_pagesize;
+
+ free = total - used;
+
+ swap_submit ("used", used, DS_TYPE_GAUGE);
+ swap_submit ("free", free, DS_TYPE_GAUGE);
+
+ return (0);
+} /* }}} int swap_read */
+/* #endif HAVE_LIBKVM_GETSWAPINFO */
+
+#elif HAVE_LIBSTATGRAB
+static int swap_read (void) /* {{{ */
+{
+ sg_swap_stats *swap;
+
+ swap = sg_get_swap_stats ();
+
+ if (swap == NULL)
+ return (-1);
+
+ swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
+ swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
+
+ return (0);
+} /* }}} int swap_read */
+/* #endif HAVE_LIBSTATGRAB */
+
+#elif HAVE_PERFSTAT
+static int swap_read (void) /* {{{ */
+{
+ if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
+ {
+ char errbuf[1024];
+ WARNING ("memory plugin: perfstat_memory_total failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (-1);
+ }
+ swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
+ swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
+
+ return (0);
+} /* }}} int swap_read */
+#endif /* HAVE_PERFSTAT */