Path: blob/master/bot/modules/stats.py
1621 views
from time import time1from re import search as research2from asyncio import gather3from aiofiles.os import path as aiopath4from psutil import (5disk_usage,6cpu_percent,7swap_memory,8cpu_count,9virtual_memory,10net_io_counters,11boot_time,12)1314from .. import bot_start_time15from ..helper.ext_utils.status_utils import get_readable_file_size, get_readable_time16from ..helper.ext_utils.bot_utils import cmd_exec, new_task17from ..helper.telegram_helper.message_utils import send_message1819commands = {20"aria2": (["aria2c", "--version"], r"aria2 version ([\d.]+)"),21"qBittorrent": (["qbittorrent-nox", "--version"], r"qBittorrent v([\d.]+)"),22"SABnzbd+": (["sabnzbdplus", "--version"], r"sabnzbdplus-([\d.]+)"),23"python": (["python3", "--version"], r"Python ([\d.]+)"),24"rclone": (["rclone", "--version"], r"rclone v([\d.]+)"),25"yt-dlp": (["yt-dlp", "--version"], r"([\d.]+)"),26"ffmpeg": (["ffmpeg", "-version"], r"ffmpeg version ([\d.]+(-\w+)?).*"),27"7z": (["7z", "i"], r"7-Zip ([\d.]+)"),28}293031@new_task32async def bot_stats(_, message):33total, used, free, disk = disk_usage("/")34swap = swap_memory()35memory = virtual_memory()36per_cpu = cpu_percent(interval=1, percpu=True)37per_cpu_str = " | ".join([f"CPU{i+1}: {round(p)}%" for i, p in enumerate(per_cpu)])38stats = f"""39<b>Commit Date:</b> {commands["commit"]}4041<b>Bot Uptime:</b> {get_readable_time(time() - bot_start_time)}42<b>OS Uptime:</b> {get_readable_time(time() - boot_time())}4344<b>Total Disk Space:</b> {get_readable_file_size(total)}45<b>Used:</b> {get_readable_file_size(used)} | <b>Free:</b> {get_readable_file_size(free)}4647<b>Upload:</b> {get_readable_file_size(net_io_counters().bytes_sent)}48<b>Download:</b> {get_readable_file_size(net_io_counters().bytes_recv)}4950<b>CPU:</b> {cpu_percent(interval=1)}%51<b>CPU Cores:</b>52{per_cpu_str}5354<b>RAM:</b> {memory.percent}%55<b>DISK:</b> {disk}%5657<b>Physical Cores:</b> {cpu_count(logical=False)}58<b>Total Cores:</b> {cpu_count()}59<b>SWAP:</b> {get_readable_file_size(swap.total)} | <b>Used:</b> {swap.percent}%6061<b>Memory Total:</b> {get_readable_file_size(memory.total)}62<b>Memory Free:</b> {get_readable_file_size(memory.available)}63<b>Memory Used:</b> {get_readable_file_size(memory.used)}6465<b>python:</b> {commands["python"]}66<b>aria2:</b> {commands["aria2"]}67<b>qBittorrent:</b> {commands["qBittorrent"]}68<b>SABnzbd+:</b> {commands["SABnzbd+"]}69<b>rclone:</b> {commands["rclone"]}70<b>yt-dlp:</b> {commands["yt-dlp"]}71<b>ffmpeg:</b> {commands["ffmpeg"]}72<b>7z:</b> {commands["7z"]}73"""74await send_message(message, stats)757677async def get_version_async(command, regex):78try:79out, err, code = await cmd_exec(command)80if code != 0:81return f"Error: {err}"82match = research(regex, out)83return match.group(1) if match else "Version not found"84except Exception as e:85return f"Exception: {str(e)}"868788@new_task89async def get_packages_version():90tasks = [get_version_async(command, regex) for command, regex in commands.values()]91versions = await gather(*tasks)92for tool, version in zip(commands.keys(), versions):93commands[tool] = version94if await aiopath.exists(".git"):95last_commit = await cmd_exec(96"git log -1 --date=short --pretty=format:'%cd <b>From</b> %cr'", True97)98last_commit = last_commit[0]99else:100last_commit = "No UPSTREAM_REPO"101commands["commit"] = last_commit102103104