Add Status Plugin

This commit is contained in:
luoshuijs 2023-12-16 16:41:57 +08:00 committed by xtaodada
parent 6bfd33b4e4
commit 5bad0b820e
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 86 additions and 12 deletions

74
plugins/system/status.py Normal file
View File

@ -0,0 +1,74 @@
import asyncio
import os
from platform import python_version
from time import time
from typing import TYPE_CHECKING
import psutil
from telegram import __version__
from core.plugin import Plugin, handler
from utils.log import logger
if TYPE_CHECKING:
from telegram import Update
from telegram.ext import ContextTypes
class Status(Plugin):
def __init__(self):
self.pid = os.getpid()
self.time_form = "%m/%d %H:%M"
@handler.command(command="status", block=False, admin=True)
async def send_log(self, update: "Update", _: "ContextTypes.DEFAULT_TYPE"):
user = update.effective_user
logger.info("用户 %s[%s] status 命令请求", user.full_name, user.id)
message = update.effective_message
current_process = psutil.Process(self.pid)
psutil.cpu_percent()
await asyncio.sleep(0.1)
cpu_percent = psutil.cpu_percent()
memory = psutil.virtual_memory()
total_memory = memory.total
used_memory = memory.used
process_cpu_use = current_process.cpu_percent()
process_use = current_process.memory_info()
start_time = current_process.create_time()
memory_text = (
f"{total_memory / (1024 * 1024 * 1024):.2f}GB/"
f"{used_memory / (1024 * 1024 * 1024):.2f}GB/"
f"{process_use.rss / (1024 * 1024 * 1024):.2f}GB"
)
text = (
"PamGram 运行状态"
f"Python 版本: `{python_version()}` \n"
f"Telegram 版本: `{__version__}` \n"
f"CPU使用率: `{cpu_percent}%/{process_cpu_use}%` \n"
f"当前使用的内存: `{memory_text}` \n"
f"运行时间: `{self.get_bot_uptime(start_time)}` \n"
)
await message.reply_markdown_v2(text)
def get_bot_uptime(self, start_time: float) -> str:
uptime_sec = time() - start_time
return self.human_time_duration(int(uptime_sec), self.time_form)
@staticmethod
def human_time_duration(seconds: int, time_form: str) -> str:
parts = {}
time_units = (
("%m", 60 * 60 * 24 * 30),
("%d", 60 * 60 * 24),
("%H", 60 * 60),
("%M", 60),
("%S", 1),
)
for unit, div in time_units:
amount, seconds = divmod(int(seconds), div)
parts[unit] = str(amount)
for key, value in parts.items():
time_form = time_form.replace(key, value)
return time_form

View File

@ -8,41 +8,41 @@ readme = "README.md"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.8" python = "^3.8"
httpx = "^0.24.0" httpx = "^0.25.0"
ujson = "^5.8.0" ujson = "^5.9.0"
Jinja2 = "^3.1.2" Jinja2 = "^3.1.2"
python-telegram-bot = { version = "^20.5", extras = ["ext", "rate-limiter"] } python-telegram-bot = { version = "^20.7", extras = ["ext", "rate-limiter"] }
sqlmodel = "^0.0.11" sqlmodel = "^0.0.14"
colorlog = "^6.7.0" colorlog = "^6.8.0"
fakeredis = "^2.19.0" fakeredis = "^2.19.0"
redis = "^5.0.1" redis = "^5.0.1"
beautifulsoup4 = "^4.12.1" beautifulsoup4 = "^4.12.1"
asyncmy = "^0.2.7" asyncmy = "^0.2.9"
aiofiles = "^23.2.1" aiofiles = "^23.2.1"
python-dotenv = "^1.0.0" python-dotenv = "^1.0.0"
alembic = "^1.12.0" alembic = "^1.13.0"
black = "^23.9.1" black = "^23.9.1"
rich = "^13.6.0" rich = "^13.6.0"
TgCrypto = { version = "^1.2.5", optional = true } TgCrypto = { version = "^1.2.5", optional = true }
Pyrogram = { version = "^2.0.102", optional = true } Pyrogram = { version = "^2.0.102", optional = true }
pytest = { version = "^7.3.0", optional = true } pytest = { version = "^7.3.0", optional = true }
pytest-asyncio = { version = "^0.21.0", optional = true } pytest-asyncio = { version = "^0.23.2", optional = true }
flaky = { version = "^3.7.0", optional = true } flaky = { version = "^3.7.0", optional = true }
lxml = "^4.9.2" lxml = "^4.9.2"
arko-wrapper = "^0.2.8" arko-wrapper = "^0.2.8"
fastapi = "<0.100.0" fastapi = "<0.105.0"
uvicorn = { extras = ["standard"], version = "^0.23.1" } uvicorn = { extras = ["standard"], version = "^0.24.0" }
sentry-sdk = "^1.31.0" sentry-sdk = "^1.31.0"
GitPython = "^3.1.30" GitPython = "^3.1.30"
openpyxl = "^3.1.1" openpyxl = "^3.1.1"
async-lru = "^2.0.4" async-lru = "^2.0.4"
thefuzz = "^0.20.0" thefuzz = "^0.20.0"
qrcode = "^7.4.2"
cryptography = "^41.0.4" cryptography = "^41.0.4"
pillow = "^10.0.1" pillow = "^10.0.1"
playwright = "^1.28.0" playwright = "^1.39.0"
aiosqlite = { extras = ["sqlite"], version = "^0.19.0" } aiosqlite = { extras = ["sqlite"], version = "^0.19.0" }
simnet = { git = "https://github.com/PaiGramTeam/SIMNet" } simnet = { git = "https://github.com/PaiGramTeam/SIMNet" }
psutil = "^5.9.6"
[tool.poetry.extras] [tool.poetry.extras]
pyro = ["Pyrogram", "TgCrypto"] pyro = ["Pyrogram", "TgCrypto"]