added format option

This commit is contained in:
2025-11-01 13:38:31 +01:00
parent 419f7acd4b
commit 85079c9272
2 changed files with 40 additions and 4 deletions

32
main.py
View File

@@ -24,8 +24,14 @@ def get_api_templates(server):
api_templates = api.template.get(selectHosts=["hostid", "name"], sortfield="name")
return [t for t in api_templates if len(t["hosts"]) > 0]
def overview(server):
def overview(server, format):
api_templates = get_api_templates(server)
if format == "human":
overview_output_human(api_templates)
elif format == "machine":
overview_output_machine(api_templates)
def overview_output_human(api_templates):
for api_template in api_templates:
print(api_template["name"])
print(f"\tVendor: {api_template['vendor_name']}")
@@ -37,6 +43,27 @@ def overview(server):
if not api_template['uuid'] in templates["templates"]:
print("\t!!! not found in templates.yml")
def overview_output_machine(api_templates):
print("name", end="\t")
print("vendor", end="\t")
print("version", end="\t")
print("uuid", end="\t")
print("hosts", end="\t")
print("known")
for api_template in api_templates:
print(api_template["name"], end="\t")
print(api_template["vendor_name"], end="\t")
print(api_template["vendor_version"], end="\t")
print(api_template["uuid"], end="\t")
for host in api_template["hosts"]:
print(host["name"], end=",")
print("\t", end="")
if api_template['uuid'] in templates["templates"]:
print("yes")
else:
print("no")
def check(server):
api_templates = get_api_templates(server)
for api_template in api_templates:
@@ -58,10 +85,11 @@ def main():
args_parser.add_argument("--overview", action="store_true", help="show overview of used templates")
args_parser.add_argument("--check", action="store_true", help="check known templates for updates")
args_parser.add_argument("--server", type=str, help="server to use from config.yml", required=True)
args_parser.add_argument("--format", type=str, help="output format: machine or human, default to human", default="human")
args = args_parser.parse_args()
if args.overview:
overview(args.server)
overview(args.server, args.format)
elif args.check:
check(args.server)