3 \�me% � @ s� d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZmZ d dl m Z ddd�Zdd � Zd d� Z dd � Zdd� Zdd� Zdd� Zedd�dd�Zedkr�e� Zej� Zej\ZZeee� dS )� N)�datetime)�IO)�dump�show)� json_dumpsc C sh | st jddd�} | jddd�}d|_|jdd d �}|jddd dddd� |jddd dddd� |jdtfd� |jddd �}|jddd dddd� |jddd dddd� |jddd dddd� |jdtfd� |jd d!d �}|jddd ddd"d� |jddd dddd� |jd t fd� |jd#d$d �}|jddd ddd"d� |jddd dddd� |jd#t fd� | S )%Nzcloudinit-analyzez,Devel tool: Analyze cloud-init logs and data)�prog�descriptionZSubcommandsZ subcommand)�title�destTZblamez5Print list of executed stages ordered by time to init)�helpz-iz--infileZstore�infilez/var/log/cloud-init.logzspecify where to read input.)�actionr �defaultr z-oz --outfile�outfile�-zspecify where to write output. )r r z.Print list of in-order events during executionz-fz--format�print_formatz%I%D @%Es +%dszspecify formatting of output.zspecify where to write output.r z%Dump cloud-init events in JSON formatzspecify where to read input. Zbootz2Print list of boot times for kernel and cloud-init)�argparse�ArgumentParserZadd_subparsersZrequiredZ add_parser�add_argumentZset_defaults� analyze_blame�analyze_show�analyze_dump�analyze_boot)�parserZ subparsersZparser_blameZparser_showZparser_dumpZparser_boot� r �/usr/lib/python3.6/__init__.py� get_parser s� r c C s� t |�\}}tj� }|\}}}}tj|�} tj|�} tj|�}y(dd� t|�D �d }tj|d �} W n tk r� d} tj}Y nX d}d}d}tj|tj|tj |i}|| }|| }| | ||||||| d � }|j || jf |�� t||� |S )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> c S s( g | ] }|d dkrd|d kr|�qS )�namez init-localzstarting searchr r )�.0�er r r � <listcomp>� s z analyze_boot.<locals>.<listcomp>� Z 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://cloudinit.readthedocs.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} ) Zk_s_tZk_e_tZk_rZbt_rZk_eZk_sZci_sysdZ ci_sysd_t�ci_start���) �configure_ior Zdist_check_timestampr Zutcfromtimestamp�_get_events� IndexErrorZ FAIL_CODEZCONTAINER_CODEZSUCCESS_CODE�write�format�clean_io)r �args�infh�outfhZkernel_infoZstatus_codeZkernel_startZ kernel_endZ ci_sysd_startZkernel_start_timestampZkernel_end_timestampZci_sysd_start_timestampZlast_init_localr"