From 728e998a001c0e255c2328ea3a7cab01ad31b064 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Diemer?= <stephane.diemer@ubicast.eu>
Date: Tue, 13 Nov 2018 14:24:06 +0100
Subject: [PATCH] Check RAM instead of swap (refs #27150).

---
 tests/test_partitions.py | 159 ++++++++++++++++++---------------------
 1 file changed, 75 insertions(+), 84 deletions(-)

diff --git a/tests/test_partitions.py b/tests/test_partitions.py
index c0b52287..4b7d32c1 100755
--- a/tests/test_partitions.py
+++ b/tests/test_partitions.py
@@ -9,62 +9,59 @@ import subprocess
 import sys
 import os
 
-BS = 512
 YELLOW = '\033[93m'
 GREEN = '\033[92m'
 RED = '\033[91m'
 DEF = '\033[0m'
 
 
-def to_gbytes(size_bytes):
-    return int(round(size_bytes / (1024 * 1024 * 1024)))
-
-
-def read_file(fname):
-    with open(fname, 'r') as f:
-        return f.read()
-
-
-paths = [
+PATHS = [
     {
         'mount_point': '/',
-        'recommended_types': ('ext4',),
+        'recommended_types': ('ext4', 'zfs'),
         'min_size_gbytes': 9,
         'reco_size_gbytes': 14,
         'min_available_gbytes': 4,
     },
     {
         'mount_point': '/home/msuser/msinstance',
-        'recommended_types': ('ext4', 'nfs', 'nfs4', 'zfs'),
+        'recommended_types': ('ext4', 'zfs', 'nfs', 'nfs4'),
         'min_size_gbytes': 5,
         'reco_size_gbytes': 300,
         'min_available_gbytes': 5,
     },
     {
         'mount_point': '/home/skyreach',
-        'recommended_types': ('ext4', 'nfs', 'nfs4', 'zfs'),
+        'recommended_types': ('ext4', 'zfs', 'nfs', 'nfs4'),
         'min_size_gbytes': 5,
         'reco_size_gbytes': 9,
         'min_available_gbytes': 2,
     },
     {
-        'type': 'swap',
-        'min_size_gbytes': 1,
-        'reco_size_gbytes': 2,
+        'type': 'memory',
+        'min_size_gbytes': 2,
+        'reco_size_gbytes': 4,
     },
 ]
 
 
-def get_swap_gbytes():
-    d = read_file('/proc/meminfo')
-    for l in d.split('\n'):
-        if 'SwapTotal'in l:
-            swap = l.split('SwapTotal:')[1].strip()
-            swap_kbytes, unit = swap.split(' ')
-            if unit != 'kB':
-                print('Warning, unexpected unit %s.' % unit)
-            swap_gbytes = int(round(int(swap_kbytes) / (1024 * 1024)))
-            return swap_gbytes
+def to_gbytes(size_bytes):
+    return int(round(size_bytes / (1024 * 1024 * 1024)))
+
+
+def get_memory_gbytes():
+    memory_gbytes = 0
+    with open('/proc/meminfo', 'r') as f:
+        for l in f:
+            if 'MemTotal:' in l:
+                memory = l.split('MemTotal:')[1].strip()
+                memory_kbytes, unit = memory.split(' ')
+                if unit != 'kB':
+                    print('Warning, unexpected unit %s.' % unit)
+                memory_gbytes = int(round(int(memory_kbytes) / (1024 * 1024)))
+    if not memory_gbytes:
+        print('Failed to get memory size.')
+    return memory_gbytes
 
 
 def get_path(path):
@@ -82,11 +79,11 @@ def check_allocation(dev):
     root_dev = os.path.basename(dev)[:3]
     if not root_dev:
         return True
-    d = read_file('/proc/partitions')
     dev_partitions = list()
-    for l in d.split('\n'):
-        if root_dev in l:
-            dev_partitions.append(l)
+    with open('/proc/partitions', 'r') as f:
+        for l in f:
+            if root_dev in l:
+                dev_partitions.append(l)
 
     max_size = 0
     total_size = 0
@@ -105,58 +102,52 @@ def check_allocation(dev):
     return True
 
 
-error = False
-warning = False
-
-
-def check_path(p):
-    global error
-    global warning
-
-    psize = None
-    mount_point = p.get('mount_point')
-    if mount_point:
-        if os.path.exists(mount_point):
-            mount_point = os.path.realpath(mount_point)
-            name = mount_point
-            dev, fstype, psize, available = get_path(mount_point)
-            if fstype not in p.get('recommended_types'):
-                print('Warning, partition of %s fs type not recommended %s(current: %s, recommended: %s)%s.' % (name, YELLOW, fstype, p['recommended_types'], DEF))
-                warning = True
-            if 'nfs' not in fstype:
-                warning = not check_allocation(dev)
-            min_available_gbytes = p.get('min_available_gbytes')
-            if min_available_gbytes and available < min_available_gbytes:
-                print('Partition of %s has less than %s GB available %s(%s GB available)%s.' % (name, min_available_gbytes, RED, available, DEF))
+if __name__ == '__main__':
+    error = False
+    warning = False
+    for p in PATHS:
+        psize = None
+        if p.get('mount_point'):
+            mount_point = p['mount_point']
+            if os.path.exists(mount_point):
+                mount_point = os.path.realpath(mount_point)
+                name = 'Partition of %s' % mount_point
+                dev, fstype, psize, available = get_path(mount_point)
+                if fstype not in p.get('recommended_types'):
+                    print('Warning: %s fs type not recommended %s(current: %s, recommended: %s)%s.' % (name, YELLOW, fstype, p['recommended_types'], DEF))
+                    warning = True
+                if 'nfs' not in fstype:
+                    warning = not check_allocation(dev)
+                min_available_gbytes = p.get('min_available_gbytes')
+                if min_available_gbytes and available < min_available_gbytes:
+                    print('%s has less than %s GB available %s(%s GB available)%s.' % (name, min_available_gbytes, RED, available, DEF))
+                    error = True
+                else:
+                    print('%s has %s GB available.' % (name, available))
+            else:
+                print('%s not found, cannot check.' % mount_point)
+        elif p.get('type') == 'memory':
+            name = 'Memory'
+            psize = get_memory_gbytes()
+
+        if psize:
+            if psize < p['min_size_gbytes']:
+                print('%s is smaller than the minimum required size %s(%s GB < %s GB)%s.' % (name, RED, psize, p['min_size_gbytes'], DEF))
                 error = True
+            elif psize < p['reco_size_gbytes']:
+                print('%s is smaller than the recommended size %s(%s GB < %s GB)%s.' % (name, YELLOW, psize, p['reco_size_gbytes'], DEF))
+                warning = True
             else:
-                print('Partition of %s has %s GB available.' % (name, available))
-        else:
-            print('%s not found, cannot check.' % mount_point)
-    elif p.get('type') == 'swap':
-        name = 'swap'
-        psize = get_swap_gbytes()
-
-    if psize:
-        if psize < p['min_size_gbytes']:
-            print('Partition of %s is smaller than the minimum required size %s(%s GB < %s GB)%s.' % (name, RED, psize, p['min_size_gbytes'], DEF))
-            error = True
-        elif psize < p['reco_size_gbytes']:
-            print('Partition of %s is smaller than the recommended size %s(%s GB < %s GB)%s.' % (name, YELLOW, psize, p['reco_size_gbytes'], DEF))
-            warning = True
-
-
-for p in paths:
-    check_path(p)
-
-if error:
-    print('Errors found.')
-    code = 1
-elif warning:
-    print('Some warnings were found.')
-    code = 3
-else:
-    print(GREEN + 'All OK.' + DEF)
-    code = 0
-
-sys.exit(code)
+                print('%s is bigger than recommended size %s(%s GB >= %s GB)%s.' % (name, GREEN, psize, p['reco_size_gbytes'], DEF))
+
+    if error:
+        print('Errors found.')
+        code = 1
+    elif warning:
+        print('Some warnings were found.')
+        code = 3
+    else:
+        print(GREEN + 'All OK.' + DEF)
+        code = 0
+
+    sys.exit(code)
-- 
GitLab