2022-02-07 18:39:34 +01:00
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 )
2022-02-07 20:30:07 +01:00
elif self . serviceData [ host ] [ " status_type " ] == " CUSTOM " :
msg = " {} , nouvelle notification de {} ( {} ) " . format ( maintainer , host , text )
2022-02-07 18:39:34 +01:00
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) "
2022-02-07 20:43:35 +01:00
if self . serviceData [ host ] [ " status_type " ] == " PROBLEM " or
self . serviceData [ host ] [ " status_type " ] == " UNKNOWN " :
msg = " {} , je détecte de multiples problèmes sur {} " . format ( maintainer , host )
elif self . serviceData [ host ] [ service ] [ " status_type " ] == " PROBLEM " :
2022-02-07 18:39:34 +01:00
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 ) :
2022-02-07 20:30:07 +01:00
# $notification.type$|$host.display_name$/$service.name$|$service.state$|$service.output$|$notification.author$|$notification.comment$
# EX: cominfra@salons.a-lec.org|CUSTOM|ctrlv.chalec.org/SSH server|CRITICAL|connect to address ctrlv.chalec.org and port 22: Connexion refusée|neox@a-lec.org|AGAIN
try :
# Get current time
curtime = datetime . datetime . now ( ) . strftime ( " % m/ %d / % Y, % H: % M: % S " )
# Get all params
destmuc , status_type , location , status_state , text , sender , comment = msg . split ( " | " )
print ( " Dest: %s , Msg: %s " % ( destmuc , msg ) )
print ( " Status: %s " % ( status_type + " ( " + status_state + " ) " ) )
# check if message is about a service or host
if len ( location . split ( " / " ) ) > 1 :
host , service = location . split ( " / " )
else :
host = location
service = False
print ( " Host: %s , Service: %s " % ( host , service ) )
print ( " Text: %s " % ( text ) )
except Exception as e :
print ( " ERROR in serviceData.push(): " + str ( e ) )
return
2022-02-07 18:39:34 +01:00
# 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
2022-02-07 20:43:35 +01:00
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
maintainer = self . serviceData [ host ] [ " maintainer " ]
if status_type != self . serviceData [ host ] [ service ] [ " status_type " ] and
status_type != self . serviceData [ host ] [ " status_type " ] :
2022-02-07 18:39:34 +01:00
self . serviceData [ host ] [ service ] [ " needUpdate " ] = True
2022-02-07 20:43:35 +01:00
2022-02-07 18:39:34 +01:00
# 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
maintainer = self . serviceData [ host ] [ " maintainer " ]
2022-02-07 20:43:35 +01:00
if status_type != self . serviceData [ host ] [ " status_type " ] :
self . serviceData [ host ] [ service ] [ " needUpdate " ] = True
2022-02-07 18:39:34 +01:00
# This is a host (not a service)
else :
# update host if needed
2022-02-07 20:43:35 +01:00
self . serviceData [ host ] [ " destmuc " ] = destmuc
self . serviceData [ host ] [ " status_state " ] = status_state
self . serviceData [ host ] [ " status_type " ] = status_type
self . serviceData [ host ] [ " text " ] = text
maintainer = self . serviceData [ host ] [ " maintainer " ]
2022-02-07 18:39:34 +01:00
if status_type != self . serviceData [ host ] [ " status_type " ] :
self . serviceData [ host ] [ " needUpdate " ] = True
# 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 ] [ " 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 (
2022-02-07 20:30:07 +01:00
" %s [ %s ]: %s %s ( %s %s ) " % ( curtime , status_state , service , text , sender , comment )
)
2022-02-07 18:39:34 +01:00
print ( self . serviceData )
print ( " Flushing... " )
self . flush ( )
print ( self . serviceData )
print ( " End of push " )