From f76e3fc0c6b6243c5b07842c341ada409495cd37 Mon Sep 17 00:00:00 2001 From: yungwine Date: Wed, 20 Mar 2024 15:17:58 +0700 Subject: [PATCH 1/3] add sending db stats to telemetry --- mytoncore/functions.py | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/mytoncore/functions.py b/mytoncore/functions.py index 4e516d26..cd659bf0 100755 --- a/mytoncore/functions.py +++ b/mytoncore/functions.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf_8 -*-l import os +import re import sys import psutil import time @@ -419,6 +420,55 @@ def GetValidatorProcessInfo(): # 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) + result[s[0]] = {} + items = re.findall(r"(\S+)\s:\s(\S+)", s[1]) + for k, v in items: + result[s[0]][k] = v + return result + + +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' + 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' + return result + + def Telemetry(local, ton): sendTelemetry = local.db.get("sendTelemetry") if sendTelemetry is not True: @@ -442,6 +492,7 @@ def Telemetry(local, ton): data["swap"] = GetSwapInfo() data["uname"] = GetUname() data["vprocess"] = GetValidatorProcessInfo() + data["db"] = get_db_stats() elections = local.try_function(ton.GetElectionEntries) complaints = local.try_function(ton.GetComplaints) From 7acc59ac6b8f466d1587c5dfa2417d281b611f46 Mon Sep 17 00:00:00 2001 From: yungwine Date: Fri, 22 Mar 2024 11:54:00 +0800 Subject: [PATCH 2/3] update db stats parsing --- mytoncore/functions.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mytoncore/functions.py b/mytoncore/functions.py index cd659bf0..4abb8a81 100755 --- a/mytoncore/functions.py +++ b/mytoncore/functions.py @@ -426,10 +426,15 @@ def parse_db_stats(path: str): result = {} for line in lines: s = line.strip().split(maxsplit=1) - result[s[0]] = {} items = re.findall(r"(\S+)\s:\s(\S+)", s[1]) - for k, v in items: - result[s[0]][k] = v + 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 From 3c3c80577ad580c725e7813f5786e338d75c6758 Mon Sep 17 00:00:00 2001 From: yungwine Date: Fri, 22 Mar 2024 11:58:00 +0800 Subject: [PATCH 3/3] improves --- mytoncore/functions.py | 30 ++++++++------------------- mytoncore/utils.py | 46 ++++++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/mytoncore/functions.py b/mytoncore/functions.py index 4abb8a81..ffb9a3ef 100755 --- a/mytoncore/functions.py +++ b/mytoncore/functions.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf_8 -*-l import os -import re import sys import psutil import time @@ -9,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, @@ -420,24 +419,6 @@ def GetValidatorProcessInfo(): # 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 - - def get_db_stats(): result = { 'rocksdb': { @@ -462,6 +443,8 @@ def get_db_stats(): 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) @@ -471,7 +454,10 @@ def get_db_stats(): else: result['celldb']['ok'] = False result['celldb']['message'] = 'db stats file is not exists' + # end if + return result +# end define def Telemetry(local, ton): @@ -497,7 +483,7 @@ def Telemetry(local, ton): data["swap"] = GetSwapInfo() data["uname"] = GetUname() data["vprocess"] = GetValidatorProcessInfo() - data["db"] = get_db_stats() + 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