summaryrefslogtreecommitdiff
diff options
-rwxr-xr-xbin/memory_compare38
1 files changed, 27 insertions, 11 deletions
diff --git a/bin/memory_compare b/bin/memory_compare
index 21c9673..0f56801 100755
--- a/bin/memory_compare
+++ b/bin/memory_compare
@@ -2,10 +2,11 @@
import os
import sys
+from subprocess import check_output, PIPE
from checkbox_support.parsers.lshwjson import LshwJsonParser
from checkbox_support.parsers.meminfo import MeminfoParser
-from subprocess import check_output, PIPE
+
class LshwJsonResult:
@@ -18,8 +19,9 @@ class LshwJsonResult:
elif 'bank' in hardware['id']:
self.banks_reported += int(hardware.get('size', 0))
+
def get_installed_memory_size():
- lshw = LshwJsonParser(check_output(['lshw','-json'],
+ lshw = LshwJsonParser(check_output(['lshw', '-json'],
universal_newlines=True,
stderr=PIPE))
result = LshwJsonResult()
@@ -30,6 +32,7 @@ def get_installed_memory_size():
else:
return result.banks_reported
+
class MeminfoResult:
memtotal = 0
@@ -37,6 +40,7 @@ class MeminfoResult:
def setMemory(self, memory):
self.memtotal = memory['total']
+
def get_visible_memory_size():
parser = MeminfoParser(open('/proc/meminfo'))
result = MeminfoResult()
@@ -44,6 +48,7 @@ def get_visible_memory_size():
return result.memtotal
+
def get_threshold(installed_memory):
GB = 1024**3
if installed_memory <= 2 * GB:
@@ -53,6 +58,7 @@ def get_threshold(installed_memory):
else:
return 10
+
def main():
if os.geteuid() != 0:
print("This script must be run as root.", file=sys.stderr)
@@ -67,22 +73,32 @@ def main():
percentage = difference / installed_memory * 100
except ZeroDivisionError:
print("Results:")
- print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024), file=sys.stderr)
- print("\tlshw reports:\t%s kB" % (installed_memory / 1024), file=sys.stderr)
- print("\nFAIL: Either lshw or /proc/meminfo returned a memory size of 0 kB", file=sys.stderr)
+ print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024),
+ file=sys.stderr)
+ print("\tlshw reports:\t%s kB" % (installed_memory / 1024),
+ file=sys.stderr)
+ print("\nFAIL: Either lshw or /proc/meminfo returned a memory size "
+ "of 0 kB", file=sys.stderr)
return 1
-
+
if percentage <= threshold:
print("Results:")
print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024))
print("\tlshw reports:\t%s kB" % (installed_memory / 1024))
- print("\nPASS: Meminfo reports %d bytes less than lshw, a difference of %.2f%%. This is less than the %d%% variance allowed." % (difference, percentage, threshold))
+ print("\nPASS: Meminfo reports %d bytes less than lshw, a "
+ "difference of %.2f%%. This is less than the "
+ "%d%% variance allowed." % (difference, percentage, threshold))
return 0
else:
- print("Results")
- print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024), file=sys.stderr)
- print("\tlshw reports:\t%s kB" % (installed_memory / 1024), file=sys.stderr)
- print("\nFAIL: Meminfo reports %d bytes less than lshw, a difference of %.2f%%. Only a variance of %d%% in reported memory is allowed." % (difference, percentage, threshold), file=sys.stderr)
+ print("Results:", file=sys.stderr)
+ print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024),
+ file=sys.stderr)
+ print("\tlshw reports:\t%s kB" % (installed_memory / 1024),
+ file=sys.stderr)
+ print("\nFAIL: Meminfo reports %d bytes less than lshw, "
+ "a difference of %.2f%%. Only a variance of %d%% in "
+ "reported memory is allowed." %
+ (difference, percentage, threshold), file=sys.stderr)
return 1
if __name__ == "__main__":