isengard-bot/serviceData.py

186 lines
7.7 KiB
Python

import datetime
class ServiceData:
def __init__(self, linkedBot):
self.serviceData = {}
self.maintainerData = {}
self.linkedBot = linkedBot
def flush(self):
for host in self.serviceData:
if not isinstance(self.serviceData[host], dict):
continue
maintainer = self.serviceData[host]["maintainer"]
if self.serviceData[host]["needUpdate"]:
destmuc = self.serviceData[host]["destmuc"]
text = self.serviceData[host]["text"]
msg = "(néant)"
if self.serviceData[host]["status_type"] == "PROBLEM":
msg = "{}, je détecte un problème sur {} ({})".format(maintainer, host, text)
elif self.serviceData[host]["status_type"] == "UNKNOWN":
msg = "{}, état de {} inconnu ({})".format(maintainer, host, text)
elif self.serviceData[host]["status_type"] == "RECOVERY":
msg = "{}, problème résolu sur {} ({})".format(maintainer, host, text)
self.linkedBot.push(destmuc, msg)
self.serviceData[host]["needUpdate"] = False
for service in self.serviceData[host]:
if not isinstance(self.serviceData[host][service], dict):
continue
if self.serviceData[host][service]["needUpdate"]:
destmuc = self.serviceData[host][service]["destmuc"]
text = self.serviceData[host][service]["text"]
msg = "(néant)"
if self.serviceData[host][service]["status_type"] == "PROBLEM":
msg = "{}, je détecte un problème de {} sur {} ({})".format(maintainer, service, host, text)
elif self.serviceData[host][service]["status_type"] == "UNKNOWN":
msg = "{}, état de {} inconnu pour {} ({})".format(maintainer, service, host, text)
elif self.serviceData[host][service]["status_type"] == "RECOVERY":
msg = "{}, problème de {} résolu sur {} ({})".format(maintainer, service, host, text)
self.linkedBot.push(destmuc, msg)
self.serviceData[host][service]["needUpdate"] = False
print("End of flush")
def push(self, msg):
# Get current time
curtime = datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
# 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))
# Is that host known
if host in self.serviceData:
# Is it a service and is it known
if service and service in self.serviceData[host]:
# update host if needed
if status_type != self.serviceData[host][service]["status_type"]:
self.serviceData[host][service]["status_state"] = status_state
self.serviceData[host][service]["status_type"] = status_type
self.serviceData[host][service]["destmuc"] = destmuc
self.serviceData[host][service]["text"] = text
self.serviceData[host][service]["needUpdate"] = True
maintainer = self.serviceData[host]["maintainer"]
# Is it an unkown service
elif service:
# create zone for service
self.serviceData[host][service] = {}
self.serviceData[host][service]["destmuc"] = destmuc
self.serviceData[host][service]["status_state"] = status_state
self.serviceData[host][service]["status_type"] = status_type
self.serviceData[host][service]["text"] = text
self.serviceData[host][service]["needUpdate"] = True
maintainer = self.serviceData[host]["maintainer"]
# This is a host (not a service)
else:
# update host if needed
if status_type != self.serviceData[host]["status_type"]:
self.serviceData[host]["destmuc"] = destmuc
self.serviceData[host]["status_state"] = status_state
self.serviceData[host]["status_type"] = status_type
self.serviceData[host]["text"] = text
self.serviceData[host]["needUpdate"] = True
maintainer = self.serviceData[host]["maintainer"]
# That host is not known
else:
# create slot for host and service
if service:
self.serviceData[host] = {}
self.serviceData[host]["destmuc"] = None
self.serviceData[host]["status_state"] = None
self.serviceData[host]["status_type"] = None
self.serviceData[host]["text"] = None
self.serviceData[host]["maintainer"] = "Tout le monde"
self.serviceData[host]["needUpdate"] = False
self.serviceData[host][service] = {}
self.serviceData[host][service]["destmuc"] = destmuc
self.serviceData[host][service]["status_state"] = status_state
self.serviceData[host][service]["status_type"] = status_type
self.serviceData[host][service]["text"] = text
self.serviceData[host][service]["raw"] = raw
self.serviceData[host][service]["needUpdate"] = True
maintainer = self.serviceData[host]["maintainer"]
# create slot for host
else:
self.serviceData[host] = {}
self.serviceData[host]["destmuc"] = destmuc
self.serviceData[host]["status_state"] = status_state
self.serviceData[host]["status_type"] = status_type
self.serviceData[host]["text"] = text
self.serviceData[host]["maintainer"] = "Tout le monde"
self.serviceData[host]["needUpdate"] = True
maintainer = self.serviceData[host]["maintainer"]
# create maintainer in maintainer data if needed
if not maintainer in self.maintainerData:
self.maintainerData[maintainer] = {}
# create host entry
if not service:
service = "GENERAL"
if not host in self.maintainerData[maintainer]:
self.maintainerData[maintainer][host] = []
self.maintainerData[maintainer][host].append(
curtime + ": " + service + "/" + raw
)
print(self.serviceData)
print("Flushing...")
self.flush()
print(self.serviceData)
print("End of push")