ADD machine monitor.

pull/3/head
kevincaires 3 years ago
parent a09c2620d1
commit dc13b070e8

@ -4,16 +4,15 @@ Bot commands.
import logging import logging
from random import choice from random import choice
from discord import Embed, Intents
from discord.ext import commands from discord.ext import commands
from discord import Intents, Embed from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS
from settings.config import IMAGE_TYPES, PERMISSIONS
from utils.database import get_by_id, get_quotes, remove_quote, set_quote, count_quotes
from utils.weather import geocode, getweatherdata, displayweather
from settings.config import PERMISSIONS, OW_API_CONFIG
from utils.database import (count_quotes, get_by_id, get_quote_contains, from utils.database import (count_quotes, get_by_id, get_quote_contains,
get_quotes, remove_quote, set_quote) get_quotes, remove_quote, set_quote)
from utils.machine_monitor import Monitor
from utils.tools import kbytes_to_gbytes
from utils.weather import displayweather, geocode, getweatherdata
client = commands.Bot(command_prefix='--', intents=Intents.all()) client = commands.Bot(command_prefix='--', intents=Intents.all())
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -284,3 +283,21 @@ async def quote_contains(bot: object, part: str) -> str:
f'{quote.quote[-10:]}\nUser: {quote.user}\n```') f'{quote.quote[-10:]}\nUser: {quote.user}\n```')
return return
@client.command(aliases=['macinfo', 'minfo'])
async def machine_info(bot: object, *args: str) -> str:
"""
Return machine information.
"""
embed = Embed(type='rich')
if not args:
embed.add_field(name='CPU', value=f'{Monitor.cpu_percent} %')
embed.add_field(name='RAM', value=f'{Monitor.memory.percent} %')
embed.add_field(name='Swap', value=f'{Monitor.swap.percent} %')
embed.add_field(name='Disk total', value=f'{kbytes_to_gbytes(Monitor.disk_usage.total)} Gb')
embed.add_field(name='Disk used', value=f'{kbytes_to_gbytes(Monitor.disk_usage.used)} Gb')
embed.add_field(name='Disk free', value=f'{kbytes_to_gbytes(Monitor.disk_usage.free)} Gb')
return await bot.send('**`Monitor`**', embed=embed)

@ -0,0 +1,27 @@
import psutil
class Monitor:
"""
Show utils information about run machine.
"""
cpu_percent = psutil.cpu_percent()
memory = psutil.virtual_memory()
swap = psutil.swap_memory()
disk_usage = psutil.disk_usage('/')
net_tcp_connections = psutil.net_connections('tcp')
net_io_counters = psutil.net_io_counters(pernic=True)
net_if_addrs = psutil.net_if_addrs()
net_if_stats = psutil.net_if_stats()
tempetatures = psutil.sensors_temperatures()
@staticmethod
def show_five_top_process() -> list:
"""
Return ten top process.
"""
_pids = psutil.pids()
_processes = [psutil.Process(_pid) for _pid in _pids]
_processes = sorted(_processes, key=lambda k: k._create_time, reverse=True)
return _processes

@ -0,0 +1,9 @@
"""
Tools module.
"""
def kbytes_to_gbytes(value: float) -> str:
"""
Transform Kb into Gb.
"""
_value = value / (1024 ** 3)
return "{:.2f}".format(_value)
Loading…
Cancel
Save