X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fswap.c;h=9c63e9bbb56ca82a7e4ce25de7219d79aa8040d2;hb=6fc6255c95b966014ee02a2a37666b2c4880ff8d;hp=0642afac78d82792318f40a57696cfe862d13ed2;hpb=2421812ea47269c50920414a6e3c32448800120f;p=collectd.git diff --git a/src/swap.c b/src/swap.c index 0642afac..9c63e9bb 100644 --- a/src/swap.c +++ b/src/swap.c @@ -35,6 +35,7 @@ #endif #include "collectd.h" + #include "common.h" #include "plugin.h" @@ -82,6 +83,10 @@ static derive_t pagesize; static _Bool report_by_device = 0; /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */ +#elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS +/* No global variables */ +/* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */ + #elif defined(VM_SWAPUSAGE) /* No global variables */ /* #endif defined(VM_SWAPUSAGE) */ @@ -108,9 +113,7 @@ static _Bool values_percentage = 0; static int swap_config (oconfig_item_t *ci) /* {{{ */ { - int i; - - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("ReportBytes", child->key) == 0) @@ -157,6 +160,8 @@ static int swap_init (void) /* {{{ */ /* #endif defined(VM_SWAPUSAGE) */ #elif HAVE_LIBKVM_GETSWAPINFO + char errbuf[_POSIX2_LINE_MAX]; + if (kvm_obj != NULL) { kvm_close (kvm_obj); @@ -165,14 +170,11 @@ static int swap_init (void) /* {{{ */ kvm_pagesize = getpagesize (); - if ((kvm_obj = kvm_open (NULL, /* execfile */ - NULL, /* corefile */ - NULL, /* swapfile */ - O_RDONLY, /* flags */ - NULL)) /* errstr */ - == NULL) + kvm_obj = kvm_openfiles (NULL, "/dev/null", NULL, O_RDONLY, errbuf); + + if (kvm_obj == NULL) { - ERROR ("swap plugin: kvm_open failed."); + ERROR ("swap plugin: kvm_openfiles failed, %s", errbuf); return (-1); } /* #endif HAVE_LIBKVM_GETSWAPINFO */ @@ -205,11 +207,11 @@ static void swap_submit_usage (char const *plugin_instance, /* {{{ */ sstrncpy (vl.type, "swap", sizeof (vl.type)); if (values_absolute) - plugin_dispatch_multivalue (&vl, 0, + plugin_dispatch_multivalue (&vl, 0, DS_TYPE_GAUGE, "used", used, "free", free, other_name, other_value, NULL); if (values_percentage) - plugin_dispatch_multivalue (&vl, 1, + plugin_dispatch_multivalue (&vl, 1, DS_TYPE_GAUGE, "used", used, "free", free, other_name, other_value, NULL); } /* }}} void swap_submit_usage */ @@ -282,7 +284,8 @@ static int swap_read_separate (void) /* {{{ */ if (total < used) continue; - swap_submit_usage (path, used, total - used, NULL, NAN); + swap_submit_usage (path, used * 1024.0, (total - used) * 1024.0, + NULL, NAN); } fclose (fh); @@ -341,8 +344,9 @@ static int swap_read_combined (void) /* {{{ */ if (swap_used < 0.0) return (EINVAL); - swap_submit_usage (NULL, swap_used, swap_free, - isnan (swap_cached) ? NULL : "cached", swap_cached); + swap_submit_usage (NULL, swap_used * 1024.0, swap_free * 1024.0, + isnan (swap_cached) ? NULL : "cached", + isnan (swap_cached) ? NAN : swap_cached * 1024.0); return (0); } /* }}} int swap_read_combined */ @@ -504,7 +508,6 @@ static int swap_read (void) /* {{{ */ char *s_paths; int swap_num; int status; - int i; gauge_t avail = 0; gauge_t total = 0; @@ -520,10 +523,10 @@ static int swap_read (void) /* {{{ */ return (0); /* Allocate and initialize the swaptbl_t structure */ - s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable)); + s = malloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable)); if (s == NULL) { - ERROR ("swap plugin: smalloc failed."); + ERROR ("swap plugin: malloc failed."); return (-1); } @@ -533,11 +536,11 @@ static int swap_read (void) /* {{{ */ s_paths = calloc (swap_num, PATH_MAX); if (s_paths == NULL) { - ERROR ("swap plugin: malloc failed."); + ERROR ("swap plugin: calloc failed."); sfree (s); return (-1); } - for (i = 0; i < swap_num; i++) + for (int i = 0; i < swap_num; i++) s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX); s->swt_n = swap_num; @@ -567,7 +570,7 @@ static int swap_read (void) /* {{{ */ /* less elements returned than requested */ swap_num = status; - for (i = 0; i < swap_num; i++) + for (int i = 0; i < swap_num; i++) { char path[PATH_MAX]; gauge_t this_total; @@ -620,7 +623,6 @@ static int swap_read (void) /* {{{ */ struct swapent *swap_entries; int swap_num; int status; - int i; gauge_t used = 0; gauge_t total = 0; @@ -659,7 +661,7 @@ static int swap_read (void) /* {{{ */ /* TODO: Report per-device stats. The path name is available from * swap_entries[i].se_path */ - for (i = 0; i < swap_num; i++) + for (int i = 0; i < swap_num; i++) { if ((swap_entries[i].se_flags & SWF_ENABLE) == 0) continue; @@ -672,6 +674,7 @@ static int swap_read (void) /* {{{ */ { ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).", total, used); + sfree (swap_entries); return (-1); } @@ -756,14 +759,13 @@ static int swap_read (void) /* {{{ */ #elif HAVE_PERFSTAT static int swap_read (void) /* {{{ */ { - perfstat_memory_total_t pmemory; + perfstat_memory_total_t pmemory = { 0 }; int status; gauge_t total; gauge_t free; gauge_t reserved; - memset (&pmemory, 0, sizeof (pmemory)); status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1); if (status < 0) {