2011-11-20 10:23:31 +01:00
|
|
|
import os
|
|
|
|
import re
|
2012-03-30 18:03:34 +02:00
|
|
|
import time
|
2012-03-30 20:48:24 +02:00
|
|
|
import string
|
2012-03-30 02:16:31 +02:00
|
|
|
import platform
|
2012-03-31 00:04:11 +02:00
|
|
|
import subprocess
|
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 16:58:39 +02:00
|
|
|
def checkProc(checked_stats):
|
2012-03-31 01:38:31 +02:00
|
|
|
status_file = open('/proc/self/status').read()
|
|
|
|
line_pairs = re.findall(r'^(\w+):\s*(.*)\s*$', status_file, re.M)
|
2011-11-20 10:23:31 +01:00
|
|
|
status = dict(line_pairs)
|
2012-03-30 16:58:39 +02:00
|
|
|
checked_stats = checked_stats.split()
|
2012-03-30 16:51:59 +02:00
|
|
|
stats = '\x02, '.join(key + ': \x02' + status[key] for key in checked_stats)
|
2012-03-30 16:58:39 +02:00
|
|
|
return stats
|
|
|
|
|
2012-03-30 18:17:31 +02:00
|
|
|
|
|
|
|
@hook.command("system", autohelp=False)
|
|
|
|
@hook.command(autohelp=False)
|
|
|
|
def sys(inp):
|
|
|
|
".sys -- Retrieves information about the host system."
|
2012-03-31 01:38:31 +02:00
|
|
|
name = platform.node()
|
|
|
|
os = platform.platform()
|
|
|
|
python_version = platform.python_implementation() + ' ' + platform.python_version()
|
|
|
|
arch = '-'.join(platform.architecture())
|
2012-03-30 18:17:31 +02:00
|
|
|
cpu = platform.machine()
|
2012-03-31 01:38:31 +02:00
|
|
|
return 'Name: \x02%s\x02, Operating System: \x02%s\x02, Python Version: \x02%s\x02, Architecture: \x02%s\x02, CPU: \x02%s\x02' % (name, os, python_version, arch, cpu)
|
2012-03-30 18:17:31 +02:00
|
|
|
|
|
|
|
|
2012-03-30 16:58:39 +02:00
|
|
|
@hook.command("memory", autohelp=False)
|
|
|
|
@hook.command(autohelp=False)
|
|
|
|
def mem(inp):
|
|
|
|
".mem -- Displays the bot's current memory usage."
|
|
|
|
if os.name == 'posix':
|
2012-03-30 20:48:24 +02:00
|
|
|
checked_stats = 'VmRSS VmSize VmPeak VmStk VmData'
|
|
|
|
memory = checkProc(checked_stats)
|
|
|
|
pretty_names = {'VmRSS': 'Real Memory', 'VmSize': 'Allocated Memory', 'VmPeak': 'Peak Allocated Memory', 'VmStk': 'Stack Size', 'VmData': 'Heap Size'}
|
|
|
|
memory = replace(memory, pretty_names)
|
|
|
|
memory = string.replace(memory, ' kB', '')
|
|
|
|
memory = memory.split('\x02')
|
|
|
|
numbers = [memory[i] for i in range(len(memory)) if i % 2 == 1]
|
|
|
|
memory = [i for i in memory if i not in numbers]
|
2012-03-30 23:52:00 +02:00
|
|
|
numbers = [str(round(float(i) / 1024, 2)) + ' MB' for i in numbers]
|
2012-03-30 21:06:36 +02:00
|
|
|
memory = [list(i) for i in zip(memory, numbers)]
|
2012-03-30 20:48:24 +02:00
|
|
|
memory = sum(memory, [])
|
|
|
|
memory = '\x02'.join(memory)
|
2012-03-30 16:58:39 +02:00
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
elif os.name == 'nt':
|
2012-03-31 01:38:31 +02:00
|
|
|
cmd = 'tasklist /FI \"PID eq %s\" /FO CSV /NH' % os.getpid()
|
2011-11-20 10:23:31 +01:00
|
|
|
out = os.popen(cmd).read()
|
2012-03-30 21:06:36 +02:00
|
|
|
memory = 0
|
2011-11-20 10:23:31 +01:00
|
|
|
for amount in re.findall(r'([,0-9]+) K', out):
|
2012-03-30 21:06:36 +02:00
|
|
|
memory += int(amount.replace(',', ''))
|
2012-03-31 01:38:31 +02:00
|
|
|
memory = str(round(float(memory) / 1024, 2))
|
2012-03-30 21:06:36 +02:00
|
|
|
memory = 'Memory Usage: \x02%s MB\x02' % memory
|
|
|
|
else:
|
|
|
|
memory = 'error: operating system not currently supported'
|
|
|
|
return memory
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2012-03-30 02:16:31 +02:00
|
|
|
|
2012-03-30 18:31:36 +02:00
|
|
|
@hook.command("uptime", autohelp=False)
|
2012-03-30 18:03:34 +02:00
|
|
|
@hook.command(autohelp=False)
|
2012-03-30 18:31:36 +02:00
|
|
|
def up(inp):
|
|
|
|
".up -- Shows the bot's uptime."
|
2012-03-31 01:38:31 +02:00
|
|
|
up_time = subprocess.check_output('ps -eo pid,etime | grep %s | awk \'{print $2}\'' % os.getpid(), shell=True)
|
|
|
|
up_time = 'Uptime: \x02' + up_time + '\x02'
|
2012-03-30 18:03:34 +02:00
|
|
|
return up_time
|
|
|
|
|
2012-03-30 18:31:36 +02:00
|
|
|
@hook.command("proc", autohelp=False)
|
2012-03-30 02:16:31 +02:00
|
|
|
@hook.command(autohelp=False)
|
2012-03-30 18:17:31 +02:00
|
|
|
def pid(inp):
|
2012-03-30 18:31:36 +02:00
|
|
|
".pid -- Prints the bot's PID."
|
2012-03-30 18:17:31 +02:00
|
|
|
return 'PID: \x02%s\x02' % os.getpid()
|