Example #1
0
def test_resolve_host():
    """Test all posible resolutions of :meth:`BaseGELFHandler._resolve_host`"""
    assert socket.gethostname() == BaseGELFHandler._resolve_host(False, None)
    assert socket.getfqdn() == BaseGELFHandler._resolve_host(True, None)
    assert socket.getfqdn() == BaseGELFHandler._resolve_host(True, "localhost")
    assert "localhost" == BaseGELFHandler._resolve_host(False, "localhost")
    assert "" == BaseGELFHandler._resolve_host(False, "")
Example #2
0
def test_resolve_host():
    """Test all posible resolutions of :meth:`BaseGELFHandler._resolve_host`"""
    assert socket.gethostname() == BaseGELFHandler._resolve_host(False, None)
    assert socket.getfqdn() == BaseGELFHandler._resolve_host(True, None)
    assert socket.gethostname() == BaseGELFHandler._resolve_host(True, "localhost")
    assert "localhost" == BaseGELFHandler._resolve_host(False, "localhost")
    assert "" == BaseGELFHandler._resolve_host(False, "")
Example #3
0
    def __init__(self,
                 host,
                 port=12201,
                 path='/gelf',
                 timeout=5,
                 fqdn=False,
                 localname=None,
                 facility=None,
                 level_names=False,
                 tls_cafile=None,
                 tls_capath=None,
                 tls_cadata=None,
                 tls_client_cert=None,
                 tls_client_key=None,
                 tls_client_password=None):
        BaseGELFHandler.__init__(self,
                                 host,
                                 port,
                                 fqdn=fqdn,
                                 localname=localname,
                                 facility=facility,
                                 level_names=level_names)
        logging.Handler.__init__(self)

        self.host = host
        self.port = port
        self.path = path
        self.timeout = timeout
Example #4
0
    def __init__(self,
                 url,
                 exchange="logging.gelf",
                 exchange_type="fanout",
                 virtual_host="/",
                 routing_key="",
                 ssl=False,
                 heartbeat=0,
                 **kwargs):
        """Initialize the GELFRabbitHandler

        :param url: RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/)
        :type url: str

        :param exchange: RabbitMQ exchange. A queue binding must be defined
            on the server to prevent GELF logs from being dropped.
        :type exchange: str

        :param exchange_type: RabbitMQ exchange type.
        :type exchange_type: str

        :param virtual_host:
        :type virtual_host: str

        :param routing_key:
        :type routing_key: str

        :param ssl: whether to add TLS to the connection
        :type ssl: bool
        """
        self.url = url
        parsed = urlparse(url)
        if parsed.scheme != "amqp":
            raise ValueError('invalid URL scheme (expected "amqp"): %s' % url)
        host = parsed.hostname or "localhost"
        port = _ifnone(parsed.port, 5672)
        self.virtual_host = (virtual_host if not unquote(parsed.path[1:]) else
                             unquote(parsed.path[1:]))
        self.cn_args = {
            "host": "%s:%s" % (host, port),
            "userid": _ifnone(parsed.username, "guest"),
            "password": _ifnone(parsed.password, "guest"),
            "virtual_host": self.virtual_host,
            "insist": False,
            "ssl": ssl,
            "heartbeat": heartbeat
        }
        self.exchange = exchange
        self.exchange_type = exchange_type
        self.routing_key = routing_key
        BaseGELFHandler.__init__(self, **kwargs)
        SocketHandler.__init__(self, host, port)
        self.addFilter(ExcludeFilter("amqp"))
Example #5
0
    def __init__(self,
                 url,
                 exchange='logging.gelf',
                 exchange_type='fanout',
                 virtual_host='/',
                 routing_key='',
                 **kwargs):
        """Initialize the GELFRabbitHandler

        :param url: RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/)
        :type url: str

        :param exchange: RabbitMQ exchange. A queue binding must be defined
            on the server to prevent GELF logs from being dropped.
        :type exchange: str

        :param exchange_type: RabbitMQ exchange type.
        :type exchange_type: str

        :param virtual_host:
        :type virtual_host: str

        :param routing_key:
        :type routing_key: str
        """
        self.url = url
        parsed = urlparse(url)
        if parsed.scheme != 'amqp':
            raise ValueError('invalid URL scheme (expected "amqp"): %s' % url)
        host = parsed.hostname or 'localhost'
        port = _ifnone(parsed.port, 5672)
        self.virtual_host = virtual_host if not unquote(
            parsed.path[1:]) else unquote(parsed.path[1:])
        self.cn_args = {
            'host': '%s:%s' % (host, port),
            'userid': _ifnone(parsed.username, 'guest'),
            'password': _ifnone(parsed.password, 'guest'),
            'virtual_host': self.virtual_host,
            'insist': False,
        }
        self.exchange = exchange
        self.exchange_type = exchange_type
        self.routing_key = routing_key
        BaseGELFHandler.__init__(self, **kwargs)
        SocketHandler.__init__(self, host, port)
        self.addFilter(ExcludeFilter('amqplib'))
Example #6
0
    def __init__(self, url, exchange='logging.gelf', exchange_type='fanout',
                 virtual_host='/', routing_key='', **kwargs):
        """Initialize the GELFRabbitHandler

        :param url: RabbitMQ URL (ex: amqp://guest:guest@localhost:5672/)
        :type url: str

        :param exchange: RabbitMQ exchange. (default 'logging.gelf').
            A queue binding must be defined on the server to prevent
            log messages from being dropped.
        :type exchange: str

        :param exchange_type: RabbitMQ exchange type (default 'fanout').
        :type exchange_type: str

        :param virtual_host:
        :type virtual_host: str

        :param routing_key:
        :type routing_key: str
        """
        self.url = url
        parsed = urlparse(url)
        if parsed.scheme != 'amqp':
            raise ValueError('invalid URL scheme (expected "amqp"): %s' % url)
        host = parsed.hostname or 'localhost'
        port = _ifnone(parsed.port, 5672)
        self.virtual_host = virtual_host if not unquote(
            parsed.path[1:]) else unquote(parsed.path[1:])
        self.cn_args = {
            'host': '%s:%s' % (host, port),
            'userid': _ifnone(parsed.username, 'guest'),
            'password': _ifnone(parsed.password, 'guest'),
            'virtual_host': self.virtual_host,
            'insist': False,
        }
        self.exchange = exchange
        self.exchange_type = exchange_type
        self.routing_key = routing_key
        BaseGELFHandler.__init__(
            self,
            **kwargs
        )
        SocketHandler.__init__(self, host, port)
        self.addFilter(ExcludeFilter('amqplib'))
Example #7
0
def test_chunk_overflow_truncate_fail():
    message = BaseGELFHandler().makePickle(
        logging.LogRecord(
            "test_chunk_overflow_truncate_fail",
            logging.INFO,
            None,
            None,
            "1" * 1000,
            None,
            None,
        ))
    with pytest.warns(GELFTruncationFailureWarning):
        list(GELFTruncatingChunker(1).chunk_message(message))
Example #8
0
def test_gelf_chunkers_overflow(gelf_chunker):
    message = BaseGELFHandler().makePickle(
        logging.LogRecord(
            "test_gelf_chunkers_overflow",
            logging.INFO,
            None,
            None,
            "1" * 1000,
            None,
            None,
        ))
    chunks = list(gelf_chunker(chunk_size=1).chunk_message(message))
    assert len(chunks) <= 128
Example #9
0
def test_chunk_overflow_truncate_fail_large_inherited_field():
    message = BaseGELFHandler(facility="this is a really long facility" *
                              5000).makePickle(
                                  logging.LogRecord(
                                      "test_chunk_overflow_truncate_fail",
                                      logging.INFO,
                                      None,
                                      None,
                                      "reasonable message",
                                      None,
                                      None,
                                  ))
    with pytest.warns(GELFTruncationFailureWarning):
        list(GELFTruncatingChunker(2).chunk_message(message))
Example #10
0
def get_mock_send_arg(mock_send):
    assert mock_send.call_args_list != []
    [[[arg], _]] = mock_send.call_args_list

    # TODO: this is inaccurate solution for mocking non-send handlers
    if isinstance(arg, logging.LogRecord):
        return json.loads(
            BaseGELFHandler(compress=False).makePickle(arg).decode("utf-8"))
    try:
        return json.loads(zlib.decompress(arg).decode("utf-8"))
    except zlib.error:  # we have a uncompress message
        try:
            return json.loads(arg.decode("utf-8"))
        except Exception:  # that is null terminated
            return json.loads(arg[:-1].decode("utf-8"))
Example #11
0
 def emit(self, record):
     data = BaseGELFHandler.makePickle(self, record)
     try:
         self.connection = HTTPSConnection(
             self.host,
             self.port,
             context=ssl._create_unverified_context(),
             timeout=self.timeout)
         self.connection.request(
             'POST',
             self.path,
             data,
             headers={'Content-type': 'application/json'})
         self.connection.close()
     except:
         print(
             "ERROR - Connection with Graylog was not possible, log was not sent."
         )
Example #12
0
def test_chunk_overflow_truncate_uncompressed():
    message = BaseGELFHandler(compress=False).makePickle(
        logging.LogRecord(
            "test_chunk_overflow_truncate_uncompressed",
            logging.INFO,
            None,
            None,
            "1" * 1000,
            None,
            None,
        ))
    with pytest.warns(GELFChunkOverflowWarning):
        chunks = list(
            GELFTruncatingChunker(chunk_size=2,
                                  compress=False).chunk_message(message))
    assert len(chunks) <= 128
    payload = rebuild_gelf_bytes_from_udp_chunks(chunks).decode("UTF-8")
    glef_json = json.loads(payload)
    assert glef_json["_chunk_overflow"] is True
    assert glef_json["short_message"] in "1" * 1000
    assert glef_json["level"] == SYSLOG_LEVELS.get(logging.ERROR,
                                                   logging.ERROR)
Example #13
0
def test_add_level_name():
    gelf_dict = dict()
    BaseGELFHandler._add_level_names(gelf_dict, MOCK_LOG_RECORD)
    assert "INFO" == gelf_dict["level_name"]
Example #14
0
def test_set_custom_facility():
    gelf_dict = dict()
    facility = "test facility"
    BaseGELFHandler._set_custom_facility(gelf_dict, facility, MOCK_LOG_RECORD)
    assert MOCK_LOG_RECORD_NAME == gelf_dict["_logger"]
    assert "test facility" == gelf_dict["facility"]
Example #15
0
def test_pack(message, expected):
    assert expected == json.loads(
        BaseGELFHandler._pack_gelf_dict(message).decode("utf-8"))
Example #16
0
def test_invalid_fqdn_localhost():
    """Test constructing :class:`graypy.handler.BaseGELFHandler` with
    specifying conflicting arguments ``fqdn`` and ``localname``"""
    with pytest.raises(ValueError):
        BaseGELFHandler(fqdn=True, localname="localhost")
Example #17
0
def test_set_custom_facility():
    gelf_dict = dict()
    facility = "test facility"
    BaseGELFHandler._set_custom_facility(gelf_dict, facility, MOCK_LOG_RECORD)
    assert MOCK_LOG_RECORD_NAME == gelf_dict["_logger"]
    assert "test facility" == gelf_dict["facility"]
Example #18
0
def test_pack(message, expected):
    assert expected == json.loads(BaseGELFHandler._pack_gelf_dict(message).decode("utf-8"))
Example #19
0
def test_add_level_name():
    gelf_dict = dict()
    BaseGELFHandler._add_level_names(gelf_dict, MOCK_LOG_RECORD)
    assert "INFO" == gelf_dict["level_name"]