freebsd branch: swap plugin: Added the possibility to use `libkvm' to collect swap...
authorocto <octo>
Thu, 8 Jun 2006 17:32:49 +0000 (17:32 +0000)
committerocto <octo>
Thu, 8 Jun 2006 17:32:49 +0000 (17:32 +0000)
For some weird reason one needs r00t for this, though..

configure.in
src/Makefile.am
src/swap.c

index c9c3a89..2f924a9 100644 (file)
@@ -262,6 +262,9 @@ AC_CHECK_HEADERS(sys/statvfs.h)
 AC_CHECK_HEADERS(sys/vfs.h)
 AC_CHECK_HEADERS(sys/vfstab.h)
 
+# For the swap plugin, FreeBSD
+AC_CHECK_HEADERS(kvm.h)
+
 # For debugging interface (variable number of arguments)
 AC_CHECK_HEADERS(stdarg.h)
 
@@ -604,7 +607,6 @@ AM_CONDITIONAL(BUILD_WITH_LIBIOKIT, test "x$with_libiokit" = "xyes")
 
 with_libstatgrab="yes"
 with_libdevstat="no"
-with_libkvm="no"
 AC_ARG_WITH(libstatgrab, [AS_HELP_STRING([--with-libstatgrab@<:@=PREFIX@:>@], [Path to libstatgrab.])],
 [
        if test "x$withval" != "xno" -a "x$withval" != "xyes"
@@ -633,7 +635,6 @@ fi
 if test "x$with_libstatgrab" = "xyes"
 then
        AC_CHECK_LIB(devstat, getdevs, [with_libdevstat="yes"], [with_libdevstat="no"])
-       AC_CHECK_LIB(kvm, kvm_getargv, [with_libkvm="yes"],     [with_libkvm="no"])
 fi
 if test "x$with_libstatgrab" = "xyes"
 then
@@ -645,7 +646,13 @@ AC_DEFINE_UNQUOTED(COLLECT_LIBSTATGRAB, [$collect_libstatgrab],
        [Wether or not to use statgrab library])
 AM_CONDITIONAL(BUILD_WITH_LIBSTATGRAB, test "x$with_libstatgrab" = "xyes")
 AM_CONDITIONAL(BUILD_WITH_LIBDEVSTAT,  test "x$with_libdevstat"  = "xyes")
-AM_CONDITIONAL(BUILD_WITH_LIBKVM,      test "x$with_libkvm"      = "xyes")
+
+AC_CHECK_LIB(kvm, kvm_getswapinfo, [with_libkvm="yes"], [with_libkvm="no"])
+if test "x$with_libkvm" = "xyes"
+then
+       AC_DEFINE(HAVE_LIBKVM, 1, [Define to 1 if you have the 'kvm' library (-lkvm)])
+fi
+AM_CONDITIONAL(BUILD_WITH_LIBKVM, test "x$with_libkvm" = "xyes")
 
 AC_ARG_WITH(lm-sensors, [AS_HELP_STRING([--with-lm-sensors@<:@=PREFIX@:>@], [Path to lm_sensors.])],
 [
index fff3f31..0a0c85d 100644 (file)
@@ -291,10 +291,10 @@ endif
 if BUILD_WITH_LIBDEVINFO
 swap_la_LDFLAGS += -ldevinfo
 endif
-if BUILD_WITH_LIBSTATGRAB
 if BUILD_WITH_LIBKVM
 swap_la_LDFLAGS += -lkvm
 endif
+if BUILD_WITH_LIBSTATGRAB
 if BUILD_WITH_LIBDEVSTAT
 swap_la_LDFLAGS += -ldevstat
 endif
index 1d284f3..7cc668d 100644 (file)
 #if HAVE_SYS_SYSCTL_H
 #  include <sys/sysctl.h>
 #endif
+#if HAVE_KVM_H
+#  include <kvm.h>
+#endif
 
 #define MODULE_NAME "swap"
 
-#if KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_SYS_SYSCTL_H || HAVE_LIBSTATGRAB
+#if KERNEL_LINUX || HAVE_LIBKSTAT || defined(VM_SWAPUSAGE) || HAVE_LIBKVM || HAVE_LIBSTATGRAB
 # define SWAP_HAVE_READ 1
 #else
 # define SWAP_HAVE_READ 0
@@ -67,9 +70,14 @@ static int pagesize;
 static kstat_t *ksp;
 /* #endif HAVE_LIBKSTAT */
 
-#elif HAVE_SYS_SYSCTL_H
+#elif defined(VM_SWAPUSAGE)
 /* No global variables */
-/* #endif HAVE_SYS_SYSCTL_H */
+/* #endif defined(VM_SWAPUSAGE) */
+
+#elif HAVE_LIBKVM
+static kvm_t *kvm_obj = NULL;
+int kvm_pagesize;
+/* #endif HAVE_LIBKVM */
 
 #elif HAVE_LIBSTATGRAB
 /* No global variables */
@@ -88,9 +96,30 @@ static void swap_init (void)
                ksp = NULL;
 /* #endif HAVE_LIBKSTAT */
 
-#elif HAVE_SYS_SYSCTL_H
+#elif defined(VM_SWAPUSAGE)
        /* No init stuff */
-/* #endif HAVE_SYS_SYSCTL_H */
+/* #endif defined(VM_SWAPUSAGE) */
+
+#elif HAVE_LIBKVM
+       if (kvm_obj != NULL)
+       {
+               kvm_close (kvm_obj);
+               kvm_obj = NULL;
+       }
+
+       kvm_pagesize = getpagesize ();
+
+       if ((kvm_obj = kvm_open (NULL, /* execfile */
+                                       NULL, /* corefile */
+                                       NULL, /* swapfile */
+                                       O_RDONLY, /* flags */
+                                       NULL)) /* errstr */
+                       == NULL)
+       {
+               syslog (LOG_ERR, "swap plugin: kvm_open failed.");
+               return;
+       }
+/* #endif HAVE_LIBKVM */
 
 #elif HAVE_LIBSTATGRAB
        /* No init stuff */
@@ -214,26 +243,16 @@ static void swap_read (void)
        swap_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
 /* #endif HAVE_LIBKSTAT */
 
-#elif HAVE_SYS_SYSCTL_H
+#elif defined(VM_SWAPUSAGE)
        int              mib[3];
        size_t           mib_len;
        struct xsw_usage sw_usage;
        size_t           sw_usage_len;
        int              status;
 
-#if defined(VM_SWAPUSAGE)
        mib_len = 2;
        mib[0]  = CTL_VM;
        mib[1]  = VM_SWAPUSAGE;
-#else
-       mib_len = 3;
-       if ((status = sysctlnametomib ("vm.swap_info", mib, &mib_len)) < 0)
-       {
-               syslog (LOG_WARN, "swap plugin: sysctlnametomib failed: %s",
-                               strerror (errno));
-               return;
-       }
-#endif
 
        sw_usage_len = sizeof (struct xsw_usage);
 
@@ -242,7 +261,34 @@ static void swap_read (void)
 
        /* The returned values are bytes. */
        swap_submit (sw_usage.xsu_used, sw_usage.xsu_avail, -1LL, -1LL);
-/* #endif HAVE_SYS_SYSCTL_H */
+/* #endif VM_SWAPUSAGE */
+
+#elif HAVE_LIBKVM
+       struct kvm_swap data_s;
+       int             status;
+
+       unsigned long long used;
+       unsigned long long free;
+       unsigned long long total;
+
+       if (kvm_obj == NULL)
+               return;
+
+       /* only one structure => only get the grand total, no details */
+       status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
+       if (status == -1)
+               return;
+
+       total = (unsigned long long) data_s.ksw_total;
+       used  = (unsigned long long) data_s.ksw_used;
+
+       total *= (unsigned long long) kvm_pagesize;
+       used  *= (unsigned long long) kvm_pagesize;
+
+       free = total - used;
+
+       swap_submit (used, free, -1LL, -1LL);
+/* #endif HAVE_LIBKVM */
 
 #elif HAVE_LIBSTATGRAB
        sg_swap_stats *swap;