Bacula/Dir Config/scripts/webhooks/bathook.py

74 lines
4.1 KiB
Python

from discord_webhook import DiscordEmbed
import re
class bathook:
def __init__(self, path_to_report, data_from):
with open (path_to_report, "r") as messagesfile: # Open the file
self.raw_messages = messagesfile.read() # Extract the data
if "Rescheduled" in self.raw_messages: # If the job was rescheduled
self.raw_messages = self.raw_messages.split("Rescheduled", 1)[-1] # Remove the failed data
self.rescheduled = "\nJob was rescheduled at least once." # We rescheduled
else:
self.rescheduled = "" # We did not reschedule
self.endtime = self.mextract(self.raw_messages, "End time:")
self.elapsedtime = self.mextract(self.raw_messages, "Elapsed time:")
self.byteswritten = self.mextract(self.raw_messages, "SD Bytes Written:")
self.usedvolumes = self.mextract(self.raw_messages, "Volume name\(s\):")
self.jobname = self.mextract(self.raw_messages, "Job:")
self.jobid = self.mextract(self.raw_messages, "JobId:")
self.level = self.mextract(self.raw_messages, "Backup Level:")
self.data_from = data_from
if "Backup OK" in self.raw_messages and "Backup OK -- with warnings" not in self.raw_messages:
self.backupstatus = "OK"
else:
self.backupstatus = "Error or warnings."
self.maxCharPerMessage = 1994
def mextract(self, report, variable):
try:
result = re.search(variable + '(.*)', report)
print(result)
return(result.group(1).lstrip()) # lstrip removes leading spaces.
except:
return("[Variable Not found]")
def get_formatted_message(self):
if "Backup OK" in self.raw_messages and "Backup OK -- with warnings" not in self.raw_messages:
truncated_stdout = ["""Job {0} (Job {1}) completed with status Backup OK, omitting full report.
All data from {2} was successfully written to tape on {3}. Backup level was {4}.
Backup took {5} And used {6} bytes.""".format(self.jobname, self.jobid, self.data_from, self.endtime, self.level, self.elapsedtime, self.byteswritten)]
if len(self.usedvolumes) > 1:
truncated_stdout[0] = truncated_stdout[0] + ("\nWrote data to volume(s) {0} {1}".format(self.usedvolumes, self.rescheduled))
else:
# Discord limits each message to 2000 chars, if the message is longer than that, truncate it
truncated_stdout = [self.raw_messages[i:i+self.maxCharPerMessage] for i in range(0, len(self.raw_messages), self.maxCharPerMessage)]
return(truncated_stdout)
def get_discord_embed(self):
if self.backupstatus == "OK":
headercolor = 3388189
headerdesc = "The most recent backup completed with status OK, and so the full report will not be shown."
else:
headercolor = 10358814
headerdesc = "The most recent backup did not complete, or completed with errors, the fill report will still not be shown."
self.embed = DiscordEmbed(title="Tape Job Results", description=headerdesc, color=headercolor)
self.embed.set_author(name="Snowsune's Tape Daemon", url="https://kitsunehosting.net/", icon_url="https://kitsunehosting.net/nextcloud/index.php/apps/files_sharing/publicpreview/zzygHFxJQXjXxMz?x=1920&y=553&a=true&file=TapeImage.JPG&scalingup=0")
self.embed.set_timestamp()
self.embed.add_embed_field(name="End Time", value=self.endtime)
self.embed.add_embed_field(name="Elapsed Time", value=self.elapsedtime)
self.embed.add_embed_field(name="Bytes Written", value=self.byteswritten)
if len(self.usedvolumes) > 0:
self.embed.add_embed_field(name="Used Volumes", value=self.usedvolumes)
self.embed.add_embed_field(name="Job Name", value=self.jobname)
self.embed.add_embed_field(name="Job ID", value=self.jobid)
self.embed.add_embed_field(name="Job Level", value=self.level)
if len(self.rescheduled) > 0:
self.embed.add_embed_field(name="Job Reschedule?", value=self.rescheduled)
return(self.embed)