o x[h,+ @sddlZddlZddlZddlZddlmZmZddlmZdddddd d d d d dd Z dZ dZ dZ e dddfZ ddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&ZGd'd(d(Zd)d*Zd+d,Zd-d.Z /d6d0d1Zd2d3Zd4d5ZdS)7N)subputil) uses_systemddelta descriptionelapsed event_typeindentlevelnameoriginresult timestamp total_time) z%dz%Dz%Ez%ez%Iz%lz%nz%oz%rz%tz%T successfulfailure containercCsTtD]\}}||vr!|dvr||d|}q||d|}q|jdi|S)N)rrrz {%s:08.5f}z{%s}) format_keyitemsreplaceformat)msgeventijrr8/usr/lib/python3/dist-packages/cloudinit/analyze/show.py format_record6srcC|r|dSdS)Nr getrrrr event_nameA r#cCr)Nrr r"rrrrGr$cCs|r t|ddSdS)N/r)r#splitr"rrr event_parentMsr'cCst|dSNr)floatr!r"rrrevent_timestampSsr*cCstjt|tjjSN)datetime fromtimestampr*timezoneutcr"rrrevent_datetimeWs r0cCs ||Sr+) total_seconds)t1t2rrr delta_seconds]s r4cCstt|t|Sr+)r4r0)startfinishrrrevent_durationasr7c CsH|}|t||t|t|ddt|dddd|S)N| r%z`->)rrr )copyupdater7r4r0r#count) start_timer5r6recordrrr event_recordes r@cCsd|S)NzTotal Time: %3.5f seconds r)rrrrtotal_time_recordrsrAc@s*eZdZdZd ddZddZddZdS) SystemctlReaderzQ Class for dealing with all systemctl subp calls in a consistent manner. NcCsDd|_tddg|_|r|j||jd|g||_dS)N systemctlshowz-p)epochrwhichargsappendextendr)selfproperty parameterrrr__init__{s  zSystemctlReader.__init__c CsTztj|jdd\}}|r|WS||_WdSty)}z|WYd}~Sd}~ww)z Make a subp call based on set args and handle errors by setting failure code :return: whether the subp call failed or not TcaptureN)rrGrE Exception)rJvalueerrsystemctl_failrrrrszSystemctlReader.subpcCs2|jr td|j|jdd}t|dS)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 ({})=r:i@B)r RuntimeErrorrrEr&r))rJrrrrparse_epoch_as_floats  z$SystemctlReader.parse_epoch_as_floatr+)__name__ __module__ __qualname____doc__rMrrVrrrrrBvs   rBcCs2trtStsdtdvrtStS)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. gentoosystem)rgather_timestamps_using_systemdr is_FreeBSD system_infolowergather_timestamps_using_dmesgTIMESTAMP_UNKNOWNrrrrdist_check_timestamps  rcc CszMtjdgdd\}}|d}|D]7}|dddkrJ|d}|dd }t|}tttt }||}t |||fWSqWt St yWYt Sw) 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 dmesgTrNrzUTF-8userrr:]) r splitlinesdecodefindr&stripr)timeruptime SUCCESS_CODErPrb) data_ split_entriesrsplitupstrippeduser_space_timestamp kernel_start kernel_endrrrras$  rac Cstttt}z!td}tdd}|}t}tr$t}||}||}Wnt yE}z t |t WYd}~Sd}~ww||||fS)z Gather timestamps that corresponds to kernel begin initialization, kernel finish initialization. and cloud-init systemd unit activation :return: the three timestamps UserspaceTimestampMonotonicInactiveExitTimestampMonotoniczcloud-init-localN) r)rkrrlrBrVrm is_containerCONTAINER_CODErPprintrb)rt delta_k_end delta_ci_s base_timestatusrucloudinit_sysderrrr]s0  r](%n) %d seconds in %I%Dc Cst|ddd}g}d}d}i}g}g}tt|D]} || } z|| d} Wn ty3d} Ynwt| dkr|rU| dd krU|t|||g}d}d}|durct| }||t | <t | t | kr~t| d kr}|t |t || | q|d | d|| q| } t | t | krt || | } |t d | d || d7}q|| q|t||||S)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 cSs|dSr(r)xrrrsz"generate_records..)keyNgr:r5r z init-localr6zStarting stage: %szFinished stage: (%n) %d seconds r)sortedrangelen IndexErrorrr!rHrAr0r'r#rr@pop)events print_format sorted_eventsrecordsr>rstage_start_time boot_records unprocessedrrnext_evtprev_evtr?rrrgenerate_recordssb          rcCs t||dS)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)rrrrr show_eventsKs rcCsX|}|stjd|jtdzt||fWSt y+d|fYSw)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 zEmpty file %s r:N) readrjsysstderrwriter exitjsonloads ValueError)infilernrrrload_events_infileXs   r)r)r,rrrk cloudinitrrcloudinit.distrosrrrm FAIL_CODEryrbrr#rr'r*r0r4r7r@rArBrcrar]rrrrrrrsP    5# J