diff options
Diffstat (limited to 'ATRI/plugins/status.py')
-rw-r--r-- | ATRI/plugins/status.py | 77 |
1 files changed, 49 insertions, 28 deletions
diff --git a/ATRI/plugins/status.py b/ATRI/plugins/status.py index bd5d9c4..efdd412 100644 --- a/ATRI/plugins/status.py +++ b/ATRI/plugins/status.py @@ -1,4 +1,6 @@ +import time import psutil +from datetime import datetime from nonebot.adapters.cqhttp import Bot, MessageEvent from ATRI.log import logger as log @@ -6,7 +8,7 @@ from ATRI.service import Service as sv from ATRI.rule import is_in_service from ATRI.exceptions import GetStatusError from ATRI.utils.apscheduler import scheduler -from ATRI.config import Config +from ATRI.config import BotSelfConfig __doc__ = """ @@ -16,8 +18,10 @@ __doc__ = """ /ping """ -ping = sv.on_command(cmd="/ping", docs="测试机器人", rule=is_in_service("ping")) - +ping = sv.on_command( + cmd="/ping", + docs="测试机器人", + rule=is_in_service('ping')) @ping.handle() async def _ping(bot: Bot, event: MessageEvent) -> None: @@ -31,8 +35,11 @@ __doc__ = """ /status """ -status = sv.on_command(cmd="/status", docs=__doc__, rule=is_in_service("status")) - +status = sv.on_command( + cmd="/status", + docs=__doc__, + rule=is_in_service('status') +) @status.handle() async def _status(bot: Bot, event: MessageEvent) -> None: @@ -40,22 +47,28 @@ async def _status(bot: Bot, event: MessageEvent) -> None: 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 + inteSENT = psutil.net_io_counters().bytes_sent / 1000000 # type: ignore + inteRECV = psutil.net_io_counters().bytes_recv / 1000000 # type: ignore + now = time.time() + boot = psutil.boot_time() + up_time = str( + datetime.utcfromtimestamp(now).replace(microsecond=0) + - datetime.utcfromtimestamp(boot).replace(microsecond=0) + ) except GetStatusError: raise GetStatusError("Failed to get status.") - + msg = "アトリは、高性能ですから!" - - if cpu > 80: # type: ignore + + if cpu > 90: # type: ignore msg = "咱感觉有些头晕..." - if mem > 80: + if mem > 90: msg = "咱感觉有点头晕并且有点累..." - elif mem > 80: + elif mem > 90: msg = "咱感觉有点累..." - elif disk > 80: + elif disk > 90: msg = "咱感觉身体要被塞满了..." - + msg0 = ( "Self status:\n" f"* CPU: {cpu}%\n" @@ -63,36 +76,41 @@ async def _status(bot: Bot, event: MessageEvent) -> None: f"* DISK: {disk}%\n" f"* netSENT: {inteSENT}MB\n" f"* netRECV: {inteRECV}MB\n" + f"* Runtime: {up_time}\n" ) + msg - + await status.finish(msg0) [email protected]_job("interval", minutes=5, misfire_grace_time=10) [email protected]_job( + 'interval', + minutes=5, + misfire_grace_time=10 +) async def _(): log.info("开始自检") 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 + inteSENT = psutil.net_io_counters().bytes_sent / 1000000 # type: ignore + inteRECV = psutil.net_io_counters().bytes_recv / 1000000 # type: ignore except GetStatusError: raise GetStatusError("Failed to get status.") - - msg = "" - if cpu > 80: # type: ignore + + msg = str() + if cpu > 90: # type: ignore msg = "咱感觉有些头晕..." - if mem > 80: + if mem > 90: msg = "咱感觉有点头晕并且有点累..." - elif mem > 80: + elif mem > 90: msg = "咱感觉有点累..." - elif disk > 80: + elif disk > 90: msg = "咱感觉身体要被塞满了..." else: log.info("运作正常") return - + msg0 = ( "Self status:\n" f"* CPU: {cpu}%\n" @@ -101,6 +119,9 @@ async def _(): f"* netSENT: {inteSENT}MB\n" f"* netRECV: {inteRECV}MB\n" ) + msg - - for sup in Config.BotSelfConfig.superusers: - await sv.NetworkPost.send_private_msg(user_id=sup, message=msg0) + + for sup in BotSelfConfig.superusers: + await sv.NetworkPost.send_private_msg( + user_id=sup, + message=msg0 + ) |