Current File : //usr/lib/python3/dist-packages/cloudinit/analyze/__pycache__/__init__.cpython-312.pyc
�

ӇgZ%���ddlZddlZddlZddlmZmZddlmZddlmZm	Z	ddl
mZdd�Zd�Z
d�Zd	�Zd
�Zd�Zd�Zd
eddfd�Zedk(r0e�Zej-�Zej0\ZZeee�yy)�N)�datetime�timezone)�IO)�dump�show)�
json_dumpsc�b�|stjdd��}|jdd��}d|_|j	dd	�
�}|jddd
ddd��|jddd
ddd��|j
dtf��|j	dd�
�}|jddd
ddd��|jddd
ddd��|jddd
ddd��|j
dtf��|j	d d!�
�}|jddd
ddd"��|jddd
ddd��|j
d tf��|j	d#d$�
�}|jddd
ddd"��|jddd
ddd��|j
d#tf��|S)%Nzcloudinit-analyzez,Devel tool: Analyze cloud-init logs and data)�prog�description�Subcommands�
subcommand)�title�destT�blamez5Print list of executed stages ordered by time to init)�helpz-iz--infile�store�infilez/var/log/cloud-init.logzspecify where to read input.)�actionr�defaultrz-oz	--outfile�outfile�-zspecify where to write output. )rrz.Print list of in-order events during executionz-fz--format�print_formatz%I%D @%Es +%dszspecify formatting of output.zspecify where to write output.rz%Dump cloud-init events in JSON formatzspecify where to read input. �bootz2Print list of boot times for kernel and cloud-init)�argparse�ArgumentParser�add_subparsers�required�
add_parser�add_argument�set_defaults�
analyze_blame�analyze_show�analyze_dump�analyze_boot)�parser�
subparsers�parser_blame�parser_show�parser_dump�parser_boots      �</usr/lib/python3/dist-packages/cloudinit/analyze/__init__.py�
get_parserr,sj����(�(�$�F�
���&�&�]��&�N�J��J���(�(��M�)��L�������
�)�
+�
��������
��
.�
�����g�}�%=��>��'�'��E�(��K�������
� �
,�
��������
�)�
+�
��������
��
-�
�����V�\�$:��;��'�'��<�(��K�������
�)�
,�
��������
��
.�
�����V�\�$:��;��'�'��I�(��K�������
�)�
,�
��������
��
-�
�����V�\�$:��;��M�c
��t|�\}}tj�}|\}}}}tj|t
j�}	tj|t
j�}
tj|t
j�}	t|�D�cgc]}|ddk(r	d|dvr|��c}d}
tj|
dt
j�}d}d	}d
}tj|tj|tj|i}||z
}||z
}|	|
|||||||d�	}|j||jdi|���t||�|Scc}w#t$rd}tj}Y��wxYw)
a�Report a list of how long different boot operations took.

    For Example:
    -- Most Recent Boot Record --
        Kernel Started at: <time>
        Kernel ended boot at: <time>
        Kernel time to boot (seconds): <time>
        Cloud-init activated by systemd at: <time>
        Time between Kernel end boot and Cloud-init activation (seconds):<time>
        Cloud-init start: <time>
    �namez
init-localzstarting searchr����	timestampz4Could not find init-local log-line in cloud-init.logz�Your Linux distro or container does not support this functionality.
You must be running a Kernel Telemetry supported distro.
Please check https://docs.cloud-init.io/en/latest/topics/analyze.html for more information on supported distros.
a*-- Most Recent Boot Record --
    Kernel Started at: {k_s_t}
    Kernel ended boot at: {k_e_t}
    Kernel time to boot (seconds): {k_r}
    Cloud-init activated by systemd at: {ci_sysd_t}
    Time between Kernel end boot and Cloud-init activation (seconds): {bt_r}
    Cloud-init start: {ci_start}
z�-- Most Recent Container Boot Record --
    Container started at: {k_s_t}
    Cloud-init activated by systemd at: {ci_sysd_t}
    Cloud-init start: {ci_start}
)	�k_s_t�k_e_t�k_r�bt_r�k_e�k_s�ci_sysd�	ci_sysd_t�ci_start�)�configure_ior�dist_check_timestampr�
fromtimestampr�utc�_get_events�
IndexError�	FAIL_CODE�CONTAINER_CODE�SUCCESS_CODE�write�format�clean_io)r/�args�infh�outfh�kernel_info�status_code�kernel_start�
kernel_end�
ci_sysd_start�kernel_start_timestamp�kernel_end_timestamp�ci_sysd_start_timestamp�e�last_init_localr:�FAILURE_MSG�SUCCESS_MSG�
CONTAINER_MSG�
status_map�kernel_runtime�between_process_runtime�kwargss                      r+r$r$ts����t�$�K�D�%��+�+�-�K�;F�8�K��z�=�%�3�3�L�(�,�,�O��#�1�1�*�h�l�l�K��&�4�4��x�|�|���%�!��&�
����y�L�(�!�Q�}�%5�5�
�
�

����)�)��K�(�(�,�,�
��	.��	-��	-��	
�������]����;��J� �,�.�N�+�j�8��(�%��'��� �,��
�F�
�K�K�.�
�;�'�.�.�8��8�9��T�5�����
���%�I���n�n��%�s$�
E.�$E)�<-E.�)E.�.F�Fc���t|�\}}d}tjdtj�}d}t	tjt|�|��D]q\}}tt|j|�d��}|jd|dzz�|jdj|�dz�|jd��s|jd	|dzz�t||�y
)azReport a list of records sorted by largest time delta.

    For example:
      30.210s (init-local) searching for datasource
       8.706s (init-network) reading and applying user-data
        166ms (modules-config) ....
        807us (modules-final) ...

    We generate event records parsing cloud-init logs, formatting the output
    and sorting by record data ('delta')
    z
     %ds (%n)z(^\s+\d+\.\d+)rT)�reverse�-- Boot Record %02d --
��
�%d boot records analyzed
N)r<�re�compile�	MULTILINE�	enumerater�show_eventsr@�sorted�filter�matchrE�joinrG)	r/rHrIrJ�blame_format�r�idx�record�srecss	         r+r!r!�s����t�$�K�D�%�"�L�
�
�
�$�b�l�l�3�A�
�C� �����T�*�L�9�����V��v�a�g�g�v�.��=��
���.�#��'�:�;�
���D�I�I�e�$�t�+�,�
���D��
�
�K�K�,��a��8�9��T�5�r-c��t|�\}}ttjt	|�|j
��D]a\}}|j
d|dzz�|j
d�|j
d�|j
dj|�dz��c|j
ddzz�t||�y)aGenerate output records using the 'standard' format to printing events.

    Example output follows:
        Starting stage: (init-local)
          ...
        Finished stage: (init-local) 0.105195 seconds

        Starting stage: (init-network)
          ...
        Finished stage: (init-network) 0.339024 seconds

        Starting stage: (modules-config)
          ...
        Finished stage: (modules-config) 0.NNN seconds

        Starting stage: (modules-final)
          ...
        Finished stage: (modules-final) 0.NNN seconds
    r^r_zUThe total time elapsed since completing an event is printed after the "@" character.
z>The time the event takes is printed after the "+" character.

r`raN)	r<rerrfr@rrErjrG)r/rHrIrJrmrns      r+r"r"�s���(�t�$�K�D�%� �����T�*�D�,=�,=�>��.���V�	���.�#��'�:�;�
���
2�	
�	���N�	
�	���D�I�I�f�%��,�-�.�
�K�K�,��a��8�9��T�5�r-c��t|�\}}|jtt|��dz�t	||�y)z%Dump cloud-init events in json formatr`N)r<rErr@rG)r/rHrIrJs    r+r#r#s5���t�$�K�D�%�	�K�K�
�;�t�,�-��4�5��T�5�r-c�p�d}tj|�\}}|stj|��\}}|S)N)�rawdata)r�load_events_infiler�dump_events)rrs�events�_s    r+r@r@s7���G��-�-�f�5�O�F�G���$�$�W�5�	����Mr-c�2�|jdk(rtj}n	t|jd�}|jdk(rtj}|fS	t|jd�}|fS#t$rDtj
j
d|jz�tjd�Y��wxYw#t$rGtj
j
d|jz�tjd�YfSwxYw)z.Common parsing and setup of input/output filesrrlzCannot open file %s
r_�w)
r�sys�stdin�open�OSError�stderrrE�exitr�stdout)rHrIrJs   r+r<r<s����{�{�c���y�y��	�����S�)�D�
�|�|�s���
�
����;��
	�����s�+�E�
��;����	��J�J���4�t�{�{�B�C��H�H�Q�K�	���	��J�J���4�t�|�|�C�D��H�H�Q�K���;��		�s%�A6�C�6A
C�C�A
D�D�file_handles�returnc�x�|D]5}|tjtjfvr�&|j��7y)zclose filehandlesN)rzr{r��close)r��file_handles  r+rGrG1s5��#����3�9�9�c�j�j�1�1������r-�__main__)N)rrbrzrr�typingr�cloudinit.analyzerr�cloudinit.atomic_helperrr,r$r!r"r#r@r<rG�__name__r%�
parse_argsrHrr/�action_functorr;r-r+�<module>r�s���
�	�
�'��(�.�b�JT�n�6"�J���.�B��4���z��
�\�F�����D�!�[�[��T�>��4���	r-