Add memory and disk space.

This commit is contained in:
Alex Manning 2022-02-12 10:41:07 +00:00
parent eaf5ccb0d3
commit 781b36cc2e
2 changed files with 55 additions and 2 deletions

51
load.py
View file

@ -1,10 +1,23 @@
import json
import os
import psutil
import psutil._common as psc
from maubot import MessageEvent, Plugin
from maubot.handlers import command
from mautrix.types import Format, MessageType, TextMessageEventContent
def to_human(data):
output = {}
for name in data._fields:
value = getattr(data, name)
if name != "percent":
value = psc.bytes2human(value)
output[name] = value
return output
class LoadBot(Plugin):
@command.new("load", help="Get load average.")
@command.argument("message", pass_raw=True)
@ -17,3 +30,41 @@ class LoadBot(Plugin):
formatted_body=f"{load}",
)
await evt.respond(content)
@command.new("mem", help="Get system memory usage.")
@command.argument("message", pass_raw=True)
async def memory_handler(self, evt: MessageEvent, message: str) -> None:
rawmem = psutil.virtual_memory()
rawswap = psutil.swap_memory()
mem = {}
mem["virt"] = to_human(rawmem)
mem["swap"] = to_human(rawswap)
mem = json.dumps(mem, indent=2)
content = TextMessageEventContent(
msgtype=MessageType.NOTICE,
format=Format.HTML,
body=f"{mem}",
formatted_body=f"{mem}",
)
await evt.respond(content)
@command.new("disk", help="Get system disk usage.")
@command.argument("message", pass_raw=True)
async def disk_handler(self, evt: MessageEvent, message: str) -> None:
result = {}
parts = psutil.disk_partitions()
for part in parts:
usage = psutil.disk_usage(part.mountpoint)
result[str(part.mountpoint)] = to_human(usage)
result = json.dumps(result, indent=2)
content = TextMessageEventContent(
msgtype=MessageType.NOTICE,
format=Format.HTML,
body=f"{result}",
formatted_body=f"{result}",
)
await evt.respond(content)

View file

@ -1,7 +1,9 @@
maubot: 0.1.0
id: uk.a09.maubot.load
version: 0.0.1
license: MIT
version: 0.0.2
license: AGPL-3.0-or-later
modules:
- load
dependencies:
- psutil
main_class: LoadBot