From 00781aaa517f36720d4396d62a89a5179c21ea4c Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 9 May 2019 10:25:28 -0400 Subject: [PATCH] Migrate Erordisplay to Logs --- .../SpiceBot_ErrorDisplay/ErrorDisplay.py | 84 ------------------- .../SpiceBot_ErrorDisplay/__init__.py | 12 --- sopel_modules/SpiceBot_Logs/ErrorDisplay.py | 30 +++++++ sopel_modules/SpiceBot_Logs/Logs.py | 33 +++++++- sopel_modules/SpiceBot_Logs/Logs_Command.py | 14 +++- sopel_modules/SpiceBot_Logs/__init__.py | 1 + 6 files changed, 73 insertions(+), 101 deletions(-) delete mode 100644 sopel_modules/SpiceBot_ErrorDisplay/ErrorDisplay.py delete mode 100644 sopel_modules/SpiceBot_ErrorDisplay/__init__.py create mode 100644 sopel_modules/SpiceBot_Logs/ErrorDisplay.py diff --git a/sopel_modules/SpiceBot_ErrorDisplay/ErrorDisplay.py b/sopel_modules/SpiceBot_ErrorDisplay/ErrorDisplay.py deleted file mode 100644 index 4cb4c97..0000000 --- a/sopel_modules/SpiceBot_ErrorDisplay/ErrorDisplay.py +++ /dev/null @@ -1,84 +0,0 @@ -# coding=utf-8 - -from __future__ import unicode_literals, absolute_import, division, print_function - -import sopel.module -from sopel.cli import run - -from sopel_modules.SpiceBot_Events.System import bot_events_recieved -from sopel_modules.SpiceBot_SBTools import command_permissions_check - -import spicemanip - -import os - - -@sopel.module.event('2005') -@sopel.module.rule('.*') -def bot_startup_monologue_start(bot, trigger): - bot_events_recieved(bot, trigger.event) - - debuglines = errordisplay_fetch(bot) - - # Check for python module errors during this startup - searchphrasefound = [] - for line in debuglines: - if "modules failed to load" in str(line) and "0 modules failed to load" not in str(line): - searchphrase = str(line).replace(" modules failed to load", "") - searchphrasefound.append(str(searchphrase) + " module(s) failed") - elif "dict files failed to load" in str(line) and "0 dict files failed to load" not in str(line): - searchphrase = str(line).replace(" dict files failed to load", "") - searchphrasefound.append(str(searchphrase) + " dict file(s) failed") - - if len(searchphrasefound): - searchphrasefound.insert(0, "Notice to Bot Admins: ") - searchphrasefound.append("Run the debug command for more information.") - bot.osd(searchphrasefound, bot.channels.keys()) - - -@sopel.module.nickname_commands('debug') -@sopel.module.thread(True) -def bot_command_hub(bot, trigger): - - if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): - bot.say("I was unable to process this Bot Nick command due to privilege issues.") - return - - bot.osd("Is Examining systemd Log(s).", trigger.sender, 'ACTION') - - debuglines = errordisplay_fetch(bot) - - if len(debuglines) == 0: - bot.osd("had no log(s) for some reason", trigger.sender, 'ACTION') - return - - for line in debuglines: - bot.osd(line) - - -def errordisplay_fetch(bot): - # servicepid = str(os.popen("systemctl show " + str(bot.nick) + " --property=MainPID").read()).split("=")[-1] - servicepid = get_running_pid(bot) - debuglines = [] - for line in os.popen(str("sudo journalctl _PID=" + str(servicepid))).read().split('\n'): - if not str(line).startswith("-- Logs begin at"): - line = str(line).split(str(os.uname()[1] + " "))[-1] - if not str(line).startswith("sudo"): - lineparts = str(line).split(": ") - del lineparts[0] - line = spicemanip.main(lineparts, 0) - debuglines.append(str(line)) - else: - debuglines.append(str(line)) - return debuglines - - -def get_running_pid(bot): - try: - filename = "/run/sopel/" + str(bot.nick) + ".pid" - with open(filename, 'r') as pid_file: - pidnum = int(pid_file.read()) - except Exception as e: - pidnum = e - pidnum = str(os.popen("systemctl show " + str(bot.nick) + " --property=MainPID").read()).split("=")[-1] - return pidnum diff --git a/sopel_modules/SpiceBot_ErrorDisplay/__init__.py b/sopel_modules/SpiceBot_ErrorDisplay/__init__.py deleted file mode 100644 index 073feca..0000000 --- a/sopel_modules/SpiceBot_ErrorDisplay/__init__.py +++ /dev/null @@ -1,12 +0,0 @@ -# coding=utf8 -"""Sopel Startup Monologue - -Sopel Startup Monologue displays to all channels that the bot is online -""" -from __future__ import unicode_literals, absolute_import, division, print_function - -from .ErrorDisplay import * - -__author__ = 'Sam Zick' -__email__ = 'sam@deathbybandaid.net' -__version__ = '0.1.1' diff --git a/sopel_modules/SpiceBot_Logs/ErrorDisplay.py b/sopel_modules/SpiceBot_Logs/ErrorDisplay.py new file mode 100644 index 0000000..b676415 --- /dev/null +++ b/sopel_modules/SpiceBot_Logs/ErrorDisplay.py @@ -0,0 +1,30 @@ +# coding=utf-8 + +from __future__ import unicode_literals, absolute_import, division, print_function + +import sopel.module + +from sopel_modules.SpiceBot_Events.System import bot_events_recieved +from .Logs import errordisplay_fetch + + +@sopel.module.event('2005') +@sopel.module.rule('.*') +def bot_startup_monologue_start(bot, trigger): + bot_events_recieved(bot, trigger.event) + + debuglines = errordisplay_fetch(bot) + + searchphrasefound = [] + for line in debuglines: + if "modules failed to load" in str(line) and "0 modules failed to load" not in str(line): + searchphrase = str(line).replace(" modules failed to load", "") + searchphrasefound.append(str(searchphrase) + " module(s) failed") + elif "dict files failed to load" in str(line) and "0 dict files failed to load" not in str(line): + searchphrase = str(line).replace(" dict files failed to load", "") + searchphrasefound.append(str(searchphrase) + " dict file(s) failed") + + if len(searchphrasefound): + searchphrasefound.insert(0, "Notice to Bot Admins: ") + searchphrasefound.append("Run the debug command for more information.") + bot.osd(searchphrasefound, bot.channels.keys()) diff --git a/sopel_modules/SpiceBot_Logs/Logs.py b/sopel_modules/SpiceBot_Logs/Logs.py index de39744..07c21c4 100644 --- a/sopel_modules/SpiceBot_Logs/Logs.py +++ b/sopel_modules/SpiceBot_Logs/Logs.py @@ -8,6 +8,10 @@ from sopel.config.types import StaticSection, ValidatedAttribute from sopel_modules.SpiceBot_SBTools import bot_logging from sopel_modules.SpiceBot_Events.System import bot_events_startup_register, bot_events_recieved, bot_events_trigger +import os + +import spicemanip + def configure(config): config.define_section("SpiceBot_Logs", SpiceBot_Logs_MainSection, validate=False) @@ -31,7 +35,7 @@ def setup(bot): def bot_logs_setup_check(bot): if 'SpiceBot_Logs' not in bot.memory: - bot.memory['SpiceBot_Logs'] = {"logs": {}, "queue": []} + bot.memory['SpiceBot_Logs'] = {"logs": {"Sopel_systemd": []}, "queue": []} def shutdown(bot): @@ -62,6 +66,33 @@ def join_log_channel(bot, trigger): return +def errordisplay_fetch(bot): + servicepid = get_running_pid(bot) + debuglines = [] + for line in os.popen(str("sudo journalctl _PID=" + str(servicepid))).read().split('\n'): + if not str(line).startswith("-- Logs begin at"): + line = str(line).split(str(os.uname()[1] + " "))[-1] + if not str(line).startswith("sudo"): + lineparts = str(line).split(": ") + del lineparts[0] + line = spicemanip.main(lineparts, 0) + debuglines.append(str(line)) + else: + debuglines.append(str(line)) + return debuglines + + +def get_running_pid(bot): + try: + filename = "/run/sopel/" + str(bot.nick) + ".pid" + with open(filename, 'r') as pid_file: + pidnum = int(pid_file.read()) + except Exception as e: + pidnum = e + pidnum = str(os.popen("systemctl show " + str(bot.nick) + " --property=MainPID").read()).split("=")[-1] + return pidnum + + @sopel.module.event('2004') @sopel.module.rule('.*') def bot_events_setup(bot, trigger): diff --git a/sopel_modules/SpiceBot_Logs/Logs_Command.py b/sopel_modules/SpiceBot_Logs/Logs_Command.py index c201128..ad9436f 100644 --- a/sopel_modules/SpiceBot_Logs/Logs_Command.py +++ b/sopel_modules/SpiceBot_Logs/Logs_Command.py @@ -8,15 +8,16 @@ import spicemanip from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, command_permissions_check, inlist, inlist_match from sopel_modules.SpiceBot_Events.System import bot_events_check +from .Logs import errordisplay_fetch -@sopel.module.nickname_commands('logs') +@sopel.module.nickname_commands('logs', 'debug') def bot_command_action(bot, trigger): while not bot_events_check(bot, '2004'): pass - if not command_permissions_check(bot, trigger, ['admins', 'owner']): + if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']): bot.say("I was unable to process this Bot Nick command due to privilege issues.") return @@ -29,10 +30,15 @@ def bot_command_action(bot, trigger): logtype = inlist_match(bot, logtype, bot.memory['SpiceBot_Logs'].keys()) - if len(bot.memory['SpiceBot_Logs']["logs"][logtype]) == 0: + if logtype != "Sopel_systemd": + logindex = bot.memory['SpiceBot_Logs']["logs"][logtype] + else: + logindex = errordisplay_fetch(bot) + + if not len(logindex): bot.osd("No logs found for " + str(logtype) + ".") return bot.osd("Is Examining " + str(logtype) + " log(s).") - for line in bot.memory['SpiceBot_Logs']["logs"][logtype]: + for line in logindex: bot.osd(" " + str(line)) diff --git a/sopel_modules/SpiceBot_Logs/__init__.py b/sopel_modules/SpiceBot_Logs/__init__.py index 4c90034..091f24f 100644 --- a/sopel_modules/SpiceBot_Logs/__init__.py +++ b/sopel_modules/SpiceBot_Logs/__init__.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals, absolute_import, division, print_functi from .Logs import * from .Logs_Command import * +from .ErrorDisplay import * __author__ = 'Sam Zick' __email__ = 'sam@deathbybandaid.net'