📄 statussections.py
字号:
# Sort reverse by column 4 (downtime) self.headings = [] self.headingDefs = [('Sysname',None), ('Handler',None), ('Down since',None), ('Downtime',None), ('',None), ('',None)] self.rows = [] self.summary = None self.historyLink = [] self.historyLink.append(('/maintenance/calendar', 'maintenance schedule')) self.historyLink.append(('/browse/service/allMatrix', 'service status')) self.filterSettings = filterSettings SectionBox.__init__(self, controlBaseName, title, getArgs, None) self.addHeadings() return def fill(self): filterSettings = self.filterSettings sql = """SELECT DISTINCT n.sysname, s.handler, ah.start_time, now() - ah.start_time AS downtime, s.up, s.serviceid, n.netboxid, ahv.val AS maint_taskid FROM alerthist AS ah NATURAL JOIN alerthistvar AS ahv, netbox AS n, service AS s WHERE ah.netboxid = n.netboxid AND ah.subid = s.serviceid AND ah.end_time = 'infinity' AND ah.eventtypeid = 'maintenanceState' AND ahv.var = 'maint_taskid'""" where_clause = '' if filterSettings: # orgid if not filterSettings['orgid'].count(FILTER_ALL_SELECTED): where_clause += " AND (" first_line = True for org in filterSettings['orgid']: if not first_line: where_clause += " OR " where_clause += "n.orgid = '" + org + "'" first_line = False where_clause += ") " # handler (service) if not filterSettings['handler'].count(FILTER_ALL_SELECTED): where_clause += " AND (" first_line = True for handler in filterSettings['handler']: if not first_line: where_clause += " OR " where_clause += "s.handler = '" + handler + "'" first_line = False where_clause += ") " # state self.listStates = filterSettings['state'] if not filterSettings['state'].count(FILTER_ALL_SELECTED): where_clause += " AND (" first_line = True for state in filterSettings['state']: if not first_line: where_clause += " OR " where_clause += "s.up = '" + state + "'" first_line = False where_clause += ") " sql = sql + where_clause + " ORDER BY now()-ah.start_time" connection = nav.db.getConnection('status', 'manage') database = connection.cursor() database.execute(sql) result = database.fetchall() # If components is down, get down since and downtime sql = """SELECT DISTINCT n.sysname, s.handler, ah.start_time, now() - ah.start_time AS downtime, s.up, s.serviceid, n.netboxid FROM alerthist AS ah, netbox AS n, service AS s WHERE ah.netboxid = n.netboxid AND ah.subid = s.serviceid AND ah.end_time = 'infinity' AND ah.eventtypeid = 'serviceState'""" sql = sql + where_clause + " ORDER BY now()-start_time" database.execute(sql) result_down = database.fetchall() height = len(result) if self.maxHeight: if height > self.maxHeight: height = self.maxHeight servicesMaintenance = 0 servicesMaintenanceUp = 0 servicesMaintenanceDown = 0 servicesMaintenanceShadow = 0 SYSNAME = 0 HANDLER = 1 STARTTIME = 2 DOWNTIME = 3 UP = 4 SERVICEID = 5 BOXID = 6 MAINTID = 7 downtimes = {} for line in result_down: downtimes[(line[SYSNAME], line[HANDLER])] = \ (line[STARTTIME], line[DOWNTIME]) for line in result: row = [] style = None servicesMaintenance += 1 if line[UP] == 'y': servicesMaintenanceUp += 1 elif line[UP] == 'n': servicesMaintenanceDown += 1 else: servicesMaintenanceShadow += 1 # Sysname row.append((line[SYSNAME], urlbuilder.createUrl(id=line[BOXID], division='netbox'), None, style)) # Handler row.append((line[HANDLER], urlbuilder.createUrl(id=line[HANDLER], division='service'), None, style)) if line[UP] == 'y': # Down since row.append(('Up', None, None, style)) # Downtime row.append(('', None, None, style)) else: (starttime, downtime) = \ downtimes[(line[SYSNAME], line[HANDLER])] # Down since row.append((starttime.strftime('%Y-%m-%d %H:%M'), None, None, style)) # Downtime downtime = '%s d, %s h, %s m' % ( str(downtime.absvalues()[0]), downtime.strftime('%H'), downtime.strftime('%M')) row.append((downtime, None, None, style)) # Wrench icon row.append((None, '/maintenance/view?id=%s' % line[MAINTID], ('/images/wrench.gif', 'Maintenance details'), None)) # History link row.append((None, BASEPATH + 'history/?type=services&id=%s' \ % (line[SERVICEID],), ('/images/status/status-history.png', 'View history for this service'), None)) if line[UP] != 'y' and self.sortBy == STARTTIME: self.rows.append([starttime, row]) elif line[UP] != 'y' and self.sortBy == DOWNTIME: self.rows.append([downtime, row]) else: self.rows.append([line[self.sortBy], row]) self.sort() servicesMaintenance = str(servicesMaintenance) servicesMaintenanceUp = str(servicesMaintenanceUp) servicesMaintenanceDown = str(servicesMaintenanceDown) servicesMaintenanceShadow = str(servicesMaintenanceShadow) if servicesMaintenance == '0': servicesMaintenance = 'No' if servicesMaintenanceUp == '0': servicesMaintenanceUp = 'No' if servicesMaintenanceDown == '0': servicesMaintenanceDown = 'No' if servicesMaintenanceShadow == '0': servicesMaintenanceShadow = 'No' self.summary = servicesMaintenance + ' services on maintenance (' + \ servicesMaintenanceUp + ' up, ' + \ servicesMaintenanceDown.lower() + ' down, ' + \ servicesMaintenanceShadow.lower() + ' in shadow)' def getFilters(controlBaseName, orgList): """ Return the filters that this section box accepts """ filterHeadings = ['Organisation','Service','State'] filterSelects = [] table = nav.db.manage.Org # Org optionsList = [(FILTER_ALL_SELECTED,'All',True)] # Restrict to orgs where user belongs #whereOrg = makeWhereList(orgList) for org in table.getAllIterator(orderBy = 'orgid'): optionsList.append((org.orgid,org.orgid,False)) filterSelects.append((controlBaseName + '_' + 'orgid',optionsList)) # Handler # FIXME: The handler list should be dynamic (see editdb.py for example) optionsList = [(FILTER_ALL_SELECTED,'All')] filterSelects.append((controlBaseName + '_' + 'handler',\ [(FILTER_ALL_SELECTED, 'All', True), ('dc', 'dc', False), ('dns', 'dns', False), ('dummy', 'dummy', False), ('ftp', 'ftp', False), ('http', 'http', False), ('https', 'https', False), ('imap', 'imap', False), ('imaps', 'imaps', False), ('ldap', 'ldap', False), ('mysql', 'mysql', False), ('oracle', 'oracle', False), ('pop3', 'pop3', False), ('postgresql', 'postgresql', False), ('radius', 'radius', False), ('rpc', 'rpc', False), ('smb', 'smb', False), ('smtp', 'smtp', False), ('ssh', 'ssh', False)] )) # State filterSelects.append((controlBaseName + '_' + 'state',\ [(FILTER_ALL_SELECTED, 'All', True), ('y', 'Up', False), ('n', 'Down', False), ('s', 'Shadow', False)] )) return (filterHeadings,filterSelects) getFilters = staticmethod(getFilters)class NetboxSectionBox(SectionBox): " Section displaying netboxes that are down or in shadow " # attribs for preferences name = 'IP Devices down' typeId = 'netbox' prefsOptions = None defaultSort = 3 sortReverse = False sortBy = defaultSort def __init__(self, controlBaseName,getArgs,title,filterSettings): # Sort reverse by column 4 (downtime) self.headings = [] self.headingDefs = [('Sysname',None), ('IP',self.ipCompare), ('Down since',None), ('Downtime',None), ('',None)] self.rows = [] self.summary = None self.historyLink = [] self.historyLink.append((BASEPATH + 'history/?type=boxes', 'history')) self.filterSettings = filterSettings SectionBox.__init__(self, controlBaseName,title,getArgs,None) self.addHeadings() return def fill(self): filterSettings = self.filterSettings sql = """SELECT n.sysname, n.ip, ah.start_time, now() - ah.start_time AS downtime, n.up, at.alerttype, n.netboxid FROM alerthist AS ah, netbox AS n, alerttype AS at WHERE ah.netboxid = n.netboxid AND at.alerttypeid = ah.alerttypeid AND ah.end_time = 'infinity' AND ah.eventtypeid = 'boxState' AND (n.up = 'n' OR n.up = 's')""" where_clause = '' if filterSettings: # orgid if not filterSettings['orgid'].count(FILTER_ALL_SELECTED): where_clause += " AND (" first_line = True for org in filterSettings['orgid']: if not first_line: where_clause += " OR " where_clause += "n.orgid = '" + org + "'" first_line = False where_clause += ") " # catid if not filterSettings['catid'].count(FILTER_ALL_SELECTED): where_clause += " AND (" first_line = True for cat in filterSettings['catid']: if not first_line: where_clause += " OR " where_clause += "n.catid = '" + cat + "'" first_line = False where_clause += ") " # state self.listStates = filterSettings['state'] if not filterSettings['state'].count(FILTER_ALL_SELECTED): where_clause += " AND (" first_line = True for state in filterSettings['state']: if not first_line: where_clause += " OR " if state=='n': # Down state = 'boxDown' elif state=='s': # Shadow state = 'boxShadow' where_clause += "at.alerttype = '" + state + "'" first_line = False where_clause += ") " else: where_clause += " AND (at.alerttype='boxDown' or " +\ "at.alerttype='boxShadow') " sql = sql + where_clause + " ORDER BY now()-start_time" connection = nav.db.getConnection('status', 'manage') database = connection.cursor() database.execute(sql) result = database.fetchall() # If components is on maintenance, do not show them sql = """SELECT n.sysname, n.ip, ah.start_time, now() - ah.start_time AS downtime, n.up, at.alerttype, n.netboxid FROM alerthist AS ah, netbox AS n, alerttype AS at WHERE ah.netboxid = n.netboxid AND ah.alerttypeid = at.alerttypeid AND ah.end_time = 'infinity' AND ah.eventtypeid = 'maintenanceState'""" database.execute(sql) result_maint = database.fetchall() height = len(result) if self.maxHeight: if height > self.maxHeight: height = self.maxHeight boxesDown = 0 boxesShadow = 0 boxesMaintenance = 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -