From d30a80eda0ec5f62bdb197cac762f41a7f66c8ce Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Sat, 23 Nov 2024 07:50:25 +0300 Subject: [PATCH] add logs --- checker.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/checker.py b/checker.py index d75d176..a76ad39 100644 --- a/checker.py +++ b/checker.py @@ -88,7 +88,7 @@ class GRIST: raise ValueError(f"Setting {key} not found") -def check_logs(): +def check_logs(logger): # Initialize counters error_count = 0 sync_count = 0 @@ -96,7 +96,9 @@ def check_logs(): # Get current time and 24 hours ago current_time = datetime.now() + logger.info(f"Current time: {current_time}") day_ago = current_time - timedelta(days=1) + logger.info(f"Max logs timestamp: {day_ago}") try: result = subprocess.run(['docker', 'compose', 'logs'], cwd='/root/node/', capture_output=True, text=True) @@ -111,19 +113,28 @@ def check_logs(): if not timestamp: continue if timestamp < day_ago: continue - if "Error from tendermint rpc" in line: error_count += 1 - if "Synced with network" in line: sync_count += 1 - + if "Error from tendermint rpc" in line: + error_count += 1 + logger.error(f"RPC error: {line}") + if "Synced with network" in line: + sync_count += 1 + logger.info(f"Synced with network: {line}") + challenge_match = re.search(r'made (\d+) secret challenges', line) - if challenge_match: total_challenges += int(challenge_match.group(1)) + if challenge_match: + total_challenges += int(challenge_match.group(1)) + logger.info(f"Made {total_challenges} secret challenges: {line}") - return { + result = { "rpc_errors": error_count, "sync_events": sync_count, "total_challenges": total_challenges } + logger.info(f"Result: {result}") + return result if __name__ == "__main__": + print("Checker started") colorama.init(autoreset=True) logger = logging.getLogger("Impact updater") logger.setLevel(logging.INFO) @@ -134,17 +145,17 @@ if __name__ == "__main__": time.sleep(random.randint(1, 600)) - grist_server = "###GRIST_SERVER###" - grist_doc_id = "###GRIST_DOC_ID###" - grist_api_key = "###GRIST_API_KEY###" - grist_row_name = socket.gethostname() - nodes_table = "Nodes" - grist = GRIST(grist_server, grist_doc_id, grist_api_key, logger) - current_vm = grist.find_record(name=grist_row_name, table=nodes_table)[0] - def grist_callback(msg): grist.update(current_vm.id, msg, nodes_table) + GRIST_SERVER = "###GRIST_SERVER###" + GRIST_DOC_ID = "###GRIST_DOC_ID###" + GRIST_API_KEY = "###GRIST_API_KEY###" + GRIST_ROW_NAME = socket.gethostname() + NODES_TABLE = "Nodes" + grist = GRIST(GRIST_SERVER, GRIST_DOC_ID, GRIST_API_KEY, logger) + current_vm = grist.find_record(name=GRIST_ROW_NAME, table=NODES_TABLE)[0] + def grist_callback(msg): grist.update(current_vm.id, msg, NODES_TABLE) try: - result = check_logs() + result = check_logs(logger) data = f"{result['sync_events']}/{result['total_challenges']}/{result['rpc_errors']}" # Syncs/Challenges/RPC errors grist_callback({ "Health": data }) print(result)