X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmemory.c;h=aff16f5434e4d81fbfc4ab614f70b46bd10915bc;hb=ef48404e2dc2ec0093d984bb8d6b60bec3faf52c;hp=d92ecf30550dd56097c41f9d0108f3b3b18193d6;hpb=79d36d9eb61ac09f253769feff144383b2b665c9;p=collectd.git diff --git a/src/memory.c b/src/memory.c index d92ecf30..aff16f54 100644 --- a/src/memory.c +++ b/src/memory.c @@ -22,7 +22,6 @@ #include "collectd.h" #include "common.h" #include "plugin.h" -#include "utils_debug.h" #ifdef HAVE_SYS_SYSCTL_H # include @@ -50,20 +49,6 @@ # define MEMORY_HAVE_READ 0 #endif -/* 2^48 = 281474976710656 */ -static data_source_t dsrc[4] = -{ - {"used", DS_TYPE_GAUGE, 0, 281474976710656.0}, - {"free", DS_TYPE_GAUGE, 0, 281474976710656.0}, - {"buffers", DS_TYPE_GAUGE, 0, 281474976710656.0}, - {"cached", DS_TYPE_GAUGE, 0, 281474976710656.0} -}; - -static data_set_t ds = -{ - "memory", 4, dsrc -}; - /* vm_statistics_data_t */ #if defined(HOST_VM_INFO) static mach_port_t port_host; @@ -109,22 +94,20 @@ static int memory_init (void) return (0); } /* int memory_init */ -static void memory_submit (long long mem_used, long long mem_buffered, - long long mem_cached, long long mem_free) +static void memory_submit (const char *type_instance, gauge_t value) { - value_t values[4]; + value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - values[0].gauge = mem_used; - values[1].gauge = mem_free; - values[2].gauge = mem_buffered; - values[3].gauge = mem_cached; + values[0].gauge = value; vl.values = values; vl.values_len = 4; vl.time = time (NULL); - strcpy (vl.host, hostname); + 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); } @@ -149,7 +132,7 @@ static int memory_read (void) (host_info_t) &vm_data, &vm_data_len)) != KERN_SUCCESS) { - syslog (LOG_ERR, "memory-plugin: host_statistics failed and returned the value %i", (int) status); + ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status); return (-1); } @@ -178,7 +161,10 @@ static int memory_read (void) inactive = vm_data.inactive_count * pagesize; free = vm_data.free_count * pagesize; - memory_submit (wired + active, -1, inactive, free); + memory_submit ("wired", wired); + memory_submit ("active", active); + memory_submit ("inactive", inactive); + memory_submit ("free", free); /* #endif HOST_VM_INFO */ #elif HAVE_SYSCTLBYNAME @@ -202,34 +188,38 @@ static int memory_read (void) "vm.stats.vm.v_cache_count", NULL }; - int sysctl_vals[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; + double sysctl_vals[8]; 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) + int value; + size_t value_len = sizeof (value); + + if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len, + NULL, 0) == 0) + { + sysctl_vals[i] = value; + DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]); + } + else { - syslog (LOG_ERR, "memory plugin: sysctlbyname (%s): %s", - sysctl_keys[i], strerror (errno)); - return (-1); + sysctl_vals[i] = NAN; } - DBG ("%26s: %6i", sysctl_keys[i], sysctl_vals[i]); - } /* for i */ + } /* for (sysctl_keys) */ /* 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 */ + if (!isnan (sysctl_vals[i])) + sysctl_vals[i] *= sysctl_vals[0]; + + memory_submit ("free", 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) @@ -246,7 +236,9 @@ static int memory_read (void) if ((fh = fopen ("/proc/meminfo", "r")) == NULL) { - syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("memory: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -274,12 +266,20 @@ static int memory_read (void) } if (fclose (fh)) - syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno)); + { + char errbuf[1024]; + WARNING ("memory: fclose: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + } if (mem_used >= (mem_free + mem_buffered + mem_cached)) { mem_used -= mem_free + mem_buffered + mem_cached; - memory_submit (mem_used, mem_buffered, mem_cached, mem_free); + memory_submit ("used", mem_used); + memory_submit ("buffered", mem_used); + memory_submit ("cached", mem_used); + memory_submit ("free", mem_used); + } /* #endif defined(KERNEL_LINUX) */ @@ -305,14 +305,20 @@ static int memory_read (void) mem_free *= pagesize; /* memory.. Why not call me up and give me */ mem_lock *= pagesize; /* some? ;) */ - memory_submit (mem_used, mem_lock, 0LL, mem_free); + memory_submit ("used", mem_used); + memory_submit ("free", mem_free); + memory_submit ("locked", mem_locked); /* #endif defined(HAVE_LIBKSTAT) */ #elif defined(HAVE_LIBSTATGRAB) sg_mem_stats *ios; if ((ios = sg_get_mem_stats ()) != NULL) - memory_submit (ios->used, 0LL, ios->cache, ios->free); + { + memory_submit ("used", ios->used); + memory_submit ("cached", ios->cached); + memory_submit ("free", ios->free); + } #endif /* HAVE_LIBSTATGRAB */ return (0); @@ -321,10 +327,8 @@ static int memory_read (void) void module_register (void) { - plugin_register_data_set (&ds); - #if MEMORY_HAVE_READ plugin_register_init ("memory", memory_init); plugin_register_read ("memory", memory_read); #endif /* MEMORY_HAVE_READ */ -} +} /* void module_register */