def stopMonitorVm(self, vmname): """ # 当界面发出停止该虚拟机指令时调用 :param vmname: :return: """ # 如果此线程存活中,发出命令关闭他 try: if self.threadsVm[vmname].isAlive(): # 更改全局执行与中断命令判断表,迫使子线程结束 breakLock = ThreadCmd.getBreakLock() breakLock.acquire() ebList = ThreadCmd.getEBList() ebList[vmname] = True breakLock.release() logger.info("等待对虚拟机" + vmname + "的监控结束") else: logger.warning("对虚拟机" + vmname + "的监控未在进行") logger.debug("线程" + str(self.threadsVm[vmname]) + "状态:" + ("存活" if self.threadsVm[vmname].isAlive() else "死亡")) except KeyError: logger.warning("对虚拟机" + vmname + "的监控未在进行")
def __init__(self): #self.vms = [] self.getVms() self.vmsConfs = {} self.vmsStates = {} for vm in self.vms: self.vmsConfs[vm] = VmConf(vm) #.vmsStates[vm] = VmState(vm) # 创建local对象,用来管理各个虚拟机 self.localVm = threading.local() # 保存各线程的列表 self.threadsVm = {} # 保存各线程名称的列表 #self.threadsName = [] # 获得读写锁 breakLock = ThreadCmd.getBreakLock() # 初始化命令表 breakLock.acquire() ebList = ThreadCmd.getEBList() for vm in self.vms: ebList[vm] = False breakLock.release()
def startMonitor(self): """ #死循环监视自身 :return: """ logger.info("开始监控虚拟机" + self.name) breakLock = ThreadCmd.getBreakLock() self.vmHist.clearHistory() while True: breakLock.acquire() ebList = ThreadCmd.getEBList() if not ebList[self.name]: # 没有通知关闭线程,立刻释放锁 breakLock.release() # 根据配置获取数据填入vm if not self.vmInsp.getNeedData(self.name, self.vm, self.vmConf): continue logger.debug(str(self.vm)) else: # 如果通知关闭线程,更改通知量,释放锁后跳出循环 ebList[self.name] = False breakLock.release() break breakLock.acquire() ebList = ThreadCmd.getEBList() if not ebList[self.name]: # 没有通知关闭线程,立刻释放锁 breakLock.release() # 分析数据 self.vmAnal.analyseData(self.vm, self.vmConf) else: # 如果通知关闭线程,释放锁后跳出循环 ebList[self.name] = False breakLock.release() break breakLock.acquire() ebList = ThreadCmd.getEBList() if not ebList[self.name]: # 没有通知关闭线程,立刻释放锁 breakLock.release() # 生成处理策略 self.policy = self.vmAnal.getPolicy() else: # 如果通知关闭线程,释放锁后跳出循环 ebList[self.name] = False breakLock.release() break breakLock.acquire() ebList = ThreadCmd.getEBList() if not ebList[self.name]: # 没有通知关闭线程,立刻释放锁 breakLock.release() # 根据历史操作和策略对vm执行相应的操作,并记录在历史操作中 self.vmExec.execute(self.name, self.vmConf.ip, self.vm, self.vmHist, self.policy) else: # 如果通知关闭线程,释放锁后跳出循环 ebList[self.name] = False breakLock.release() break logger.info("虚拟机" + self.name + "监控完毕") return while True: #根据配置获取数据填入vm self.vmInsp.getNeedData(self.name, self.vm, self.vmConf) #分析数据 self.vmAnal.analyseData(self.vm, self.vmConf) #生成处理策略 self.policy = self.vmAnal.getPolicy() #根据历史操作和策略对vm执行相应的操作,并记录在历史操作中 self.vmExec.execute(self.name, self.vmConf.ip, self.vm, self.vmHist, self.policy)