HEX
Server: LiteSpeed
System: Linux us-phx-web1284.main-hosting.eu 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: u300739242 (300739242)
PHP: 8.2.30
Disabled: system, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/graypy/__pycache__/handler.cpython-37.pyc
B

�P�e�e�
@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlmZmZdZdZejddkr�eeZZn
eeZZejdkr�ejZne�ed�d	i�ZyddlZWn ek
r�ddlmZYnXejd
ej dej!dej"dej#d
iZ$dZ%Gdd�dej&e�Z'Gdd�de(�Z)Gdd�de*�Z+Gdd�de)�Z,Gdd�de+�Z-Gdd�de)�Z.Gdd�de'e�Z/Gdd�de'e�Z0Gdd �d e0�Z1Gd!d"�d"e'�Z2dS)#zILogging Handlers that send messages in Graylog Extended Log Format (GELF)�N)�DatagramHandler�
SocketHandleri�i��)r��ABC��r���c@s�eZdZdZddd�Zdd�Zd	d
�Zedd��Zed
d��Z	edd��Z
edd��Zedd��Zedd��Z
edd��Zedd��Zedd��ZdS)�BaseGELFHandlerz�Abstract class defining the basic functionality of converting a
    :obj:`logging.LogRecord` into a GELF log. Provides the boilerplate for
    all GELF handlers defined within graypy.TFNcCsJtj�|�||_||_|r(|r(td��||_||_||_||_	||_
dS)a�Initialize the BaseGELFHandler

        :param debugging_fields: If :obj:`True` add debug fields from the
            log record into the GELF logs to be sent to Graylog.
        :type debugging_fields: bool

        :param extra_fields: If :obj:`True` add extra fields from the log
            record into the GELF logs to be sent to Graylog.
        :type extra_fields: bool

        :param fqdn: If :obj:`True` use the fully qualified domain name of
            localhost to populate the ``host`` GELF field.
        :type fqdn: bool

        :param localname: If specified and ``fqdn`` is :obj:`False`, use the
            specified hostname to populate the ``host`` GELF field.
        :type localname: str or None

        :param facility: If specified, replace the ``facility`` GELF field
            with the specified value. Also add a additional ``_logger``
            GELF field containing the ``LogRecord.name``.
        :type facility: str

        :param level_names: If :obj:`True` use python logging error level name
            strings instead of syslog numerical values.
        :type level_names: bool

        :param compress: If :obj:`True` compress the GELF message before
            sending it to the Graylog server.
        :type compress: bool
        z8cannot specify 'fqdn' and 'localname' arguments togetherN)�logging�Handler�__init__�debugging_fields�extra_fields�
ValueError�fqdn�	localname�facility�level_names�compress)�selfrrrrrrrrr�?/opt/alt/python37/lib/python3.7/site-packages/graypy/handler.pyr9s)zBaseGELFHandler.__init__cCs,|�|�}|�|�}|jr$t�|�n|}|S)aConvert a :class:`logging.LogRecord` into bytes representing
        a GELF log

        :param record: :class:`logging.LogRecord` to convert into a GELF log.
        :type record: logging.LogRecord

        :return: bytes representing a GELF log.
        :rtype: bytes
        )�_make_gelf_dict�_pack_gelf_dictr�zlib)r�record�	gelf_dict�packed�picklerrr�
makePickleos


zBaseGELFHandler.makePicklecCs�d|�|j|j�|jr"|j�|�n|��|jt�|j	|j	�|j
pD|jd�}|�||�|j
rh|�||�|j
dk	r�|�||j
|�|jr�|�||�|jr�|�||�|S)a&Create a dictionary representing a GELF log from a
        python :class:`logging.LogRecord`

        :param record: :class:`logging.LogRecord` to create a GELF log from.
        :type record: logging.LogRecord

        :return: Dictionary representing a GELF log.
        :rtype: dict
        z1.0)�version�host�
short_message�	timestamp�levelrN)�
_resolve_hostrr�	formatter�format�
getMessage�created�
SYSLOG_LEVELS�get�levelnor�name�_add_full_messager�_add_level_names�_set_custom_facilityr�_add_debugging_fieldsr�_add_extra_fields)rrrrrrr~s"
zBaseGELFHandler._make_gelf_dictcCst�|j�|d<dS)a�Add the ``level_name`` field to the ``gelf_dict`` which notes
        the logging level via the string error level names instead of
        numerical values

        :param gelf_dict: Dictionary representing a GELF log.
        :type gelf_dict: dict

        :param record: :class:`logging.LogRecord` to extract a logging
            level from to insert into the given ``gelf_dict``.
        :type record: logging.LogRecord
        �
level_nameN)r
�getLevelNamer.)rrrrrr1�s
z BaseGELFHandler._add_level_namescCs|�||jd��dS)aYSet the ``gelf_dict``'s ``facility`` field to the specified value

        Also add a additional ``_logger`` field containing the
        ``LogRecord.name``.

        :param gelf_dict: Dictionary representing a GELF log.
        :type gelf_dict: dict

        :param facility_value: Value to set as the ``gelf_dict``'s
            ``facility`` field.
        :type facility_value: str

        :param record: :class:`logging.LogRecord` to extract it's record
            name to insert into the given ``gelf_dict`` as the ``_logger``
            field.
        :type record: logging.LogRecord
        )rZ_loggerN)�updater/)rZfacility_valuerrrrr2�sz$BaseGELFHandler._set_custom_facilitycCs8d}|jrd�tj|j��}|jr(|j}|r4||d<dS)a�Add the ``full_message`` field to the ``gelf_dict`` if any
        traceback information exists within the logging record

        :param gelf_dict: Dictionary representing a GELF log.
        :type gelf_dict: dict

        :param record: :class:`logging.LogRecord` to extract a full
            logging message from to insert into the given ``gelf_dict``.
        :type record: logging.LogRecord
        N�
�full_message)�exc_info�join�	traceback�format_exception�exc_text)rrr9rrrr0�s
z!BaseGELFHandler._add_full_messagecCs |rt��S|dk	r|St��S)a�Resolve the ``host`` GELF field

        :param fqdn: Boolean indicating whether to use :meth:`socket.getfqdn`
            to obtain the ``host`` GELF field.
        :type fqdn: bool

        :param localname: Use specified hostname as the ``host`` GELF field.
        :type localname: str or None

        :return: String representing the ``host`` GELF field.
        :rtype: str
        N)�socket�getfqdn�gethostname)rrrrrr'�s
zBaseGELFHandler._resolve_hostcCs@|�|j|j|j|j|jd��t|dd�}|dk	r<||d<dS)aEAdd debugging fields to the given ``gelf_dict``

        :param gelf_dict: Dictionary representing a GELF log.
        :type gelf_dict: dict

        :param record: :class:`logging.LogRecord` to extract debugging
            fields from to insert into the given ``gelf_dict``.
        :type record: logging.LogRecord
        )�file�lineZ	_function�_pidZ_thread_name�processNameNZ
_process_name)r7�pathname�lineno�funcName�process�
threadName�getattr)rrZpnrrrr3�sz%BaseGELFHandler._add_debugging_fieldscCs>d}x4|j��D]&\}}||kr|�d�s||d|<qWdS)a+Add extra fields to the given ``gelf_dict``

        However, this does not add additional fields in to ``message_dict``
        that are either duplicated from standard :class:`logging.LogRecord`
        attributes, duplicated from the python logging module source
        (e.g. ``exc_text``), or violate GELF format (i.e. ``id``).

        .. seealso::

            The list of standard :class:`logging.LogRecord` attributes can be
            found at:

                http://docs.python.org/library/logging.html#logrecord-attributes

        :param gelf_dict: Dictionary representing a GELF log.
        :type gelf_dict: dict

        :param record: :class:`logging.LogRecord` to extract extra fields
            from to insert into the given ``gelf_dict``.
        :type record: logging.LogRecord
        )�args�asctimer+r:r>�filenamerH�id�	levelnamer.rG�module�msecs�message�msgr/rFrIrE�relativeCreated�threadrJ�_z_%sN)�__dict__�items�
startswith)rrZ	skip_list�key�valuerrrr4	sz!BaseGELFHandler._add_extra_fieldscCs&|�|�}tj|d|jd�}|�d�S)a�Convert a given ``gelf_dict`` into JSON-encoded UTF-8 bytes, thus,
        creating an uncompressed GELF log ready for consumption by Graylog.

        Since we cannot be 100% sure of what is contained in the ``gelf_dict``
        we have to do some sanitation.

        :param gelf_dict: Dictionary representing a GELF log.
        :type gelf_dict: dict

        :return: Bytes representing a uncompressed GELF log.
        :rtype: bytes
        z,:)�
separators�defaultzutf-8)�_sanitize_to_unicode�json�dumps�_object_to_json�encode)�clsrrrrrr>s
zBaseGELFHandler._pack_gelf_dictcsft|t�r$t�fdd�|��D��St|ttf�rJ|��fdd�|D��St|t�rb|jddd�}|S)z�Convert all strings records of the object to unicode

        :param obj: Object to sanitize to unicode.
        :type obj: object

        :return: Unicode string representing the given object.
        :rtype: str
        c3s&|]\}}��|���|�fVqdS)N)r_)�.0�k�v)rdrr�	<genexpr>\sz7BaseGELFHandler._sanitize_to_unicode.<locals>.<genexpr>csg|]}��|��qSr)r_)re�i)rdrr�
<listcomp>`sz8BaseGELFHandler._sanitize_to_unicode.<locals>.<listcomp>zutf-8�replace)�errors)�
isinstance�dictrY�list�tuple�	__class__�data�decode)rd�objr)rdrr_Ps



z$BaseGELFHandler._sanitize_to_unicodecCst|tj�r|��St|�S)a�Convert objects that cannot be natively serialized into JSON
        into their string representation (for later JSON serialization).

        :class:`datetime.datetime` based objects will be converted into a
        ISO formatted timestamp string.

        :param obj: Object to convert into a string representation.
        :type obj: object

        :return: String representing the given object.
        :rtype: str
        )rm�datetime�	isoformat�repr)rtrrrrbeszBaseGELFHandler._object_to_json)TTFNNFT)�__name__�
__module__�__qualname__�__doc__rr!r�staticmethodr1r2r0r'r3r4�classmethodrr_rbrrrrr4s&
."5rc@s@eZdZdZefdd�Zdd�Zedd��Zdd	�Z	d
d�Z
dS)
�BaseGELFChunkera,Base UDP GELF message chunker

    .. warning::
        This will silently drop chunk overflowing GELF messages.
        (i.e. GELF messages that consist of more than 128 chunks)

    .. note::
        UDP GELF message chunking is only supported for the
        :class:`.handler.GELFUDPHandler`.
    cCs
||_dS)z�Initialize the BaseGELFChunker.

        :param chunk_size: Message chunk size. Messages larger than this
            size should be sent to Graylog in multiple chunks.
        :type chunk_size: int
        N)�
chunk_size)rrrrrr�szBaseGELFChunker.__init__cCstt�t|�d|j��S)z�Get the number of chunks a GELF message requires

        :return: Number of chunks the specified GELF message requires.
        :rtype: int
        g�?)�int�math�ceil�lenr)rrSrrr�_message_chunk_number�sz%BaseGELFChunker._message_chunk_numberc	Cs,d�dt�d|�t�d|�t�d|�|g�S)N�s�Q�B)r;�struct�pack)�
message_idZ	chunk_seq�total_chunks�chunkrrr�_encode�s


zBaseGELFChunker._encodec#s`����}t�dd�}xDt��fdd�tdt���j�D��D]\}}��||||�Vq>WdS)z�Generate and iter chunks for a GELF message

        :param message: GELF message to generate and iter chunks for.
        :type; bytes

        :return: Iterator of the chunks of a GELF message.
        :rtype: Iterator[bytes]
        rl����c3s |]}�||�j�VqdS)N)r)reri)rSrrrrh�sz3BaseGELFChunker._gen_gelf_chunks.<locals>.<genexpr>N)r��random�randint�	enumerate�ranger�rr�)rrSr�r��sequencer�r)rSrr�_gen_gelf_chunks�s	
 z BaseGELFChunker._gen_gelf_chunksccs0|�|�tkrdSx|�|�D]
}|VqWdS)aChunk a GELF message

        Silently drop chunk overflowing GELF messages.

        :param message: GELF message to chunk.
        :type message: bytes

        :return: Iterator of the chunks of a GELF message.
        :rtype: Iterator[bytes], None
        N)r��GELF_MAX_CHUNK_NUMBERr�)rrSr�rrr�
chunk_message�szBaseGELFChunker.chunk_messageN)rxryrzr{�	WAN_CHUNKrr�r|r�r�r�rrrrr~xs
	r~c@seZdZdZdS)�GELFChunkOverflowWarningzEWarning that a chunked GELF UDP message requires more than 128 chunksN)rxryrzr{rrrrr��sr�c@seZdZdZdd�ZdS)�GELFWarningChunkerzBGELF UDP message chunker that warns and drops overflowing messagesccsB|�|�tkr$t�d�|�t�dSx|�|�D]
}|Vq0WdS)z�Chunk a GELF message

        Issue a :class:`.handler.GELFChunkOverflowWarning` on chunk
        overflowing GELF messages. Then drop them.
        z"chunk overflowing GELF message: {}N)r�r��warnings�warnr)r�r�)rrSr�rrrr��sz GELFWarningChunker.chunk_messageN)rxryrzr{r�rrrrr��sr�c@seZdZdZdS)�GELFTruncationFailureWarningzaWarning that the truncation of a chunked GELF UDP message failed
    to prevent chunk overflowingN)rxryrzr{rrrrr��sr�c@s2eZdZdZedejfdd�Zdd�Zdd�Z	d	S)
�GELFTruncatingChunkerz<GELF UDP message chunker that truncates overflowing messagesTcCst�||�||_||_dS)ayInitialize the GELFTruncatingChunker

        :param compress: Boolean noting whether the given GELF messages are
            originally compressed.
        :type compress: bool

        :param gelf_packer: Function handle for packing a GELF dictionary
            into bytes. Should be of the form ``gelf_packer(gelf_dict)``.
        :type gelf_packer: Callable[dict]
        N)r~r�gelf_packerr)rrrr�rrrr�szGELFTruncatingChunker.__init__c	Cs|jrt�|�}n|}t�|�d��}|d|dd|dt�tj	tj	�|ddd�}t
|�|jrrt�|�|��n|�|��}|d	d
|j
|�}xft|dd�D]H}||d	<|�|�}|jr�t�|�}|�|�t
kr�|S|d
|j
�}q�Wtd�|���d
S)
aAttempt to truncate a chunk overflowing GELF message

        :param raw_message: Original bytes of a chunk overflowing GELF message.
        :type raw_message: bytes

        :return: Truncated and simplified version of raw_message.
        :rtype: bytes
        zUTF-8r"r#�r%rT)r"r#r$r%r&rZ_chunk_overflowr$N���zCtruncation failed preventing chunk overflowing for GELF message: {})rr�
decompressr`�loadsrsr,r-r
�ERRORr�r�r�rr�r�r))	rZraw_messagerSrZsimplified_gelf_dictZgelf_chunks_freeZtruncated_short_messageZclipZpacked_messagerrr�gen_chunk_overflow_gelf_log�s6	

z1GELFTruncatingChunker.gen_chunk_overflow_gelf_logc
csz|�|�tkr\t�d�|�t�y|�|�}Wn,tk
rZ}zt�|�dSd}~XYnXx|�|�D]
}|VqhWdS)a�Chunk a GELF message

        Issue a :class:`.handler.GELFChunkOverflowWarning` on chunk
        overflowing GELF messages. Then attempt to truncate and simplify the
        chunk overflowing GELF message so that it may be successfully
        chunked without overflowing.

        If the truncation and simplification of the chunk overflowing GELF
        message fails issue a :class:`.handler.GELFTruncationFailureWarning`
        and drop the overflowing GELF message.
        z-truncating GELF chunk overflowing message: {}N)	r�r�r�r�r)r�r�r�r�)rrS�wr�rrrr�-s
z#GELFTruncatingChunker.chunk_messageN)
rxryrzr{r�rrrr�r�rrrrr��s5r�cs0eZdZdZde�fdd�Z�fdd�Z�ZS)�GELFUDPHandlerzGELF UDP handleri�/cKs&tj|f|�t�|||�||_dS)adInitialize the GELFUDPHandler

        .. note::
            By default a :class:`.handler.GELFWarningChunker` is used as the
            ``gelf_chunker``. Thus, GELF messages that chunk overflow will
            issue a :class:`.handler.GELFChunkOverflowWarning` and will be
            dropped.

        :param host: GELF UDP input host.
        :type host: str

        :param port: GELF UDP input port.
        :type port: int

        :param gelf_chunker: :class:`.handler.BaseGELFChunker` instance to
            handle chunking larger GELF messages.
        :type gelf_chunker: GELFWarningChunker
        N)rrr�gelf_chunker)rr#�portr��kwargsrrrrJszGELFUDPHandler.__init__csLt|�|jjkr"tt|��|�n&x$|j�|�D]}tt|��|�q0WdS)N)r�r�r�superr��sendr�)r�sr�)rqrrr�aszGELFUDPHandler.send)rxryrzr{r�rr��
__classcell__rr)rqrr�Gsr�cs*eZdZdZddd�Z�fdd�Z�ZS)�GELFTCPHandlerzGELF TCP handler�/cKs(tj|fddi|��t�|||�dS)a�Initialize the GELFTCPHandler

        :param host: GELF TCP input host.
        :type host: str

        :param port: GELF TCP input port.
        :type port: int

        .. attention::
            GELF TCP does not support compression due to the use of the null
            byte (``\0``) as frame delimiter.

            Thus, :class:`.handler.GELFTCPHandler` does not support setting
            ``compress`` to :obj:`True` and is locked to :obj:`False`.
        rFN)rrr)rr#r�r�rrrrlszGELFTCPHandler.__init__cstt|��|�dS)aOAdd a null terminator to generated pickles as TCP frame objects
        need to be null terminated

        :param record: :class:`logging.LogRecord` to create a null
            terminated GELF log.
        :type record: logging.LogRecord

        :return: Null terminated bytes representing a GELF log.
        :rtype: bytes
        �)r�r�r!)rr)rqrrr!szGELFTCPHandler.makePickle)r�)rxryrzr{rr!r�rr)rqrr�is
r�c@s$eZdZdZd
dd�Zddd	�ZdS)�GELFTLSHandlerz!GELF TCP handler with TLS support�/FNcKst|r|dkrtd��|dk	r,|dkr,td��tj|f||d�|��||_|rTtjntj|_||_|rj|n||_	dS)a�Initialize the GELFTLSHandler

        :param host: GELF TLS input host.
        :type host: str

        :param port: GELF TLS input port.
        :type port: int

        :param validate: If :obj:`True`, validate the Graylog server's
            certificate. In this case specifying ``ca_certs`` is also
            required.
        :type validate: bool

        :param ca_certs: Path to CA bundle file.
        :type ca_certs: str

        :param certfile: Path to the client certificate file.
        :type certfile: str

        :param keyfile: Path to the client private key. If the private key is
            stored with the certificate, this parameter can be ignored.
        :type keyfile: str
        Nz%CA bundle file path must be specifiedzcertfile must be specified)r#r�)
rr�r�ca_certs�ssl�
CERT_REQUIRED�	CERT_NONE�reqs�certfile�keyfile)rr#r��validater�r�r�r�rrrr�s!zGELFTLSHandler.__init__�cCsVt�tjtj�}t|d�r$|�|�tj||j|j|j	|j
d�}|�|j|j
f�|S)zCreate a TLS wrapped socket�
settimeout)r��	cert_reqsr�r�)r?�AF_INET�SOCK_STREAM�hasattrr�r��wrap_socketr�r�r�r��connectr#r�)r�timeoutZplain_socketZwrapped_socketrrr�
makeSocket�s


zGELFTLSHandler.makeSocket)r�FNNN)r�)rxryrzr{rr�rrrrr��s
'r�c@s"eZdZdZddd�Zdd	�Zd
S)�GELFHTTPHandlerzGELF HTTP handler�/T�/gelf�cKsFtj|fd|i|��||_||_||_||_i|_|rBd|jd<dS)a�Initialize the GELFHTTPHandler

        :param host: GELF HTTP input host.
        :type host: str

        :param port: GELF HTTP input port.
        :type port: int

        :param compress: If :obj:`True` compress the GELF message before
            sending it to the Graylog server.
        :type compress: bool

        :param path: Path of the HTTP input.
            (see http://docs.graylog.org/en/latest/pages/sending_data.html#gelf-via-http)
        :type path: str

        :param timeout: Number of seconds the HTTP client should wait before
            it discards the request if the Graylog server doesn't respond.
        :type timeout: int
        rzgzip,deflatezContent-EncodingN)rrr#r��pathr��headers)rr#r�rr�r�r�rrrr�szGELFHTTPHandler.__init__cCs8|�|�}tj|j|j|jd�}|�d|j||j�dS)aConvert a :class:`logging.LogRecord` to GELF and emit it to Graylog
        via a HTTP POST request

        :param record: :class:`logging.LogRecord` to convert into a GELF log
            and emit to Graylog via a HTTP POST request.
        :type record: logging.LogRecord
        )r#r�r��POSTN)	r!�httplib�HTTPConnectionr#r�r��requestr�r�)rrr �
connectionrrr�emit�s
zGELFHTTPHandler.emitN)r�Tr�r�)rxryrzr{rr�rrrrr��s
!r�)3r{r��abcrur`r
r�r�r?r�r��sysr<r�logging.handlersrrr�Z	LAN_CHUNK�version_info�bytes�strrr�text�unicoder�ABCMetar��ImportError�http.client�client�CRITICALr��WARNING�INFO�DEBUGr,r�rr�objectr~�Warningr�r�r�r�r�r�r�r�rrrr�<module>sX


FMf"$E