#!/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 "" print "" print "" print "" print "" print "" print "System time: " + datetime.datetime.now().strftime("%m/%d/%Y %I:%M %p") + "
" print "Updated every 60 seconds" + "

" 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 "\ " % 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 = "%s" % (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 = "" % (style, target) else: print "" % (style, target) print "
%s
%s%s


" 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 '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()