o
    x[h‹  ã                	   @   s¢   d dl Z d dlZd dlmZmZ d dlmZmZmZ ddddddd	d
dœZdZ	dZ
dZdd„ Zdd„ Zdd„ Zdd„ Zddd„Zdd„ ZedkrOeeƒ ƒ dS dS )é    N)ÚdatetimeÚtimezone)Úatomic_helperÚsubpÚutilzfinished running cloud-initz%starting search for local datasourcesz!searching for network datasourceszrunning config moduleszfinalizing moduleszrunning modules forzrunning single module )Úfinishedz
init-localúinit-networkÚinitzmodules-configzmodules-finalÚmodulesÚsinglez%Y-%m-%d %H:%M:%S,%fz%b %d %H:%M:%S.%f %Yz%b %d %H:%M:%S %Yc                 C   sª   dd„ t ddƒD ƒ}|  ¡ d |v r6t}d| v rt}t | d tt ¡ jƒ |¡j	t
jd�}| ¡ }t|ƒS d	| v rMt | t¡j	t
jd�}| ¡ }t|ƒS t| ƒ}t|ƒS )
Nc                 S   s   g | ]}t j| ‘qS © )ÚcalendarÚ
month_abbr)Ú.0Úmr   r   ú8/usr/lib/python3/dist-packages/cloudinit/analyze/dump.pyÚ
<listcomp>    s    z#parse_timestamp.<locals>.<listcomp>é   é   r   Ú.ú )Útzinfoú,)ÚrangeÚsplitÚDEFAULT_FMTÚCLOUD_INIT_JOURNALCTL_FMTr   ÚstrptimeÚstrÚnowÚyearÚreplacer   ÚutcÚ	timestampÚCLOUD_INIT_ASCTIME_FMTÚparse_timestamp_from_dateÚfloat)ÚtimestampstrÚmonthsÚFMTÚdtr#   r   r   r   Úparse_timestamp   s*   þýõÿþr+   c                   C   s   dt   ddg¡jv S )zÑGNU date includes a string containing the word GNU in it in
    help output. Posix date does not. Use this to indicate on Linux
    systems without GNU date that the extended parsing is not
    available.
    ÚGNUÚdatez--help)r   Ústdoutr   r   r   r   Úhas_gnu_date9   s   r/   c                 C   sR   t  ¡ st d¡rd}ntƒ rd}ntd| › d�ƒ‚tt |ddd| g¡j ¡ ƒS )NÚgdater-   z-Unable to parse timestamp without GNU date: [ú]z-uz+%s.%3Nz-d)	r   Úis_Linuxr   Úwhichr/   Ú
ValueErrorr&   r.   Ústrip)r'   r-   r   r   r   r%   B   s   
ÿÿr%   c                 C   sØ  d}dd|g}d}|D ]
}|| v rd} nq|sd S |   |¡\}}| d¡r1d |  ¡ dd	… ¡}d
|v rQ|  d
¡\}}|d|  ¡ d  7 }d|v rP|  ¡ d	 }	n |  ¡ d	 }	||krh| ¡ }|j dd�d }n	|  |	¡d  ¡ }d|v r£d}
d|v rœ|  d¡d  ¡ }|  d¡\}}| dd¡ dd¡}|dkr›d}nd S t| }n|  ¡ dd… \}}
}|  |¡d  ¡ }| d¡|t|ƒd|
 d¡dœ}|d dkrê|  d¡d }|  |¡d  d¡ ¡ }||d< | ¡ |d< |S )Nz cloud-init[z - z [CLOUDINIT] FTú:r   r   éÿÿÿÿr   z,%sr   )ÚmaxsplitúCloud-init v.ÚstartÚrunningz at ú'Ú ú-r	   r   é   Ú	cloudinit)ÚnameÚdescriptionr#   ÚoriginÚ
event_typerD   ÚfinishÚresultrB   )	r   ÚendswithÚjoinr5   Úlstripr!   Ústage_to_descriptionÚrstripr+   )ÚlineÚamazon_linux_2_sepÚ
separatorsÚfoundÚsepÚtimehostÚeventstrr'   ÚextraÚhostnamerD   Ústage_and_timestampÚ
event_nameÚ_Úevent_descriptionÚ_pymodloglvlÚeventrF   Údescr   r   r   Úparse_ci_loglineP   sb   
þ
€€
ûr\   c              
   C   s˜   g }d }g d¢}t | |gƒstdƒ‚|r| ¡ }n|  ¡ }|D ](}|D ]#}||v rFzt|ƒ}W n ty>   tj d¡ Y nw |rF| |¡ q#q||fS )N)zstart:zfinish:r9   z2Either cisource or rawdata parameters are requiredzSkipping invalid entry
)	Úanyr4   Ú
splitlinesÚ	readlinesr\   ÚsysÚstderrÚwriteÚappend)ÚcisourceÚrawdataÚeventsrZ   ÚCI_EVENT_MATCHESÚdatarL   Úmatchr   r   r   Údump_events¦   s*   
ÿ
€ù	rj   c                  C   s2   t tjƒdkrttjd ƒ} ntj} t t| ƒ¡S )Nr   )Úlenr`   ÚargvÚopenÚstdinr   Ú
json_dumpsrj   )rd   r   r   r   ÚmainÀ   s   rp   Ú__main__)NN)r   r`   r   r   r@   r   r   r   rJ   r$   r   r   r+   r/   r%   r\   rj   rp   Ú__name__Úprintr   r   r   r   Ú<module>   s2   ø	
V	ÿ