This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
plugin.py
57 lines (49 loc) · 1.79 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from datetime import datetime
from nonebot.adapters import MessageTemplate
from nonebot.adapters.onebot.v11 import Bot, Message, MessageEvent
from nonebot.permission import SUPERUSER
from nonebot.plugin import on_fullmatch
from .process import ProcessesManager, RunningProcessDetail
handler = on_fullmatch(("gocq", "gocq状态"), permission=SUPERUSER)
STATUS_MESSAGE_TEMPLATE = MessageTemplate(
"帐号{account}统计数据:\n" "日志条数: {total_logs}\n" "重启次数: {restarts}\n",
Message,
)
RUNNING_MESSAGE_TEMPLATE = MessageTemplate(
"进程状态:running\n"
"➡️ CPU: {cpu_percent:.3%}\n"
"➡️ 内存: {memory:.3f}MB\n"
"➡️ 在线时间: {uptime}\n",
Message,
)
STOPPED_MESSAGE_TEMPLATE = MessageTemplate(
"进程状态:stopped\n" "➡️ 退出代码: {code}",
Message,
)
@handler.handle()
async def _(bot: Bot, event: MessageEvent):
messages = Message()
for process in ProcessesManager.all():
try:
status = await process.status()
except RuntimeError:
continue
messages += STATUS_MESSAGE_TEMPLATE.format(
account=process.account.uin,
total_logs=status.total_logs,
restarts=status.restarts,
)
messages += (
RUNNING_MESSAGE_TEMPLATE.format(
cpu_percent=status.details.cpu_percent,
memory=status.details.memory_used / 1024**2,
uptime=(
datetime.now() - datetime.fromtimestamp(status.details.start_time)
),
)
if isinstance(status.details, RunningProcessDetail)
else STOPPED_MESSAGE_TEMPLATE.format(
code=status.details.code if status.details else None
)
)
await bot.send(event, messages)