def test_from_json(): init() json_string = '{"timestamp":"Sat Oct 11 17:13:46 UTC 2003", "priority": "info","publisher": "node_context", "payload" : { "message": "TestMessage"}}' msg = Message.from_json(json_string) assert msg.priority == "info" json_string = '{"timestamp":"Sat Oct 11 17:13:46 UTC 2003", "priority": "None","publisher": "node_context", "payload" : { "message": "TestMessage"}}' msg = Message.from_json(json_string) assert msg.priority == "debug"
def read_socket(self, sock, *args): try: size = self._msg_length(sock) data = self._read(sock, size) frmt = "=%ds" % size msg = struct.unpack(frmt, data) message = Message.from_json(msg[0]) # Logger is in commons so passing alert from here alert_conditions = [ "alert_condition_status", "alert_condition_state", "alert_condition_unset" ] if message.priority == NOTICE_PRIORITY: alert = True for alert_condition in alert_conditions: if alert_condition not in message.payload: alert = False break if alert: update_alert(message) Logger(message) except (socket.error, socket.timeout): exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_tb, file=sys.stderr) except (TypeError, ValueError, KeyError, AttributeError): sys.stderr.write("Unable to log the message.%s\n" % data) exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_tb, file=sys.stderr)
def read_socket(self, sock, address): try: self.data = sock.recv(RECEIVE_DATA_SIZE) message = Message.from_json(self.data) Logger(message) except (socket_error, socket_timeout): exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception( exc_type, exc_value, exc_tb, file=sys.stderr) except (TypeError, ValueError, KeyError, AttributeError): sys.stderr.write( "Unable to log the message.%s\n" % self.data) exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception( exc_type, exc_value, exc_tb, file=sys.stderr)
def __init__(self, message, socket_path=None): if message.publisher == "node_agent": try: json_str = Message.to_json(message) message = Message.from_json(json_str) Logger(message) except (TypeError, ValueError, KeyError, AttributeError): sys.stderr.write( "Unable to log the message.%s\n" % message) exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception( exc_type, exc_value, exc_tb, file=sys.stderr) else: self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.socket_path = socket_path if self.socket_path is None: self.socket_path = NS.config.data['logging_socket_path'] self._write(message)
def __init__(self, message, socket_path=None): # node_agent log messages other than notice # priority are directly pushed into logger if message.publisher == "node_agent" and \ message.priority != "notice": try: json_str = Message.to_json(message) message = Message.from_json(json_str) Logger(message) except (TypeError, ValueError, KeyError, AttributeError): sys.stderr.write( "Unable to log the message.%s\n" % message) exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception( exc_type, exc_value, exc_tb, file=sys.stderr) else: self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.socket_path = socket_path if self.socket_path is None: self.socket_path = NS.config.data['logging_socket_path'] self._write(message)