2003-10-16 16:53:42 +02:00
|
|
|
#!/usr/bin/env python
|
2003-09-30 12:47:05 +02:00
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# 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$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import 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
|
|
|
|
import threading
|
2003-11-15 05:37:04 +01:00
|
|
|
from itertools import islice, ifilter, imap
|
2003-09-30 12:47:05 +02:00
|
|
|
|
2003-10-14 08:39:31 +02:00
|
|
|
import conf
|
2003-09-30 12:47:05 +02:00
|
|
|
import utils
|
|
|
|
import world
|
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
|
|
|
|
|
|
|
def configure(onStart, afterConnect, advanced):
|
|
|
|
# This will be called by setup.py to configure this module. onStart and
|
|
|
|
# afterConnect are both lists. Append to onStart the commands you would
|
|
|
|
# like to be run when the bot is started; append to afterConnect the
|
|
|
|
# commands you would like to be run when the bot has finished connecting.
|
|
|
|
from questions import expect, anything, something, yn
|
|
|
|
onStart.append('load Status')
|
|
|
|
|
2003-10-14 08:39:31 +02:00
|
|
|
class UptimeDB(object):
|
|
|
|
def __init__(self, filename='uptimes'):
|
2004-01-18 08:58:26 +01:00
|
|
|
dataDir = conf.supybot.directories.data()
|
|
|
|
self.filename = os.path.join(dataDir, filename)
|
2003-10-14 08:39:31 +02:00
|
|
|
if os.path.exists(self.filename):
|
|
|
|
fd = file(self.filename)
|
|
|
|
s = fd.read()
|
|
|
|
fd.close()
|
|
|
|
s = s.replace('\n', ' ')
|
|
|
|
self.uptimes = eval(s)
|
|
|
|
else:
|
|
|
|
self.uptimes = []
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
fd = file(self.filename, 'w')
|
2003-10-15 08:25:32 +02:00
|
|
|
fd.write(repr(self.top(50)))
|
2003-10-14 08:39:31 +02:00
|
|
|
fd.write('\n')
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def add(self):
|
2003-11-23 14:01:19 +01:00
|
|
|
if world.startedAt != 0 and \
|
|
|
|
not any(lambda t: t[0] == world.startedAt, self.uptimes):
|
2003-10-14 08:39:31 +02:00
|
|
|
self.uptimes.append((world.startedAt, None))
|
|
|
|
|
|
|
|
def top(self, n=3):
|
|
|
|
def decorator(t):
|
2003-11-25 10:45:01 +01:00
|
|
|
return t[1] - t[0]
|
2003-10-14 08:39:31 +02:00
|
|
|
def invertCmp(cmp):
|
|
|
|
def f(x, y):
|
|
|
|
return -cmp(x, y)
|
|
|
|
return f
|
|
|
|
def notNone(t):
|
2003-11-25 10:45:01 +01:00
|
|
|
return t[1] is not None and t[0] != 0
|
2003-10-14 08:39:31 +02:00
|
|
|
utils.sortBy(decorator, self.uptimes, cmp=invertCmp(cmp))
|
2003-11-23 14:01:19 +01:00
|
|
|
return list(islice(ifilter(notNone, self.uptimes), n))
|
2003-10-14 08:39:31 +02:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
for (i, t) in enumerate(self.uptimes):
|
|
|
|
if t[0] == world.startedAt:
|
|
|
|
self.uptimes[i] = (t[0], time.time())
|
|
|
|
|
|
|
|
|
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
|
2003-10-14 08:39:31 +02:00
|
|
|
self.uptimes = UptimeDB()
|
|
|
|
self.uptimes.add()
|
2003-10-20 05:44:10 +02:00
|
|
|
self.uptimes.update()
|
2003-09-30 12:47:05 +02:00
|
|
|
|
|
|
|
def inFilter(self, irc, msg):
|
2003-10-17 23:51:31 +02:00
|
|
|
self.uptimes.update()
|
2003-09-30 12:47:05 +02:00
|
|
|
self.recvdMsgs += 1
|
2003-10-14 08:41:52 +02:00
|
|
|
self.recvdBytes += len(msg)
|
2003-09-30 12:47:05 +02:00
|
|
|
return msg
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2003-10-14 08:39:31 +02:00
|
|
|
def die(self):
|
|
|
|
self.uptimes.update()
|
|
|
|
self.uptimes.die()
|
|
|
|
|
|
|
|
def bestuptime(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the highest uptimes attained by the bot.
|
|
|
|
"""
|
|
|
|
L = self.uptimes.top()
|
|
|
|
if not L:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('I don\'t have enough data to answer that.')
|
2003-10-14 08:39:31 +02:00
|
|
|
return
|
|
|
|
def format((started, ended)):
|
|
|
|
return '%s until %s; up for %s' % \
|
2004-01-18 08:58:26 +01:00
|
|
|
(time.strftime(conf.supybot.humanTimestampFormat(),
|
2003-10-14 08:39:31 +02:00
|
|
|
time.localtime(started)),
|
2004-01-18 08:58:26 +01:00
|
|
|
time.strftime(conf.supybot.humanTimestampFormat(),
|
2003-10-14 08:39:31 +02:00
|
|
|
time.localtime(ended)),
|
|
|
|
utils.timeElapsed(ended-started))
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.commaAndify(imap(format, L)))
|
2003-10-14 08:39:31 +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-01-08 04:12:14 +01:00
|
|
|
irc.reply('I have received %s messages for a total of %s bytes. '
|
|
|
|
'I have sent %s messages for a total of %s bytes.' %
|
|
|
|
(self.recvdMsgs, self.recvdBytes,
|
|
|
|
self.sentMsgs, self.sentBytes))
|
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()
|
|
|
|
timeRunning = now - world.startedAt
|
2004-01-01 20:12:14 +01:00
|
|
|
if user+system < timeRunning+1: # Fudge for FPU inaccuracies.
|
|
|
|
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()
|
2003-10-24 15:38:55 +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 '
|
2004-01-01 20:12:14 +01:00
|
|
|
'time. %sOut of %s I have %s active.' %
|
|
|
|
(user, system, user + system, children,
|
2003-12-12 16:41:33 +01:00
|
|
|
utils.nItems('thread', world.threadsSpawned, 'spawned'),
|
2003-10-24 15:38:55 +02:00
|
|
|
activeThreads))
|
2004-01-01 20:12:14 +01:00
|
|
|
mem = 'an unknown amount'
|
2003-10-24 15:38:55 +02:00
|
|
|
pid = os.getpid()
|
2003-10-28 21:18:33 +01:00
|
|
|
plat = sys.platform
|
2003-10-24 15:38:55 +02:00
|
|
|
try:
|
2003-10-28 21:18:33 +01:00
|
|
|
if plat.startswith('linux') or plat.startswith('sunos') or \
|
2004-01-01 20:12:14 +01:00
|
|
|
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.
|
2004-01-18 08:58:26 +01:00
|
|
|
mem = r.readline().strip()
|
2004-01-01 20:12:14 +01:00
|
|
|
finally:
|
|
|
|
r.close()
|
2003-10-24 15:38:55 +02:00
|
|
|
elif sys.platform.startswith('netbsd'):
|
2004-01-01 20:12:14 +01:00
|
|
|
mem = '%s kB' % os.stat('/proc/%s/mem')[7]
|
2003-10-24 15:38:55 +02:00
|
|
|
response += ' I\'m taking up %s kB of memory.' % mem
|
2003-11-26 19:21:12 +01:00
|
|
|
except Exception:
|
2003-12-01 13:39:38 +01:00
|
|
|
self.log.exception('Uncaught exception in cpu:')
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(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:
|