# -*-coding:utf-8-*- import threading import time import commands import resource from xml.dom.minidom import parseString from xml.dom.minidom import getDOMImplementation from common_utils import get_neokylinha_status status_message = {} mutex = threading.Lock() class ErrorInfoThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.__monitor = threading.Lock() self.__exclude = threading.Lock() self.readers = 0 def acquire_read(self): with self.__monitor: self.readers += 1 if self.readers == 1: return self.__exclude.acquire() return False def release_read(self): with self.__monitor: self.readers -= 1 if self.readers == 0: return self.__exclude.release() return False def acquire_write(self): return self.__exclude.acquire() def release_write(self): return self.__exclude.release() def run(self): while (1): global status_message time.sleep(5) # 加入写锁 if self.acquire_write(): try: neokylinha_status = get_neokylinha_status() if neokylinha_status['action']: status, output = commands.getstatusoutput("crm_mon -1 --as-xml") failures_xml = parseString(output).documentElement for failures in failures_xml.getElementsByTagName('failures'): status_message = {} for failure in failures.getElementsByTagName('failure'): failure_str = failure.toprettyxml() op_key = failure.getAttribute('op_key') op_key_list = op_key.split('_') rsc_strs = op_key_list[0:-2] rsc_id = "" for rsc_str in rsc_strs: rsc_str = rsc_str + "_" rsc_id += rsc_str # 去除最后的"_" rsc_id = rsc_id[0:-1] if rsc_id in status_message: status_message[rsc_id].append(failure_str) else: status_message[rsc_id] = [] status_message[rsc_id].append(failure_str) self.release_write() except Exception: self.release_write() #print "Get rsc error info failed" if __name__ == "__main__": # my_thread = ErrorInfoThread() # my_thread.start() all_rsc = resource.get_all_rsc() for rsc in all_rsc: if 'subrscs' in rsc: for subrsc in rsc['subrscs']: rsc_id = subrsc['id'] status_message[rsc_id] = resource.get_rsc_error_info(rsc_id) rsc_id = rsc['id'] status_message[rsc_id] = resource.get_rsc_error_info(rsc_id) print status_message