2024-04-22 23:55:33 +08:00
|
|
|
import platform
|
|
|
|
import time
|
|
|
|
|
|
|
|
import nonebot
|
|
|
|
import psutil
|
|
|
|
from cpuinfo import cpuinfo
|
2024-04-30 15:23:12 +08:00
|
|
|
from nonebot import require
|
2024-05-16 21:17:10 +08:00
|
|
|
from nonebot.adapters import satori
|
|
|
|
|
2024-05-12 02:47:14 +08:00
|
|
|
from liteyuki.utils import __NAME__, __VERSION__
|
|
|
|
from liteyuki.utils.base.config import get_config
|
|
|
|
from liteyuki.utils.base.data_manager import TempConfig, common_db
|
|
|
|
from liteyuki.utils.base.language import Language
|
|
|
|
from liteyuki.utils.base.resource import get_loaded_resource_packs, get_path
|
|
|
|
from liteyuki.utils.message.html_tool import template2image
|
2024-05-16 21:17:10 +08:00
|
|
|
from liteyuki.utils import satori_utils
|
|
|
|
from .counter_for_satori import satori_counter
|
2024-04-22 23:55:33 +08:00
|
|
|
|
2024-04-30 15:23:12 +08:00
|
|
|
require("nonebot_plugin_apscheduler")
|
|
|
|
from nonebot_plugin_apscheduler import scheduler
|
|
|
|
|
2024-04-22 23:55:33 +08:00
|
|
|
protocol_names = {
|
2024-05-16 21:17:10 +08:00
|
|
|
0: "iPad",
|
|
|
|
1: "Android Phone",
|
|
|
|
2: "Android Watch",
|
|
|
|
3: "Mac",
|
|
|
|
5: "iPad",
|
|
|
|
6: "Android Pad",
|
2024-04-22 23:55:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
Universal Interface
|
|
|
|
data
|
|
|
|
- bot
|
|
|
|
- name: str
|
|
|
|
icon: str
|
|
|
|
id: int
|
|
|
|
protocol_name: str
|
|
|
|
groups: int
|
|
|
|
friends: int
|
|
|
|
message_sent: int
|
|
|
|
message_received: int
|
|
|
|
app_name: str
|
|
|
|
- hardware
|
|
|
|
- cpu
|
|
|
|
- percent: float
|
|
|
|
- name: str
|
|
|
|
- mem
|
|
|
|
- percent: float
|
|
|
|
- total: int
|
|
|
|
- used: int
|
|
|
|
- free: int
|
|
|
|
- swap
|
|
|
|
- percent: float
|
|
|
|
- total: int
|
|
|
|
- used: int
|
|
|
|
- free: int
|
|
|
|
- disk: list
|
|
|
|
- name: str
|
|
|
|
- percent: float
|
|
|
|
- total: int
|
|
|
|
"""
|
2024-04-30 15:23:12 +08:00
|
|
|
status_card_cache = {} # lang -> bytes
|
|
|
|
|
|
|
|
|
|
|
|
# 60s刷新一次
|
|
|
|
@scheduler.scheduled_job("cron", second="*/40")
|
|
|
|
async def refresh_status_card():
|
|
|
|
nonebot.logger.debug("Refreshing status card cache...")
|
|
|
|
global status_card_cache
|
|
|
|
bot_data = await get_bots_data()
|
|
|
|
hardware_data = await get_hardware_data()
|
|
|
|
liteyuki_data = await get_liteyuki_data()
|
|
|
|
for lang in status_card_cache.keys():
|
|
|
|
status_card_cache[lang] = await generate_status_card(
|
|
|
|
bot_data,
|
|
|
|
hardware_data,
|
|
|
|
liteyuki_data,
|
|
|
|
lang=lang,
|
|
|
|
use_cache=False
|
|
|
|
)
|
2024-04-22 23:55:33 +08:00
|
|
|
|
|
|
|
|
2024-05-16 21:17:10 +08:00
|
|
|
async def generate_status_card(bot: dict, hardware: dict, liteyuki: dict, lang="zh-CN", bot_id="0",
|
|
|
|
use_cache=False) -> bytes:
|
2024-04-27 02:20:44 +08:00
|
|
|
if not use_cache:
|
|
|
|
return await template2image(
|
|
|
|
get_path("templates/status.html", abs_path=True),
|
|
|
|
{
|
2024-05-16 21:17:10 +08:00
|
|
|
"data": {
|
|
|
|
"bot": bot,
|
|
|
|
"hardware": hardware,
|
|
|
|
"liteyuki": liteyuki,
|
|
|
|
"localization": get_local_data(lang)
|
|
|
|
}
|
2024-04-27 02:20:44 +08:00
|
|
|
},
|
|
|
|
debug=True
|
|
|
|
)
|
|
|
|
else:
|
2024-04-30 15:23:12 +08:00
|
|
|
if lang not in status_card_cache:
|
|
|
|
status_card_cache[lang] = await generate_status_card(bot, hardware, liteyuki, lang=lang, bot_id=bot_id)
|
|
|
|
return status_card_cache[lang]
|
2024-04-22 23:55:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_local_data(lang_code) -> dict:
|
|
|
|
lang = Language(lang_code)
|
|
|
|
return {
|
2024-05-16 21:17:10 +08:00
|
|
|
"friends": lang.get("status.friends"),
|
|
|
|
"groups": lang.get("status.groups"),
|
|
|
|
"plugins": lang.get("status.plugins"),
|
|
|
|
"bots": lang.get("status.bots"),
|
|
|
|
"message_sent": lang.get("status.message_sent"),
|
|
|
|
"message_received": lang.get("status.message_received"),
|
|
|
|
"cpu": lang.get("status.cpu"),
|
|
|
|
"memory": lang.get("status.memory"),
|
|
|
|
"swap": lang.get("status.swap"),
|
|
|
|
"disk": lang.get("status.disk"),
|
2024-04-22 23:55:33 +08:00
|
|
|
|
2024-05-16 21:17:10 +08:00
|
|
|
"usage": lang.get("status.usage"),
|
|
|
|
"total": lang.get("status.total"),
|
|
|
|
"used": lang.get("status.used"),
|
|
|
|
"free": lang.get("status.free"),
|
2024-04-23 21:49:16 +08:00
|
|
|
|
2024-05-16 21:17:10 +08:00
|
|
|
"days": lang.get("status.days"),
|
|
|
|
"hours": lang.get("status.hours"),
|
|
|
|
"minutes": lang.get("status.minutes"),
|
|
|
|
"seconds": lang.get("status.seconds"),
|
|
|
|
"runtime": lang.get("status.runtime"),
|
|
|
|
"threads": lang.get("status.threads"),
|
|
|
|
"cores": lang.get("status.cores"),
|
|
|
|
"process": lang.get("status.process"),
|
|
|
|
"resources": lang.get("status.resources"),
|
|
|
|
"description": lang.get("status.description"),
|
2024-04-22 23:55:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def get_bots_data(self_id: str = "0") -> dict:
|
|
|
|
"""获取当前所有机器人数据
|
|
|
|
Returns:
|
|
|
|
"""
|
|
|
|
result = {
|
2024-05-16 21:17:10 +08:00
|
|
|
"self_id": self_id,
|
|
|
|
"bots": [],
|
2024-04-22 23:55:33 +08:00
|
|
|
}
|
|
|
|
for bot_id, bot in nonebot.get_bots().items():
|
|
|
|
groups = 0
|
|
|
|
friends = 0
|
|
|
|
status = {}
|
|
|
|
bot_name = bot_id
|
|
|
|
version_info = {}
|
2024-05-16 21:17:10 +08:00
|
|
|
if isinstance(bot, satori.Bot):
|
|
|
|
try:
|
|
|
|
bot_name = (await satori_utils.user_infos.get(bot.self_id)).name
|
|
|
|
groups = str(await satori_utils.count_groups(bot))
|
|
|
|
friends = str(await satori_utils.count_friends(bot))
|
|
|
|
status = {}
|
|
|
|
version_info = await bot.get_version_info()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
# API fetch
|
|
|
|
bot_name = (await bot.get_login_info())["nickname"]
|
|
|
|
groups = len(await bot.get_group_list())
|
|
|
|
friends = len(await bot.get_friend_list())
|
|
|
|
status = await bot.get_status()
|
|
|
|
version_info = await bot.get_version_info()
|
|
|
|
except Exception:
|
|
|
|
pass
|
2024-04-22 23:55:33 +08:00
|
|
|
|
|
|
|
statistics = status.get("stat", {})
|
|
|
|
app_name = version_info.get("app_name", "UnknownImplementation")
|
|
|
|
if app_name in ["Lagrange.OneBot", "LLOneBot", "Shamrock"]:
|
|
|
|
icon = f"https://q.qlogo.cn/g?b=qq&nk={bot_id}&s=640"
|
2024-05-16 21:17:10 +08:00
|
|
|
elif isinstance(bot, satori.Bot):
|
|
|
|
app_name = "Satori"
|
|
|
|
icon = (await bot.login_get()).user.avatar
|
2024-04-22 23:55:33 +08:00
|
|
|
else:
|
|
|
|
icon = None
|
|
|
|
bot_data = {
|
2024-05-16 21:17:10 +08:00
|
|
|
"name": bot_name,
|
|
|
|
"icon": icon,
|
|
|
|
"id": bot_id,
|
|
|
|
"protocol_name": protocol_names.get(version_info.get("protocol_name"), "Online"),
|
|
|
|
"groups": groups,
|
|
|
|
"friends": friends,
|
|
|
|
"message_sent": satori_counter.msg_sent if isinstance(bot, satori.Bot) else statistics.get("message_sent", 0),
|
|
|
|
"message_received": satori_counter.msg_received if isinstance(bot, satori.Bot) else statistics.get("message_received", 0),
|
|
|
|
"app_name": app_name
|
2024-04-22 23:55:33 +08:00
|
|
|
}
|
|
|
|
result["bots"].append(bot_data)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
async def get_hardware_data() -> dict:
|
|
|
|
mem = psutil.virtual_memory()
|
2024-04-24 00:44:45 +08:00
|
|
|
all_processes = psutil.Process().children(recursive=True)
|
|
|
|
all_processes.append(psutil.Process())
|
|
|
|
|
2024-04-26 15:02:46 +08:00
|
|
|
mem_used_process = 0
|
2024-04-24 00:44:45 +08:00
|
|
|
process_mem = {}
|
|
|
|
for process in all_processes:
|
|
|
|
try:
|
|
|
|
ps_name = process.name().replace(".exe", "")
|
|
|
|
if ps_name not in process_mem:
|
|
|
|
process_mem[ps_name] = 0
|
|
|
|
process_mem[ps_name] += process.memory_info().rss
|
2024-04-26 15:02:46 +08:00
|
|
|
mem_used_process += process.memory_info().rss
|
2024-04-24 00:44:45 +08:00
|
|
|
except Exception:
|
|
|
|
pass
|
2024-04-22 23:55:33 +08:00
|
|
|
swap = psutil.swap_memory()
|
|
|
|
cpu_brand_raw = cpuinfo.get_cpu_info().get("brand_raw", "Unknown")
|
|
|
|
if "AMD" in cpu_brand_raw:
|
|
|
|
brand = "AMD"
|
|
|
|
elif "Intel" in cpu_brand_raw:
|
|
|
|
brand = "Intel"
|
|
|
|
else:
|
|
|
|
brand = "Unknown"
|
|
|
|
result = {
|
2024-05-16 21:17:10 +08:00
|
|
|
"cpu": {
|
|
|
|
"percent": psutil.cpu_percent(),
|
|
|
|
"name": f"{brand} {cpuinfo.get_cpu_info().get('arch', 'Unknown')}",
|
|
|
|
"cores": psutil.cpu_count(logical=False),
|
|
|
|
"threads": psutil.cpu_count(logical=True),
|
|
|
|
"freq": psutil.cpu_freq().current # MHz
|
|
|
|
},
|
|
|
|
"memory": {
|
|
|
|
"percent": mem.percent,
|
|
|
|
"total": mem.total,
|
|
|
|
"used": mem.used,
|
|
|
|
"free": mem.free,
|
|
|
|
"usedProcess": mem_used_process,
|
|
|
|
},
|
|
|
|
"swap": {
|
|
|
|
"percent": swap.percent,
|
|
|
|
"total": swap.total,
|
|
|
|
"used": swap.used,
|
|
|
|
"free": swap.free
|
|
|
|
},
|
|
|
|
"disk": [],
|
2024-04-22 23:55:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for disk in psutil.disk_partitions(all=True):
|
|
|
|
try:
|
|
|
|
disk_usage = psutil.disk_usage(disk.mountpoint)
|
2024-04-26 15:02:46 +08:00
|
|
|
if disk_usage.total == 0:
|
2024-04-26 20:20:53 +08:00
|
|
|
continue # 虚拟磁盘
|
2024-04-22 23:55:33 +08:00
|
|
|
result["disk"].append({
|
2024-05-16 21:17:10 +08:00
|
|
|
"name": disk.mountpoint,
|
|
|
|
"percent": disk_usage.percent,
|
|
|
|
"total": disk_usage.total,
|
|
|
|
"used": disk_usage.used,
|
|
|
|
"free": disk_usage.free
|
2024-04-22 23:55:33 +08:00
|
|
|
})
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
async def get_liteyuki_data() -> dict:
|
2024-05-12 00:18:53 +08:00
|
|
|
temp_data: TempConfig = common_db.where_one(TempConfig(), default=TempConfig())
|
2024-04-22 23:55:33 +08:00
|
|
|
result = {
|
2024-05-16 21:17:10 +08:00
|
|
|
"name": list(get_config("nickname", [__NAME__]))[0],
|
|
|
|
"version": __VERSION__,
|
|
|
|
"plugins": len(nonebot.get_loaded_plugins()),
|
|
|
|
"resources": len(get_loaded_resource_packs()),
|
|
|
|
"nonebot": f"{nonebot.__version__}",
|
|
|
|
"python": f"{platform.python_implementation()} {platform.python_version()}",
|
|
|
|
"system": f"{platform.system()} {platform.release()}",
|
|
|
|
"runtime": time.time() - temp_data.data.get("start_time", time.time()), # 运行时间秒数
|
|
|
|
"bots": len(nonebot.get_bots())
|
2024-04-22 23:55:33 +08:00
|
|
|
}
|
|
|
|
return result
|