2003-10-16 16:53:42 +02:00
|
|
|
#!/usr/bin/env python
|
2003-09-30 12:47:05 +02:00
|
|
|
|
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-09-30 12:47:05 +02:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
A simple module to handle various informational commands querying the bot's
|
|
|
|
current status and statistics.
|
|
|
|
"""
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-09-30 12:47:05 +02:00
|
|
|
|
|
|
|
import os
|
2003-10-14 08:39:31 +02:00
|
|
|
import sys
|
2003-10-04 15:53:13 +02:00
|
|
|
import sets
|
|
|
|
import time
|
2004-08-11 19:04:57 +02:00
|
|
|
import os.path
|
2003-10-04 15:53:13 +02:00
|
|
|
import threading
|
2003-11-15 05:37:04 +01:00
|
|
|
from itertools import islice, ifilter, imap
|
2003-09-30 12:47:05 +02:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.world as world
|
|
|
|
import supybot.privmsgs as privmsgs
|
2004-08-03 09:03:56 +02:00
|
|
|
import supybot.registry as registry
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.callbacks as callbacks
|
2003-09-30 12:47:05 +02:00
|
|
|
|
2004-08-03 09:03:56 +02:00
|
|
|
conf.registerPlugin('Status')
|
|
|
|
conf.registerGroup(conf.supybot.plugins.Status, 'cpu')
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Status.cpu, 'children',
|
|
|
|
registry.Boolean(True, """Determines whether the cpu command will list the
|
|
|
|
time taken by children as well as the bot's process."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Status.cpu, 'threads',
|
2004-08-17 22:28:00 +02:00
|
|
|
registry.Boolean(False, """Determines whether the cpu command will provide
|
2004-08-03 09:03:56 +02:00
|
|
|
the number of threads spawned and active."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Status.cpu, 'memory',
|
|
|
|
registry.Boolean(True, """Determines whether the cpu command will report
|
|
|
|
the amount of memory being used by the bot."""))
|
2003-09-30 12:47:05 +02:00
|
|
|
|
|
|
|
class Status(callbacks.Privmsg):
|
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
|
|
|
self.sentMsgs = 0
|
|
|
|
self.recvdMsgs = 0
|
|
|
|
self.sentBytes = 0
|
|
|
|
self.recvdBytes = 0
|
2004-07-15 08:56:54 +02:00
|
|
|
self.connected = {}
|
2003-09-30 12:47:05 +02:00
|
|
|
|
2004-01-30 06:01:46 +01:00
|
|
|
def __call__(self, irc, msg):
|
2003-09-30 12:47:05 +02:00
|
|
|
self.recvdMsgs += 1
|
2003-10-14 08:41:52 +02:00
|
|
|
self.recvdBytes += len(msg)
|
2004-01-30 06:01:46 +01:00
|
|
|
callbacks.Privmsg.__call__(self, irc, msg)
|
2003-09-30 12:47:05 +02:00
|
|
|
|
|
|
|
def outFilter(self, irc, msg):
|
|
|
|
self.sentMsgs += 1
|
2003-10-14 08:41:52 +02:00
|
|
|
self.sentBytes += len(msg)
|
2003-09-30 12:47:05 +02:00
|
|
|
return msg
|
|
|
|
|
2004-07-15 08:56:54 +02:00
|
|
|
def do001(self, irc, msg):
|
|
|
|
self.connected[irc] = time.time()
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-07-31 05:45:46 +02:00
|
|
|
def status(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the status of the bot.
|
|
|
|
"""
|
|
|
|
networks = {}
|
|
|
|
for Irc in world.ircs:
|
|
|
|
networks.setdefault(Irc.network, []).append(Irc.nick)
|
|
|
|
networks = networks.items()
|
|
|
|
networks.sort()
|
|
|
|
networks = ['%s as %s' % (net, utils.commaAndify(nicks))
|
|
|
|
for (net, nicks) in networks]
|
2004-08-17 22:28:00 +02:00
|
|
|
L = ['I am connected to %s.' % utils.commaAndify(networks)]
|
|
|
|
if world.profiling:
|
2004-08-17 23:00:55 +02:00
|
|
|
L.append('I am currently in code profiling mode.')
|
2004-08-17 22:28:00 +02:00
|
|
|
irc.reply(' '.join(L))
|
|
|
|
|
|
|
|
def threads(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the current threads that are active.
|
|
|
|
"""
|
|
|
|
threads = [t.getName() for t in threading.enumerate()]
|
|
|
|
threads.sort()
|
|
|
|
s = 'I have spawned %s; %s %s still currently active: %s.' % \
|
|
|
|
(utils.nItems('thread', world.threadsSpawned),
|
|
|
|
utils.nItems('thread', len(threads)), utils.be(len(threads)),
|
|
|
|
utils.commaAndify(threads))
|
|
|
|
irc.reply(s)
|
2004-07-31 05:45:46 +02:00
|
|
|
|
2003-12-01 13:39:38 +01:00
|
|
|
def net(self, irc, msg, args):
|
2003-09-30 12:47:05 +02:00
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns some interesting network-related statistics.
|
|
|
|
"""
|
2004-07-15 17:05:41 +02:00
|
|
|
try:
|
|
|
|
elapsed = time.time() - self.connected[irc.getRealIrc()]
|
|
|
|
timeElapsed = utils.timeElapsed(elapsed)
|
|
|
|
except KeyError:
|
|
|
|
timeElapsed = 'an indeterminate amount of time'
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('I have received %s messages for a total of %s bytes. '
|
2004-07-15 08:56:54 +02:00
|
|
|
'I have sent %s messages for a total of %s bytes. '
|
2004-07-31 05:45:46 +02:00
|
|
|
'I have been connected to %s for %s.' %
|
2004-01-08 04:12:14 +01:00
|
|
|
(self.recvdMsgs, self.recvdBytes,
|
2004-07-31 05:45:46 +02:00
|
|
|
self.sentMsgs, self.sentBytes, irc.server, timeElapsed))
|
2003-09-30 12:47:05 +02:00
|
|
|
|
2003-12-01 13:39:38 +01:00
|
|
|
def cpu(self, irc, msg, args):
|
2003-09-30 12:47:05 +02:00
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns some interesting CPU-related statistics on the bot.
|
|
|
|
"""
|
|
|
|
(user, system, childUser, childSystem, elapsed) = os.times()
|
2003-10-16 13:27:45 +02:00
|
|
|
now = time.time()
|
2004-08-03 09:03:56 +02:00
|
|
|
target = msg.args[0]
|
2003-10-16 13:27:45 +02:00
|
|
|
timeRunning = now - world.startedAt
|
2004-08-03 09:03:56 +02:00
|
|
|
if self.registryValue('cpu.children', target) and \
|
|
|
|
user+system < timeRunning+1: # Fudge for FPU inaccuracies.
|
2004-01-01 20:12:14 +01:00
|
|
|
children = 'My children have taken %.2f seconds of user time ' \
|
|
|
|
'and %.2f seconds of system time ' \
|
|
|
|
'for a total of %.2f seconds of CPU time. ' % \
|
|
|
|
(childUser, childSystem, childUser+childSystem)
|
|
|
|
else:
|
|
|
|
children = ''
|
2003-09-30 12:47:05 +02:00
|
|
|
activeThreads = threading.activeCount()
|
2004-08-03 09:03:56 +02:00
|
|
|
response = 'I have taken %.2f seconds of user time and %.2f seconds ' \
|
|
|
|
'of system time, for a total of %.2f seconds of CPU ' \
|
|
|
|
'time. %s' % (user, system, user + system, children)
|
|
|
|
if self.registryValue('cpu.threads', target):
|
|
|
|
spawned = utils.nItems('thread', world.threadsSpawned)
|
|
|
|
response += 'I have spawned %s; I currently have %s still ' \
|
|
|
|
'running.' % (spawned, activeThreads)
|
|
|
|
if self.registryValue('cpu.memory', target):
|
|
|
|
mem = 'an unknown amount'
|
|
|
|
pid = os.getpid()
|
|
|
|
plat = sys.platform
|
|
|
|
try:
|
|
|
|
if plat.startswith('linux') or plat.startswith('sunos') or \
|
|
|
|
plat.startswith('freebsd') or plat.startswith('openbsd') or \
|
|
|
|
plat.startswith('darwin'):
|
|
|
|
try:
|
|
|
|
r = os.popen('ps -o rss -p %s' % pid)
|
|
|
|
r.readline() # VSZ Header.
|
|
|
|
mem = r.readline().strip()
|
|
|
|
finally:
|
|
|
|
r.close()
|
|
|
|
elif sys.platform.startswith('netbsd'):
|
2004-08-17 22:28:00 +02:00
|
|
|
mem = '%s kB' % os.stat('/proc/%s/mem' % pid)[7]
|
2004-08-03 09:03:56 +02:00
|
|
|
response += ' I\'m taking up %s kB of memory.' % mem
|
|
|
|
except Exception:
|
|
|
|
self.log.exception('Uncaught exception in cpu.memory:')
|
|
|
|
irc.reply(utils.normalizeWhitespace(response))
|
2003-09-30 12:47:05 +02:00
|
|
|
|
2003-12-01 13:39:38 +01:00
|
|
|
def cmd(self, irc, msg, args):
|
2003-09-30 13:04:44 +02:00
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns some interesting command-related statistics.
|
|
|
|
"""
|
2003-10-04 00:55:34 +02:00
|
|
|
commands = 0
|
|
|
|
callbacksPrivmsg = 0
|
2003-09-30 13:04:44 +02:00
|
|
|
for cb in irc.callbacks:
|
|
|
|
if isinstance(cb, callbacks.Privmsg) and cb.public:
|
2003-10-04 00:55:34 +02:00
|
|
|
if not isinstance(cb, callbacks.PrivmsgRegexp):
|
|
|
|
callbacksPrivmsg += 1
|
|
|
|
for attr in dir(cb):
|
|
|
|
if cb.isCommand(attr) and \
|
|
|
|
attr == callbacks.canonicalName(attr):
|
|
|
|
commands += 1
|
|
|
|
s = 'I offer a total of %s in %s. I have processed %s.' % \
|
2003-12-12 16:41:33 +01:00
|
|
|
(utils.nItems('command', commands),
|
|
|
|
utils.nItems('plugin', callbacksPrivmsg, 'command-based'),
|
|
|
|
utils.nItems('command', world.commandsProcessed))
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-10-04 00:55:34 +02:00
|
|
|
|
|
|
|
def commands(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns a list of the commands offered by the bot.
|
|
|
|
"""
|
|
|
|
commands = sets.Set()
|
|
|
|
for cb in irc.callbacks:
|
|
|
|
if isinstance(cb, callbacks.Privmsg) and \
|
|
|
|
not isinstance(cb, callbacks.PrivmsgRegexp) and cb.public:
|
2003-09-30 13:04:44 +02:00
|
|
|
for attr in dir(cb):
|
2003-09-30 15:51:32 +02:00
|
|
|
if cb.isCommand(attr) and \
|
|
|
|
attr == callbacks.canonicalName(attr):
|
2003-09-30 13:04:44 +02:00
|
|
|
commands.add(attr)
|
|
|
|
commands = list(commands)
|
|
|
|
commands.sort()
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.commaAndify(commands))
|
2003-09-30 13:04:44 +02:00
|
|
|
|
2003-09-30 12:47:05 +02:00
|
|
|
def uptime(self, irc, msg, args):
|
2004-01-02 22:36:45 +01:00
|
|
|
"""takes no arguments
|
2003-09-30 12:47:05 +02:00
|
|
|
|
|
|
|
Returns the amount of time the bot has been running.
|
|
|
|
"""
|
|
|
|
response = 'I have been running for %s.' % \
|
|
|
|
utils.timeElapsed(time.time() - world.startedAt)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(response)
|
2003-09-30 12:47:05 +02:00
|
|
|
|
2004-01-02 22:36:45 +01:00
|
|
|
def server(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the server the bot is on.
|
|
|
|
"""
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(irc.server)
|
2004-01-02 22:36:45 +01:00
|
|
|
|
2003-09-30 12:47:05 +02:00
|
|
|
|
|
|
|
Class = Status
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|