"
if os.path.exists(datfile) == False:
print "Error, please check datfile parameter"
return
self.infile = open(datfile, "r")
# look for hosts and services status
for i, val in enumerate(cfg):
print "
\
%s
" % val
for idx, v in enumerate(cfg[val]['members']):
# reset file cursor
self.infile.seek(0)
style = target = ''
if v.find(':') == -1:
status = self.find_host_status(v.strip())
target = "%s" % (v.strip(), v.strip())
if status == '0':
style = 'statusHOSTUP'
elif status == '1':
style = 'statusHOSTDOWN'
elif status == '2':
style = 'statusHOSTUNREACHABLE'
elif status == '3':
style = 'statusHOSTPENDING'
elif status == 'None':
style = 'white'
target = v.strip()
else:
status = self.find_service_status(v.split(':')[0].strip(),
v.split(':')[1].strip())
target = "%s" \
% (v.split(':')[0].strip(), v.split(':')[1].strip(), v.strip())
if status == '0':
style = 'statusOK'
elif status == '1':
style = 'statusWARNING'
elif status == '2':
style = 'statusCRITICAL'
elif status == '3':
style = 'statusUNKNOWN'
elif status == '4':
style = 'statusPENDING'
elif status == 'None':
style = 'white'
target = v.strip()
if (idx + 1) % 5 == 0:
print "
%s
" % (style, target)
else:
print "
%s
" % (style, target)
print "
"
print ""
"""
look for has_been_checked param
it shows if service/host has pending status
"""
def look_pending(self):
while 1:
line = self.infile.readline().strip()
if line == "}":
break
param = line.split("=")[0]
if param == "has_been_checked":
checked = line[17:]
return checked
"""
look for current_state param and return it
used in service and host checks
"""
def look_status(self):
while 1:
line = self.infile.readline().strip()
if line == "}":
break
param = line.split("=")[0]
if param == "current_state":
current_state = line[14:]
return current_state
"""
host - host_name
service - service_description
together they represent unique combination in nagios
"""
def find_service_status(self, host, service):
while 1:
line = self.infile.readline()
if line == "":
break
if line[:7] == "service": # start of object
# points to host_name
line = self.infile.readline().strip()
if (line[10:] == host): # our host?
# points to service_description
line = self.infile.readline().strip()
if (line[20:] == service): # our service?
checked = self.look_pending()
if checked == '0': # has pending status
return '4'
state = self.look_status()
return state
return 'None'
"""
host - host_name
"""
def find_host_status(self, host):
while 1:
line = self.infile.readline()
if line == "":
break
if line[:4] == "host":
# points to host_name
line = self.infile.readline().strip()
if (line[10:] == host): # our host?
checked = self.look_pending()
if checked == '0': # has pending status
return '3'
state = self.look_status()
return state
return 'None'
dashboard = Dashboard()