Files
zuplates/main.py

69 lines
2.7 KiB
Python
Executable File

#!/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"])
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):
api_templates = get_api_templates(server)
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(server):
api_templates = get_api_templates(server)
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)
args = args_parser.parse_args()
if args.overview:
overview(args.server)
elif args.check:
check(args.server)
if __name__ == "__main__":
main()