From eee704095403c0c0261727a797965afb34223877 Mon Sep 17 00:00:00 2001 From: KuetTai Date: Wed, 20 Mar 2024 07:55:54 +0000 Subject: [PATCH] improve debug flag --- main.py | 2 +- services/Evaluator.py | 9 ++++++++- services/rds/drivers/RdsCommon.py | 9 +++++++++ services/rds/drivers/RdsMssql.py | 3 ++- utils/Tools.py | 10 +++++----- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 8b33128..292a023 100644 --- a/main.py +++ b/main.py @@ -34,7 +34,7 @@ def number_format(num, places=2): # print(crossAccounts) -DEBUG = True if debugFlag in _C.CLI_TRUE_KEYWORD_ARRAY or debugFlag is True else False +DEBUG = False if debugFlag in _C.CLI_TRUE_KEYWORD_ARRAY or debugFlag is True else False testmode = True if testmode in _C.CLI_TRUE_KEYWORD_ARRAY or testmode is True else False crossAccounts = True if crossAccounts in _C.CLI_TRUE_KEYWORD_ARRAY or crossAccounts is True else False _cli_options['crossAccounts'] = crossAccounts diff --git a/services/Evaluator.py b/services/Evaluator.py index 03be166..851f849 100644 --- a/services/Evaluator.py +++ b/services/Evaluator.py @@ -18,13 +18,17 @@ def run(self, serviceName): rulePrefix = serviceName.__name__ + '::rules' rules = Config.get(rulePrefix, []) + debugFlag = Config.get('DEBUG') + ecnt = cnt = 0 emsg = [] methods = [method for method in dir(self) if method.startswith('__') is False and method.startswith('_check') is True] for method in methods: if not rules or str.lower(method[6:]) in rules: try: - # print('--- --- fn: ' + method) + if debugFlag: + print('--- --- fn: ' + method) + getattr(self, method)() cnt += 1 except botocore.exceptions.ClientError as e: @@ -53,6 +57,9 @@ def run(self, serviceName): 'exceptions': scanned['exceptions'] + ecnt }) + if debugFlag: + self.showInfo() + def showInfo(self): print("Class: {}".format(self.classname)) print(self.getInfo()) diff --git a/services/rds/drivers/RdsCommon.py b/services/rds/drivers/RdsCommon.py index 7717049..1fd06d1 100644 --- a/services/rds/drivers/RdsCommon.py +++ b/services/rds/drivers/RdsCommon.py @@ -440,6 +440,9 @@ def _checkFreeStorage(self): GBYTES = 1024 * 1024 * 1024 dp = results['Datapoints'] + if len(dp) == 0: + return + freesize = round(dp[0]['Average'] / GBYTES, 4) ratio = freesize / self.db['AllocatedStorage'] @@ -578,6 +581,9 @@ def _checkCPUUtilization(self): Statistics=['Minimum', 'Maximum', 'Average'] ) monthDp = resp.get('Datapoints') + if len(monthDp) == 0: + return + monthAverage = monthDp[0]['Average'] monthMax = monthDp[0]['Maximum'] @@ -637,8 +643,11 @@ def _checkFreeMemory(self): ) dp = resp.get('Datapoints') + if len(dp) == 0: + return serverGB = self.instInfo['specification']['memoryInGiB'] + freeMemoryMin = dp[0]['Minimum'] / rawToGBRatio freeMemoryMax = dp[0]['Maximum'] / rawToGBRatio freeMemoryAvg = dp[0]['Average'] / rawToGBRatio diff --git a/services/rds/drivers/RdsMssql.py b/services/rds/drivers/RdsMssql.py index 694f106..8219f5d 100644 --- a/services/rds/drivers/RdsMssql.py +++ b/services/rds/drivers/RdsMssql.py @@ -1,4 +1,4 @@ -import math +import math, ast from utils.Tools import _pr from .RdsCommon import RdsCommon @@ -78,6 +78,7 @@ def _checkParamMaxServerMemory(self): if 'DBInstanceClassMemory' in maxMemorySettings: maxMemorySettings = maxMemorySettings.replace("{", "") maxMemorySettings = maxMemorySettings.replace("}", "") + maxMemorySettings = eval(maxMemorySettings, {"DBInstanceClassMemory": memInKBytes}) ## Need to be review diff --git a/utils/Tools.py b/utils/Tools.py index de312e4..7ca6852 100644 --- a/utils/Tools.py +++ b/utils/Tools.py @@ -7,20 +7,20 @@ from netaddr import IPAddress -def _pr(s): +def _pr(s, forcePrint = False): DEBUG = Config.get('DEBUG') - if DEBUG == True: + if forcePrint or DEBUG == True: print(s) def _info(s): _printStatus("info", s) def _warn(s): - _printStatus("\033[1;41m__!! WARNING !!__\033[0m", s) + _printStatus("\033[1;41m__!! WARNING !!__\033[0m", s, forcePrint=True) -def _printStatus(status, s): +def _printStatus(status, s, forcePrint = False): p = "["+status+"] "+ s - _pr(p) + _pr(p, forcePrint) def checkIsPrivateIp(ipaddr): ip = ipaddr.split('/')