Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / memory.c
index e169503..6ea6205 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/memory.c
- * Copyright (C) 2005-2007  Florian octo Forster
+ * Copyright (C) 2005-2008  Florian octo Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
 # include <mach/vm_statistics.h>
 #endif
 
-#if defined (HOST_VM_INFO) || HAVE_SYSCTLBYNAME || KERNEL_LINUX || HAVE_LIBKSTAT
-# define MEMORY_HAVE_READ 1
-#else
-# define MEMORY_HAVE_READ 0
+#if HAVE_STATGRAB_H
+# include <statgrab.h>
 #endif
 
 /* vm_statistics_data_t */
-#if defined(HOST_VM_INFO)
+#if HAVE_HOST_STATISTICS
 static mach_port_t port_host;
 static vm_size_t pagesize;
-/* #endif HOST_VM_INFO */
+/* #endif HAVE_HOST_STATISTICS */
 
 #elif HAVE_SYSCTLBYNAME
 /* no global variables */
@@ -66,15 +64,22 @@ static vm_size_t pagesize;
 #elif HAVE_LIBKSTAT
 static int pagesize;
 static kstat_t *ksp;
-#endif /* HAVE_LIBKSTAT */
+/* #endif HAVE_LIBKSTAT */
+
+#elif HAVE_LIBSTATGRAB
+/* no global variables */
+/* endif HAVE_LIBSTATGRAB */
+
+#else
+# error "No applicable input method."
+#endif
 
-#if MEMORY_HAVE_READ
 static int memory_init (void)
 {
-#if defined(HOST_VM_INFO)
+#if HAVE_HOST_STATISTICS
        port_host = mach_host_self ();
        host_page_size (port_host, &pagesize);
-/* #endif HOST_VM_INFO */
+/* #endif HAVE_HOST_STATISTICS */
 
 #elif HAVE_SYSCTLBYNAME
 /* no init stuff */
@@ -87,8 +92,11 @@ static int memory_init (void)
 #elif defined(HAVE_LIBKSTAT)
        /* getpagesize(3C) tells me this does not fail.. */
        pagesize = getpagesize ();
-       if (get_kstat (&ksp, "unix", 0, "system_pages"))
+       if (get_kstat (&ksp, "unix", 0, "system_pages") != 0)
+       {
                ksp = NULL;
+               return (-1);
+       }
 #endif /* HAVE_LIBKSTAT */
 
        return (0);
@@ -102,19 +110,18 @@ static void memory_submit (const char *type_instance, gauge_t value)
        values[0].gauge = value;
 
        vl.values = values;
-       vl.values_len = 4;
-       vl.time = time (NULL);
-       strcpy (vl.host, hostname_g);
-       strcpy (vl.plugin, "memory");
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
-       vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-
-       plugin_dispatch_values ("memory", &vl);
+       vl.values_len = 1;
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
+       sstrncpy (vl.type, "memory", sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+
+       plugin_dispatch_values (&vl);
 }
 
 static int memory_read (void)
 {
-#if defined(HOST_VM_INFO)
+#if HAVE_HOST_STATISTICS
        kern_return_t status;
        vm_statistics_data_t   vm_data;
        mach_msg_type_number_t vm_data_len;
@@ -165,7 +172,7 @@ static int memory_read (void)
        memory_submit ("active",   active);
        memory_submit ("inactive", inactive);
        memory_submit ("free",     free);
-/* #endif HOST_VM_INFO */
+/* #endif HAVE_HOST_STATISTICS */
 
 #elif HAVE_SYSCTLBYNAME
        /*
@@ -190,7 +197,6 @@ static int memory_read (void)
        };
        double sysctl_vals[8];
 
-       size_t len;
        int    i;
 
        for (i = 0; sysctl_keys[i] != NULL; i++)
@@ -202,7 +208,7 @@ static int memory_read (void)
                                        NULL, 0) == 0)
                {
                        sysctl_vals[i] = value;
-                       DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]);
+                       DEBUG ("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]);
                }
                else
                {
@@ -216,13 +222,13 @@ static int memory_read (void)
                        sysctl_vals[i] *= sysctl_vals[0];
 
        memory_submit ("free",     sysctl_vals[2]);
-       memory_submit ("wired",    sysctl_vals[2]);
-       memory_submit ("active",   sysctl_vals[2]);
-       memory_submit ("inactive", sysctl_vals[2]);
-       memory_submit ("cache",    sysctl_vals[2]);
+       memory_submit ("wired",    sysctl_vals[3]);
+       memory_submit ("active",   sysctl_vals[4]);
+       memory_submit ("inactive", sysctl_vals[5]);
+       memory_submit ("cache",    sysctl_vals[6]);
 /* #endif HAVE_SYSCTLBYNAME */
 
-#elif defined(KERNEL_LINUX)
+#elif KERNEL_LINUX
        FILE *fh;
        char buffer[1024];
        
@@ -276,14 +282,13 @@ static int memory_read (void)
        {
                mem_used -= mem_free + mem_buffered + mem_cached;
                memory_submit ("used",     mem_used);
-               memory_submit ("buffered", mem_used);
-               memory_submit ("cached",   mem_used);
-               memory_submit ("free",     mem_used);
-
+               memory_submit ("buffered", mem_buffered);
+               memory_submit ("cached",   mem_cached);
+               memory_submit ("free",     mem_free);
        }
-/* #endif defined(KERNEL_LINUX) */
+/* #endif KERNEL_LINUX */
 
-#elif defined(HAVE_LIBKSTAT)
+#elif HAVE_LIBKSTAT
        long long mem_used;
        long long mem_free;
        long long mem_lock;
@@ -307,28 +312,25 @@ static int memory_read (void)
 
        memory_submit ("used",   mem_used);
        memory_submit ("free",   mem_free);
-       memory_submit ("locked", mem_locked);
-/* #endif defined(HAVE_LIBKSTAT) */
+       memory_submit ("locked", mem_lock);
+/* #endif HAVE_LIBKSTAT */
 
-#elif defined(HAVE_LIBSTATGRAB)
+#elif HAVE_LIBSTATGRAB
        sg_mem_stats *ios;
 
        if ((ios = sg_get_mem_stats ()) != NULL)
        {
                memory_submit ("used",   ios->used);
-               memory_submit ("cached", ios->cached);
+               memory_submit ("cached", ios->cache);
                memory_submit ("free",   ios->free);
        }
 #endif /* HAVE_LIBSTATGRAB */
 
        return (0);
 }
-#endif /* MEMORY_HAVE_READ */
 
 void module_register (void)
 {
-#if MEMORY_HAVE_READ
        plugin_register_init ("memory", memory_init);
        plugin_register_read ("memory", memory_read);
-#endif /* MEMORY_HAVE_READ */
 } /* void module_register */