50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""Validate Zabbix portfolio template and sample assets."""
|
||
|
|
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parents[1]
|
||
|
|
|
||
|
|
|
||
|
|
def load_json(path: Path) -> dict:
|
||
|
|
with path.open(encoding="utf-8") as handle:
|
||
|
|
return json.load(handle)
|
||
|
|
|
||
|
|
|
||
|
|
def validate_template(path: Path) -> None:
|
||
|
|
data = load_json(path)
|
||
|
|
for field in ["template", "items", "triggers"]:
|
||
|
|
if field not in data:
|
||
|
|
raise ValueError(f"{path}: missing {field}")
|
||
|
|
if not data["items"]:
|
||
|
|
raise ValueError(f"{path}: template must define at least one item")
|
||
|
|
for item in data["items"]:
|
||
|
|
for field in ["key", "name", "type", "value_type"]:
|
||
|
|
if field not in item:
|
||
|
|
raise ValueError(f"{path}: item missing {field}")
|
||
|
|
|
||
|
|
|
||
|
|
def validate_sample(path: Path) -> None:
|
||
|
|
data = load_json(path)
|
||
|
|
for field in ["host", "proxy", "mode", "checks"]:
|
||
|
|
if field not in data:
|
||
|
|
raise ValueError(f"{path}: missing {field}")
|
||
|
|
if data["mode"] not in ["active", "passive"]:
|
||
|
|
raise ValueError(f"{path}: mode must be active or passive")
|
||
|
|
if not data["checks"]:
|
||
|
|
raise ValueError(f"{path}: checks cannot be empty")
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> None:
|
||
|
|
for path in sorted((ROOT / "templates").glob("*.json")):
|
||
|
|
validate_template(path)
|
||
|
|
for path in sorted((ROOT / "samples").glob("*.json")):
|
||
|
|
validate_sample(path)
|
||
|
|
print("Zabbix template and sample assets are valid")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|