Files
zuplates/main.py

97 lines
3.7 KiB
Python
Raw Normal View History

2025-10-31 22:23:47 +01:00
#!/usr/bin/env python3
# https://www.zabbix.com/documentation/current/en/manual/api/reference
from zabbix_utils import ZabbixAPI
import yaml
import requests
import argparse
with (open("config.yml")) as y:
config = yaml.safe_load(y)
with (open("templates.yml")) as y:
templates = yaml.safe_load(y)
def get_raw_url(api_template):
template = templates["templates"][api_template["uuid"]]
vendor = templates["vendors"][template["vendor"]]
return f"\t{vendor['raw']}{template['path']}?{vendor['qry']}"
def get_api_templates(server):
api = ZabbixAPI(url=config["zabbix"]["servers"][server]["url"])
api.login(token=config["zabbix"]["servers"][server]["token"])
2025-10-31 22:23:47 +01:00
api_templates = api.template.get(selectHosts=["hostid", "name"], sortfield="name")
return [t for t in api_templates if len(t["hosts"]) > 0]
2025-11-01 13:38:31 +01:00
def overview(server, format):
api_templates = get_api_templates(server)
2025-11-01 13:38:31 +01:00
if format == "human":
overview_output_human(api_templates)
elif format == "machine":
overview_output_machine(api_templates)
def overview_output_human(api_templates):
2025-10-31 22:23:47 +01:00
for api_template in api_templates:
print(api_template["name"])
print(f"\tVendor: {api_template['vendor_name']}")
print(f"\tVersion: {api_template['vendor_version']}")
print(f"\tUUID: {api_template['uuid']}")
print("\tused by hosts:")
for host in api_template["hosts"]:
print(f"\t\t{host['name']}")
if not api_template['uuid'] in templates["templates"]:
print("\t!!! not found in templates.yml")
2025-11-01 13:38:31 +01:00
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)
2025-10-31 22:23:47 +01:00
for api_template in api_templates:
if api_template["uuid"] in templates["templates"]:
raw_url = get_raw_url(api_template)
resp = requests.get(raw_url)
resp.raise_for_status()
remote_templates = yaml.safe_load(resp.text)
for remote_template in remote_templates["zabbix_export"]["templates"]:
if remote_template["uuid"] == api_template["uuid"]:
if remote_template["vendor"]["version"] != api_template["vendor_version"]:
with open(f"downloads/{api_template["name"]}.yml", "w") as lt:
lt.write(resp.text)
print(f"found update for {api_template["name"]}")
def main():
args_parser = argparse.ArgumentParser()
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)
2025-11-01 13:38:31 +01:00
args_parser.add_argument("--format", type=str, help="output format: machine or human, default to human", default="human")
2025-10-31 22:23:47 +01:00
args = args_parser.parse_args()
if args.overview:
2025-11-01 13:38:31 +01:00
overview(args.server, args.format)
2025-10-31 22:23:47 +01:00
elif args.check:
check(args.server)
2025-10-31 22:23:47 +01:00
if __name__ == "__main__":
main()