HEX
Server: LiteSpeed
System: Linux node612.namehero.net 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User: dfwparty (1186)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/sos/report/plugins/__pycache__/loki.cpython-36.pyc
3

��'jm�@s^ddlmZddlmZmZmZddlmZmZmZddl	m
Z
mZmZGdd�de
e�Z
dS)�)�Optional)�datetime�	timedelta�timezone)�JSONDecodeError�dumps�loads)�Plugin�IndependentPlugin�	PluginOptc
@s�eZdZdZdZdZd&ZdZdZd'Z	e
ddd	d
�e
dddd
�e
d
ddd
�e
ddedd�e
ddedd�gZe
ee
ed�dd�Ze
ee
ed�dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%S)(�Lokia

    Collects logs and configuration from Loki.

    This plugin interacts with the Loki API to fetch logs based on specified
    labels and provides options for pagination and label detection.
    It also collects relevant configuration files and masks sensitive
    information. It works with both charmed and non-charmed Loki.
    To fetch internal Loki logs, run it from the Loki container.
    You can also run it from another machine and fetch only logs from
    Loki API, by providing the following parameters:
        `-k loki.collect-logs=true -k loki.endpoint=LOKI_URL`

    Usage:
        sos report -o loki -k loki.collect-logs=true             -k loki.labels=severity:charm -k loki.detect-labels=true             -k loki.paginate=true -k loki.endpoint=LOKI_URL
    zLoki service�loki�servicesi��dzcollect-logsFzcollect logs from Loki API)�default�descz
detect-labelszTfetch logs for all available labels. May result in multiple files with the same logs�paginatez'fetch all available logs from Loki API.�labels�z1colon-delimited list of labels to fetch logs from)rZval_typer�endpointzhttp://localhost:3100zDloki endpoint to fetch logs from. Defaults to http://localhost:3100.)�start�endcCs`|stjtj�}|s"|tdd�}|jd�}|jd�}d|�d|�d|�d|�dtj�d	�}|S)
N�)Zdaysz%Y-%m-%dT%H:%M:%S.%fZzcurl -G -s 'z3/loki/api/v1/query_range' --data-urlencode 'query={z!=~".+"}' --data-urlencode 'start=z' --data-urlencode 'end=z' --data-urlencode 'limit=z' )r�nowrZutcrZstrftimer�LOKI_QUERY_LIMIT)�selfr�labelrrZstart_formattedZ
end_formattedZcommand�r�/usr/lib/python3.6/loki.py�
query_command<s

&zLoki.query_commandcCsR|j|j||||��}yt|d�Stk
rL|jd|�d��|dSXdS)N�outputz-An error was returned from Loki API on label z-. Error message stored, not querying further.)Zexec_cmdrrrZ	_log_warn)rrrrrr rrr�get_logsOsz
Loki.get_logscCsZ|dd}ttj�j�d�}x4|D],}x&|dD]}t|d�}t||�}q4Wq&W|S)N�data�resultiʚ;�valuesr)�intrr�	timestamp�min)r�logsZlog_streams�earliest_log�stream�logr&rrr�get_earliest_log_timestamp[s
zLoki.get_earliest_log_timestampcCs|j||dd�}|j|�d���}|jt|dd��WdQRXt|t�rLdS|r�|j|�}ttj	�j
�d�}d}x�|tjkr�||kr�tj
|d�}	|j||d|	�}
|j|�d|����}|jt|
dd��WdQRXt|
t�r�dS|}|j|
�}|d7}qtWdS)Nz.log�)�indentiʚ;rz.log.r)r!Zcollection_file�writer�
isinstance�strr,r%rrr&r�MAX_PAGINATION_ITERATIONSZ
fromtimestamp)rrrrr(Zlogfiler)Zprevious_earliest_logZiterations_countZ
log_timestampZnew_logsrrr�get_logs_for_labeles.




zLoki.get_logs_for_labelcCs�|jd�}|j|�|jd�}|j|�|jd�|jd�|jd�r�|jd�pRd}g|_|jd�}|r�t|t�r�|r�|jj|jd	��|jd
�r�|j	d|�d��}t
|d
�}|jj|d�dS)Nz/etc/loki/*.yamlz/etc/worker/*.yamlz/var/log/loki/*zpebble logs loki -n 10000zcollect-logsrzhttp://localhost:3100r�:z
detect-labelszcurl -G -s 'z/loki/api/v1/labels'r r")Z	path_joinZ
add_copy_specZadd_cmd_output�
get_optionrr0r1�extend�splitZcollect_cmd_outputr)rZels_config_fileZcoordinated_workers_config_filerZ
labels_optionZ
labels_cmdZlabels_jsonrrr�setup�s$








z
Loki.setupcCs:|jd�pd}x&|jD]}|jd�}|j|||�qWdS)Nrzhttp://localhost:3100r)r5rr3)rrrrrrr�collect�s
zLoki.collectcCshddg}dddddg}dd	j|��d
�}dd	j|��d�}x(|D] }|j||d�|j||d�q@WdS)
NZ
access_key_idZsecret_access_keyz /etc/loki/loki-local-config.yamlz/etc/loki/config.yamlz/etc/loki/local-config.yamlz/etc/loki/loki.yamlz/etc/worker/config.yaml�(�|z)\s*(:|=)(\S*\n.*?\\n)z)\s*(:|=)\s*[a-zA-Z0-9]*z
\1\2*********)�joinZdo_file_sub)rZprotect_keysZ
loki_filesZmatch_exp_multilZ	match_exp�filerrr�postproc�s 
z
Loki.postprocN)r)r
)�__name__�
__module__�__qualname__�__doc__Z
short_descZplugin_nameZprofilesrr2Zpackagesrr1Zoption_listrrrr!r,r3r8r9r>rrrrrs4

rN)ZtypingrrrrZjsonrrrZsos.report.pluginsr	r
rrrrrr�<module>s