diff options
author | Kyomotoi <[email protected]> | 2021-07-08 22:09:00 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2021-07-08 22:09:00 +0800 |
commit | be2747e4d4b820ca0f1f988d3b77a628da26fe7b (patch) | |
tree | e1a59dd79ecd973a7d704568dcdc018f1f1b651a /ATRI/plugins/status/data_source.py | |
parent | a4e1b9d1581d756ef79ad063d1c0bd6b2fd13c1d (diff) | |
download | ATRI-be2747e4d4b820ca0f1f988d3b77a628da26fe7b.tar.gz ATRI-be2747e4d4b820ca0f1f988d3b77a628da26fe7b.tar.bz2 ATRI-be2747e4d4b820ca0f1f988d3b77a628da26fe7b.zip |
🔖♻️🐛🔧🔥📝 更新版本:YHN-001-A03
🔖 更新版本至:YHN-001-A03
✨ 新增插件:
- 涩图
- 闲聊(文爱
♻️ 重构:
- Service
- 所有插件
🐛 修复部分小bug
🔧 暂时移除部分设置
🔥 删除:
- 插件:nsfw、wife。日后加回
- 插件 essential 中部分内容
📝 更新README
Diffstat (limited to 'ATRI/plugins/status/data_source.py')
-rw-r--r-- | ATRI/plugins/status/data_source.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/ATRI/plugins/status/data_source.py b/ATRI/plugins/status/data_source.py new file mode 100644 index 0000000..c353313 --- /dev/null +++ b/ATRI/plugins/status/data_source.py @@ -0,0 +1,71 @@ +import time +import psutil +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.exceptions import GetStatusError + + +__doc__ = """ +检查咱自身状态 +""" + + +class IsSurvive(Service): + + def __init__(self): + Service.__init__(self, "状态", __doc__, rule=is_in_service("状态")) + + @staticmethod + def ping() -> str: + return "I'm fine." + + @staticmethod + def get_status(): + 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 + + 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 > 90: # type: ignore + msg = "咱感觉有些头晕..." + is_ok = False + if mem > 90: + msg = "咱感觉有点头晕并且有点累..." + is_ok = False + elif mem > 90: + msg = "咱感觉有点累..." + is_ok = False + elif disk > 90: + msg = "咱感觉身体要被塞满了..." + is_ok = False + else: + log.info("资源占用正常") + is_ok = True + + msg0 = ( + "Self status:\n" + f"* CPU: {cpu}%\n" + f"* MEM: {mem}%\n" + f"* DISK: {disk}%\n" + f"* netSENT: {inteSENT}MB\n" + f"* netRECV: {inteRECV}MB\n" + f"* Runtime: {up_time}\n" + ) + msg + + return msg0, is_ok |