From 810df5157c3f2ee69254f9934b40f27b37e14a50 Mon Sep 17 00:00:00 2001 From: damage Date: Fri, 31 Oct 2025 22:23:47 +0100 Subject: [PATCH] init ci --- .gitignore | 3 ++ .vscode/launch.json | 24 ++++++++++++++++ downloads/.keep | 0 main.py | 68 +++++++++++++++++++++++++++++++++++++++++++++ templates.yml | 11 ++++++++ 5 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 downloads/.keep create mode 100644 main.py create mode 100644 templates.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..46ddf99 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.venv +/config.yml +/downloads/*.yml diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..140e398 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "overview", + "type": "debugpy", + "request": "launch", + "program": "main.py", + "console": "integratedTerminal", + "args": "--overview" + }, + { + "name": "check", + "type": "debugpy", + "request": "launch", + "program": "main.py", + "console": "integratedTerminal", + "args": "--check" + } + ] +} \ No newline at end of file diff --git a/downloads/.keep b/downloads/.keep new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..78bbd67 --- /dev/null +++ b/main.py @@ -0,0 +1,68 @@ +#!/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() \ No newline at end of file diff --git a/templates.yml b/templates.yml new file mode 100644 index 0000000..257fe89 --- /dev/null +++ b/templates.yml @@ -0,0 +1,11 @@ +templates: + a3dc630729e443139f4e608954fa6e19: + vendor: Zabbix + path: os/freebsd/template_os_freebsd.yaml + 5630ec1b1baf449abe1bc5521f85fe6c: + vendor: Zabbix + path: app/certificate_agent2/template_app_certificate_agent2.yaml +vendors: + Zabbix: + raw: https://git.zabbix.com/projects/ZBX/repos/zabbix/raw/templates/ + qry: at=refs%2Fheads%2Frelease%2F7.0