From 8c8065a8192daeb7e8bea9942f1ac9bc93b894dc Mon Sep 17 00:00:00 2001 From: Joe S <31870999+KenwoodFox@users.noreply.github.com> Date: Tue, 18 Aug 2020 15:18:08 -0400 Subject: [PATCH] Even better than the sequel baby --- .../webhooks/Robotics-Bacula-Webhook.py | 32 +++---------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/Dir Config/scripts/webhooks/Robotics-Bacula-Webhook.py b/Dir Config/scripts/webhooks/Robotics-Bacula-Webhook.py index 31eeba0..8ed4f95 100644 --- a/Dir Config/scripts/webhooks/Robotics-Bacula-Webhook.py +++ b/Dir Config/scripts/webhooks/Robotics-Bacula-Webhook.py @@ -1,36 +1,12 @@ -from discord_webhook import DiscordWebhook, DiscordEmbed -from mextract import mextract +from discord_webhook import DiscordWebhook +from bathook import bathook webhookURL = "https://discordapp.com/api/webhooks/740630755259973713/CNhxPBJ4C6UZ-m1DVXUQ54KeJQD3I9Di0sAwldmyjw-s7dMPUWplq7rhGXegpWGtKSve" maxCharPerMessage = 1994 -rescheduled = "" -with open ("/etc/bacula/scripts/webhooks/RoboticsMail.md", "r") as myfile: - cleaned_stdout=myfile.read() +bathook = bathook("/etc/bacula/scripts/webhooks/RoboticsMail.md", "1721's google drive") -if "Rescheduled" in cleaned_stdout: # If the job was rescheduled - cleaned_stdout = cleaned_stdout.split("Rescheduled", 1)[-1] # Remove the failed data - rescheduled = "\nJob was rescheduled at least once." - -endtime = mextract(cleaned_stdout, "End time:") -elapsedtime = mextract(cleaned_stdout, "Elapsed time:") -byteswritten = mextract(cleaned_stdout, "SD Bytes Written:") -usedvolumes = mextract(cleaned_stdout, "Volume name\(s\):") - -if "Backup OK" in cleaned_stdout and "Backup OK -- with warnings" not in cleaned_stdout: - truncated_stdout = ["""Job Backup-Robotics completed with status Backup OK, omitting full report. -All data from 1721's google drive was successfully written to tape on """ + endtime + """. -Backup took """ + elapsedtime + " And used " + byteswritten + " bytes."] - if len(usedvolumes) > 1: - truncated_stdout[0] = truncated_stdout[0] + ("\nWrote data to volume(s) " + usedvolumes + rescheduled) -else: - # Discord limits each message to 2000 chars, if the message is longer than that, truncate it - truncated_stdout = [cleaned_stdout[i:i+maxCharPerMessage] for i in range(0, len(cleaned_stdout), maxCharPerMessage)] - -print (truncated_stdout) # Print the truncated message - - -for message_part in truncated_stdout: # For every message part in the truncated output, run this loop +for message_part in bathook.get_formatted_message(): # For every message part in the truncated output, run this loop message_part = "```" + message_part + "```" # Encapsulate the messsage print(message_part) webhook = DiscordWebhook(url=webhookURL, content=message_part) # Attach the message