diff options
author | Kyomotoi <[email protected]> | 2022-05-13 23:59:19 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2022-05-13 23:59:19 +0800 |
commit | 817b55b84ab65e0404b15195bed97a46d977f3b9 (patch) | |
tree | b957216155b870eebd8a81f9a4d6cc18b59e97da /ATRI/plugins/status/data_source.py | |
parent | 7042d94213532191a0c72d34d7c85193184c079f (diff) | |
download | ATRI-817b55b84ab65e0404b15195bed97a46d977f3b9.tar.gz ATRI-817b55b84ab65e0404b15195bed97a46d977f3b9.tar.bz2 ATRI-817b55b84ab65e0404b15195bed97a46d977f3b9.zip |
🚚 独立控制台
Diffstat (limited to 'ATRI/plugins/status/data_source.py')
-rw-r--r-- | ATRI/plugins/status/data_source.py | 79 |
1 files changed, 6 insertions, 73 deletions
diff --git a/ATRI/plugins/status/data_source.py b/ATRI/plugins/status/data_source.py index 1658468..ddd3568 100644 --- a/ATRI/plugins/status/data_source.py +++ b/ATRI/plugins/status/data_source.py @@ -1,24 +1,13 @@ import os import time -import json import psutil -import socket -import string -from pathlib import Path -from random import sample from datetime import datetime from ATRI.service import Service -from ATRI.log import logger as log from ATRI.rule import is_in_service -from ATRI.utils import request -from ATRI.exceptions import GetStatusError, WriteError -from .models import PlatfromRuntimeInfo, BotRuntimeInfo +from ATRI.exceptions import GetStatusError -STATUS_DIR = Path(".") / "data" / "database" / "status" -STATUS_DIR.mkdir(exist_ok=True) - _status_msg = """ > Status Overview @@ -44,15 +33,13 @@ class Status(Service): return "I'm fine." @staticmethod - def get_status(is_for_fn: bool = False) -> tuple: - data_p = Path(".") / "data" - + def get_status() -> tuple: try: cpu = psutil.cpu_percent(interval=1) mem = psutil.virtual_memory().percent disk = psutil.disk_usage("/").percent - inteSENT = psutil.net_io_counters().bytes_sent / 1000000 # type: ignore - inteRECV = psutil.net_io_counters().bytes_recv / 1000000 # type: ignore + inte_send = psutil.net_io_counters().bytes_sent / 1000000 # type: ignore + inte_recv = psutil.net_io_counters().bytes_recv / 1000000 # type: ignore process = psutil.Process(os.getpid()) b_cpu = process.cpu_percent(interval=1) @@ -88,71 +75,17 @@ class Status(Service): else: is_ok = True - if is_for_fn: - return ( - PlatfromRuntimeInfo( - stat_msg=msg, - cpu_percent=str(cpu), - mem_percent=mem, - disk_percent=str(disk), - inte_send=str(inteSENT), - inte_recv=str(inteRECV), - boot_time=boot_time, - ).dict(), - BotRuntimeInfo( - cpu_percent=str(b_cpu), - mem_percent=str(b_mem), - bot_run_time=bot_time, - ).dict(), - ) - msg0 = _status_msg.format( p_cpu=cpu, p_mem=mem, p_disk=disk, b_cpu=f"{b_cpu}%", b_mem="%.1f%%" % b_mem, - inteSENT=inteSENT, - inteRECV=inteRECV, + inteSENT=inte_send, + inteRECV=inte_recv, bot_time=bot_time, boot_time=boot_time, msg=msg, ) return msg0, is_ok - - @staticmethod - async def get_host_ip(is_pub: bool): - if is_pub: - data = await request.get("https://ifconfig.me/ip") - return data.text - - s = None - try: - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(('8.8.8.8', 80)) - ip = s.getsockname()[0] - return ip - finally: - if s: - s.close() - - @staticmethod - def get_random_str(k: int) -> str: - return "".join(sample(string.ascii_letters + string.digits, k)) - - @staticmethod - def get_auth_info() -> dict: - df = STATUS_DIR / "data.json" - if not df.is_file(): - try: - with open(df, "w", encoding="utf-8") as w: - w.write(json.dumps({})) - except WriteError: - raise WriteError("Writing file: " + str(df) + " failed!") - - base_data: dict = json.loads(df.read_bytes()) - data = base_data.get("data", None) - if not data: - return {"data": None} - return data |