def run(self): #database.lock(self.data['SOURCEIP']) varBinds, err = snmp.get(self.conn, snmp.sysObjectID) if err: database.release(self.data['SOURCEIP']) return False else: sprint("Start Thread for device", self.data['SOURCEIP'], varBinds[0][1].prettyPrint()) if varBinds[0][1][6] == 9: backup_class = BackupDeviceCISCO elif varBinds[0][1][6] == 890: backup_class = BackupDeviceZyXEL else: eprint("Unknown device vendor", self.data['SOURCEIP'], varBinds[0][1].prettyPrint()) database.update_vendor_oid(self.data['SOURCEIP'], str(varBinds[0][1].prettyPrint())) database.release(self.data['SOURCEIP']) return False with backup_class(varBinds, self.data, self.conn) as backup: if backup.run(): if backup.save() and config.compare: backup.compare() database.clear(self.data['SOURCEIP']) else: eprint("Backup", self.data['SOURCEIP'], "error") database.release(self.data['SOURCEIP'])
def getInterfaceDesc(self, interface): snmp = self.snmp oid = self.oid # <interface descriptions OID><interface number> is what we're looking for desc = snmp.get(oid['if']['ifdesc'] + str(interface)) logger.info("Returning interface description %s", desc) return desc
def get_values(conn, obj): varBinds, Err = snmp.get(conn, obj) sprint(len(varBinds)) for varBind in varBinds: name, value = varBind sprint(varBind, 'Name:', name.items(), 'Val:', value.prettyPrint()) values = {} return {}
def getInterfaceName(self, interface): snmp = self.snmp oid = self.oid # <interface names OID><interface number> is what we're looking for name = snmp.get(oid['if']['ifname'] + str(interface)) if name: interface = name logger.info("Returning interface name %s", interface) return interface
def wait(self, obj, idx): ot = ObjectType(ObjectIdentity(self.mib, obj, idx)) cs, err = snmp.get(self.conn, ot) if err: return False sprint("init", self.data['SOURCEIP'], self.mib, cs[0].prettyPrint()) i = server.save_timeout // 2 while i > 0 and cs[0][1] == self.cs_waiting and not err: time.sleep(1) i -= 1 cs, err = snmp.get(self.conn, ot) sprint("wait", i, self.data['SOURCEIP'], self.mib, cs[0].prettyPrint()) if err: return False if i <= 0: eprint("Download config", self.data['SOURCEIP'], self.mib, 'did not start in timeout!') i = server.save_timeout while i > 0 and cs[0][1] == self.cs_running and not err: time.sleep(1) i -= 1 cs, err = snmp.get(self.conn, ot) sprint("downloading", i, self.data['SOURCEIP'], self.mib, cs[0].prettyPrint()) if err: return False if i <= 0: eprint("Download config", self.data['SOURCEIP'], self.mib, 'timeout!') elif cs[0][1] == self.cs_successful: if os.path.getsize(self.srv_path) <= 0: eprint("Downloaded config", self.data['SOURCEIP'], self.mib, 'size zero!') return False return True else: eprint("Save config", self.data['SOURCEIP'], self.mib, 'exit with', cs[0][1].prettyPrint()) return False
def get_neighbors(ip, lvl, prn): conn = snmp.ConnectionData(ip) obj = ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)) varBinds, err = snmp.get(conn, obj) if err: sys.exit(1) sprint('Scan:', ip, varBinds[0][1].prettyPrint()) sprint('Get:', ip, 'NeighborIpAddress', '...') mtxrNeighborIpAddress = get_values( conn, ObjectType(ObjectIdentity('MIKROTIK-MIB', 'mtxrNeighborIpAddress', 5)))
def getInterfaceSpeed(self, interface, user_format='M'): snmp = self.snmp oid = self.oid speed = None divide = {'G': 1000000000, 'M': 1000000, 'K': 1000, 'B': 1} if user_format.upper() not in divide: user_format = 'M' # <interface speeds OID><interface number> is what we're looking for speed = snmp.get(oid['if']['ifspeed'] + str(interface)) if speed: speedInBits = int(speed) speed = speedInBits / divide[user_format.upper()] logger.info("Returning interface speed %s", speed) return speed
def getInterfaceSpeed(self, interface, format='M'): snmp = self.snmp oid = self.oid speed = None divide = {'G': 1000000000, 'M': 1000000, 'K': 1000, 'B': 1} if format.upper() not in divide: format = 'M' # <interface speeds OID><interface number> is what we're looking for speed = snmp.get(oid['if']['ifspeed'] + str(interface)) if speed: speedInBits = int(speed) speed = speedInBits / divide[format.upper()] logger.info("Returning interface speed %s", speed) return speed
def loop_monitor(self): """ Monitoring loop """ logging.debug('loop_monitor for ' + `self.target`) while 1: try: if not self.poll_routes(): return if snmp.get(self.target, snmp.load_symbol('IF-MIB', 'ifAdminStatus') + (self.if_index,))[0][1] != 1: logging.info(`self.target` + ' interface ' + self.if_name + ' is inactive') sleep(5) continue break except Exception, e: logging.debug('Could not check interface status: ' + `e`) sleep(5) continue