]> jfr.im git - erebus.git/blob - modules/resources.py
control - be aware of dependents
[erebus.git] / modules / resources.py
1 # Erebus IRC bot - Author: Erebus Team
2 # simple module example
3 # This file is released into the public domain; see http://unlicense.org/
4
5 # module info
6 modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
11 }
12
13 # preamble
14 import modlib
15 lib = modlib.modlib(__name__)
16 modstart = lib.modstart
17 modstop = lib.modstop
18
19 # module code
20 import resource
21
22 @lib.hook(needchan=False, glevel=lib.MANAGER)
23 @lib.help(None, "show RAM usage")
24 def ram(bot, user, chan, realtarget, *args):
25 if chan is not None and realtarget == chan.name: replyto = chan
26 else: replyto = user
27
28 try:
29 res = resource.getrusage(resource.RUSAGE_BOTH)
30 except:
31 res = resource.getrusage(resource.RUSAGE_SELF)
32
33 bot.fastmsg(replyto, "Memory usage (MiB): %r" % (res.ru_maxrss/1024.0))
34
35 @lib.hook(needchan=False, glevel=lib.MANAGER)
36 @lib.help(None, "show resource usage")
37 def resources(bot, user, chan, realtarget, *args):
38 if chan is not None and realtarget == chan.name: replyto = chan
39 else: replyto = user
40
41 try:
42 res = resource.getrusage(resource.RUSAGE_BOTH)
43 except:
44 res = resource.getrusage(resource.RUSAGE_SELF)
45
46 bot.slowmsg(replyto, "Resource usage:")
47 for i, v in (
48 ('utime (s)', res.ru_utime),
49 ('stime (s)', res.ru_stime),
50 ('memory (MiB)', (res.ru_maxrss/1024.0)),
51 ('I/O (blocks)', res.ru_inblock+res.ru_oublock),
52 ('page faults', res.ru_majflt),
53 ('signals', res.ru_nsignals),
54 ('context switches (voluntary)', res.ru_nvcsw),
55 ('context switches (involuntary)', res.ru_nivcsw),
56 ):
57 bot.slowmsg(replyto, "- %s: %r" % (i, v))
58 bot.slowmsg(replyto, "EOL.")