3 'jm@s^ddlmZddlmZmZmZddlmZmZmZddl m Z m Z m Z Gddde e Z dS))Optional)datetime timedeltatimezone)JSONDecodeErrordumpsloads)PluginIndependentPlugin PluginOptc @seZdZdZdZdZd&ZdZdZd'Z e ddd d e d dd d e d ddd e dde dde dde ddgZ e ee edddZe ee edddZddZddZdd 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 servicelokiservicesidz collect-logsFzcollect logs from Loki API)defaultdescz detect-labelszTfetch logs for all available labels. May result in multiple files with the same logspaginatez'fetch all available logs from Loki API.labelsz1colon-delimited list of labels to fetch logs from)rZval_typerendpointzhttp://localhost:3100zDloki endpoint to fetch logs from. Defaults to http://localhost:3100.)startendc Cs`|stjtj}|s"|tdd}|jd}|jd}d|d|d|d|dtjd }|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' )rnowrZutcrZstrftimer LOKI_QUERY_LIMIT)selfrlabelrrZstart_formattedZ end_formattedZcommandr/usr/lib/python3.6/loki.py query_command<s   &zLoki.query_commandc CsR|j|j||||}y t|dStk rL|jd|d|dSXdS)Noutputz-An error was returned from Loki API on label z-. Error message stored, not querying further.)Zexec_cmdrrrZ _log_warn)rrrrrr rrrget_logsOs  z Loki.get_logscCsZ|dd}ttjjd}x4|D],}x&|dD]}t|d}t||}q4Wq&W|S)Ndataresultiʚ;valuesr)intrr timestampmin)rlogsZ log_streams earliest_logstreamlogr&rrrget_earliest_log_timestamp[s   zLoki.get_earliest_log_timestampc Cs|j||dd}|j|d}|jt|ddWdQRXt|trLdS|r|j|}ttj j d}d}x|t j kr||krtj |d} |j||d| } |j|d|}|jt| ddWdQRXt| trdS|}|j| }|d7}qtWdS)Nz.log)indentiʚ;rz.log.r)r!Zcollection_filewriter isinstancestrr,r%rrr&r MAX_PAGINATION_ITERATIONSZ fromtimestamp) rrrrr(Zlogfiler)Zprevious_earliest_logZiterations_countZ log_timestampZnew_logsrrrget_logs_for_labeles.      zLoki.get_logs_for_labelcCs|jd}|j||jd}|j||jd|jd|jdr|jdpRd}g|_|jd}|rt|tr|r|jj|jd |jd r|j d |d }t |d }|jj|ddS)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_optionrr0r1extendsplitZcollect_cmd_outputr)rZels_config_fileZcoordinated_workers_config_filerZ labels_optionZ labels_cmdZ labels_jsonrrrsetups$          z Loki.setupcCs:|jdp d}x&|jD]}|jd}|j|||qWdS)Nrzhttp://localhost:3100r)r5rr3)rrrrrrrcollects  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*********)joinZ do_file_sub)rZ protect_keysZ loki_filesZmatch_exp_multilZ match_expfilerrrpostprocs  z Loki.postprocN)r)r )__name__ __module__ __qualname____doc__Z short_descZ plugin_nameZprofilesrr2Zpackagesr r1Z option_listrrrr!r,r3r8r9r>rrrrr s4   r N)ZtypingrrrrZjsonrrrZsos.report.pluginsr r r r rrrr s