#!/usr/bin/env bash set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" usage() { cat <<'EOF' Usage: scripts/new_template.sh [method] Examples: scripts/new_template.sh app apache "Apache by HTTP" http scripts/new_template.sh network cisco/ios "Cisco IOS by SNMP" snmp Arguments: category app / db / network / os / hardware / cloud / container / web / virtualization / power / printer / custom product Directory name in lowercase snake_case. Use vendor/product when grouping by vendor. template-name Zabbix template technical name. method Collection method (default: agent). EOF } if [[ $# -lt 3 || $# -gt 4 ]]; then usage exit 1 fi category="$1" product="$2" name="$3" method="${4:-agent}" allowed_categories="app db network os hardware cloud container web virtualization power printer san custom" if ! [[ " $allowed_categories " == *" $category "* ]]; then echo "ERROR: Unknown category: $category" >&2 usage exit 1 fi if ! [[ "$product" =~ ^[a-z0-9][a-z0-9_]*(/[a-z0-9][a-z0-9_]*)?$ ]]; then echo "ERROR: product must use lowercase letters, digits, and _: $product" >&2 exit 1 fi allowed_methods="agent agent2 snmp http ipmi jmx ssh telnet odbc external script calculated dependent other" if ! [[ " $allowed_methods " == *" $method "* ]]; then echo "ERROR: Unknown method: $method" >&2 exit 1 fi if [[ "$name" == *$'\n'* || "$name" == *$'\r'* ]]; then echo "ERROR: template-name must not contain line breaks." >&2 exit 1 fi if [[ "$name" != *" by "* ]]; then echo "WARN: Template names should include 'by ': $name" >&2 fi package_dir="$ROOT/templates/$category/$product" if [[ -e "$package_dir" ]]; then echo "ERROR: Already exists: ${package_dir#"$ROOT/"}" >&2 exit 1 fi maintainer="${MAINTAINER:-$(git -C "$ROOT" config user.name 2>/dev/null || true)}" maintainer="${maintainer:-unknown}" vendor="${VENDOR:-${product%%/*}}" zabbix_min="${ZABBIX_MIN:-7.0}" if ! [[ "$zabbix_min" =~ ^[0-9]+\.[0-9]+$ ]]; then echo "ERROR: ZABBIX_MIN must use the 7.0 format: $zabbix_min" >&2 exit 1 fi mkdir -p "$package_dir/$zabbix_min" # Escape sed replacement characters. escape_sed() { printf '%s' "$1" | sed -e 's/[\\&|]/\\&/g' } sed \ -e "s|{{TEMPLATE_NAME}}|$(escape_sed "$name")|g" \ -e "s|{{ID}}|$(escape_sed "$category/$product")|g" \ -e "s|{{CATEGORY}}|$(escape_sed "$category")|g" \ -e "s|{{VENDOR}}|$(escape_sed "$vendor")|g" \ -e "s|{{METHOD}}|$(escape_sed "$method")|g" \ -e "s|{{ZABBIX_MIN}}|$(escape_sed "$zabbix_min")|g" \ "$ROOT/docs/template-README.md" > "$package_dir/README.md" cat > "$package_dir/CHANGELOG.md" < "$package_dir/manifest.yaml" </dev/null 2>&1 && python3 -c 'import yaml' >/dev/null 2>&1; then if ! python3 "$ROOT/scripts/gen_index.py" >/dev/null; then echo "WARN: Could not update the template index in README.md. Run 'make index'." >&2 fi fi echo "Created: ${package_dir#"$ROOT/"}" echo echo "Next steps:" echo " 1. Export the template as YAML from Zabbix $zabbix_min." echo " 2. Place the export in ${package_dir#"$ROOT/"}/$zabbix_min/ and remove .gitkeep." echo " 3. Replace the TODO entries in README.md and manifest.yaml." echo " 4. Run 'make lint'."