class Cleaner(ZEyeUtil.Thread): zeyeDB = None radiusList = {} subnetList = {} ipList = {} def __init__(self): """ 2 min between two syncs updates """ self.sleepingTimer = 86400 self.myName = "DHCP Cleaner" ZEyeUtil.Thread.__init__(self) def run(self): self.launchMsg() while True: self.setRunning(True) self.cleanOldDHCPEntries() self.setRunning(False) # We launch cleanup tommorow at midnight tommorow = datetime.datetime.now().replace(hour=0,minute=0,second=0) + datetime.timedelta(days=1) tommorow_interval = int(tommorow.strftime('%s')) - int(datetime.datetime.now().strftime('%s')) time.sleep(tommorow_interval) def cleanOldDHCPEntries(self): try: self.zeyeDB = ZEyeSQLMgr() self.zeyeDB.initForZEye() # Cleanup old reservations self.zeyeDB.Delete("z_eye_dhcp_ip","expiration_date < now()") self.zeyeDB.Commit() except Exception, e: self.logCritical(e) sys.exit(1); finally:
class RadiusSyncer(ZEyeUtil.Thread): zeyeDB = None radiusList = {} subnetList = {} ipList = {} def __init__(self): """ 2 min between two syncs updates """ self.sleepingTimer = 120 self.myName = "DHCP/Radius Sync" ZEyeUtil.Thread.__init__(self) def run(self): self.launchMsg() while True: self.setRunning(True) self.syncDHCPAndRadius() self.setRunning(False) def syncDHCPAndRadius(self): try: self.zeyeDB = ZEyeSQLMgr() self.zeyeDB.initForZEye() pgres = self.zeyeDB.Select("z_eye_radius_dhcp_import","dbname,addr,port,groupname,dhcpsubnet") if self.zeyeDB.getRowCount() > 0: # Buffer for better performances self.loadRadiusList() self.loadIPList() self.loadSubnetList() for idx in pgres: if len(idx[0]) > 0 and len(idx[1]) > 0 and idx[2] != 0 and len(idx[3]) > 0 and len(idx[4]) > 0: thread.start_new_thread(self.doSyncDHCPRadius,(idx[0],idx[1],idx[2],idx[3],idx[4])) except Exception, e: self.logCritical(e) sys.exit(1); finally:
def doSyncDHCPRadius(self,dbname,addr,port,groupname,subnet): self.incrThreadNb() # We test if Radius has been loaded if addr not in self.radiusList or port not in self.radiusList[addr] or dbname not in self.radiusList[addr][port]: self.logError("Warning %s cannot be sync with radius (%s:%s/%s). Radius not exists" % (subnet,addr,port,dbname)) self.decrThreadNb() return dbtype = self.radiusList[addr][port][dbname][2] login = self.radiusList[addr][port][dbname][0] pwd = self.radiusList[addr][port][dbname][1] zdbMgr = ZEyeSQLMgr() try: self.logDebug("sync subnet '%s' with '%s:%s/%s'" % (subnet,addr,port,dbname)) if zdbMgr.initForZEye() == False: self.logError("unable to connect to Z-Eye database" % (addr,port,dbname)) self.decrThreadNb() return radDBMgr = ZEyeSQLMgr() if radDBMgr.configAndTryConnect(dbtype,addr,port,dbname,login,pwd) == False: self.logError("unable to connect to %s:%s/%s" % (addr,port,dbname)) self.decrThreadNb() return # We must get the MAX id to create the new entries maxId = radDBMgr.GetMax("radcheck","id") if maxId == None: maxId = 1 for mac in self.subnetList[subnet]["mac"]: maxId += 1 # We must improve it to don't create already existing entries radDBMgr.Delete("radusergroup","username = '******'" % mac) radDBMgr.Delete("radcheck","username = '******'" % mac) radDBMgr.Insert("radusergroup","username,groupname,priority","'%s','%s','0'" % (mac,groupname)) radDBMgr.Insert("radcheck","id,username,attribute,op,value","'%s','%s','Auth-Type',':=','Accept'" % (maxId,mac)) radDBMgr.Commit() self.logDebug("sync subnet '%s' with '%s:%s/%s' finished" % (subnet,addr,port,dbname)) radDBMgr.close() zdbMgr.close() except Exception, e: self.logCritical("doSyncDHCPRadius %s" % e)