octo@leeloo:~/collectd $ svn merge -r822:823 branches/disk-linux trunk
authorocto <octo>
Fri, 16 Jun 2006 23:02:01 +0000 (23:02 +0000)
committerocto <octo>
Fri, 16 Jun 2006 23:02:01 +0000 (23:02 +0000)
configure.in
src/Makefile.am
src/memory.c
src/swap.c

index 8ca08a1..a1dbca6 100644 (file)
@@ -263,6 +263,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)
 
@@ -605,7 +608,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"
@@ -634,7 +636,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
@@ -646,7 +647,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 ff4da23..c868f5e 100644 (file)
@@ -302,10 +302,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 00fd1bf..b8b7229 100644 (file)
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
+#include "utils_debug.h"
+
+#ifdef HAVE_SYS_SYSCTL_H
+# include <sys/sysctl.h>
+#endif
 
 #ifdef HAVE_MACH_KERN_RETURN_H
 # include <mach/kern_return.h>
@@ -40,7 +45,7 @@
 # include <mach/vm_statistics.h>
 #endif
 
-#if defined (HOST_VM_INFO) || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT)
+#if defined (HOST_VM_INFO) || HAVE_SYSCTLBYNAME || KERNEL_LINUX || HAVE_LIBKSTAT
 # define MEMORY_HAVE_READ 1
 #else
 # define MEMORY_HAVE_READ 0
@@ -67,11 +72,15 @@ static mach_port_t port_host;
 static vm_size_t pagesize;
 /* #endif HOST_VM_INFO */
 
-#elif defined(KERNEL_LINUX)
+#elif HAVE_SYSCTLBYNAME
+/* no global variables */
+/* #endif HAVE_SYSCTLBYNAME */
+
+#elif KERNEL_LINUX
 /* no global variables */
 /* #endif KERNEL_LINUX */
 
-#elif defined(HAVE_LIBKSTAT)
+#elif HAVE_LIBKSTAT
 static int pagesize;
 static kstat_t *ksp;
 #endif /* HAVE_LIBKSTAT */
@@ -83,6 +92,10 @@ static void memory_init (void)
        host_page_size (port_host, &pagesize);
 /* #endif HOST_VM_INFO */
 
+#elif HAVE_SYSCTLBYNAME
+/* no init stuff */
+/* #endif HAVE_SYSCTLBYNAME */
+
 #elif defined(KERNEL_LINUX)
 /* no init stuff */
 /* #endif KERNEL_LINUX */
@@ -170,6 +183,57 @@ static void memory_read (void)
        memory_submit (wired + active, -1, inactive, free);
 /* #endif HOST_VM_INFO */
 
+#elif HAVE_SYSCTLBYNAME
+       /*
+        * vm.stats.vm.v_page_size: 4096
+        * vm.stats.vm.v_page_count: 246178
+        * vm.stats.vm.v_free_count: 28760
+        * vm.stats.vm.v_wire_count: 37526
+        * vm.stats.vm.v_active_count: 55239
+        * vm.stats.vm.v_inactive_count: 113730
+        * vm.stats.vm.v_cache_count: 10809
+        */
+       char *sysctl_keys[8] =
+       {
+               "vm.stats.vm.v_page_size",
+               "vm.stats.vm.v_page_count",
+               "vm.stats.vm.v_free_count",
+               "vm.stats.vm.v_wire_count",
+               "vm.stats.vm.v_active_count",
+               "vm.stats.vm.v_inactive_count",
+               "vm.stats.vm.v_cache_count",
+               NULL
+       };
+       int sysctl_vals[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
+
+       size_t len;
+       int    i;
+       int    status;
+
+       for (i = 0; sysctl_keys[i] != NULL; i++)
+       {
+               len = sizeof (int);
+               if ((status = sysctlbyname (sysctl_keys[i],
+                                               (void *) &sysctl_vals[i], &len,
+                                               NULL, 0)) < 0)
+               {
+                       syslog (LOG_ERR, "memory plugin: sysctlbyname (%s): %s",
+                                       sysctl_keys[i], strerror (errno));
+                       return;
+               }
+               DBG ("%26s: %6i", sysctl_keys[i], sysctl_vals[i]);
+       } /* for i */
+
+       /* multiply all all page counts with the pagesize */
+       for (i = 1; sysctl_keys[i] != NULL; i++)
+               sysctl_vals[i] = sysctl_vals[i] * sysctl_vals[0];
+
+       memory_submit (sysctl_vals[3] + sysctl_vals[4], /* wired + active */
+                       sysctl_vals[6],                 /* cache */
+                       sysctl_vals[5],                 /* inactive */
+                       sysctl_vals[2]);                /* free */
+/* #endif HAVE_SYSCTLBYNAME */
+
 #elif defined(KERNEL_LINUX)
        FILE *fh;
        char buffer[1024];
index 7dd8d68..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,22 +243,52 @@ static void swap_read (void)
        swap_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
 /* #endif HAVE_LIBKSTAT */
 
-#elif HAVE_SYS_SYSCTL_H
-       int mib[2];
+#elif defined(VM_SWAPUSAGE)
+       int              mib[3];
+       size_t           mib_len;
        struct xsw_usage sw_usage;
        size_t           sw_usage_len;
+       int              status;
 
-       mib[0] = CTL_VM;
-       mib[1] = VM_SWAPUSAGE;
+       mib_len = 2;
+       mib[0]  = CTL_VM;
+       mib[1]  = VM_SWAPUSAGE;
 
        sw_usage_len = sizeof (struct xsw_usage);
 
-       if (sysctl (mib, 2, &sw_usage, &sw_usage_len, NULL, 0) != 0)
+       if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
                return;
 
        /* 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;