X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fswap.c;h=61c9e2845c60a61e97a2cead10442bfd4e14a540;hp=cbe1ab11077957a6621627195ee7c55647211332;hb=54619dc85fd308b21ed09a0271e5c7383c7921b9;hpb=45492ba3d0119874e2f2878d68089fc8f62268a4 diff --git a/src/swap.c b/src/swap.c index cbe1ab11..61c9e284 100644 --- a/src/swap.c +++ b/src/swap.c @@ -37,8 +37,8 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" +#include "utils/common/common.h" #if HAVE_SYS_SWAP_H #include @@ -74,14 +74,14 @@ #if KERNEL_LINUX #define SWAP_HAVE_REPORT_BY_DEVICE 1 static derive_t pagesize; -static _Bool report_bytes = 0; -static _Bool report_by_device = 0; +static bool report_bytes; +static bool report_by_device; /* #endif KERNEL_LINUX */ #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS #define SWAP_HAVE_REPORT_BY_DEVICE 1 static derive_t pagesize; -static _Bool report_by_device = 0; +static bool report_by_device; /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */ #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS @@ -93,7 +93,7 @@ static _Bool report_by_device = 0; /* #endif defined(VM_SWAPUSAGE) */ #elif HAVE_LIBKVM_GETSWAPINFO -static kvm_t *kvm_obj = NULL; +static kvm_t *kvm_obj; int kvm_pagesize; /* #endif HAVE_LIBKVM_GETSWAPINFO */ @@ -109,8 +109,9 @@ static int pagesize; #error "No applicable input method." #endif /* HAVE_LIBSTATGRAB */ -static _Bool values_absolute = 1; -static _Bool values_percentage = 0; +static bool values_absolute = true; +static bool values_percentage; +static bool report_io = true; static int swap_config(oconfig_item_t *ci) /* {{{ */ { @@ -136,23 +137,25 @@ static int swap_config(oconfig_item_t *ci) /* {{{ */ cf_util_get_boolean(child, &values_absolute); else if (strcasecmp("ValuesPercentage", child->key) == 0) cf_util_get_boolean(child, &values_percentage); + else if (strcasecmp("ReportIO", child->key) == 0) + cf_util_get_boolean(child, &report_io); else WARNING("swap plugin: Unknown config option: \"%s\"", child->key); } - return (0); + return 0; } /* }}} int swap_config */ static int swap_init(void) /* {{{ */ { #if KERNEL_LINUX pagesize = (derive_t)sysconf(_SC_PAGESIZE); -/* #endif KERNEL_LINUX */ + /* #endif KERNEL_LINUX */ #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS /* getpagesize(3C) tells me this does not fail.. */ pagesize = (derive_t)getpagesize(); -/* #endif HAVE_SWAPCTL */ + /* #endif HAVE_SWAPCTL */ #elif defined(VM_SWAPUSAGE) /* No init stuff */ @@ -172,9 +175,9 @@ static int swap_init(void) /* {{{ */ if (kvm_obj == NULL) { ERROR("swap plugin: kvm_openfiles failed, %s", errbuf); - return (-1); + return -1; } -/* #endif HAVE_LIBKVM_GETSWAPINFO */ + /* #endif HAVE_LIBKVM_GETSWAPINFO */ #elif HAVE_LIBSTATGRAB /* No init stuff */ @@ -184,7 +187,7 @@ static int swap_init(void) /* {{{ */ pagesize = getpagesize(); #endif /* HAVE_PERFSTAT */ - return (0); + return 0; } /* }}} int swap_init */ static void swap_submit_usage(char const *plugin_instance, /* {{{ */ @@ -200,10 +203,10 @@ static void swap_submit_usage(char const *plugin_instance, /* {{{ */ sstrncpy(vl.type, "swap", sizeof(vl.type)); if (values_absolute) - plugin_dispatch_multivalue(&vl, 0, DS_TYPE_GAUGE, "used", used, "free", + plugin_dispatch_multivalue(&vl, false, DS_TYPE_GAUGE, "used", used, "free", free, other_name, other_value, NULL); if (values_percentage) - plugin_dispatch_multivalue(&vl, 1, DS_TYPE_GAUGE, "used", used, "free", + plugin_dispatch_multivalue(&vl, true, DS_TYPE_GAUGE, "used", used, "free", free, other_name, other_value, NULL); } /* }}} void swap_submit_usage */ @@ -231,10 +234,8 @@ static int swap_read_separate(void) /* {{{ */ fh = fopen("/proc/swaps", "r"); if (fh == NULL) { - char errbuf[1024]; - WARNING("swap plugin: fopen (/proc/swaps) failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + WARNING("swap plugin: fopen (/proc/swaps) failed: %s", STRERRNO); + return -1; } while (fgets(buffer, sizeof(buffer), fh) != NULL) { @@ -273,7 +274,7 @@ static int swap_read_separate(void) /* {{{ */ fclose(fh); - return (0); + return 0; } /* }}} int swap_read_separate */ static int swap_read_combined(void) /* {{{ */ @@ -288,10 +289,8 @@ static int swap_read_combined(void) /* {{{ */ fh = fopen("/proc/meminfo", "r"); if (fh == NULL) { - char errbuf[1024]; - WARNING("swap plugin: fopen (/proc/meminfo) failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + WARNING("swap plugin: fopen (/proc/meminfo) failed: %s", STRERRNO); + return -1; } while (fgets(buffer, sizeof(buffer), fh) != NULL) { @@ -313,7 +312,7 @@ static int swap_read_combined(void) /* {{{ */ fclose(fh); if (isnan(swap_total) || isnan(swap_free)) - return (ENOENT); + return ENOENT; /* Some systems, OpenVZ for example, don't provide SwapCached. */ if (isnan(swap_cached)) @@ -323,70 +322,48 @@ static int swap_read_combined(void) /* {{{ */ assert(!isnan(swap_used)); if (swap_used < 0.0) - return (EINVAL); + return EINVAL; 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); + return 0; } /* }}} int swap_read_combined */ static int swap_read_io(void) /* {{{ */ { - FILE *fh; char buffer[1024]; - _Bool old_kernel = 0; - uint8_t have_data = 0; derive_t swap_in = 0; derive_t swap_out = 0; - fh = fopen("/proc/vmstat", "r"); + FILE *fh = fopen("/proc/vmstat", "r"); if (fh == NULL) { - /* /proc/vmstat does not exist in kernels <2.6 */ - fh = fopen("/proc/stat", "r"); - if (fh == NULL) { - char errbuf[1024]; - WARNING("swap: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); - } else - old_kernel = 1; + WARNING("swap: fopen(/proc/vmstat): %s", STRERRNO); + return -1; } while (fgets(buffer, sizeof(buffer), fh) != NULL) { char *fields[8]; - int numfields; + int numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)); - numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)); + if (numfields != 2) + continue; - if (!old_kernel) { - if (numfields != 2) - continue; - - if (strcasecmp("pswpin", fields[0]) == 0) { - strtoderive(fields[1], &swap_in); - have_data |= 0x01; - } else if (strcasecmp("pswpout", fields[0]) == 0) { - strtoderive(fields[1], &swap_out); - have_data |= 0x02; - } - } else /* if (old_kernel) */ - { - if (numfields != 3) - continue; - - if (strcasecmp("page", fields[0]) == 0) { - strtoderive(fields[1], &swap_in); - strtoderive(fields[2], &swap_out); - } + if (strcasecmp("pswpin", fields[0]) == 0) { + strtoderive(fields[1], &swap_in); + have_data |= 0x01; + } else if (strcasecmp("pswpout", fields[0]) == 0) { + strtoderive(fields[1], &swap_out); + have_data |= 0x02; } } /* while (fgets) */ fclose(fh); if (have_data != 0x03) - return (ENOENT); + return ENOENT; if (report_bytes) { swap_in = swap_in * pagesize; @@ -396,7 +373,7 @@ static int swap_read_io(void) /* {{{ */ swap_submit_derive("in", swap_in); swap_submit_derive("out", swap_out); - return (0); + return 0; } /* }}} int swap_read_io */ static int swap_read(void) /* {{{ */ @@ -406,11 +383,12 @@ static int swap_read(void) /* {{{ */ else swap_read_combined(); - swap_read_io(); + if (report_io) + swap_read_io(); - return (0); + return 0; } /* }}} int swap_read */ - /* #endif KERNEL_LINUX */ +/* #endif KERNEL_LINUX */ /* * Under Solaris, two mechanisms can be used to read swap statistics, swapctl @@ -432,10 +410,8 @@ static int swap_read_kstat(void) /* {{{ */ struct anoninfo ai; if (swapctl(SC_AINFO, &ai) == -1) { - char errbuf[1024]; - ERROR("swap plugin: swapctl failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + ERROR("swap plugin: swapctl failed: %s", STRERRNO); + return -1; } /* @@ -464,7 +440,7 @@ static int swap_read_kstat(void) /* {{{ */ swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize); swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv); - return (0); + return 0; } /* }}} int swap_read_kstat */ /* #endif 0 && HAVE_LIBKSTAT */ @@ -483,15 +459,15 @@ static int swap_read(void) /* {{{ */ swap_num = swapctl(SC_GETNSWP, NULL); if (swap_num < 0) { ERROR("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num); - return (-1); + return -1; } else if (swap_num == 0) - return (0); + return 0; /* Allocate and initialize the swaptbl_t structure */ s = malloc(swap_num * sizeof(swapent_t) + sizeof(struct swaptable)); if (s == NULL) { ERROR("swap plugin: malloc failed."); - return (-1); + return -1; } /* Memory to store the path names. We only use these paths when the @@ -501,7 +477,7 @@ static int swap_read(void) /* {{{ */ if (s_paths == NULL) { ERROR("swap plugin: calloc failed."); sfree(s); - return (-1); + return -1; } for (int i = 0; i < swap_num; i++) s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX); @@ -509,12 +485,10 @@ static int swap_read(void) /* {{{ */ status = swapctl(SC_LIST, s); if (status < 0) { - char errbuf[1024]; - ERROR("swap plugin: swapctl (SC_LIST) failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("swap plugin: swapctl (SC_LIST) failed: %s", STRERRNO); sfree(s_paths); sfree(s); - return (-1); + return -1; } else if (swap_num < status) { /* more elements returned than requested */ ERROR("swap plugin: I allocated memory for %i structure%s, " @@ -523,7 +497,7 @@ static int swap_read(void) /* {{{ */ swap_num, (swap_num == 1) ? "" : "s", status); sfree(s_paths); sfree(s); - return (-1); + return -1; } else if (swap_num > status) /* less elements returned than requested */ swap_num = status; @@ -558,17 +532,17 @@ static int swap_read(void) /* {{{ */ total, avail); sfree(s_paths); sfree(s); - return (-1); + return -1; } - /* If the "separate" option was specified (report_by_device == 1), all + /* If the "separate" option was specified (report_by_device == true) all * values have already been dispatched from within the loop. */ if (!report_by_device) swap_submit_usage(NULL, total - avail, avail, NULL, NAN); sfree(s_paths); sfree(s); - return (0); + return 0; } /* }}} int swap_read */ /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */ @@ -585,21 +559,21 @@ static int swap_read(void) /* {{{ */ swap_num = swapctl(SWAP_NSWAP, NULL, 0); if (swap_num < 0) { ERROR("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num); - return (-1); + return -1; } else if (swap_num == 0) - return (0); + return 0; swap_entries = calloc(swap_num, sizeof(*swap_entries)); if (swap_entries == NULL) { ERROR("swap plugin: calloc failed."); - return (-1); + return -1; } status = swapctl(SWAP_STATS, swap_entries, swap_num); if (status != swap_num) { ERROR("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status); sfree(swap_entries); - return (-1); + return -1; } #if defined(DEV_BSIZE) && (DEV_BSIZE > 0) @@ -623,13 +597,13 @@ static int swap_read(void) /* {{{ */ "swap plugin: Total swap space (%g) is less than used swap space (%g).", total, used); sfree(swap_entries); - return (-1); + return -1; } swap_submit_usage(NULL, used, total - used, NULL, NAN); sfree(swap_entries); - return (0); + return 0; } /* }}} int swap_read */ /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */ @@ -648,13 +622,13 @@ static int swap_read(void) /* {{{ */ sw_usage_len = sizeof(struct xsw_usage); if (sysctl(mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0) - return (-1); + return -1; /* The returned values are bytes. */ swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used, (gauge_t)sw_usage.xsu_avail, NULL, NAN); - return (0); + return 0; } /* }}} int swap_read */ /* #endif VM_SWAPUSAGE */ @@ -668,12 +642,12 @@ static int swap_read(void) /* {{{ */ gauge_t total; if (kvm_obj == NULL) - return (-1); + return -1; /* only one structure => only get the grand total, no details */ status = kvm_getswapinfo(kvm_obj, &data_s, 1, 0); if (status == -1) - return (-1); + return -1; total = (gauge_t)data_s.ksw_total; used = (gauge_t)data_s.ksw_used; @@ -683,7 +657,7 @@ static int swap_read(void) /* {{{ */ swap_submit_usage(NULL, used, total - used, NULL, NAN); - return (0); + return 0; } /* }}} int swap_read */ /* #endif HAVE_LIBKVM_GETSWAPINFO */ @@ -694,11 +668,11 @@ static int swap_read(void) /* {{{ */ swap = sg_get_swap_stats(); if (swap == NULL) - return (-1); + return -1; swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN); - return (0); + return 0; } /* }}} int swap_read */ /* #endif HAVE_LIBSTATGRAB */ @@ -715,10 +689,8 @@ static int swap_read(void) /* {{{ */ status = perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1); if (status < 0) { - char errbuf[1024]; - WARNING("swap plugin: perfstat_memory_total failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + WARNING("swap plugin: perfstat_memory_total failed: %s", STRERRNO); + return -1; } total = (gauge_t)(pmemory.pgsp_total * pagesize); @@ -726,10 +698,13 @@ static int swap_read(void) /* {{{ */ reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize); swap_submit_usage(NULL, total - free, free, "reserved", reserved); - swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize); - swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize); - return (0); + if (report_io) { + swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize); + swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize); + } + + return 0; } /* }}} int swap_read */ #endif /* HAVE_PERFSTAT */ @@ -738,5 +713,3 @@ void module_register(void) { plugin_register_init("swap", swap_init); plugin_register_read("swap", swap_read); } /* void module_register */ - -/* vim: set fdm=marker : */