HEX
Server: Apache/2.4.65 (Debian)
System: Linux kubikelcreative 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) x86_64
User: www-data (33)
PHP: 8.4.13
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_rsyslog.cpython-39.pyc
a

��`|8�@s�dZddlZddlZddlmZddlmZddlmZdZdZ	dZ
iZd	Zd
Z
dZdZd
ZdZdZe�e�Ze�d�Ze�d�Ze
dfdd�Zdd�Zdd�Zd!dd�ZGdd�de�Zd"dd�Zdd �Z dS)#a�
.. _cc_rsyslog:

Rsyslog
-------
**Summary:** configure system loggig via rsyslog

This module configures remote system logging using rsyslog.

The rsyslog config file to write to can be specified in ``config_filename``,
which defaults to ``20-cloud-config.conf``. The rsyslog config directory to
write config files to may be specified in ``config_dir``, which defaults to
``/etc/rsyslog.d``.

A list of configurations for rsyslog can be specified under the ``configs`` key
in the ``rsyslog`` config. Each entry in ``configs`` is either a string or a
dictionary. Each config entry contains a configuration string and a file to
write it to. For config entries that are a dictionary, ``filename`` sets the
target filename and ``content`` specifies the config string to write. For
config entries that are only a string, the string is used as the config string
to write. If the filename to write the config to is not specified, the value of
the ``config_filename`` key is used. A file with the selected filename will be
written inside the directory specified by ``config_dir``.

The command to use to reload the rsyslog service after the config has been
updated can be specified in ``service_reload_command``. If this is set to
``auto``, then an appropriate command for the distro will be used. This is the
default behavior. To manually set the command, use a list of command args (e.g.
``[systemctl, restart, rsyslog]``).

Configuration for remote servers can be specified in ``configs``, but for
convenience it can be specified as key value pairs in ``remotes``. Each key
is the name for an rsyslog remote entry. Each value holds the contents of the
remote config for rsyslog. The config consists of the following parts:

    - filter for log messages (defaults to ``*.*``)
    - optional leading ``@`` or ``@@``, indicating udp and tcp respectively
      (defaults to ``@``, for udp)
    - ipv4 or ipv6 hostname or address. ipv6 addresses must be in ``[::1]``
      format, (e.g. ``@[fd00::1]:514``)
    - optional port number (defaults to ``514``)

This module will provide sane defaults for any part of the remote entry that is
not specified, so in most cases remote hosts can be specified just using
``<name>: <address>``.

For backwards compatibility, this module still supports legacy names for the
config entries. Legacy to new mappings are as follows:

    - ``rsyslog`` -> ``rsyslog/configs``
    - ``rsyslog_filename`` -> ``rsyslog/config_filename``
    - ``rsyslog_dir`` -> ``rsyslog/config_dir``

.. note::
    The legacy config format does not support specifying
    ``service_reload_command``.

**Internal name:** ``cc_rsyslog``

**Module frequency:** per instance

**Supported distros:** all

**Config keys**::

    rsyslog:
        config_dir: config_dir
        config_filename: config_filename
        configs:
            - "*.* @@192.158.1.1"
            - content: "*.*   @@192.0.2.1:10514"
              filename: 01-example.conf
            - content: |
                *.*   @@syslogd.example.com
        remotes:
            maas: "192.168.1.1"
            juju: "10.0.4.1"
        service_reload_command: [your, syslog, restart, command]

**Legacy config keys**::

    rsyslog:
        - "*.* @@192.158.1.1"
    rsyslog_dir: /etc/rsyslog-config.d/
    rsyslog_filename: 99-local.conf
�N)�log)�subp)�utilz20-cloud-config.confz/etc/rsyslog.d�auto�configsZconfig_filenameZ
config_dirZservice_reload_commandZrsyslog_filenameZrsyslog_dir�remotesz[ ]*[#]+[ ]*z_^(?P<proto>[@]{0,2})(([\[](?P<bracket_addr>[^\]]*)[\]])|(?P<addr>[^:]*))([:](?P<port>[0-9]+))?$FcCs>d}|tkr(|rdd|g}q,d|dg}n|}tj|dd�dS)N�rsyslog�	systemctlzreload-or-try-restart�service�restartT)�capture)�
DEF_RELOADr)�command�systemdr
�cmd�r�=/usr/lib/python3/dist-packages/cloudinit/config/cc_rsyslog.py�
reload_syslog�srcCs�|�di�}t|�d�t�rRt|�d�i}t|vr>|t|t<t|vrR|t|t<tgtfttt	ftt
t	fttt	tfft
ttff}|D](\}}}||vs�t|||�s�|||<q�|S)Nr)�get�
isinstance�list�KEYNAME_CONFIGS�KEYNAME_LEGACY_FILENAME�KEYNAME_FILENAME�KEYNAME_LEGACY_DIR�KEYNAME_DIR�DEF_DIR�str�DEF_FILENAME�KEYNAME_RELOADr
�KEYNAME_REMOTES�DEF_REMOTES�dict)�cfg�mycfgZfillup�key�defaultZvtypesrrr�load_config�s"�
r'c
	Cs�g}t|�D]�\}}t|t�rNd|vr8t�d|d�q|d}|�d|�}n|}|}|��}|stt�d|d�qtj�	||�}d}||vr�d}|�
|�z*d}	|�d	�s�d	}	tj
|||	|d
�Wqty�t�td|�Yq0q|S)N�contentz%No 'content' entry in config entry %s��filenamezEntry %s has an empty filename�ab�wb��
)�omodezFailed to write to %s)�	enumeraterr"�LOG�warningr�strip�os�path�join�append�endswithr�
write_file�	Exception�logexc)
r�	def_fname�cfg_dir�filesZcur_pos�entr(r*r/Zendlrrr�apply_rsyslog_changes�s:
�

r@cCszt�|�\}}|��}Wnty6|d}}Yn0|����}d}t|�dkrZ|}n"t|�dkrp|\}}ntd|��t�|�}|s�td|��|�d�}|�d�p�|�d�}|�d�}	|�d	�r�|�	d
�s�td|��|r�|s�|}t
|||||	d�}
|
��|
S)
Nr)�zline had multiple spaces: %szInvalid host specification '%s'�proto�addrZbracket_addr�port�[�]z"host spec had invalid brackets: %s��name�matchrBrCrD)�
COMMENT_RE�splitr3�
ValueError�len�HOST_PORT_RErI�group�
startswithr8�SyslogRemotesLine�validate)�linerH�data�comment�toksrI�	host_portrBrCrD�trrr�parse_remotes_line s6



�rYc@s.eZdZd
dd�Zdd�Zdd�Zdd	�ZdS)rQNcCs\|sd}||_||_|sd}|dkr*d}n|dkr6d}||_||_|rRt|�|_nd|_dS)Nz*.*�udp�@�@@�tcp)rHrIrBrC�intrD)�selfrHrIrBrCrDrrr�__init__FszSyslogRemotesLine.__init__c
Cs\|jrJzt|j�Wn4tyH}ztd|j�|�WYd}~n
d}~00|jsXtd��dS)Nzport '%s' is not an integerzaddress is required)rDr^rLrC)r_�errrrRZs��zSyslogRemotesLine.validatecCsd|j|j|j|j|jfS)Nz.[name=%s match=%s proto=%s address=%s port=%s]rG)r_rrr�__repr__fs�zSyslogRemotesLine.__repr__cCs�|jd}|jdkr|d7}n|jdkr0|d7}d|jvrN|d|jd7}n
||j7}|jrl|d	|j7}|jr�|d
|j7}|S)N� rZr[r]r\�:rErFz:%sz # %s)rIrBrCrDrH)r_�bufrrr�__str__ks





zSyslogRemotesLine.__str__)NNNNN)�__name__�
__module__�__qualname__r`rRrbrfrrrrrQEs�
rQcCs�|sdSg}|dur|�|�|��D]^\}}|s4q&z|�tt||d���Wq&ty�}zt�d|||�WYd}~q&d}~00q&|dur�|�|�d�|�dS)N)rHz!failed loading remote %s: %s [%s]r.)r7�itemsrrYrLr1r2r6)r�header�footer�linesrHrSrarrr�remotes_to_rsyslog_cfgs
(
rnc

Cs�d|vr|�d|�dSt|�}|t}|trH|�t|tddd��|ds^|�d�dSt|t|t|td�}|s�|�d	�dSzt	|t
|j��d
�f}Wn6t
jy�}	zd}|�d|	�WYd}	~	n
d}	~	00|r�|��|�d
||�dS)Nrz;Skipping module named %s, no 'rsyslog' key in configurationz# begin remotesz
# end remotes)rkrlrz.Empty config rsyslog['configs'], nothing to do)rr<r=z0restart of syslog not necessary, no changes made)rrFzFailed to reload syslogz%s configured %s files)�debugr'rr r7rnr@rrrr�distro�uses_systemdr�ProcessExecutionErrorr2�
cycle_logging)
rHr#�cloudr�_argsr$rZchangesZ	restartedrarrr�handle�sJ���
�
�"rv)N)NN)!�__doc__r4�re�	cloudinitr�loggingrrrrr
r!rrrrrrr �	getLoggerrgr1�compilerJrNrr'r@rY�objectrQrnrvrrrr�<module>	s:-

�(
%: