def getSwitchInfo(): """ Query DB for summary info on all switches currently monitored """ swDB = switchdb.DB() raw_info = swDB.getAllSummary() switchList = [] for row in raw_info: row = list(row) switch = {} switch["name"] = row[0] switch["serial"] = row[1] switch["swver"] = row[2] switch["ip"] = row[3] switch["check"] = row[4] switch["total"] = row[5] switch["up"] = row[6] switch["down"] = row[7] switch["disabled"] = row[8] if switch["total"] == 0: switch["capacity"] = 0 else: switch["capacity"] = (switch["up"] / switch["total"]) * 100 switchList.append(switch) swDB.close() return switchList
def getSwitchDetail(serial): """ Query DB for details on one specific device by serial number """ swDB = switchdb.DB() raw_info = swDB.getSwitchDetail(serial) switch = {} for row in raw_info: switch["name"] = row[0] switch["serial"] = row[1] switch["model"] = row[2] switch["swver"] = row[3] switch["ip"] = row[4] switch["check"] = row[5] switch["total"] = row[6] switch["up"] = row[7] switch["down"] = row[8] switch["disabled"] = row[9] switch["int10m"] = row[10] switch["int100m"] = row[11] switch["int1g"] = row[12] switch["int10g"] = row[13] switch["int25g"] = row[14] switch["int40g"] = row[15] switch["int100g"] = row[16] switch["copper"] = row[17] switch["sfp"] = row[18] switch["virtual"] = row[19] if switch["total"] == 0: switch["capacity"] = 0 else: switch["capacity"] = int((switch["up"] / switch["total"]) * 100) swDB.close() return switch
def deleteDevice(serial): """ Call to DB to delete a device by serial number """ swDB = switchdb.DB() swDB.deleteBySerial(serial) swDB.close()
def getSwitchInfo(): """ Query DB for summary info on all switches currently monitored """ swDB = switchdb.DB() raw_info = swDB.getAllSummary() switchList = [] for row in raw_info: row = list(row) switch = {} switch['name'] = row[0] switch['serial'] = row[1] switch['swver'] = row[2] switch['ip'] = row[3] switch['check'] = row[4] switch['total'] = row[5] switch['up'] = row[6] switch['down'] = row[7] switch['disabled'] = row[8] if switch['total'] == 0: switch['capacity'] = 0 else: switch['capacity'] = (switch['up'] / switch['total']) * 100 switchList.append(switch) swDB.close() return switchList
def getSwitchDetail(serial): """ Query DB for details on one specific device by serial number """ swDB = switchdb.DB() raw_info = swDB.getSwitchDetail(serial) switch = {} for row in raw_info: switch['name'] = row[0] switch['serial'] = row[1] switch['model'] = row[2] switch['swver'] = row[3] switch['ip'] = row[4] switch['check'] = row[5] switch['total'] = row[6] switch['up'] = row[7] switch['down'] = row[8] switch['disabled'] = row[9] switch['int10m'] = row[10] switch['int100m'] = row[11] switch['int1g'] = row[12] switch['int10g'] = row[13] switch['int25g'] = row[14] switch['int40g'] = row[15] switch['int100g'] = row[16] switch['copper'] = row[17] switch['sfp'] = row[18] switch['virtual'] = row[19] if switch['total'] == 0: switch['capacity'] = 0 else: switch['capacity'] = int((switch['up'] / switch['total']) * 100) swDB.close() return switch
def addDeviceToDB(devicelist): """ Update DB entries for each switch from the config file """ print("Opening DB connection...") swDB = switchdb.DB() # Get a list of current switches in the database # Compare between new config file - see what should be added/removed curswitches = swDB.getAllSummary() currentSwitches = [row[3] for row in curswitches] newSwitches = [devicelist[switch]["address"] for switch in devicelist] swRemove = set(currentSwitches).difference(newSwitches) swAdd = set(newSwitches).difference(currentSwitches) # If switches to remove, purge from the database if len(swRemove) > 0: print(f"Found {len(swRemove)} switches no longer in config file") for switchIP in swRemove: print(f"Removing switch ({switchIP}) from DB...") swDB.deleteSwitch(switchIP) print("Adding devices to DB...") for switch in devicelist: switchIP = devicelist[switch]["address"] if switchIP in swAdd: print(f"Adding switch ({switch} / {switchIP}) to DB...") swDB.addSwitch(str(switch), str(switchIP)) else: print(f"Switch ({switch} / {switchIP}) already in DB. Skipping...") swDB.close()
def updateLastRun(): """ Call to DB - update last run time """ swDB = switchdb.DB() print("Updating last run time in DB...") swDB.updateLastRun() swDB.close()
def getLastUpdate(): """ Check DB for last runtime of backend script This is published on the main page to see when stats were last updated """ swDB = switchdb.DB() lastupdate = swDB.getLastUpdate() swDB.close() return lastupdate
def updateCheckStatus(device, ip, status): """ Update the last_check database field, which indicates if the check passed or failed """ swDB = switchdb.DB() print(f"Updating check status for {device} to {status}") swDB.updateStatus(device, ip, status) swDB.close()
def updateDB(device, ip, sysinfo, portinfo): """ Insert new system & port information into the database """ swDB = switchdb.DB() print(f"Updating system info for {device} in DB...") swDB.updateSysInfo(device, ip, sysinfo) print(f"Updating port info for {device} in DB...") swDB.updatePorts(device, ip, portinfo) swDB.close()
def getNetworkWide(): """ Query DB for all switch statistcs, then tally results & return to web page """ swDB = switchdb.DB() result = swDB.getNetworkWideStats() swDB.close() network = { "models": [], "swvers": [], "total": 0, "up": 0, "down": 0, "disabled": 0, "int10m": 0, "int100m": 0, "int1g": 0, "int10g": 0, "int25g": 0, "int40g": 0, "int100g": 0, "copper": 0, "sfp": 0, "virtual": 0, } modellist = [] swlist = [] for row in result: if "N/A" not in row[0]: modellist.append(row[0]) if "N/A" not in row[1]: swlist.append(row[1]) network["total"] += row[2] network["up"] += row[3] network["down"] += row[4] network["disabled"] += row[5] network["int10m"] += row[6] network["int100m"] += row[7] network["int1g"] += row[8] network["int10g"] += row[9] network["int25g"] += row[10] network["int40g"] += row[11] network["int100g"] += row[12] network["copper"] += row[13] network["sfp"] += row[14] network["virtual"] += row[15] # Get 5 most common models / software versions network["models"] = Counter(modellist).most_common(5) network["swvers"] = Counter(swlist).most_common(5) return network
def getNetworkWide(): """ Query DB for all switch statistcs, then tally results & return to web page """ swDB = switchdb.DB() result = swDB.getNetworkWideStats() swDB.close() network = { 'models': [], 'swvers': [], 'total': 0, 'up': 0, 'down': 0, 'disabled': 0, 'int10m': 0, 'int100m': 0, 'int1g': 0, 'int10g': 0, 'int25g': 0, 'int40g': 0, 'int100g': 0, 'copper': 0, 'sfp': 0, 'virtual': 0 } modellist = [] swlist = [] for row in result: if 'N/A' not in row[0]: modellist.append(row[0]) if 'N/A' not in row[1]: swlist.append(row[1]) network['total'] += row[2] network['up'] += row[3] network['down'] += row[4] network['disabled'] += row[5] network['int10m'] += row[6] network['int100m'] += row[7] network['int1g'] += row[8] network['int10g'] += row[9] network['int25g'] += row[10] network['int40g'] += row[11] network['int100g'] += row[12] network['copper'] += row[13] network['sfp'] += row[14] network['virtual'] += row[15] # Get 5 most common models / software versions network['models'] = Counter(modellist).most_common(5) network['swvers'] = Counter(swlist).most_common(5) return network
def getInterfaceDetail(serial): """ Query DB for interface details on one specific device by management IP """ swDB = switchdb.DB() raw_info = swDB.getInterfaceDetail(serial) interfaceList = [] for row in raw_info: row = list(row) interface = {} interface["name"] = row[0] interface["description"] = row[1] interface["physical_address"] = row[2] interface["oper_status"] = row[3] interface["oper_speed"] = row[4] interface["oper_duplex"] = row[5] interfaceList.append(interface) return interfaceList