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 init(self, msg): sprint('Initialise BACKUP instance', msg) if not os.path.exists('/usr/local/share/snmp/mibs'): os.mkdir('/usr/local/share/snmp/mibs') if not os.path.exists('/usr/local/share/snmp/pymibs'): os.mkdir('/usr/local/share/snmp/pymibs') database.clear_locks() return True
def setup(self, data): for obj, idx, value in data: sprint("def setup", obj, idx, value) ot = ObjectType(ObjectIdentity(self.mib, obj, idx), value) result, err = snmp.set(self.conn, ot) if err: return None return True
def do_compare(msg, opt=[]): l0ip = get_l0ip(msg['SOURCEIP']) diff_folder = folder.format(syslog=msg, l0ip=l0ip) diff_path = get_diff_path(msg, config_folder=diff_folder, l0ip=l0ip) file_first, file_last = get_first_last(diff_folder) if (file_last is None): return False elif same(file_first, file_last, opt): if os.path.exists(diff_path): os.remove(diff_path) return False diff_cmd = _diff_out + opt + [file_first, file_last] sprint("Generate difference", diff_path) with open(diff_path, 'w') as df: diff = Popen(diff_cmd, stdout=df) diff.communicate() if diff.returncode > 1: raise DiffError(' '.join(diff_cmd)) return True os.chmod(diff_path, 0o664) os.chown(diff_path, -1, 4)
def do_save(src, msg, opt): l0ip = get_l0ip(msg['SOURCEIP']) dst_folder = get_config_folder(msg, l0ip=l0ip) if not os.path.exists(dst_folder): os.makedirs(dst_folder) os.chmod(dst_folder, 0o775) os.chown(dst_folder, -1, 4) n = 0 dst = get_config_path(msg, config_folder=dst_folder, l0ip=l0ip) if no_duplicates: file_first, file_last = get_first_last(dst_folder) if file_first is not None: if file_last is None: file_test = file_first else: file_test = file_last if same(file_test, src, opt): sprint("backup", msg['SOURCEIP'], "complete, configs are same") return False sprint("backup", msg['SOURCEIP'], 'complete', dst) copyfile(src, dst) os.chmod(dst, 0o664) os.chown(dst, -1, 4) return True
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 deinit(self): sprint('DeInitialise BACKUP instance') database.clear_locks() if database.save_on_exit: database.save return True
def __init__(self): sprint('Create BACKUP instance') self.thread = BackupThread()
def run(self, args): sprint("add", args['SOURCEIP'], "to queue") # TODO: add hits to table to count trys BackupDevice(args).start() return True
def deinit(self): sprint('DeInitialise NOTIFY instance') if database.save_on_exit: database.save return True
def init(self, msg): sprint('Initialise BACKUP instance', msg) return True
def __init__(self): sprint('Create NOTIFY instance')