]> jfr.im git - erebus.git/blobdiff - modules/resources.py
urls - allow port in url
[erebus.git] / modules / resources.py
index 27cc7da8c75f7bcfc067fb824c2b974abfac41dd..5fc4309a9c135b5c90d0b97daa9c00a8129ec390 100644 (file)
@@ -1,13 +1,15 @@
 # Erebus IRC bot - Author: Erebus Team
-# simple module example
+# vim: fileencoding=utf-8
+# resource-usage reporting module
 # This file is released into the public domain; see http://unlicense.org/
 
 # module info
 modinfo = {
        'author': 'Erebus Team',
        'license': 'public domain',
-       'compatible': [1], # compatible module API versions
-       'depends': [], # other modules required to work properly?
+       'compatible': [0],
+       'depends': [],
+       'softdeps': ['help'],
 }
 
 # preamble
@@ -17,11 +19,12 @@ modstart = lib.modstart
 modstop = lib.modstop
 
 # module code
-import resource
+import resource, time
 
-@lib.hook(needchan=False, glevel=lib.MANAGER)
+@lib.hook(needchan=False, wantchan=True, glevel=lib.MANAGER)
+@lib.help(None, "show RAM usage")
 def ram(bot, user, chan, realtarget, *args):
-       if chan is not None and realtarget == chan.name: replyto = chan
+       if chan is not None: replyto = chan
        else: replyto = user
 
        try:
@@ -29,20 +32,26 @@ def ram(bot, user, chan, realtarget, *args):
        except:
                res = resource.getrusage(resource.RUSAGE_SELF)
 
-       bot.msg(replyto, "Memory usage (MiB): %r" % (res.ru_maxrss/1024.0))
+       bot.fastmsg(replyto, "Memory usage (MiB): %r" % (res.ru_maxrss/1024.0))
 
-@lib.hook(needchan=False, glevel=lib.MANAGER)
+@lib.hook(needchan=False, wantchan=True, glevel=lib.MANAGER)
+@lib.help(None, "show resource usage")
 def resources(bot, user, chan, realtarget, *args):
-       if chan is not None and realtarget == chan.name: replyto = chan
+       if chan is not None: replyto = chan
        else: replyto = user
 
+       uptime = time.time() - bot.parent.starttime
+       m, s = divmod(uptime, 60)
+       h, m = divmod(m, 60)
+       d, h = divmod(h, 24)
        try:
                res = resource.getrusage(resource.RUSAGE_BOTH)
        except:
                res = resource.getrusage(resource.RUSAGE_SELF)
 
-       bot.msg(replyto, "Resource usage:")
+       bot.slowmsg(replyto, "Resource usage:")
        for i, v in (
+               ('uptime (s)', "%d (%d days %02d:%02d:%02d)" % (uptime, d, h, m, s)),
                ('utime (s)', res.ru_utime),
                ('stime (s)', res.ru_stime),
                ('memory (MiB)', (res.ru_maxrss/1024.0)),
@@ -52,5 +61,5 @@ def resources(bot, user, chan, realtarget, *args):
                ('context switches (voluntary)', res.ru_nvcsw),
                ('context switches (involuntary)', res.ru_nivcsw),
        ):
-               bot.msg(replyto, "- %s: %r" % (i, v))
-       bot.msg(replyto, "EOL.")
+               bot.slowmsg(replyto, "- %s: %s" % (i, v))
+       bot.slowmsg(replyto, "EOL.")