# Copyright (C) 2017 Canonical Ltd.
#
# This file is part of cloud-init. See LICENSE file for license information.
import argparse
import re
import sys
from datetime import datetime, timezone
from typing import IO, Dict, List, Optional, Tuple, Union
from cloudinit.analyze import dump, show
from cloudinit.atomic_helper import json_dumps
def get_parser(
parser: Optional[argparse.ArgumentParser] = None,
) -> argparse.ArgumentParser:
if not parser:
parser = argparse.ArgumentParser(
prog="cloudinit-analyze",
description="Devel tool: Analyze cloud-init logs and data",
)
subparsers = parser.add_subparsers(title="Subcommands", dest="subcommand")
subparsers.required = True
parser_blame = subparsers.add_parser(
"blame", help="Print list of executed stages ordered by time to init"
)
parser_blame.add_argument(
"-i",
"--infile",
action="store",
dest="infile",
default="/var/log/cloud-init.log",
help="specify where to read input.",
)
parser_blame.add_argument(
"-o",
"--outfile",
action="store",
dest="outfile",
default="-",
help="specify where to write output. ",
)
parser_blame.set_defaults(action=("blame", analyze_blame))
parser_show = subparsers.add_parser(
"show", help="Print list of in-order events during execution"
)
parser_show.add_argument(
"-f",
"--format",
action="store",
dest="print_format",
default="%I%D @%Es +%ds",
help="specify formatting of output.",
)
parser_show.add_argument(
"-i",
"--infile",
action="store",
dest="infile",
default="/var/log/cloud-init.log",
help="specify where to read input.",
)
parser_show.add_argument(
"-o",
"--outfile",
action="store",
dest="outfile",
default="-",
help="specify where to write output.",
)
parser_show.set_defaults(action=("show", analyze_show))
parser_dump = subparsers.add_parser(
"dump", help="Dump cloud-init events in JSON format"
)
parser_dump.add_argument(
"-i",
"--infile",
action="store",
dest="infile",
default="/var/log/cloud-init.log",
help="specify where to read input. ",
)
parser_dump.add_argument(
"-o",
"--outfile",
action="store",
dest="outfile",
default="-",
help="specify where to write output. ",
)
parser_dump.set_defaults(action=("dump", analyze_dump))
parser_boot = subparsers.add_parser(
"boot", help="Print list of boot times for kernel and cloud-init"
)
parser_boot.add_argument(
"-i",
"--infile",
action="store",
dest="infile",
default="/var/log/cloud-init.log",
help="specify where to read input. ",
)
parser_boot.add_argument(
"-o",
"--outfile",
action="store",
dest="outfile",
default="-",
help="specify where to write output.",
)
parser_boot.set_defaults(action=("boot", analyze_boot))
return parser
def analyze_boot(name: str, args: argparse.Namespace) -> str:
"""Report a list of how long different boot operations took.
For Example:
-- Most Recent Boot Record --
Kernel Started at: