diff --git a/mytoncore/functions.py b/mytoncore/functions.py index 4e516d26..ffb9a3ef 100755 --- a/mytoncore/functions.py +++ b/mytoncore/functions.py @@ -8,8 +8,8 @@ import requests import subprocess -from mytoncore.mytoncore import MyTonCore, Dec2HexAddr -from mytoncore.tonblocksscanner import TonBlocksScanner +from mytoncore.mytoncore import MyTonCore +from mytoncore.utils import parse_db_stats from mypylib.mypylib import ( b2mb, get_timestamp, @@ -419,6 +419,47 @@ def GetValidatorProcessInfo(): # end define +def get_db_stats(): + result = { + 'rocksdb': { + 'ok': True, + 'message': '', + 'data': {} + }, + 'celldb': { + 'ok': True, + 'message': '', + 'data': {} + }, + } + rocksdb_stats_path = '/var/ton-work/db/db_stats.txt' + celldb_stats_path = '/var/ton-work/db/celldb/db_stats.txt' + if os.path.exists(rocksdb_stats_path): + try: + result['rocksdb']['data'] = parse_db_stats(rocksdb_stats_path) + except Exception as e: + result['rocksdb']['ok'] = False + result['rocksdb']['message'] = f'failed to fetch db stats: {e}' + else: + result['rocksdb']['ok'] = False + result['rocksdb']['message'] = 'db stats file is not exists' + # end if + + if os.path.exists(celldb_stats_path): + try: + result['celldb']['data'] = parse_db_stats(celldb_stats_path) + except Exception as e: + result['celldb']['ok'] = False + result['celldb']['message'] = f'failed to fetch db stats: {e}' + else: + result['celldb']['ok'] = False + result['celldb']['message'] = 'db stats file is not exists' + # end if + + return result +# end define + + def Telemetry(local, ton): sendTelemetry = local.db.get("sendTelemetry") if sendTelemetry is not True: @@ -442,6 +483,7 @@ def Telemetry(local, ton): data["swap"] = GetSwapInfo() data["uname"] = GetUname() data["vprocess"] = GetValidatorProcessInfo() + data["dbStats"] = get_db_stats() elections = local.try_function(ton.GetElectionEntries) complaints = local.try_function(ton.GetComplaints) diff --git a/mytoncore/utils.py b/mytoncore/utils.py index 9743b0fc..0a8bdc91 100644 --- a/mytoncore/utils.py +++ b/mytoncore/utils.py @@ -1,5 +1,6 @@ import base64 import json +import re def str2b64(s): @@ -49,19 +50,19 @@ def b642hex(input): def xhex2hex(x): - try: - b = x[1:] - h = b.lower() - return h - except: - return None + try: + b = x[1:] + h = b.lower() + return h + except: + return None #end define def hex2base64(h): # TODO: remove duplicates - b = bytes.fromhex(h) - b64 = base64.b64encode(b) - s = b64.decode("utf-8") - return s + b = bytes.fromhex(h) + b64 = base64.b64encode(b) + s = b64.decode("utf-8") + return s #end define @@ -73,7 +74,26 @@ def str2bool(str): def ng2g(ng): - if ng is None: - return - return int(ng)/10**9 + if ng is None: + return + return int(ng)/10**9 #end define + + +def parse_db_stats(path: str): + with open(path) as f: + lines = f.readlines() + result = {} + for line in lines: + s = line.strip().split(maxsplit=1) + items = re.findall(r"(\S+)\s:\s(\S+)", s[1]) + if len(items) == 1: + item = items[0] + if float(item[1]) > 0: + result[s[0]] = float(item[1]) + else: + if any(float(v) > 0 for k, v in items): + result[s[0]] = {} + result[s[0]] = {k: float(v) for k, v in items} + return result +# end define