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:## FixMe:from configobj import ConfigObjimport cgi, os.path, sys, datetimeclass Dashboard: def __init__(self): datfile = "/usr/local/nagios/var/status.dat" cfg = ConfigObj('/usr/local/nagios/dashboard.cfg') sys.stderr = sys.stdout 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><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: status = self.find_host_status(v.strip()) target = "<a href='/nagios/cgi-bin/extinfo.cgi?type=1&host=%s'\ >%s</a>" % (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 = "<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()) 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 "</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 '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()