This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/system.py

47 lines
1.6 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
import os
import re
2012-03-30 02:16:31 +02:00
import platform
2011-11-20 10:23:31 +01:00
from util import hook
2012-03-30 16:51:59 +02:00
def replace(text, wordDic):
rc = re.compile('|'.join(map(re.escape, wordDic)))
def translate(match):
return wordDic[match.group(0)]
return rc.sub(translate, text)
2012-03-30 03:22:22 +02:00
@hook.command("memory", autohelp=False)
2011-11-20 10:23:31 +01:00
@hook.command(autohelp=False)
def mem(inp):
2012-02-28 03:03:43 +01:00
".mem -- Display the bot's current memory usage."
2011-11-20 10:23:31 +01:00
if os.name == 'posix':
status_file = open("/proc/%d/status" % os.getpid()).read()
line_pairs = re.findall(r"^(\w+):\s*(.*)\s*$", status_file, re.M)
status = dict(line_pairs)
2012-03-30 16:51:59 +02:00
checked_stats = 'VmRSS VmSize VmPeak VmStk'.split()
stats = '\x02, '.join(key + ': \x02' + status[key] for key in checked_stats)
pretty_names = {'VmRSS': 'Real Memory', 'VmSize': 'Allocated Memory', 'VmPeak': 'Peak Allocated Memory', 'VmStk': 'Stack Size'}
stats = replace(stats, pretty_names)
return stats
2011-11-20 10:23:31 +01:00
elif os.name == 'nt':
cmd = "tasklist /FI \"PID eq %s\" /FO CSV /NH" % os.getpid()
out = os.popen(cmd).read()
total = 0
for amount in re.findall(r'([,0-9]+) K', out):
total += int(amount.replace(',', ''))
2012-03-30 03:22:22 +02:00
return '\x02Memory Usage: %s kB\x02' % total
2011-11-20 10:23:31 +01:00
return mem.__doc__
2012-03-30 02:16:31 +02:00
2012-03-30 03:22:22 +02:00
@hook.command("system", autohelp=False)
2012-03-30 02:16:31 +02:00
@hook.command(autohelp=False)
def sys(inp):
".sys -- Retrieves information about the host system."
python_version = platform.python_version()
os = platform.platform(aliased=True)
cpu = platform.machine()
return "Platform: %s, Python Version: %s, CPU: %s" % (os, python_version, cpu)