]> jfr.im git - erebus.git/blame - modules/resources.py
update comments
[erebus.git] / modules / resources.py
CommitLineData
72137af7 1# Erebus IRC bot - Author: Erebus Team
4477123d 2# vim: fileencoding=utf-8
bac69af4 3# resource-usage reporting module
72137af7 4# This file is released into the public domain; see http://unlicense.org/
5
6# module info
7modinfo = {
8 'author': 'Erebus Team',
9 'license': 'public domain',
fa93b933 10 'compatible': [0],
a62d0d18 11 'depends': [],
12 'softdeps': ['help'],
72137af7 13}
14
15# preamble
16import modlib
17lib = modlib.modlib(__name__)
18modstart = lib.modstart
19modstop = lib.modstop
20
21# module code
fc16e064 22import resource, time
72137af7 23
f5aec865 24@lib.hook(needchan=False, wantchan=True, glevel=lib.MANAGER)
5f03d045 25@lib.help(None, "show RAM usage")
fb20be7c 26def ram(bot, user, chan, realtarget, *args):
f5aec865 27 if chan is not None: replyto = chan
72137af7 28 else: replyto = user
29
30 try:
31 res = resource.getrusage(resource.RUSAGE_BOTH)
32 except:
33 res = resource.getrusage(resource.RUSAGE_SELF)
34
a8657439 35 bot.fastmsg(replyto, "Memory usage (MiB): %r" % (res.ru_maxrss/1024.0))
72137af7 36
f5aec865 37@lib.hook(needchan=False, wantchan=True, glevel=lib.MANAGER)
5f03d045 38@lib.help(None, "show resource usage")
fb20be7c 39def resources(bot, user, chan, realtarget, *args):
f5aec865 40 if chan is not None: replyto = chan
72137af7 41 else: replyto = user
42
fc16e064 43 uptime = time.time() - bot.parent.starttime
44 m, s = divmod(uptime, 60)
45 h, m = divmod(m, 60)
46 d, h = divmod(h, 24)
72137af7 47 try:
48 res = resource.getrusage(resource.RUSAGE_BOTH)
49 except:
50 res = resource.getrusage(resource.RUSAGE_SELF)
51
a8657439 52 bot.slowmsg(replyto, "Resource usage:")
10e2a6b9 53 for i, v in (
fc16e064 54 ('uptime (s)', "%d (%d days %02d:%02d:%02d)" % (uptime, d, h, m, s)),
72137af7 55 ('utime (s)', res.ru_utime),
56 ('stime (s)', res.ru_stime),
10e2a6b9 57 ('memory (MiB)', (res.ru_maxrss/1024.0)),
72137af7 58 ('I/O (blocks)', res.ru_inblock+res.ru_oublock),
59 ('page faults', res.ru_majflt),
60 ('signals', res.ru_nsignals),
61 ('context switches (voluntary)', res.ru_nvcsw),
62 ('context switches (involuntary)', res.ru_nivcsw),
10e2a6b9 63 ):
fc16e064 64 bot.slowmsg(replyto, "- %s: %s" % (i, v))
a8657439 65 bot.slowmsg(replyto, "EOL.")