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

View File

@@ -24,7 +24,7 @@ zabbix:
* user must have access to API `template.get` * user must have access to API `template.get`
## usage ## usage
* get an overview of *used* templates: `./main.py --overview` * get an overview of *used* templates: `./main.py --overview --server foo`
``` ```
Borg Backup Passive Borg Backup Passive
Vendor: Vendor:
@@ -41,7 +41,15 @@ FreeBSD by Zabbix agent
router.exmaple.com router.exmaple.com
... ...
``` ```
* check for new versions and download known templates: `./main.py --check` * get an overview of *used* templates in machine readable format: `./main.py --overview --server foo --format machine`
* copy&paste'able into libre calc
```
name vendor version uuid hosts known
Borg Backup Passive 3077e7298167465b96d30503bcff4769 devloop.de,blah.example.com,moep.example.com no
FreeBSD by Zabbix agent Zabbix 7.0-2 a3dc630729e443139f4e608954fa6e19 router.exmaple.com yes
...
```
* check for new versions and download known templates: `./main.py --check --server foo`
``` ```
found update for Template Module Generic SNMPv2 found update for Template Module Generic SNMPv2
found update for Template OS FreeBSD found update for Template OS FreeBSD

32
main.py
View File

@@ -24,8 +24,14 @@ def get_api_templates(server):
api_templates = api.template.get(selectHosts=["hostid", "name"], sortfield="name") api_templates = api.template.get(selectHosts=["hostid", "name"], sortfield="name")
return [t for t in api_templates if len(t["hosts"]) > 0] 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) 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: for api_template in api_templates:
print(api_template["name"]) print(api_template["name"])
print(f"\tVendor: {api_template['vendor_name']}") print(f"\tVendor: {api_template['vendor_name']}")
@@ -37,6 +43,27 @@ def overview(server):
if not api_template['uuid'] in templates["templates"]: if not api_template['uuid'] in templates["templates"]:
print("\t!!! not found in templates.yml") 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): def check(server):
api_templates = get_api_templates(server) api_templates = get_api_templates(server)
for api_template in api_templates: 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("--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("--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("--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() args = args_parser.parse_args()
if args.overview: if args.overview:
overview(args.server) overview(args.server, args.format)
elif args.check: elif args.check:
check(args.server) check(args.server)