Files
zuplates/main.py

68 lines
2.5 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():
api = ZabbixAPI(url=config["zabbix"]["api"]["url"])
api.login(token=config["zabbix"]["api"]["token"])
api_templates = api.template.get(selectHosts=["hostid", "name"], sortfield="name")
return [t for t in api_templates if len(t["hosts"]) > 0]
def overview():
api_templates = get_api_templates()
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")
def check():
api_templates = get_api_templates()
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 = args_parser.parse_args()
if args.overview:
overview()
elif args.check:
check()
if __name__ == "__main__":
main()