o
    Û1jT  ã                   @   s^   d dl mZ d dlmZmZmZ d dlmZmZmZ d dl	m
Z
mZmZ G dd„ de
eƒZdS )é    )ÚOptional)ÚdatetimeÚ	timedeltaÚtimezone)ÚJSONDecodeErrorÚdumpsÚloads)ÚPluginÚIndependentPluginÚ	PluginOptc                
   @   sÈ   e Zd ZdZdZdZdZdZdZdZ	e
dd	d
d�e
dd	dd�e
dd	dd�e
ddedd�e
ddedd�gZdee dee fdd„Zdee dee fdd„Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'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ˆ  éd   )r   úcollect-logsFzcollect logs from Loki API)ÚdefaultÚdescú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)r   Úval_typer   Úendpointúhttp://localhost:3100zDloki endpoint to fetch logs from. Defaults to http://localhost:3100.ÚstartÚendc                 C   s`   |st  tj¡}|s|tdd� }| d¡}| d¡}d|› d|› d|› d|› dtj› d	�}|S )
Né   )Údaysz%Y-%m-%dT%H:%M:%S.%fZú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   Únowr   Úutcr   Ústrftimer   ÚLOKI_QUERY_LIMIT)Úselfr   Úlabelr   r   Ústart_formattedÚend_formattedÚcommand© r(   ú9/usr/lib/python3/dist-packages/sos/report/plugins/loki.pyÚquery_command<   s"   

ÿþýüÿzLoki.query_commandc                 C   sR   |   |  ||||¡¡}zt|d ƒW S  ty(   |  d|› d�¡ |d  Y S w )NÚoutputz-An error was returned from Loki API on label z-. Error message stored, not querying further.)Úexec_cmdr*   r   r   Ú	_log_warn)r#   r   r$   r   r   r+   r(   r(   r)   Úget_logsO   s   
ÿûzLoki.get_logsc                 C   sR   |d d }t t ¡  ¡ d ƒ}|D ]}|d D ]}t |d ƒ}t||ƒ}qq|S )NÚdataÚresulté Êš;Úvaluesr   )Úintr   r   Ú	timestampÚmin)r#   ÚlogsÚlog_streamsÚearliest_logÚstreamÚlogr4   r(   r(   r)   Úget_earliest_log_timestamp[   s   þzLoki.get_earliest_log_timestampc                 C   sF  |   ||d d ¡}|  |› d�¡�}| t|dd�¡ W d   ƒ n1 s$w   Y  t|tƒr0d S |r›|  |¡}tt 	¡  
¡ d ƒ}d}|tjk r�||k rŸt |d ¡}	|   ||d |	¡}
|  |› d|› �¡�}| t|
dd�¡ W d   ƒ n1 syw   Y  t|
tƒr…d S |}|  |
¡}|d7 }|tjk r¡||k sLd S d S d S d S d S )Nz.logé   )Úindentr1   r   z.log.r   )r.   Úcollection_fileÚwriter   Ú
isinstanceÚstrr;   r3   r   r   r4   r   ÚMAX_PAGINATION_ITERATIONSÚfromtimestamp)r#   r   r$   r   r6   Úlogfiler8   Úprevious_earliest_logÚiterations_countÚlog_timestampÚnew_logsr(   r(   r)   Úget_logs_for_labele   sD   ÿ

ÿ
ÿþ
ÿ
ñùÿzLoki.get_logs_for_labelc                 C   sÌ   |   d¡}|  |¡ |   d¡}|  |¡ |  d¡ |  d¡ |  d¡rb|  d¡p)d}g | _|  d¡ }rDt|tƒrD|rD| j | d	¡¡ |  d
¡rd|  	d|› d�¡}t
|d ƒ}| j |d ¡ d S d S d S )Nz/etc/loki/*.yamlz/etc/worker/*.yamlz/var/log/loki/*zpebble logs loki -n 10000r   r   r   r   ú:r   r   z/loki/api/v1/labels'r+   r/   )Ú	path_joinÚadd_copy_specÚadd_cmd_outputÚ
get_optionr   r@   rA   ÚextendÚsplitÚcollect_cmd_outputr   )r#   Úels_config_fileÚcoordinated_workers_config_filer   Úlabels_optionÚ
labels_cmdÚlabels_jsonr(   r(   r)   Úsetup„   s(   








ÿôz
Loki.setupc                 C   s6   |   d¡pd}| jD ]}|   d¡}|  |||¡ q
d S )Nr   r   r   )rN   r   rI   )r#   r   r$   r   r(   r(   r)   Úcollect�   s
   

þzLoki.collectc                 C   s^   ddg}g d¢}dd  |¡› d�}dd  |¡› d�}|D ]}|  ||d¡ |  ||d¡ qd S )	NÚaccess_key_idÚsecret_access_key)z /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*********)ÚjoinÚdo_file_sub)r#   Úprotect_keysÚ
loki_filesÚmatch_exp_multilÚ	match_expÚfiler(   r(   r)   Úpostproc£   s    þþþûzLoki.postprocN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
short_descÚplugin_nameÚprofilesr"   rB   Úpackagesr   rA   Úoption_listr   r   r*   r.   r;   rI   rW   rX   rd   r(   r(   r(   r)   r      sF    ÿÿÿÿÿö

ÿ

ÿ
r   N)Útypingr   r   r   r   Újsonr   r   r   Úsos.report.pluginsr	   r
   r   r   r(   r(   r(   r)   Ú<module>   s
   