diff --git a/utils/commands.py b/utils/commands.py index 3d2c0fe..375f325 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,44 @@ 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') + supported_args = [ + 'network' + ] + + 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') + + if args[0] not in supported_args: + return await bot.send('The argument is not supported!') + + if args[0] == 'network': + ios = Monitor.net_io_counters + + for io in ios: + embed.clear_fields() + embed.add_field(name='Bytes received', value=ios[io].bytes_recv, inline=True) + embed.add_field(name='Bytes sent', value=ios[io].bytes_sent, inline=True) + embed.add_field(name='Packets received', value=ios[io].packets_recv, inline=True) + embed.add_field(name='Packets sent', value=ios[io].packets_sent, inline=True) + embed.add_field(name='Drop in', value=ios[io].dropin, inline=True) + embed.add_field(name='Drop out', value=ios[io].dropout, inline=True) + embed.add_field(name='Error in', value=ios[io].errin, inline=True) + embed.add_field(name='Error out', value=ios[io].errout, inline=True) + await bot.send(f'**`{io}`**', embed=embed) + + return + + 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)