
    x[hZ%                         d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlmZm	Z	 d dl
mZ ddZd Zd Zd	 Zd
 Zd Zd ZdeddfdZedk(  r0 e       Zej-                         Zej0                  \  ZZ eee       yy)    N)datetimetimezone)IO)dumpshow)
json_dumpsc                 b   | st        j                  dd      } | j                  dd      }d|_        |j	                  dd	
      }|j                  dddddd       |j                  dddddd       |j                  dt        f       |j	                  dd
      }|j                  dddddd       |j                  dddddd       |j                  dddddd       |j                  dt        f       |j	                  d d!
      }|j                  dddddd"       |j                  dddddd       |j                  d t        f       |j	                  d#d$
      }|j                  dddddd"       |j                  dddddd       |j                  d#t        f       | S )%Nzcloudinit-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.logzspecify where to read input.)actionr   defaultr   z-oz	--outfileoutfile-zspecify where to write output. )r   r   z.Print list of in-order events during executionz-fz--formatprint_formatz%I%D @%Es +%dszspecify formatting of output.zspecify where to write output.r   z%Dump cloud-init events in JSON formatzspecify 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
 &&]&NJJ((M ) L )+   .   g}%=>''E ( K  ,   )+   -   V\$:;''< ( K ),   .   V\$:;''I ( K ),   -   V\$:;M    c           
         t        |      \  }}t        j                         }|\  }}}}t        j                  |t
        j                        }	t        j                  |t
        j                        }
t        j                  |t
        j                        }	 t        |      D cg c]  }|d   dk(  r	d|d   v r| c}d   }t        j                  |d   t
        j                        }d}d	}d
}t        j                  |t        j                  |t        j                  |i}||z
  }||z
  }|	|
|||||||d	}|j                   ||   j                  di |       t        ||       |S c c}w # t        $ r d}t        j                  }Y w xY w)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-localzstarting searchr   	timestampz4Could not find init-local log-line in cloud-init.logzYour 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$   t   s    t$KD%++-K;F8Kz=%33L(,,O#11*hllK&44x||% !&
yL(!Q}%55 

  ))K((,,
	. 	- 	-  	];J  ,.N+j8 (%' ,
F 
KK.
;'..889T5
  %Inn%s$   E. $E)<-E. )E. .FFc                    t        |      \  }}d}t        j                  dt        j                        }d}t	        t        j                  t        |      |            D ]q  \  }}t        t        |j                  |      d      }|j                  d|dz   z         |j                  dj                  |      dz          |j                  d       s |j                  d	|dz   z         t        ||       y
)az  Report 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+)r   T)reverse-- Boot Record %02d --
   
%d boot records analyzed
N)r<   recompile	MULTILINE	enumerater   show_eventsr@   sortedfiltermatchrE   joinrG   )	r/   rH   rI   rJ   blame_formatridxrecordsrecss	            r+   r!   r!      s     t$KD%"L


$bll3A
C T*L9 V vaggv.=.#':;DIIe$t+,D 
KK,a89T5r-   c                    t        |      \  }}t        t        j                  t	        |      |j
                              D ]a  \  }}|j                  d|dz   z         |j                  d       |j                  d       |j                  dj                  |      dz          c |j                  ddz   z         t        ||       y)a  Generate 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`   ra   N)	r<   re   r   rf   r@   r   rE   rj   rG   )r/   rH   rI   rJ   rm   rn   s         r+   r"   r"      s    ( t$KD% T*D,=,=> .V 	.#':;2	
 	N	
 	DIIf%,-. 
KK,a89T5r-   c                     t        |      \  }}|j                  t        t        |            dz          t	        ||       y)z%Dump cloud-init events in json formatr`   N)r<   rE   r   r@   rG   )r/   rH   rI   rJ   s       r+   r#   r#     s5    t$KD%	KK
;t,-45T5r-   c                 p    d }t        j                  |       \  }}|st        j                  |      \  }}|S )N)rawdata)r   load_events_infiler   dump_events)r   rs   events_s       r+   r@   r@     s7    G--f5OFG$$W5	Mr-   c                 2   | j                   dk(  rt        j                  }n	 t        | j                   d      }| j                  dk(  rt        j                  }|fS 	 t        | j                  d      }|fS # t        $ rD t        j
                  j                  d| j                   z         t        j                  d       Y w xY w# t        $ rG t        j
                  j                  d| j                  z         t        j                  d       Y fS w xY w)z.Common parsing and setup of input/output filesr   rl   zCannot open file %s
r_   w)
r   sysstdinopenOSErrorstderrrE   exitr   stdout)rH   rI   rJ   s      r+   r<   r<     s    {{cyy	S)D
 ||s

 ;	s+E
 ;  	JJ4t{{BCHHQK	  	JJ4t||CDHHQK;		s%   A6 C 6A
CCA
DDfile_handlesreturnc                  x    | D ]5  }|t         j                  t         j                  fv r&|j                          7 y)zclose filehandlesN)rz   r{   r   close)r   file_handles     r+   rG   rG   1  s5    # 399cjj11r-   __main__)N)r   rb   rz   r   r   typingr   cloudinit.analyzer   r   cloudinit.atomic_helperr   r,   r$   r!   r"   r#   r@   r<   rG   __name__r%   
parse_argsrH   r   r/   action_functorr;   r-   r+   <module>r      s   
  	 
 '  ( .bJTn6"J.B 4  z\FD![[T>4	 r-   