rendered paste body#!/usr/bin/python
## DashBoard version 0.0.1
## Custom view on certain service/host groups in Nagios
## - author: Franjo Stipanovic
## - date: 30.4.2007.
## ChangeLog:
## - 2.5.2007. code optimization
## FixMe:
## - total counters not presented to user yet
from configobj import ConfigObj
import cgi, os.path, sys, datetime
class Dashboard:
def __init__(self):
datfile = "/usr/local/nagios/var/status.dat"
cfg = ConfigObj('/usr/local/nagios/dashboard.cfg')
sys.stderr = sys.stdout
# total counters for hosts and services status
# - up,down,unreachable,pending,error
hTotal = [0, 0, 0, 0, 0]
# - ok,warning,critical,unknown,pending,error
sTotal = [0, 0, 0, 0, 0, 0]
# list for host and service status
hStatus = ['statusHOSTUP','statusHOSTDOWN','statusHOSTUNREACHABLE','statusHOSTPENDING','white']
sStatus = ['statusOK','statusWARNING','statusCRITICAL','statusUNKNOWN','statusPENDING','white']
print "Content-type: text/html\n\n"
print "<html><head><title></title>"
print "<meta http-equiv='refresh' content='60'>"
print "<link rel=stylesheet type=text/css href=/nagios/stylesheets/common.css>"
print "<link rel=stylesheet type=text/css href=/nagios/stylesheets/dashboard.css>"
print "</head>"
print "<body>"
print "System time: " + datetime.datetime.now().strftime("%m/%d/%Y %I:%M %p") + "<br>"
print "Updated every 60 seconds" + "<br><br>"
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 "<table class=tbl align=center border=0 cellpadding=3 cellspacing=2>\
<tr><td class=status colspan=4>%s</td></tr><tr>" % val
for idx, v in enumerate(cfg[val]['members']):
# reset file cursor
self.infile.seek(0)
style = target = ''
if v.find(':') == -1: # host
status = self.find_host_status(v.strip())
if status != '4':
target = "<a href='/nagios/cgi-bin/extinfo.cgi?type=1&host=%s'\
>%s</a>" % (v.strip(), v.strip())
else:
target = v.strip()
style = hStatus[int(status)]
hTotal[int(status)] += 1
else: # service
status = self.find_service_status(v.split(':')[0].strip(),
v.split(':')[1].strip())
if status != '5':
target = "<a href='/nagios/cgi-bin/extinfo.cgi?type=2&host=%s\
&service=%s>%s</a>" \
% (v.split(':')[0].strip(), v.split(':')[1].strip(),
v.strip())
else:
target = v.strip()
style = sStatus[int(status)]
sTotal[int(status)] += 1
if idx % 4 == 0:
print "</tr><tr><td class=%s>%s</td>" % (style, target)
else:
print "<td class=%s>%s</td>" % (style, target)
print "</table><br><br>"
print "</body></html>"
"""
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 '5' # not found - error
"""
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 '4'
dashboard = Dashboard()