2024-11-24 02:51:12 +03:00
|
|
|
# flake8: noqa
|
|
|
|
# pylint: disable=broad-exception-raised, raise-missing-from, too-many-arguments, redefined-outer-name
|
|
|
|
# pylance: disable=reportMissingImports, reportMissingModuleSource, reportGeneralTypeIssues
|
|
|
|
# type: ignore
|
|
|
|
|
2024-11-23 04:32:31 +03:00
|
|
|
import re
|
2024-11-23 06:27:45 +03:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2024-11-23 04:32:31 +03:00
|
|
|
import subprocess
|
|
|
|
import os
|
2024-11-23 06:27:45 +03:00
|
|
|
import time
|
|
|
|
import random
|
2024-11-23 04:32:31 +03:00
|
|
|
import sys
|
|
|
|
import pkg_resources
|
2024-11-24 02:51:12 +03:00
|
|
|
import requests
|
2024-11-24 04:31:42 +03:00
|
|
|
import json
|
2024-11-23 04:32:31 +03:00
|
|
|
required_packages = ['grist-api', 'colorama']
|
|
|
|
installed_packages = [pkg.key for pkg in pkg_resources.working_set]
|
|
|
|
|
|
|
|
for package in required_packages:
|
|
|
|
if package not in installed_packages:
|
|
|
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package, '--break-system-packages'])
|
|
|
|
|
|
|
|
from grist_api import GristDocAPI
|
|
|
|
import colorama
|
2024-11-24 02:51:12 +03:00
|
|
|
|
2024-11-23 04:32:31 +03:00
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
|
|
|
|
|
|
|
|
class GRIST:
|
|
|
|
def __init__(self, server, doc_id, api_key, logger):
|
|
|
|
self.server = server
|
|
|
|
self.doc_id = doc_id
|
|
|
|
self.api_key = api_key
|
|
|
|
self.logger = logger
|
|
|
|
self.grist = GristDocAPI(doc_id, server=server, api_key=api_key)
|
|
|
|
|
|
|
|
def table_name_convert(self, table_name):
|
|
|
|
return table_name.replace(" ", "_")
|
|
|
|
|
|
|
|
def to_timestamp(self, dtime: datetime) -> int:
|
|
|
|
if dtime.tzinfo is None:
|
|
|
|
dtime = dtime.replace(tzinfo=timezone(timedelta(hours=3)))
|
|
|
|
return int(dtime.timestamp())
|
|
|
|
|
|
|
|
def insert_row(self, data, table):
|
|
|
|
data = {key.replace(" ", "_"): value for key, value in data.items()}
|
|
|
|
row_id = self.grist.add_records(self.table_name_convert(table), [data])
|
|
|
|
return row_id
|
|
|
|
|
|
|
|
def update_column(self, row_id, column_name, value, table):
|
|
|
|
if isinstance(value, datetime):
|
|
|
|
value = self.to_timestamp(value)
|
|
|
|
column_name = column_name.replace(" ", "_")
|
|
|
|
self.grist.update_records(self.table_name_convert(table), [{ "id": row_id, column_name: value }])
|
|
|
|
|
|
|
|
def delete_row(self, row_id, table):
|
|
|
|
self.grist.delete_records(self.table_name_convert(table), [row_id])
|
|
|
|
|
|
|
|
def update(self, row_id, updates, table):
|
|
|
|
for column_name, value in updates.items():
|
|
|
|
if isinstance(value, datetime):
|
|
|
|
updates[column_name] = self.to_timestamp(value)
|
|
|
|
updates = {column_name.replace(" ", "_"): value for column_name, value in updates.items()}
|
|
|
|
self.grist.update_records(self.table_name_convert(table), [{"id": row_id, **updates}])
|
|
|
|
|
|
|
|
def fetch_table(self, table):
|
|
|
|
return self.grist.fetch_table(self.table_name_convert(table))
|
|
|
|
|
|
|
|
def find_record(self, id=None, state=None, name=None, table=None):
|
|
|
|
if table is None:
|
|
|
|
raise ValueError("Table is not specified")
|
|
|
|
table_data = self.grist.fetch_table(self.table_name_convert(table))
|
|
|
|
if id is not None:
|
|
|
|
record = [row for row in table_data if row.id == id]
|
|
|
|
return record
|
|
|
|
if state is not None and name is not None:
|
|
|
|
record = [row for row in table_data if row.State == state and row.name == name]
|
|
|
|
return record
|
|
|
|
if state is not None:
|
|
|
|
record = [row for row in table_data if row.State == state]
|
|
|
|
return record
|
|
|
|
if name is not None:
|
|
|
|
record = [row for row in table_data if row.Name == name]
|
|
|
|
return record
|
|
|
|
|
|
|
|
def find_settings(self, key, table):
|
|
|
|
table = self.fetch_table(self.table_name_convert(table))
|
|
|
|
for record in table:
|
|
|
|
if record.Setting == key:
|
|
|
|
if record.Value is None or record.Value == "":
|
|
|
|
raise ValueError(f"Setting {key} blank")
|
|
|
|
return record.Value
|
|
|
|
raise ValueError(f"Setting {key} not found")
|
|
|
|
|
|
|
|
|
2024-11-24 02:51:12 +03:00
|
|
|
def check_logs(log_handler):
|
2024-11-23 04:32:31 +03:00
|
|
|
# Initialize counters
|
|
|
|
error_count = 0
|
|
|
|
sync_count = 0
|
|
|
|
total_challenges = 0
|
2024-11-24 02:51:12 +03:00
|
|
|
|
2024-11-23 04:32:31 +03:00
|
|
|
try:
|
2024-11-24 02:51:12 +03:00
|
|
|
logs = subprocess.run(['docker', 'compose', 'logs', '--since', '24h'], cwd='/root/node/', capture_output=True, text=True, check=True)
|
|
|
|
log_content = logs.stdout
|
2024-11-23 04:32:31 +03:00
|
|
|
except subprocess.CalledProcessError as e:
|
2024-11-24 02:51:12 +03:00
|
|
|
raise RuntimeError(f"Error running docker compose logs: {e}") from e
|
2024-11-23 04:32:31 +03:00
|
|
|
|
|
|
|
for line in log_content.split('\n'):
|
2024-11-23 07:50:25 +03:00
|
|
|
if "Error from tendermint rpc" in line:
|
|
|
|
error_count += 1
|
2024-11-24 02:51:12 +03:00
|
|
|
log_handler.error(f"RPC error: {line}")
|
2024-11-24 02:42:23 +03:00
|
|
|
if "Is your verifier's account funded" in line:
|
2024-11-24 02:51:12 +03:00
|
|
|
log_handler.error(f"Verifier account not funded: {line}")
|
2024-11-24 02:42:23 +03:00
|
|
|
error_count += 1
|
2024-11-23 07:50:25 +03:00
|
|
|
if "Synced with network" in line:
|
|
|
|
sync_count += 1
|
2024-11-24 02:51:12 +03:00
|
|
|
log_handler.info(f"Synced with network: {line}")
|
2024-11-23 07:50:25 +03:00
|
|
|
|
2024-11-23 04:32:31 +03:00
|
|
|
challenge_match = re.search(r'made (\d+) secret challenges', line)
|
2024-11-23 07:50:25 +03:00
|
|
|
if challenge_match:
|
|
|
|
total_challenges += int(challenge_match.group(1))
|
2024-11-24 02:51:12 +03:00
|
|
|
log_handler.info(f"Made {total_challenges} secret challenges: {line}")
|
|
|
|
data = {
|
2024-11-24 02:42:23 +03:00
|
|
|
"errors": error_count,
|
2024-11-23 04:32:31 +03:00
|
|
|
"sync_events": sync_count,
|
|
|
|
"total_challenges": total_challenges
|
|
|
|
}
|
2024-11-24 02:51:12 +03:00
|
|
|
log_handler.info(f"Result: {data}")
|
|
|
|
return data
|
2024-11-23 04:32:31 +03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
colorama.init(autoreset=True)
|
2024-11-24 01:21:20 +03:00
|
|
|
logger = logging.getLogger("Checker")
|
2024-11-23 04:32:31 +03:00
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
logger.addHandler(ch)
|
|
|
|
|
2024-11-24 04:31:42 +03:00
|
|
|
logger.info("Checker started")
|
2024-11-24 04:24:54 +03:00
|
|
|
random_sleep = random.randint(1, 600)
|
|
|
|
logger.info(f"Sleeping for {random_sleep} seconds")
|
|
|
|
time.sleep(random_sleep)
|
2024-11-23 05:10:51 +03:00
|
|
|
|
2024-11-24 04:31:42 +03:00
|
|
|
grist_data = {}
|
|
|
|
with open('/root/node/grist.json', 'r', encoding='utf-8') as f:
|
|
|
|
grist_data = json.loads(f.read())
|
|
|
|
|
2024-11-23 07:50:25 +03:00
|
|
|
GRIST_ROW_NAME = socket.gethostname()
|
|
|
|
NODES_TABLE = "Nodes"
|
2024-11-24 04:31:42 +03:00
|
|
|
grist = GRIST(grist_data.get('grist_server'), grist_data.get('grist_doc_id'), grist_data.get('grist_api_key'), logger)
|
2024-11-23 07:50:25 +03:00
|
|
|
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)
|
2024-11-23 04:32:31 +03:00
|
|
|
|
2024-11-24 02:45:59 +03:00
|
|
|
for attempt in range(3):
|
|
|
|
try:
|
|
|
|
result = check_logs(logger)
|
|
|
|
data = f"{result['sync_events']}/{result['total_challenges']}/{result['errors']}" # Syncs/Challenges/Errors
|
|
|
|
grist_callback({ "Health": data })
|
|
|
|
print(result)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error on attempt {attempt+1}/3: {e}")
|
|
|
|
if attempt == 2:
|
|
|
|
grist_callback({ "Health": f"Error: {e}" })
|
|
|
|
if attempt < 2:
|
|
|
|
time.sleep(5)
|