def _log_IEM(self): """Sends an IEM to logging msg handler""" json_data = json.dumps( { "sensor_request_type": { "node_data": { "status": "update", "sensor_type": "node:os:raid_data", "device": self._devices, "drives": self._drives } } }, sort_keys=True) # Send the event to node data message handler to generate json message and send out internal_json_msg = json.dumps({ 'actuator_request_type': { 'logging': { 'log_level': 'LOG_WARNING', 'log_type': 'IEM', 'log_msg': f'{json_data}' } } }) # Send the event to logging msg handler to send IEM message to journald self._write_internal_msgQ(LoggingMsgHandler.name(), internal_json_msg)
def _log_iem(self, json_data): """Create IEM and send to logging msg handler""" log_msg = f"IEC: 020004001: SNMP Trap Received, {self._trap_name}" internal_json_msg = json.dumps( {"actuator_request_type" : { "logging": { "log_level": "LOG_WARNING", "log_type": "IEM", "log_msg": f"{log_msg}:{json.dumps(json_data, sort_keys=True)}" } } }) # Send the event to logging msg handler to send IEM message to journald self._write_internal_msgQ(LoggingMsgHandler.name(), internal_json_msg)
def _log_IEM(self, alert_type, details, ext): """Sends an IEM to logging msg handler""" json_data = self._gen_json_msg(alert_type, details, ext) # Send the event to storage encl message handler to generate json message # and send out internal_json_msg=json.dumps( {'actuator_request_type': {'logging': {'log_level': 'LOG_WARNING', 'log_type': 'IEM', 'log_msg': f'{json_data}'} } }) # Send the event to logging msg handler to send IEM message to journald self._write_internal_msgQ(LoggingMsgHandler.name(), internal_json_msg)
def _process_msg(self, jsonMsg): """Parses the incoming message and hands off to the appropriate logger """ logger.debug(f"_process_msg, jsonMsg: {jsonMsg}") if isinstance(jsonMsg, dict) is False: jsonMsg = json.loads(jsonMsg) # Parse out the uuid so that it can be sent back in Ack message uuid = None if jsonMsg.get("sspl_ll_msg_header") is not None and \ jsonMsg.get("sspl_ll_msg_header").get("uuid") is not None: uuid = jsonMsg.get("sspl_ll_msg_header").get("uuid") logger.debug(f"_processMsg, uuid: {uuid}") # Handle service start, stop, restart, status requests if "actuator_request_type" in jsonMsg and \ "service_controller" in jsonMsg["actuator_request_type"]: logger.debug("_processMsg, msg_type: service_controller") service_name = jsonMsg.get("actuator_request_type") \ .get("service_controller").get("service_name") service_request = jsonMsg.get("actuator_request_type") \ .get("service_controller").get("service_request") request = f"{service_request}:{service_name}" if service_name not in self.monitored_services: logger.error(f"{service_name} - service not monitored") msg = ("Check if supplied service name is valid, %s is not " "monitored or managed." % service_name) self.send_error_response(service_request, service_name, msg, errno.EINVAL) return elif service_request not in ["disable", "enable"]: status = self._dbus_service.is_enabled(service_name) if status == "disabled": logger.error(f"{service_name} - service is disabled") msg = ("%s is disabled, enable request needed before " "current - %s request can be processed." % (service_name, service_request)) self.send_error_response(service_request, service_name, msg, errno.EPERM) return # If the state is INITIALIZED, We can assume that actuator is # ready to perform operation. if actuator_state_manager.is_initialized("Service"): logger.debug(f"_process_msg, service_actuator name: \ {self._service_actuator.name()}") self._execute_request(self._service_actuator, jsonMsg, uuid) # If the state is INITIALIZING, need to send message elif actuator_state_manager.is_initializing("Service"): # This state will not be reached. Kept here for consistency. logger.info("Service actuator is initializing") self.send_error_response(service_request, service_name, \ "BUSY - Service actuator is initializing.", errno.EBUSY) elif actuator_state_manager.is_imported("Service"): # This case will be for first request only. Subsequent # requests will go to INITIALIZED state case. logger.info("Service actuator is imported and initializing") from actuators.IService import IService actuator_state_manager.set_state( "Service", actuator_state_manager.INITIALIZING) service_actuator_class = self._query_utility(IService) if service_actuator_class: # NOTE: Instantiation part should not time consuming # otherwise ServiceMsgHandler will get block and will # not be able serve any subsequent requests. This applies # to instantiation of evey actuator. self._service_actuator = service_actuator_class() logger.info(f"_process_msg, service_actuator name: \ {self._service_actuator.name()}") self._execute_request(self._service_actuator, jsonMsg, uuid) actuator_state_manager.set_state( "Service", actuator_state_manager.INITIALIZED) else: logger.info("Service actuator is not instantiated") # If there is no entry for actuator in table, We can assume # that it is not loaded for some reason. else: logger.warn("Service actuator is not loaded or not supported") # Handle events generated by the service monitor elif "sensor_request_type" in jsonMsg and \ "service_status_alert" in jsonMsg["sensor_request_type"]: logger.debug(f"Received alert from ServiceMonitor : {jsonMsg}") jsonMsg1 = ServiceMonitorMsg( jsonMsg["sensor_request_type"]).getJson() self._write_internal_msgQ("EgressProcessor", jsonMsg1) # Create an IEM if the resulting service state is failed specific_info = \ jsonMsg['sensor_request_type']['service_status_alert']['specific_info'] state = specific_info['state'] if state == "failed": internal_json_msg = json.dumps( {"actuator_request_type" : { "logging": { "log_level": "LOG_WARNING", "log_type": "IEM", "log_msg": "IEC: 020003001: Service entered a "\ "Failed state : %s" % (json.dumps(specific_info, sort_keys=True)) } } }) # Send the event to logging msg handler to send IEM message to journald self._write_internal_msgQ(LoggingMsgHandler.name(), internal_json_msg)
def _process_msg(self, body): """Parses the incoming message and hands off to the appropriate module""" try: # Encode and remove blankspace,\n,\t - Leaving as it might be useful # ingressMsgTxt = json.dumps(body, ensure_ascii=True).encode('utf8') # ingressMsg = json.loads(' '.join(ingressMsgTxt.split())) # Enable debugging if it's found in the message if "debug" in body.lower(): self._set_debug(True) # Try encoding message to handle escape chars if present try: log_msg = body.encode('utf8') except Exception as de: logger.debug( "_process_msg, no encoding applied, writing to syslog") log_msg = body # See if there is log level at the beginning log_level = log_msg[0: log_msg.index(" ")] if LOGLEVELS.get(log_level) is not None: priority = LOGLEVELS[log_level] log_msg = log_msg[log_msg.index(" "):] else: priority = LOG_INFO # Default to info log level log_level = "LOG_INFO" # See if there is an id available event_code = None try: event_code_start = log_msg.index("IEC:") + 4 event_code_stop = log_msg.index(":", event_code_start) # Parse out the event code and remove any blank spaces event_code = log_msg[event_code_start: event_code_stop].strip() self._log_debug("log_msg, event_code: %s" % event_code) except Exception as e: # Log message has no IEC to use as message_id in journal, ignoring self._log_debug( 'Log message has no IEC to use as message_id in journal, ignoring: error: {}'.format( e)) # Not an IEM so just dump it to the journal and don't worry about email and routing back to CMU if event_code is None: if use_journal: journal.send(log_msg, MESSAGE_ID=event_code, PRIORITY=priority, SYSLOG_IDENTIFIER="sspl-ll") else: logger.info(log_msg) else: # Send the IEM to the logging msg handler to be processed internal_json_msg = json.dumps( {"actuator_request_type": { "logging": { "log_level": log_level, "log_type": "IEM", "log_msg": log_msg } } }) # Send the event to logging msg handler to send IEM message to journald self._write_internal_msgQ(LoggingMsgHandler.name(), internal_json_msg) except Exception as ex: logger.error("_process_msg: %r" % ex)
def _process_msg(self, jsonMsg): """Parses the incoming message and hands off to the appropriate logger """ self._log_debug(f"_process_msg, jsonMsg: {jsonMsg}") if isinstance(jsonMsg, dict) is False: jsonMsg = json.loads(jsonMsg) # Parse out the uuid so that it can be sent back in Ack message uuid = None if jsonMsg.get("sspl_ll_msg_header") is not None and \ jsonMsg.get("sspl_ll_msg_header").get("uuid") is not None: uuid = jsonMsg.get("sspl_ll_msg_header").get("uuid") self._log_debug(f"_processMsg, uuid: {uuid}") # Handle service start, stop, restart, status requests if jsonMsg.get("actuator_request_type").get( "service_controller") is not None: self._log_debug("_processMsg, msg_type: service_controller") service_name = jsonMsg.get("actuator_request_type") \ .get("service_controller").get("service_name") service_request = jsonMsg.get("actuator_request_type") \ .get("service_controller").get("service_request") request = f"{service_request}:{service_name}" # If the state is INITIALIZED, We can assume that actuator is # ready to perform operation. if actuator_state_manager.is_initialized("Service"): self._log_debug( f"_process_msg, service_actuator name: {self._service_actuator.name()}" ) self._execute_request(self._service_actuator, jsonMsg, uuid) # If the state is INITIALIZING, need to send message elif actuator_state_manager.is_initializing("Service"): # This state will not be reached. Kept here for consistency. logger.info("Service actuator is initializing") busy_json_msg = AckResponseMsg(request, "BUSY", uuid, error_no=errno.EBUSY).getJson() self._write_internal_msgQ("RabbitMQegressProcessor", busy_json_msg) elif actuator_state_manager.is_imported("Service"): # This case will be for first request only. Subsequent # requests will go to INITIALIZED state case. logger.info("Service actuator is imported and initializing") from actuators.IService import IService actuator_state_manager.set_state( "Service", actuator_state_manager.INITIALIZING) service_actuator_class = self._query_utility(IService) if service_actuator_class: # NOTE: Instantiation part should not time consuming # otherwise ServiceMsgHandler will get block and will # not be able serve any subsequent requests. This applies # to instantiation of evey actuator. self._service_actuator = service_actuator_class() logger.info( f"_process_msg, service_actuator name: {self._service_actuator.name()}" ) self._execute_request(self._service_actuator, jsonMsg, uuid) actuator_state_manager.set_state( "Service", actuator_state_manager.INITIALIZED) else: logger.info("Service actuator is not instantiated") # If there is no entry for actuator in table, We can assume # that it is not loaded for some reason. else: logger.warn("Service actuator is not loaded or not supported") # Handle events generated by the service watchdogs elif jsonMsg.get("actuator_request_type").get( "service_watchdog_controller") is not None: self._log_debug( "_processMsg, msg_type: service_watchdog_controller") # Parse out values to be sent service_name = jsonMsg.get("actuator_request_type").get( "service_watchdog_controller").get("service_name") state = jsonMsg.get("actuator_request_type").get( "service_watchdog_controller").get("state") prev_state = jsonMsg.get("actuator_request_type").get( "service_watchdog_controller").get("previous_state") substate = jsonMsg.get("actuator_request_type").get( "service_watchdog_controller").get("substate") prev_substate = jsonMsg.get("actuator_request_type").get( "service_watchdog_controller").get("previous_substate") pid = jsonMsg.get("actuator_request_type").get( "service_watchdog_controller").get("pid") prev_pid = jsonMsg.get("actuator_request_type").get( "service_watchdog_controller").get("previous_pid") # Pull out the service_request and if it's equal to "status" then get current status (state, substate) service_request = jsonMsg.get("actuator_request_type").get( "service_watchdog_controller").get("service_request") if service_request != "None": # Query the Zope GlobalSiteManager for an object implementing IService if self._service_actuator is None: from actuators.IService import IService self._service_actuator = self._query_utility(IService)() self._log_debug( f"_process_msg, service_actuator name: {self._service_actuator.name()}" ) service_name, state, substate = self._service_actuator.perform_request( jsonMsg) self._log_debug( f"_processMsg, service_name: {service_name}, state: {state}, substate: {substate}" ) self._log_debug( f"_processMsg, prev state: {prev_state}, prev substate: {prev_substate}" ) # Create a service watchdog message and send it out jsonMsg = ServiceWatchdogMsg(service_name, state, prev_state, substate, prev_substate, pid, prev_pid).getJson() self._write_internal_msgQ("RabbitMQegressProcessor", jsonMsg) # Create an IEM if the resulting service state is failed if "fail" in state.lower() or \ "fail" in substate.lower(): json_data = { "service_name": service_name, "state": state, "previous_state": prev_state, "substate": substate, "previous_substate": prev_substate, "pid": pid, "previous_pid": prev_pid } internal_json_msg = json.dumps({ "actuator_request_type": { "logging": { "log_level": "LOG_WARNING", "log_type": "IEM", "log_msg": f"IEC: 020003001: Service entered a Failed state : {json.dumps(json_data, sort_keys=True)}" } } }) # Send the event to logging msg handler to send IEM message to journald self._write_internal_msgQ(LoggingMsgHandler.name(), internal_json_msg)