[icinga-checkins] icinga.org: icinga2/master: Fixed precision for percentage calculations with large units

git at icinga.org git at icinga.org
Fri Feb 19 11:30:37 CET 2016


Module: icinga2
Branch: master
Commit: 5c18c1ed2da855711fea43e04e0d5d00b071923f
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=5c18c1ed2da855711fea43e04e0d5d00b071923f

Author: Per von Zweigbergk <pvz at itassistans.se>
Date:   Sat Oct 31 14:47:36 2015 +0100

Fixed precision for percentage calculations with large units

The check_memory and check_swap plugins on Windows were incorrectly
rounding the memory/swap measurements to the nearest unit prior to
calculating a percentage. This was causing imprecise percentage
values when the unit selected meant that the values in question had
few significant figures.

fixes #10497

Signed-off-by: Jean Flach <jean-marcel.flach at netways.de>

---

 plugins/check_memory.cpp |    8 ++++----
 plugins/check_memory.h   |    3 ++-
 plugins/check_swap.cpp   |    8 ++++----
 plugins/check_swap.h     |    1 +
 4 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/plugins/check_memory.cpp b/plugins/check_memory.cpp
index 254c2b2..48cd604 100644
--- a/plugins/check_memory.cpp
+++ b/plugins/check_memory.cpp
@@ -163,7 +163,6 @@ INT printOutput(printInfoStruct& printInfo)
 		std::wcout << L"Constructing output string" << '\n';
 
 	state state = OK;
-	double fswap = ((double)printInfo.aRam / (double)printInfo.tRam) * 100.0;
 
 	if (printInfo.warn.rend(printInfo.aRam, printInfo.tRam))
 		state = WARNING;
@@ -173,17 +172,17 @@ INT printOutput(printInfoStruct& printInfo)
 
 	switch (state) {
 	case OK:
-		std::wcout << L"MEMORY OK - " << fswap << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
+		std::wcout << L"MEMORY OK - " << printInfo.percentFree << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
 			<< printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
 			<< L";0;" << printInfo.tRam << '\n';
 		break;
 	case WARNING:
-		std::wcout << L"MEMORY WARNING - " << fswap << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
+		std::wcout << L"MEMORY WARNING - " << printInfo.percentFree << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
 			<< printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
 			<< L";0;" << printInfo.tRam << '\n';
 		break;
 	case CRITICAL:
-		std::wcout << L"MEMORY CRITICAL - " << fswap << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
+		std::wcout << L"MEMORY CRITICAL - " << printInfo.percentFree << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
 			<< printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
 			<< L";0;" << printInfo.tRam << '\n';
 		break;
@@ -205,6 +204,7 @@ INT check_memory(printInfoStruct& printInfo)
 
 	printInfo.tRam = round(pMemBuf->ullTotalPhys / pow(1024.0, printInfo.unit));
 	printInfo.aRam = round(pMemBuf->ullAvailPhys / pow(1024.0, printInfo.unit));
+	printInfo.percentFree = 100.0 * pMemBuf->ullAvailPhys / pMemBuf->ullTotalPhys;
 
 	if (debug)
 		std::wcout << L"Found pMemBuf->dwTotalPhys: " << pMemBuf->ullTotalPhys << '\n'
diff --git a/plugins/check_memory.h b/plugins/check_memory.h
index b3ef53c..ccb05fc 100644
--- a/plugins/check_memory.h
+++ b/plugins/check_memory.h
@@ -26,7 +26,8 @@
 struct printInfoStruct
 {
 	threshold warn, crit;
-	DWORDLONG tRam, aRam;
+	DOUBLE tRam, aRam;
+	DOUBLE percentFree;
 	Bunit unit = BunitMB;
 };
 
diff --git a/plugins/check_swap.cpp b/plugins/check_swap.cpp
index d321bad..1ecaa0d 100644
--- a/plugins/check_swap.cpp
+++ b/plugins/check_swap.cpp
@@ -163,7 +163,6 @@ INT printOutput(printInfoStruct& printInfo)
 		std::wcout << L"Constructing output string" << '\n';
 
 	state state = OK;
-	double fswap = ((double)printInfo.aSwap / (double)printInfo.tSwap) * 100.0;
 
 	if (printInfo.warn.rend(printInfo.aSwap, printInfo.tSwap))
 		state = WARNING;
@@ -173,17 +172,17 @@ INT printOutput(printInfoStruct& printInfo)
 
 	switch (state) {
 	case OK:
-		std::wcout << L"SWAP OK - " << fswap << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
+		std::wcout << L"SWAP OK - " << printInfo.percentFree << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
 			<< printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap) 
 			<< L";0;" << printInfo.tSwap << '\n';
 		break;
 	case WARNING:
-		std::wcout << L"SWAP WARNING - " << fswap << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
+		std::wcout << L"SWAP WARNING - " << printInfo.percentFree << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
 			<< printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap) 
 			<< L";0;" << printInfo.tSwap << '\n';
 		break;
 	case CRITICAL:
-		std::wcout << L"SWAP CRITICAL - " << fswap << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
+		std::wcout << L"SWAP CRITICAL - " << printInfo.percentFree << L"% free | swap=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
 			<< printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap) 
 			<< L";0;" << printInfo.tSwap << '\n';
 		break;
@@ -204,6 +203,7 @@ INT check_swap(printInfoStruct& printInfo)
 
 	printInfo.tSwap = round(MemBuf.ullTotalPageFile / pow(1024.0, printInfo.unit));
 	printInfo.aSwap = round(MemBuf.ullAvailPageFile / pow(1024.0, printInfo.unit));
+	printInfo.percentFree = 100.0 * MemBuf.ullAvailPageFile / MemBuf.ullTotalPageFile;
 
 	return -1;
 }
diff --git a/plugins/check_swap.h b/plugins/check_swap.h
index 894ed2a..4f51e5f 100644
--- a/plugins/check_swap.h
+++ b/plugins/check_swap.h
@@ -26,6 +26,7 @@ struct printInfoStruct
 {
 	threshold warn, crit;
 	DOUBLE tSwap, aSwap;
+	DOUBLE percentFree;
 	Bunit unit = BunitMB;
 };
 



More information about the icinga-checkins mailing list