oB]c@sddlZddlZddlZddlZddlZddlZddlmZddlm Z i dd6dd6dd 6d d 6d d 6dd6dd6dd6dd6dd6dd6Z dj ge j D]*\Z Zdje jdde^qZdZdZd ZedddfZd!Zd"Zd#Zd$Zd%Zd&Zd'Zd(Zd)Zd*Zd+Z d,e!fd-YZ"d.Z#d/Z$d0Z%e&d1e&e&d2Z'd3Z(d4Z)dS(5iN(tutil(t uses_systemdtdeltas%dt descriptions%Dtelapseds%Et event_types%etindents%Itlevels%ltnames%ntorigins%otresults%rt timestamps%tt total_times%Tt s{0}: {1}t%s%%t successfultfailuret containercCstxdtjD]V\}}||kr |dkrJ|j|d|}qc|j|d|}q q W|j|S(NRRR s {%s:08.5f}s{%s}(RRs timestamp(t format_keytitemstreplacetformat(tmsgteventtitj((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyt format_record=s   cCstd|jD}|d}g}xc|D][}|d}tjj|}tj|djd}tj |||j |q3W|S(Ncss-|]#\}}|dkr||fVqdS(tcontentN(scontent((t.0tktv((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pys IstfilestpathRtascii( tdictRtosR tbasenametbase64t b64decodetdecodeRt write_filetappend(RRRtsavedtftfnametfn_localtfcontent((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pytdump_event_filesHs   cCs|r|jdSdS(NR(tgettNone(R((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyt event_nameVs cCs|r|jdSdS(NR(R0R1(R((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyR\s cCs!|rt|jddSdS(Nt/i(R2tsplitR1(R((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyt event_parentbscCst|jdS(NR (tfloatR0(R((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pytevent_timestamphscCstjjt|S(N(tdatetimetutcfromtimestampR7(R((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pytevent_datetimelscCs||jS(N(t total_seconds(tt1tt2((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyt delta_secondspscCstt|t|S(N(R>R:(tstarttfinish((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pytevent_durationtscCsi|j}|jit||d6t|t|d6ddt|jdddd6|S( NRRt|R R3is`->R(tcopytupdateRAR>R:R2tcount(t start_timeR?R@trecord((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyt event_recordxs   *cCsd|S(NsTotal Time: %3.5f seconds ((R ((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyttotal_time_recordstSystemctlReadercBs,eZdZddZdZdZRS(sQ Class for dealing with all systemctl subp calls in a consistent manner. cCsZd|_ddg|_|r1|jj|n|jjd|g|j|_dS(Ns/bin/systemctltshows-p(R1tepochtargsR)textendtsubpR(tselftpropertyt parameter((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyt__init__s  cCsTy9tj|jdt\}}|r+|S||_dSWntk rO}|SXdS(s Make a subp call based on set args and handle errors by setting failure code :return: whether the subp call failed or not tcaptureN(RRORMtTrueRLR1t Exception(RPtvalueterrtsystemctl_fail((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyROs cCsH|jr$tdj|jn|jjdd}t|dS(s{ If subp call succeeded, return the timestamp from subp as a float. :return: timestamp as a float sBSubprocess call to systemctl has failed, returning error code ({})t=ii@B(Rt RuntimeErrorRRLR4R6(RPR ((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pytparse_epoch_as_floats  N(t__name__t __module__t__doc__R1RSROR\(((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyRJs cCsCtrtStjs8dtjdjkr?tStS(s2 Determine which init system a particular linux distro is using. Each init system (systemd, upstart, 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. tgentootsystem(Rtgather_timestamps_using_systemdRt is_FreeBSDt system_infotlowertgather_timestamps_using_dmesgtTIMESTAMP_UNKNOWN(((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pytdist_check_timestamps c Csytjdgdt\}}|dj}x|D]}|jdjddkr8|jdj}|djd}t|}tt j ttj }||}t |||fSq8WWnt k rnXt S( s 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 tdmesgRTisUTF-8tuseriit](RRORUt splitlinesR'tfindR4tstripR6ttimetuptimet SUCCESS_CODERVRg( tdatat_t split_entriesRtsplituptstrippedtuser_space_timestampt kernel_startt kernel_end((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyRfs  "   c Cs/ttjttj}ytdj}tddj}|}t}tjrtj dkrttjttj }yt j d}|j }Wn(tk r}tdj|nXt}qt}n||}||}Wntk r} | GHtSX||||fS(s Gather timestamps that corresponds to kernel begin initialization, kernel finish initialization. and cloud-init systemd unit activation :return: the three timestamps tUserspaceTimestampMonotonictInactiveExitTimestampMonotonicscloud-init-localis/proc/1/cmdlinesBCould not determine container boot time from /proc/1/cmdline. ({})(ii(R6RoRRpRJR\Rqt is_containertsyst version_infot monotonicR#tstattst_atimetOSErrorR[RtCONTAINER_CODEt FAIL_CODERVRg( Rxt delta_k_endt delta_ci_st base_timetstatust file_statRXRytcloudinit_sysdte((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyRbs0" "     s(%n) %d seconds in %I%DcCsNt|dd}g}d }d}i} g} g} g} xtdt|D]} || }y|| d}Wntk rd }nXt|dkr|jd| kr|jt|| j|g}d }d}n|d krg} t |}|| t |1sgiiR?RR@sStarting stage: %ssFinished stage: (%n) %d secondss RN(tsortedR1trangetlent IndexErrorRR0R)RIR:R5R2RRHtpop(teventst blame_sortt print_formatt dump_filest log_datafilest sorted_eventstrecordsRFR tstage_start_timet stages_seent boot_recordst unprocessedRRtnext_evttprev_evtRG((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pytgenerate_records sZ            cCst|d|S(s< 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(RR((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pyt show_eventsls cCsB|j}ytj||fSWntk r=d|fSXdS(s 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 N(treadtjsontloadst ValueErrorR1(tinfileRr((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pytload_events_infileys   (*R%R8RR#RoR}t cloudinitRtcloudinit.distrosRRtjoinRRRRRtformatting_helpRqRRRgRR/R2RR5R7R:R>RARHRItobjectRJRhRfRbtFalseRRR(((s:/usr/lib/python2.7/site-packages/cloudinit/analyze/show.pytsZ        =         3  /J