2005-01-26 08:33:19 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
2012-09-01 16:16:48 +02:00
|
|
|
# Copyright (c) 2009, James McCoy
|
2021-10-17 09:54:06 +02:00
|
|
|
# Copyright (c) 2010-2021, Valentin Lorentz
|
2005-01-26 08:33:19 +01: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.
|
|
|
|
###
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import threading
|
2010-08-06 20:48:21 +02:00
|
|
|
import multiprocessing
|
2009-11-22 20:31:58 +01:00
|
|
|
import subprocess
|
2005-01-26 08:33:19 +01:00
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.world as world
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.callbacks as callbacks
|
2010-10-20 09:10:03 +02:00
|
|
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
|
|
|
_ = PluginInternationalization('Status')
|
2005-01-26 08:33:19 +01:00
|
|
|
|
2005-02-09 08:04:04 +01:00
|
|
|
class Status(callbacks.Plugin):
|
2014-11-30 21:07:41 +01:00
|
|
|
"""This plugin allows you to view different bot statistics, for example,
|
|
|
|
uptime."""
|
2005-01-29 20:16:29 +01:00
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(Status, self)
|
|
|
|
self.__parent.__init__(irc)
|
2005-01-29 22:24:17 +01:00
|
|
|
# XXX It'd be nice if these could be kept in the registry.
|
2005-01-26 08:33:19 +01:00
|
|
|
self.sentMsgs = 0
|
|
|
|
self.recvdMsgs = 0
|
|
|
|
self.sentBytes = 0
|
|
|
|
self.recvdBytes = 0
|
|
|
|
self.connected = {}
|
|
|
|
|
|
|
|
def __call__(self, irc, msg):
|
|
|
|
self.recvdMsgs += 1
|
|
|
|
self.recvdBytes += len(msg)
|
2005-02-09 08:04:04 +01:00
|
|
|
self.__parent.__call__(irc, msg)
|
2005-01-26 08:33:19 +01:00
|
|
|
|
|
|
|
def outFilter(self, irc, msg):
|
|
|
|
self.sentMsgs += 1
|
|
|
|
self.sentBytes += len(msg)
|
|
|
|
return msg
|
|
|
|
|
|
|
|
def do001(self, irc, msg):
|
|
|
|
self.connected[irc] = time.time()
|
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:33:19 +01:00
|
|
|
def status(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the status of the bot.
|
|
|
|
"""
|
2023-10-05 21:13:55 +02:00
|
|
|
# Initialize dictionaries
|
|
|
|
nicks = {}
|
2005-01-26 08:33:19 +01:00
|
|
|
networks = {}
|
2023-10-05 21:13:55 +02:00
|
|
|
# Iterate through each IRC network
|
2005-01-26 08:33:19 +01:00
|
|
|
for Irc in world.ircs:
|
2023-10-05 21:13:55 +02:00
|
|
|
network_name = Irc.network
|
|
|
|
channels = Irc.state.channels
|
|
|
|
|
|
|
|
# Initialize counts for this network
|
|
|
|
channel_counts = len(channels)
|
|
|
|
op_counts = sum(1 for channel in channels.values() if Irc.nick in channel.ops)
|
|
|
|
halfop_counts = sum(1 for channel in channels.values() if Irc.nick in channel.halfops)
|
|
|
|
voice_counts = sum(1 for channel in channels.values() if Irc.nick in channel.voices)
|
|
|
|
normal_counts = sum(1 for channel in channels.values() if Irc.nick in channel.users)
|
|
|
|
|
|
|
|
# Store the counts in dictionaries
|
|
|
|
nicks[network_name] = Irc.nick
|
|
|
|
networks[network_name] = {
|
|
|
|
'Channels': channel_counts,
|
|
|
|
'Ops': op_counts,
|
|
|
|
'Half-Ops': halfop_counts,
|
|
|
|
'Voiced': voice_counts,
|
|
|
|
'Regular': normal_counts
|
|
|
|
}
|
|
|
|
|
|
|
|
# Prepare the response
|
|
|
|
response_lines = []
|
|
|
|
for network_name, counts in networks.items():
|
|
|
|
response_lines.append(
|
|
|
|
format(
|
|
|
|
_('I am connected to %s as %s: Channels: %s, Ops: %s, Half-Ops: %s, Voiced: %s, Regular: %s'),
|
|
|
|
network_name,
|
|
|
|
nicks[network_name],
|
|
|
|
counts['Channels'],
|
|
|
|
counts['Ops'],
|
|
|
|
counts['Half-Ops'],
|
|
|
|
counts['Voiced'],
|
|
|
|
counts['Regular']
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2005-01-26 08:33:19 +01:00
|
|
|
if world.profiling:
|
2023-10-05 21:13:55 +02:00
|
|
|
response_lines.append(_('I am currently in code profiling mode.'))
|
|
|
|
response = format(_("%L"), response_lines)
|
|
|
|
irc.reply(response)
|
2005-01-26 08:33:19 +01:00
|
|
|
status = wrap(status)
|
2023-10-05 21:13:55 +02:00
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:33:19 +01:00
|
|
|
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()
|
2010-10-20 09:10:03 +02:00
|
|
|
s = format(_('I have spawned %n; %n %b still currently active: %L.'),
|
2005-01-29 22:24:17 +01:00
|
|
|
(world.threadsSpawned, 'thread'),
|
|
|
|
(len(threads), 'thread'), len(threads), threads)
|
2005-01-26 08:33:19 +01:00
|
|
|
irc.reply(s)
|
|
|
|
threads = wrap(threads)
|
|
|
|
|
2010-08-05 19:54:54 +02:00
|
|
|
def processes(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
2010-08-06 20:48:21 +02:00
|
|
|
Returns the number of processes that have been spawned, and list of
|
|
|
|
ones that are still active.
|
2010-08-05 19:54:54 +02:00
|
|
|
"""
|
2010-08-06 20:48:21 +02:00
|
|
|
ps = [multiprocessing.current_process().name]
|
|
|
|
ps = ps + [p.name for p in multiprocessing.active_children()]
|
|
|
|
s = format('I have spawned %n; %n %b still currently active: %L.',
|
|
|
|
(world.processesSpawned, 'process'),
|
|
|
|
(len(ps), 'process'),
|
|
|
|
len(ps), ps)
|
2010-08-05 19:54:54 +02:00
|
|
|
irc.reply(s)
|
|
|
|
processes = wrap(processes)
|
|
|
|
|
2005-01-26 08:33:19 +01:00
|
|
|
def net(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns some interesting network-related statistics.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
elapsed = time.time() - self.connected[irc.getRealIrc()]
|
2005-02-15 14:57:57 +01:00
|
|
|
timeElapsed = utils.timeElapsed(elapsed)
|
2005-01-26 08:33:19 +01:00
|
|
|
except KeyError:
|
2010-10-20 09:10:03 +02:00
|
|
|
timeElapsed = _('an indeterminate amount of time')
|
2010-10-23 10:38:52 +02:00
|
|
|
irc.reply(format(_('I have received %s messages for a total of %S. '
|
2010-10-16 11:37:58 +02:00
|
|
|
'I have sent %s messages for a total of %S. '
|
2010-10-23 10:38:52 +02:00
|
|
|
'I have been connected to %s for %s.'),
|
2010-10-16 11:37:58 +02:00
|
|
|
self.recvdMsgs, self.recvdBytes,
|
|
|
|
self.sentMsgs, self.sentBytes, irc.server, timeElapsed))
|
2005-01-26 08:33:19 +01:00
|
|
|
net = wrap(net)
|
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:33:19 +01:00
|
|
|
def cpu(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns some interesting CPU-related statistics on the bot.
|
|
|
|
"""
|
|
|
|
(user, system, childUser, childSystem, elapsed) = os.times()
|
|
|
|
now = time.time()
|
2019-08-24 17:50:05 +02:00
|
|
|
target = (msg.channel, irc.network)
|
2005-01-26 08:33:19 +01:00
|
|
|
timeRunning = now - world.startedAt
|
2019-08-24 17:50:05 +02:00
|
|
|
if self.registryValue('cpu.children', *target) and \
|
2005-01-26 08:33:19 +01:00
|
|
|
user+system < timeRunning+1: # Fudge for FPU inaccuracies.
|
2010-10-20 09:10:03 +02:00
|
|
|
children = _('My children have taken %.2f seconds of user time '
|
|
|
|
'and %.2f seconds of system time '
|
2011-08-10 11:26:47 +02:00
|
|
|
'for a total of %.2f seconds of CPU time.') % \
|
2005-01-26 08:33:19 +01:00
|
|
|
(childUser, childSystem, childUser+childSystem)
|
|
|
|
else:
|
|
|
|
children = ''
|
|
|
|
activeThreads = threading.activeCount()
|
2010-10-20 09:10:03 +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)
|
2019-08-24 17:50:05 +02:00
|
|
|
if self.registryValue('cpu.threads', *target):
|
2005-01-29 22:24:17 +01:00
|
|
|
response += format('I have spawned %n; I currently have %i still '
|
|
|
|
'running.',
|
|
|
|
(world.threadsSpawned, 'thread'), activeThreads)
|
2019-08-24 17:50:05 +02:00
|
|
|
if self.registryValue('cpu.memory', *target):
|
2015-12-07 16:32:46 +01:00
|
|
|
mem = None
|
2005-01-26 08:33:19 +01:00
|
|
|
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'):
|
2009-11-22 20:31:58 +01:00
|
|
|
cmd = 'ps -o rss -p %s' % pid
|
2005-01-26 08:33:19 +01:00
|
|
|
try:
|
2009-11-22 20:31:58 +01:00
|
|
|
inst = subprocess.Popen(cmd.split(), close_fds=True,
|
2012-08-04 13:13:16 +02:00
|
|
|
stdin=open(os.devnull),
|
2009-11-22 20:31:58 +01:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
except OSError:
|
2010-10-20 09:10:03 +02:00
|
|
|
irc.error(_('Unable to run ps command.'), Raise=True)
|
|
|
|
(out, foo) = inst.communicate()
|
2009-11-22 20:31:58 +01:00
|
|
|
inst.wait()
|
2010-10-16 11:37:58 +02:00
|
|
|
mem = int(out.splitlines()[1])
|
2005-01-26 08:33:19 +01:00
|
|
|
elif sys.platform.startswith('netbsd'):
|
2010-10-16 11:37:58 +02:00
|
|
|
mem = int(os.stat('/proc/%s/mem' % pid)[7])
|
2015-12-07 16:32:46 +01:00
|
|
|
if mem:
|
|
|
|
response += format(_(' I\'m taking up %S of memory.'),
|
|
|
|
mem*1024)
|
|
|
|
else:
|
|
|
|
response += _(' I\'m taking up an unknown amount of memory.')
|
2005-01-26 08:33:19 +01:00
|
|
|
except Exception:
|
|
|
|
self.log.exception('Uncaught exception in cpu.memory:')
|
2005-01-27 07:59:08 +01:00
|
|
|
irc.reply(utils.str.normalizeWhitespace(response))
|
2005-01-26 08:33:19 +01:00
|
|
|
cpu = wrap(cpu)
|
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:33:19 +01:00
|
|
|
def cmd(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns some interesting command-related statistics.
|
|
|
|
"""
|
|
|
|
commands = 0
|
2005-02-09 08:04:04 +01:00
|
|
|
callbacksPlugin = 0
|
2005-01-26 08:33:19 +01:00
|
|
|
for cb in irc.callbacks:
|
2005-02-20 00:44:50 +01:00
|
|
|
if isinstance(cb, callbacks.Plugin):
|
2005-02-09 08:04:04 +01:00
|
|
|
callbacksPlugin += 1
|
2005-02-20 00:44:50 +01:00
|
|
|
commands += len(cb.listCommands())
|
2010-10-20 09:10:03 +02:00
|
|
|
s = format(_('I offer a total of %n in %n. I have processed %n.'),
|
2005-01-29 22:24:17 +01:00
|
|
|
(commands, 'command'),
|
2005-02-09 08:04:04 +01:00
|
|
|
(callbacksPlugin, 'command-based', 'plugin'),
|
2005-01-29 22:24:17 +01:00
|
|
|
(world.commandsProcessed, 'command'))
|
2005-01-26 08:33:19 +01:00
|
|
|
irc.reply(s)
|
|
|
|
cmd = wrap(cmd)
|
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:33:19 +01:00
|
|
|
def commands(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns a list of the commands offered by the bot.
|
|
|
|
"""
|
2005-01-29 22:24:17 +01:00
|
|
|
commands = set()
|
2005-01-26 08:33:19 +01:00
|
|
|
for cb in irc.callbacks:
|
2005-02-20 00:44:50 +01:00
|
|
|
if isinstance(cb, callbacks.Plugin):
|
|
|
|
for command in cb.listCommands():
|
|
|
|
commands.add(command)
|
|
|
|
irc.reply(format('%L', sorted(commands)))
|
2005-01-26 08:33:19 +01:00
|
|
|
commands = wrap(commands)
|
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:33:19 +01:00
|
|
|
def uptime(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the amount of time the bot has been running.
|
|
|
|
"""
|
2010-10-20 09:10:03 +02:00
|
|
|
response = _('I have been running for %s.') % \
|
2005-02-15 14:57:57 +01:00
|
|
|
utils.timeElapsed(time.time() - world.startedAt)
|
2005-01-26 08:33:19 +01:00
|
|
|
irc.reply(response)
|
|
|
|
uptime = wrap(uptime)
|
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-26 08:33:19 +01:00
|
|
|
def server(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the server the bot is on.
|
|
|
|
"""
|
|
|
|
irc.reply(irc.server)
|
|
|
|
server = wrap(server)
|
|
|
|
|
2012-02-18 12:30:13 +01:00
|
|
|
@internationalizeDocstring
|
|
|
|
def network(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the network the bot is on.
|
|
|
|
"""
|
|
|
|
irc.reply(irc.network)
|
|
|
|
network = wrap(network)
|
|
|
|
|
2005-01-26 08:33:19 +01:00
|
|
|
|
|
|
|
Class = Status
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|