From dc13b070e804b9a555202fd53601a142457cb7fb Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sun, 21 Aug 2022 12:30:08 -0300 Subject: [PATCH] ADD machine monitor. --- utils/commands.py | 29 +++++++++++++++++++++++------ utils/machine_monitor.py | 27 +++++++++++++++++++++++++++ utils/tools.py | 9 +++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 utils/machine_monitor.py create mode 100644 utils/tools.py diff --git a/utils/commands.py b/utils/commands.py index 3d2c0fe..d32d9ef 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -4,16 +4,15 @@ Bot commands. import logging from random import choice +from discord import Embed, Intents from discord.ext import commands -from discord import Intents, Embed -from settings.config import IMAGE_TYPES, PERMISSIONS +from settings.config import IMAGE_TYPES, OW_API_CONFIG, 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, 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()) 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```') 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) diff --git a/utils/machine_monitor.py b/utils/machine_monitor.py new file mode 100644 index 0000000..ea71663 --- /dev/null +++ b/utils/machine_monitor.py @@ -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 diff --git a/utils/tools.py b/utils/tools.py new file mode 100644 index 0000000..1400273 --- /dev/null +++ b/utils/tools.py @@ -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)