def writer_loop(): """ The main loop. $HOST_FROM,$FACILITY,$PRIORITY,$LEVEL,$TAG,$PROGRAM,$ISODATE,$MSG Please see http://www.balabit.com/sites/default/files/documents/syslog-ng-admin-guide_en.html/reference_macros.html for a description of the macros used above. """ dbconn = DatabaseConnection() NODES = {} rows = dbconn.select("SELECT hostname, id FROM nm_node") for row in rows: NODES[row[0]] = row[1] while True: in_line = sys.stdin.readline() if not in_line: #EOF occured break host, facility, priority, level, tag, program, isodate, msg = in_line.split('[-]') host = host.strip() node_id = NODES.get(host, None) if node_id is None: rows = dbconn.select("SELECT id FROM nm_node WHERE hostname=%s",(host,)) if rows: NODES[host] = rows[0][0] node_id = rows[0][0] dbconn.modify("INSERT INTO logs (node_id, host, facility, priority, level, tag, program, log_timestamp, msg) \ VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)", (node_id, host, facility.strip(), priority.strip(), level.strip(), tag.strip(), program.strip(), isodate.strip(), msg.strip()))
def test_02_simulate_call(self): dbconn = DatabaseConnection() caller = friBase.FriCaller() packet = { 'uuid': '32542523esdf23r32r3fr', 'hostname': 'test_node_01', 'ip_address': '193.13.12.22', 'mac_address': '34:34:34:34:34:34', 'login': '******', 'password': '******', 'processor': 'Intel i7 3.4Ghz', 'memory': '4023' } code,msg = caller.call('127.0.0.1', packet, 1986) self.assertEqual(code, 0, msg) rows = dbconn.select("SELECT hostname FROM nm_node WHERE node_uuid='32542523esdf23r32r3fr'") self.assertEqual(len(rows), 1) self.assertEqual(rows[0][0], 'test_node_01') #change params and call packet['ip_address'] = '123.23.123.33' code,msg = caller.call('127.0.0.1', packet, 1986) self.assertEqual(code, 0, msg) rows = dbconn.select("SELECT ip_address, login FROM nm_node WHERE node_uuid='32542523esdf23r32r3fr'") self.assertEqual(len(rows), 1) self.assertEqual(rows[0][0], '123.23.123.33') self.assertEqual(rows[0][1], 'fabregas') #ip_address is optional del packet['ip_address'] code,msg = caller.call('127.0.0.1', packet, 1986) self.assertEqual(code, 0, msg) #ip_address is optional del packet['login'] code,msg = caller.call('127.0.0.1', packet, 1986) self.assertNotEqual(code, 0, msg)
class NodesMonitor(threading.Thread): def __init__(self): self.__monitor_timeout = Config.nodes_monitor_timeout self.__threads = [] self.__nodes_queue = Queue() self.__dbconn = DatabaseConnection() self.__stoped = False for i in xrange(Config.monitor_workers_count): thread = MonitorWorkerThread(self.__nodes_queue, Config.monitor_wait_response_timeout) thread.setName('MonitorWorkerThread#%i'%i) thread.start() self.__threads.append(thread) threading.Thread.__init__(self, name='NodesMonitor') def stop(self): if self.__stoped: return self.__stoped = True for i in self.__threads: self.__nodes_queue.put(FINISH_FLAG) self.__nodes_queue.join() def run(self): logger.info('NodesMonitor started!') while not self.__stoped: try: t0_point = datetime.now() rows = self.__dbconn.select("SELECT N.hostname, N.current_state FROM nm_node N, nm_cluster C \ WHERE N.cluster_id=C.id AND N.admin_status=%s AND C.status=%s", (ANS_ACTIVE, CS_ACTIVE)) logger.debug('NodesMonitor: Selected %i nodes for checking state'%len(rows)) for row in rows: self.__nodes_queue.put((row[0], row[1])) self.__nodes_queue.join() except Exception, err: logger.error('NodesMonitor failed: %s' % err) finally:
def test_02_simulate_node(self): db = DatabaseConnection() state = db.modify("UPDATE nm_node SET current_state=0 WHERE hostname='127.0.0.1'") # shutdown node :) node = NodeSimulator() def start_server(node): node.start() thread.start_new_thread(start_server, (node,)) try: time.sleep(1.5) state = db.select("SELECT current_state FROM nm_node WHERE hostname='127.0.0.1'")[0][0] self.assertEqual(state, 1) # ON node.stop() time.sleep(1.5) state = db.select("SELECT current_state FROM nm_node WHERE hostname='127.0.0.1'")[0][0] self.assertEqual(state, 0) # OFF finally: node.stop() print "STOPED"
class BootEventListener(FriServer): def __init__(self): self.__dbconn = DatabaseConnection() FriServer.__init__(self, hostname='0.0.0.0', port=LISTENER_PORT, workers_count=1) def onDataReceive( self, json_object ): #get hostname hostname = self.__get_element(json_object, 'hostname') #get ip_address (this field is optional) ip_address = json_object.get('ip_address', None) #get login login = self.__get_element(json_object, 'login') #get password password = self.__get_element(json_object, 'password') #get processor processor = self.__get_element(json_object, 'processor') #get memory memory = self.__get_element(json_object, 'memory') #get mac_address mac_address = self.__get_element(json_object, 'mac_address') #get uuid uuid = self.__get_element(json_object, 'uuid') #process boot event self.__process_event(uuid, hostname, login, password, mac_address, ip_address, processor, memory) def __get_element(self, json_object, name): element = json_object.get(name, None) if element is None: raise Exception('Element <%s> is not found in boot event packet!'%name) if type(element) == str: element = element.strip() return element def __process_event(self, uuid, hostname, login, password, mac_address, ip_address, processor, memory): hw_info = 'Processor: %s\nMemory: %s'%(processor, memory) rows = self.__dbconn.select("SELECT hostname FROM nm_node WHERE node_uuid=%s", (uuid,)) if not rows: #this is new node, create it in database in NEW status self.__dbconn.modify("INSERT INTO nm_node (node_uuid, hostname, login, password, mac_address, ip_address, admin_status, current_state, hw_info)\ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", (uuid, hostname, login, password, mac_address, ip_address, NAS_NEW, NCS_UP, hw_info)) else: #we already has this node in database, update it self.__dbconn.modify("UPDATE nm_node SET hostname=%s, login=%s, password=%s, mac_address=%s, ip_address=%s, hw_info=%s, current_state=%s\ WHERE node_uuid=%s", (hostname, login, password, mac_address, ip_address, hw_info, NCS_UP, uuid)) caller = self._get_operation_caller() if caller: logger.debug('Synchronize %s node parameters'%hostname) ret_code, ret_message = caller.call_nodes_operation(ADMIN, [hostname], SYNC_OPER, {}) logger.debug('call SYNC operation result: [%s] %s'%(ret_code, ret_message)) def _get_operation_caller(self): #try import nodes manager caller try: from blik.nodesManager.dbusClient import DBUSInterfaceClient client = DBUSInterfaceClient() return client except ImportError, err: logger.warning('Boot manager require nodes manager for automatic changing hostname.') return None