cli: Add 'ara host metrics'
This provides a first implementation for retrieving host metrics through the CLI. Change-Id: Iaf2cb3960a60113b6068cc36c236b6ff733585b9
This commit is contained in:
parent
7e6635da73
commit
4b5f09df87
155
ara/cli/host.py
155
ara/cli/host.py
|
@ -271,3 +271,158 @@ class HostDelete(Command):
|
|||
|
||||
# TODO: Improve client to be better at handling exceptions
|
||||
client.delete("/api/v1/hosts/%s" % args.host_id)
|
||||
|
||||
|
||||
class HostMetrics(Lister):
|
||||
""" Provides metrics about hosts """
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(HostMetrics, self).get_parser(prog_name)
|
||||
parser = global_arguments(parser)
|
||||
# fmt: off
|
||||
# Host search arguments and ordering as per ara.api.filters.HostFilter
|
||||
# TODO: non-exhaustive (searching for failed, ok, unreachable, etc.)
|
||||
parser.add_argument(
|
||||
"--name",
|
||||
metavar="<name>",
|
||||
default=None,
|
||||
help=("Filter for hosts matching the provided name (full or partial)"),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--playbook",
|
||||
metavar="<playbook_id>",
|
||||
default=None,
|
||||
help=("Filter for hosts for a specified playbook id"),
|
||||
)
|
||||
|
||||
changed = parser.add_mutually_exclusive_group()
|
||||
changed.add_argument(
|
||||
"--with-changed",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help=("Filter for hosts with changed results")
|
||||
)
|
||||
changed.add_argument(
|
||||
"--without-changed",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help=("Filter out hosts without changed results")
|
||||
)
|
||||
|
||||
failed = parser.add_mutually_exclusive_group()
|
||||
failed.add_argument(
|
||||
"--with-failed",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help=("Filter for hosts with failed results")
|
||||
)
|
||||
failed.add_argument(
|
||||
"--without-failed",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help=("Filter out hosts without failed results")
|
||||
)
|
||||
|
||||
unreachable = parser.add_mutually_exclusive_group()
|
||||
unreachable.add_argument(
|
||||
"--with-unreachable",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help=("Filter for hosts with unreachable results")
|
||||
)
|
||||
unreachable.add_argument(
|
||||
"--without-unreachable",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help=("Filter out hosts without unreachable results")
|
||||
)
|
||||
parser.add_argument(
|
||||
"--order",
|
||||
metavar="<order>",
|
||||
default="-updated",
|
||||
help=(
|
||||
"Orders hosts by a field ('id', 'created', 'updated', 'name')\n"
|
||||
"Defaults to '-updated' descending so the most recent host is at the top.\n"
|
||||
"The order can be reversed by omitting the '-': ara host list --order=updated\n"
|
||||
"This influences the API request, not the ordering of the metrics."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--limit",
|
||||
metavar="<limit>",
|
||||
default=os.environ.get("ARA_CLI_LIMIT", 1000),
|
||||
help=("Return metrics for the first <limit> determined by the ordering. Defaults to ARA_CLI_LIMIT or 1000.")
|
||||
)
|
||||
# fmt: on
|
||||
return parser
|
||||
|
||||
def take_action(self, args):
|
||||
client = get_client(
|
||||
client=args.client,
|
||||
endpoint=args.server,
|
||||
timeout=args.timeout,
|
||||
username=args.username,
|
||||
password=args.password,
|
||||
verify=False if args.insecure else True,
|
||||
run_sql_migrations=False,
|
||||
)
|
||||
query = {}
|
||||
if args.name is not None:
|
||||
query["name"] = args.name
|
||||
|
||||
if args.playbook is not None:
|
||||
query["playbook"] = args.playbook
|
||||
|
||||
if args.with_changed:
|
||||
query["changed__gt"] = 0
|
||||
if args.without_changed:
|
||||
query["changed__lt"] = 1
|
||||
if args.with_failed:
|
||||
query["failed__gt"] = 0
|
||||
if args.without_failed:
|
||||
query["failed__lt"] = 1
|
||||
if args.with_unreachable:
|
||||
query["unreachable__gt"] = 0
|
||||
if args.without_unreachable:
|
||||
query["unreachable__lt"] = 1
|
||||
|
||||
query["order"] = args.order
|
||||
query["limit"] = args.limit
|
||||
|
||||
resp = client.get("/api/v1/hosts", **query)
|
||||
|
||||
# Group hosts by name
|
||||
hosts = {}
|
||||
for host in resp["results"]:
|
||||
name = host["name"]
|
||||
if name not in hosts:
|
||||
hosts[name] = []
|
||||
hosts[name].append(host)
|
||||
|
||||
data = {}
|
||||
for name, host_results in hosts.items():
|
||||
data[name] = {
|
||||
"name": name,
|
||||
"count": len(host_results),
|
||||
"changed": 0,
|
||||
"failed": 0,
|
||||
"ok": 0,
|
||||
"skipped": 0,
|
||||
"unreachable": 0,
|
||||
}
|
||||
|
||||
for host in host_results:
|
||||
for status in ["changed", "failed", "ok", "skipped", "unreachable"]:
|
||||
data[name][status] += host[status]
|
||||
|
||||
columns = ("name", "count", "changed", "failed", "ok", "skipped", "unreachable")
|
||||
# fmt: off
|
||||
return (
|
||||
columns, (
|
||||
[data[host][column] for column in columns]
|
||||
for host in sorted(data.keys())
|
||||
)
|
||||
)
|
||||
# fmt: on
|
||||
|
|
|
@ -240,6 +240,38 @@ ara host delete
|
|||
|
||||
.. command-output:: ara host delete --help
|
||||
|
||||
ara host metrics
|
||||
----------------
|
||||
|
||||
.. command-output:: ara host metrics --help
|
||||
|
||||
Examples:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# Return metrics about more than the last 1000 hosts
|
||||
ara host metrics --limit 10000
|
||||
|
||||
# Return host metrics in json or csv
|
||||
ara host metrics -f json
|
||||
ara host metrics -f csv
|
||||
|
||||
# Return metrics for hosts matching a name
|
||||
ara host metrics --name localhost
|
||||
|
||||
# Return metrics for hosts involved in a specific playbook
|
||||
ara host metrics --playbook 9001
|
||||
|
||||
# Return metrics only for hosts with changed, failed or unreachable results
|
||||
ara host metrics --with-changed
|
||||
ara host metrics --with-failed
|
||||
ara host metrics --with-unreachable
|
||||
|
||||
# Return metrics only for hosts without changed, failed or unreachable results
|
||||
ara host metrics --without-changed
|
||||
ara host metrics --without-failed
|
||||
ara host metrics --without-unreachable
|
||||
|
||||
ara record list
|
||||
---------------
|
||||
|
||||
|
|
|
@ -46,6 +46,7 @@ ara.cli =
|
|||
host list = ara.cli.host:HostList
|
||||
host show = ara.cli.host:HostShow
|
||||
host delete = ara.cli.host:HostDelete
|
||||
host metrics = ara.cli.host:HostMetrics
|
||||
record list = ara.cli.record:RecordList
|
||||
record show = ara.cli.record:RecordShow
|
||||
record delete = ara.cli.record:RecordDelete
|
||||
|
|
Loading…
Reference in New Issue