
    th*7                        d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	m
Z
mZmZ d dlmZmZ d dlmZ ddddd	d
ddddddZdZdZdZedddfZeZdededefdZde	e   de	e   fdZde	e   de	e   fdZde	e   de	e   fdZdedefdZdede j                   fdZde j                   de j                   defd Z d!ed"edefd#Z!d$e j                   d!ed"edefd%Z"dedefd&Z# G d' d(      Z$de
eeeef   fd)Z%de
eeeef   fd*Z&de
eeeef   fd+Z'	 d2d,ee   d-edeee      fd.Z(d,ee   d-edeee      fd/Z)d0ede
e	e   ef   fd1Z*y)3    N)IOAnyDictListOptionalTupleUnioncast)subputil)uses_systemddeltadescriptionelapsed
event_typeindentlevelnameoriginresult	timestamp
total_time)z%dz%Dz%Ez%ez%Iz%lz%nz%oz%rz%tz%T
successfulfailure	containermsgeventreturnc                     t         j                         D ]9  \  }}|| v s|dv r| j                  |d|z        } %| j                  |d|z        } ;  | j                  di |S )N)r   r   r   z
{%s:08.5f}z{%s} )
format_keyitemsreplaceformat)r   r   ijs       8/usr/lib/python3/dist-packages/cloudinit/analyze/show.pyformat_recordr)   9   sm      " 11855kk!\A%56kk!VaZ01 3::    c                 *    | r| j                  d      S y )Nr   getr   s    r(   
event_namer/   D   s    yy  r*   c                 *    | r| j                  d      S y )Nr   r,   r.   s    r(   r   r   J   s    yy&&r*   c                 F    t        |       }|r|j                  d      d   S y )N/r   )r/   split)r   r   s     r(   event_parentr4   P   s%    eDzz#q!!r*   c                 T    | j                  d      }|t        d      t        |      S )Nr   zEvent is missing a 'timestamp')r-   
ValueErrorfloat)r   tss     r(   event_timestampr9   W   s+    	;	B	z9::9r*   c                     t         j                   j                  t        |       t         j                  j                        S N)datetimefromtimestampr9   timezoneutcr.   s    r(   event_datetimer@   ^   s1    ** 1 1 5 5 r*   t1t2c                 (    || z
  j                         S r;   )total_seconds)rA   rB   s     r(   delta_secondsrE   d   s    G""$$r*   startfinishc                 >    t        t        |       t        |            S r;   )rE   r@   )rF   rG   s     r(   event_durationrI   h   s    .v0FGGr*   
start_timec           	          |j                         }t        |      }d}|r|d|j                  d      dz
  z  dz   z  }|j                  t	        ||      t        | t        |            |d       |S )N| r2      z`->)r   r   r   )copyr/   countupdaterI   rE   r@   )rJ   rF   rG   recordr   r   s         r(   event_recordrS   l   sv    
 [[]FeDF#C1,-55
MM#E62$Z1FG	
 Mr*   c                     d| z  S )NzTotal Time: %3.5f seconds
r!   )r   s    r(   total_time_recordrU      s    (:55r*   c                   P    e Zd ZdZd	dedee   fdZdeeeef      fdZ	de
fdZy)
SystemctlReaderzQ
    Class for dealing with all systemctl subp calls in a consistent manner.
    Nproperty	parameterc                     d | _         t        j                  d      dg| _        |r| j                  j	                  |       | j                  j                  d|dg       | j                         | _        y )N	systemctlshowz-pz--timestamp=us+utc)stdoutr   whichargsappendextend_subpr   )selfrX   rY   s      r(   __init__zSystemctlReader.__init__   s^    %)**[16:	IIY' 			$*>?@
 zz|r*   r   c                     	 t        j                   | j                  d      \  }}|r|S || _        y# t        $ r}|cY d}~S d}~ww xY w)z
        Make a subp call based on set args and handle errors by setting
        failure code

        :return: whether the subp call failed or not
        TcaptureN)r   r_   r]   	Exception)rc   valueerrsystemctl_fails       r(   rb   zSystemctlReader._subp   sG    	"499d;JE3
DK 	"!!	"s   '2 2 	AAAAc                    | j                   r$t        dj                  | j                               | j                  t        d      | j                  j	                  d      d   j                         }|j                         rt        |      dz  }|S t        j                  j                  |d      j                  t        j                  j                        j                         }|S )z{
        If subp call succeeded, return the timestamp from subp as a float.

        :return: timestamp as a float
        zBSubprocess call to systemctl has failed, returning error code ({})z.stdout of subprocess call to systemctl is None=rN   i@B z%a %Y-%m-%d %H:%M:%S.%f %Z)tzinfo)r   RuntimeErrorr%   r]   r3   strip	isnumericr7   r<   strptimer$   r>   r?   r   )rc   valr   s      r(   convert_val_to_floatz$SystemctlReader.convert_val_to_float   s     <<,,2F4<<,@  ;;@  kk$Q'--/==? c
W,I  !!**30LM 1 1 5 56  r*   r;   )__name__
__module____qualname____doc__strr   rd   r	   rh   rb   r7   rt   r!   r*   r(   rW   rW      sD    $ $# $$"xc9n 56 " +e +r*   rW   c                      t               r
t               S t        j                         s'dt        j                         d   j                         v r
t               S t        S )a)  
    Determine which init system a particular linux distro is using.
    Each init system (systemd, etc) has a different way of
    providing timestamps.

    :return: timestamps of kernelboot, kernelendboot, and cloud-initstart
    or TIMESTAMP_UNKNOWN if the timestamps cannot be retrieved.
    gentoosystem)r   gather_timestamps_using_systemdr   
is_FreeBSDsystem_infolowergather_timestamps_using_dmesgTIMESTAMP_UNKNOWNr!   r*   r(   dist_check_timestampr      sM     ~.00 H(8(8(:8(D(J(J(LL,.. r*   c                     	 t        j                   dgd      \  } }| d   j                         }|D ]  }|j                  d      j                  d      dk7  s'|j                  d      j	                         }|d   j                  d	      }t        |      }t        t        j                               t        t        j                               z
  }||z   }t        |||fc S  	 t        S # t        $ r Y t        S w xY w)
a  
    Gather timestamps that corresponds to kernel begin initialization,
    kernel finish initialization using dmesg as opposed to systemctl

    :return: the two timestamps plus a dummy timestamp to keep consistency
    with gather_timestamps_using_systemd
    dmesgTrf   r   zUTF-8userr   rN   ])r   
splitlinesdecodefindr3   rp   r7   timer   uptimeSUCCESS_CODErh   r   )	data_split_entriesr&   splitupstrippeduser_space_timestampkernel_start
kernel_ends	            r(   r   r      s    ))WIt4aQ**, 	JAxx %%f-3((7+113"1:++C0 (-X$$TYY[1E$++-4HH),@@
 $\:zII	J"   s   AC+ B	C+ #C+ +	C<;C<c                     	 t        j                         r3t        d      j                         } t        d      j                         }n2t        d      j                         } t        d      j                         }t        d      j                         |z
  }t        dd      j                         |z
  }t        j                         rt        nt        }|| | |z   | |z   fS # t        $ r}t        |       t        cY d}~S d}~ww xY w)z
    Gather timestamps that corresponds to kernel begin initialization,
    kernel finish initialization. and cloud-init systemd unit activation

    :return: the three timesread_propertystamps
    UserspaceTimestampUserspaceTimestampMonotonicKernelTimestampKernelTimestampMonotonicInactiveExitTimestampMonotoniczcloud-init-localN)	r   is_containerrW   rt   rh   printr   CONTAINER_CODEr   )r   monotonic_offsetr   cloudinit_sysdestatuss         r(   r}   r}     s!   &!  +$""$   /- ""$  +!""$   /* ""$  -""$ 	 02D""$ 	  $002^F 	z!~%	   ! 	a  !s   B2C   	D)C?9D?Deventsprint_formatc                    t        | d       }g }d}d}i }g }g }t        t        |            D ]  }	| |	   }
	 | |	dz      }t	        |
      dk(  r|rE|
j                  d      dk(  r1|j                  t        |             |j                  |       g }d}d}|t        |
      }||t        |
      <   t        |
      t        |      k(  rFt	        |      d	k(  st        t        |      }|j                  t        |t        ||
|                   |j                  d
|
j                  d      z         |j                  |
       |j                         }t        |
      t        |      k(  rI|s@t        |||
      }|j                  t        d|      dz          ||j                  d      xs dz  }|j                  |        |j                  t        |             |j                  |       |S # t        $ r d}Y w xY w)as  
    Take in raw events and create parent-child dependencies between events
    in order to order events in chronological order.

    :param events: JSONs from dump that represents events taken from logs
    :param print_format: formatting to represent event, time stamp,
    and time taken by the event in one line

    :return: boot records ordered chronologically
    c                     | d   S )Nr   r!   )xs    r(   <lambda>z"generate_records.<locals>.<lambda>W  s
    ; r*   )keyNg        rN   rF   r   z
init-localrG   zStarting stage: %szFinished stage: (%n) %d seconds
r   )sortedrangelen
IndexErrorr   r-   r`   rU   r@   r4   r/   r
   r   r)   rS   pop)r   r   sorted_eventsrecordsrJ   r   stage_start_timeboot_recordsunprocessedr   r   next_evtprev_evtrR   s                 r(   generate_recordsr   H  s    6'?@MGJJLK3}%& 2-q		a!e}H e'599V,<0<=##G,!
 
!+E2
8B e!45 % Jx$88h'83  $D(3HNN%((UHE 3eii6GGH""5)"(H% Jx$88)*hFFNN%=v  &**W"5"<<J ""8,e2-h NN$Z01 e  	H	s   G55HHc                     t        | |      S )a<  
    A passthrough method that makes it easier to call generate_records()

    :param events: JSONs from dump that represents events taken from logs
    :param print_format: formatting to represent event, time stamp,
    and time taken by the event in one line

    :return: boot records ordered chronologically
    )r   )r   )r   r   s     r(   show_eventsr     s     F>>r*   infilec                    | j                         }|j                         sAt        j                  j	                  d| j
                  z         t        j                  d       	 t        j                  |      |fS # t        $ r d|fcY S w xY w)z
    Takes in a log file, read it, and convert to json.

    :param infile: The Log file to be read

    :return: json version of logfile, raw file
    zEmpty file %s
rN   N)
readrp   sysstderrwriter   exitjsonloadsr6   )r   r   s     r(   load_events_infiler     sq     ;;=D::<

*V[[89zz$%% Tzs   #A: :B
	B
)z(%n) %d seconds in %I%D)+r<   r   r   r   typingr   r   r   r   r   r   r	   r
   	cloudinitr   r   cloudinit.distrosr   r"   r   	FAIL_CODEr   r   Eventry   r)   r/   r   r4   r7   r9   r@   rE   rI   rS   rU   rW   r   r   r}   r   r   r   r!   r*   r(   <module>r      sf     
  D D D   *, 










 	B+ s 5 S huo (3- huo (3-  HSM 5 U % H$5$5 %h'' %X->-> %5 %H% H H5 H!!  	*6% 6C 6R RjeCu$<= ,uS%-E'F >6sE5%/G)H 6v 2MKMM 
$s)_M`
?U 
?3 
?4S	? 
?r eHSM3,>&? r*   