isengard-bot/sharedBuffer.py

168 lines
6.6 KiB
Python
Raw Normal View History

2022-02-05 00:46:57 +01:00
class SharedBuffer:
2022-02-06 01:29:59 +01:00
2022-02-05 00:46:57 +01:00
def __init__(self, linkedBot):
2022-02-06 01:29:59 +01:00
self.buf = {}
2022-02-05 00:46:57 +01:00
self.linkedBot = linkedBot
def flush(self):
2022-02-06 01:29:59 +01:00
for host in self.buf:
if not isinstance(self.buf[host], dict):
continue
maintainer = self.buf[host]["maintainer"]
if self.buf[host]["needUpdate"]:
destmuc = self.buf[host]["destmuc"]
text = self.buf[host]["text"]
msg = "(néant)"
if self.buf[host]["status_type"] == "PROBLEM":
msg = "{}, je détecte un problème sur {} ({})".format(maintainer, host, text)
elif self.buf[host]["status_type"] == "UNKNOWN":
msg = "{}, état de {} inconnu ({})".format(maintainer, host, text)
elif self.buf[host]["status_type"] == "RECOVERY":
msg = "{}, problème résolu sur {} ({})".format(maintainer, host, text)
self.linkedBot.send_message(
mto=destmuc,
mbody=msg,
mtype='groupchat')
self.buf[host]["needUpdate"] = False
print("Sent to {}, msg: {}".format(destmuc, msg))
for service in self.buf[host]:
if not isinstance(self.buf[host][service], dict):
continue
if self.buf[host][service]["needUpdate"]:
destmuc = self.buf[host][service]["destmuc"]
text = self.buf[host][service]["text"]
msg = "(néant)"
if self.buf[host][service]["status_type"] == "PROBLEM":
msg = "{}, je détecte un problème de {} sur {} ({})".format(maintainer, service, host, text)
elif self.buf[host][service]["status_type"] == "UNKNOWN":
msg = "{}, état de {} inconnu pour {} ({})".format(maintainer, service, host, text)
elif self.buf[host][service]["status_type"] == "RECOVERY":
msg = "{}, problème de {} résolu sur {} ({})".format(maintainer, service, host, text)
self.linkedBot.send_message(
mto=destmuc,
mbody=msg,
2022-02-05 00:46:57 +01:00
mtype='groupchat')
2022-02-06 01:29:59 +01:00
self.buf[host][service]["needUpdate"] = False
print("Sent to {}, msg: {}".format(destmuc, msg))
print("End of flush")
2022-02-05 00:46:57 +01:00
def push(self, msg):
2022-02-06 01:29:59 +01:00
# separate dest and message
destmuc, rmsg = msg.split("|")
# separate and parse lines
cutmsg_l0 = rmsg.split("\n")[0].split(" ")
cutmsg_l1 = rmsg.split("\n")[1].split("-")
# get the status of location
status_type = cutmsg_l0[0].replace(" ", "")
status_state = cutmsg_l0[-1].replace(" ", "")
# get the location
location = ""
for i in range(len(cutmsg_l0) - 2):
location += cutmsg_l0[i+1] + " "
location = location[:-1]
# check if message is about a service or host
if len(location.split("/")) > 1:
host, service = location.split("/")
else:
host = location
service = False
text = status_state + ": " + cutmsg_l1[1].split("(")[0][1:]
raw = rmsg.split("\n")[1]
print("Dest: %s, Msg: %s" % (destmuc, rmsg))
print("Status: %s" % (status_type + " (" + status_state + ")"))
print("Host: %s, Service: %s" % (host, service))
print("Text: %s" % (text))
if host in self.buf:
if service and service in self.buf[host]:
if status_state != self.buf[host][service]["status_state"] \
or status_type != self.buf[host][service]["status_type"]:
self.buf[host][service]["status_state"] = status_state
self.buf[host][service]["status_type"] = status_type
self.buf[host][service]["destmuc"] = destmuc
self.buf[host][service]["text"] = text
self.buf[host][service]["raw"] = raw
self.buf[host][service]["needUpdate"] = True
else:
# exit if problem resolved (but unkown in the first place)
if status_type == "RECOVERY":
return
# create zone for service
self.buf[host][service] = {}
self.buf[host][service]["destmuc"] = destmuc
self.buf[host][service]["status_state"] = status_state
self.buf[host][service]["status_type"] = status_type
self.buf[host][service]["text"] = text
self.buf[host][service]["raw"] = raw
self.buf[host][service]["needUpdate"] = True
else:
# exit if problem resolved (but unkown in the first place)
if status_type == "RECOVERY":
return
# create zone for service
if service:
self.buf[host] = {}
self.buf[host]["destmuc"] = None
self.buf[host]["status_state"] = None
self.buf[host]["status_type"] = None
self.buf[host]["text"] = None
self.buf[host]["raw"] = None
self.buf[host]["maintainer"] = None
self.buf[host]["needUpdate"] = False
self.buf[host][service] = {}
self.buf[host][service]["destmuc"] = destmuc
self.buf[host][service]["status_state"] = status_state
self.buf[host][service]["status_type"] = status_type
self.buf[host][service]["text"] = text
self.buf[host][service]["raw"] = raw
self.buf[host][service]["needUpdate"] = True
else:
self.buf[host] = {}
self.buf[host]["destmuc"] = destmuc
self.buf[host]["status_state"] = status_state
self.buf[host]["status_type"] = status_type
self.buf[host]["text"] = text
self.buf[host]["raw"] = raw
self.buf[host]["maintainer"] = None
self.buf[host]["needUpdate"] = True
print(self.buf)
print("Sending...")
self.flush()
print("Sent.")
print(self.buf)
print("End of push")
#PROBLEM video.chalec.org/System load CRITICAL
#CRITICAL - Charge moyenne: 6.45, 5.61, 4.12 (Commentaire: [] )