Refactor, delete unnecessary functions
This commit is contained in:
parent
53e7c01a3c
commit
c4dec14587
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.idea
|
||||||
158
bot.py
158
bot.py
@ -2,103 +2,89 @@ import irc.bot, subprocess
|
|||||||
import irc.strings
|
import irc.strings
|
||||||
import psutil, platform
|
import psutil, platform
|
||||||
|
|
||||||
|
|
||||||
def get_size(bytes, suffix="B"):
|
def get_size(bytes, suffix="B"):
|
||||||
factor=1024
|
factor = 1024
|
||||||
for unit in ["", "K", "M", "G", "T", "P"]:
|
for unit in ["", "K", "M", "G", "T", "P"]:
|
||||||
if bytes < factor:
|
if bytes < factor:
|
||||||
return f"{bytes:.2f}{unit}{suffix}"
|
return f"{bytes:.2f}{unit}{suffix}"
|
||||||
bytes /= factor
|
bytes /= factor
|
||||||
|
|
||||||
|
|
||||||
class TestBot(irc.bot.SingleServerIRCBot):
|
class TestBot(irc.bot.SingleServerIRCBot):
|
||||||
def __init__(self, channel, nickname, server, port=6667):
|
def __init__(self, channel, nickname, server, port=6667):
|
||||||
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
|
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
|
||||||
self.channel = channel
|
self.channel = channel
|
||||||
|
|
||||||
def on_welcome(self, c, e):
|
|
||||||
c.mode(self._nickname, '+B')
|
|
||||||
c.join(self.channel)
|
|
||||||
|
|
||||||
def on_privmsg(self, c, e):
|
|
||||||
self.do_command(e, e.arguments[0])
|
|
||||||
|
|
||||||
def on_pubmsg(self, c, e):
|
|
||||||
a = e.arguments[0].split(":", 1)
|
|
||||||
if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(
|
|
||||||
self.connection.get_nickname()
|
|
||||||
):
|
|
||||||
self.do_command(e, a[1].strip())
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
|
def on_welcome(self, c, e):
|
||||||
|
c.mode(self._nickname, '+B')
|
||||||
|
c.join(self.channel)
|
||||||
|
|
||||||
def do_command(self, e, cmd):
|
def on_privmsg(self, c, e):
|
||||||
nick = e.source.nick
|
self.do_command(e, e.arguments[0])
|
||||||
c = self.connection
|
|
||||||
|
|
||||||
if cmd == "stats":
|
|
||||||
for chname, chobj in self.channels.items():
|
|
||||||
c.notice(nick, "--- Channel statistics ---")
|
|
||||||
c.notice(nick, "Channel: " + chname)
|
|
||||||
users = sorted(chobj.users())
|
|
||||||
c.notice(nick, "Users: " + ", ".join(users))
|
|
||||||
opers = sorted(chobj.opers())
|
|
||||||
c.notice(nick, "Opers: " + ", ".join(opers))
|
|
||||||
voiced = sorted(chobj.voiced())
|
|
||||||
c.notice(nick, "Voiced: " + ", ".join(voiced))
|
|
||||||
if cmd.lower() == "uptime":
|
|
||||||
result = subprocess.run(["uptime" ], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
||||||
output = result.stdout.decode().strip()
|
|
||||||
#res = output[output.find('load average'):]
|
|
||||||
c.notice(nick, output)
|
|
||||||
|
|
||||||
if cmd.lower() == "uname":
|
|
||||||
#>>> print(f"Version: {uname.version}")
|
|
||||||
#Version: #1 SMP Debian 4.19.208-1 (2021-09-29)
|
|
||||||
uname = platform.uname()
|
|
||||||
end = 'System name:' + uname.version
|
|
||||||
c.notice(nick, end)
|
|
||||||
|
|
||||||
|
|
||||||
if cmd.lower() == "ram":
|
|
||||||
svmem = psutil.virtual_memory()
|
|
||||||
total = 'Total RAM:' + get_size(svmem.total)
|
|
||||||
used = 'Used Ram: '+ get_size(svmem.used)
|
|
||||||
percentage ='Percent:' + str(svmem.percent) + '%'
|
|
||||||
avail = 'Available:' + get_size(svmem.available)
|
|
||||||
c.notice(nick, total)
|
|
||||||
c.notice(nick, avail)
|
|
||||||
c.notice(nick, used)
|
|
||||||
c.notice(nick, percentage)
|
|
||||||
if cmd.lower() == "cpu":
|
|
||||||
percent = psutil.cpu_percent()
|
|
||||||
end = 'CPU load: ' + str(percent) + '%'
|
|
||||||
c.notice(nick, end)
|
|
||||||
|
|
||||||
if cmd.lower() == "network":
|
|
||||||
if_addrs = psutil.net_if_addrs()
|
|
||||||
for interface_name, interface_addresses in if_addrs.items():
|
|
||||||
for address in interface_addresses:
|
|
||||||
#print(f"=== Interface: {interface_name} ===")
|
|
||||||
header = 'Interface:' + interface_name
|
|
||||||
c.notice(nick, header)
|
|
||||||
if str(address.family) == 'AddressFamily.AF_INET':
|
|
||||||
IP = 'IP Address:' + address.address
|
|
||||||
mask = 'Netmask:' + address.netmask
|
|
||||||
c.notice(nick, IP)
|
|
||||||
c.notice(nick, mask)
|
|
||||||
|
|
||||||
if cmd.lower() == "help":
|
def on_pubmsg(self, c, e):
|
||||||
end = 'Available commands are: help, stats, uptime, ram, cpu, uname, network'
|
a = e.arguments[0].split(":", 1)
|
||||||
c.notice(nick, end)
|
if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(
|
||||||
|
self.connection.get_nickname()
|
||||||
|
):
|
||||||
|
self.do_command(e, a[1].strip())
|
||||||
|
return
|
||||||
|
|
||||||
|
def do_command(self, e, cmd):
|
||||||
|
nick = e.source.nick
|
||||||
|
c = self.connection
|
||||||
|
if cmd.lower() == "uptime":
|
||||||
|
result = subprocess.run(["uptime"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
|
output = result.stdout.decode().strip()
|
||||||
|
c.notice(nick, output)
|
||||||
|
|
||||||
|
elif cmd.lower() == "uname":
|
||||||
|
# >>> print(f"Version: {uname.version}")
|
||||||
|
# Version: #1 SMP Debian 4.19.208-1 (2021-09-29)
|
||||||
|
uname = platform.uname()
|
||||||
|
end = 'System name:' + uname.version
|
||||||
|
c.notice(nick, end)
|
||||||
|
|
||||||
|
elif cmd.lower() == "ram":
|
||||||
|
svmem = psutil.virtual_memory()
|
||||||
|
total = 'Total RAM:' + get_size(svmem.total)
|
||||||
|
used = 'Used Ram: ' + get_size(svmem.used)
|
||||||
|
percentage = 'Percent:' + str(svmem.percent) + '%'
|
||||||
|
avail = 'Available:' + get_size(svmem.available)
|
||||||
|
c.notice(nick, total)
|
||||||
|
c.notice(nick, avail)
|
||||||
|
c.notice(nick, used)
|
||||||
|
c.notice(nick, percentage)
|
||||||
|
|
||||||
|
elif cmd.lower() == "cpu":
|
||||||
|
percent = psutil.cpu_percent()
|
||||||
|
end = 'CPU load: {}%'.format(percent)
|
||||||
|
c.notice(nick, end)
|
||||||
|
|
||||||
|
elif cmd.lower() == "network":
|
||||||
|
if_addrs = psutil.net_if_addrs()
|
||||||
|
for interface_name, interface_addresses in if_addrs.items():
|
||||||
|
header = 'Interface: ' + interface_name
|
||||||
|
c.notice(nick, header)
|
||||||
|
for address in interface_addresses:
|
||||||
|
if str(address.family) == 'AddressFamily.AF_INET':
|
||||||
|
ip = 'IP Address: ' + address.address
|
||||||
|
mask = 'Netmask: ' + address.netmask
|
||||||
|
c.notice(nick, ' {} / {}'.format(ip, mask))
|
||||||
|
|
||||||
|
elif cmd.lower() == "help":
|
||||||
|
end = 'Available commands are: help, uptime, ram, cpu, uname, network'
|
||||||
|
c.notice(nick, end)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
channel = '#main'
|
channel = '#main'
|
||||||
nickname = 'GandCStat'
|
nickname = 'TomasGlTestStatBot'
|
||||||
server = '10.200.201.4'
|
server = 's1.tomas.gl'
|
||||||
bot = TestBot(channel, nickname, server)
|
bot = TestBot(channel, nickname, server)
|
||||||
bot.start()
|
bot.start()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user