#!/usr/bin/env bash # proxmox-ve.sh — VM- und Container-Metriken direkt vom PVE-Host. # # Läuft als root auf dem PVE-Knoten und nutzt `pvesh` (lokale API, volle # Rechte). Damit entfällt der API-Token komplett — kein Sys.Audit, keine ACL. # # Warum nicht inputs.proxmox: Das Plugin fragt zuerst /nodes//dns ab. # Fehlt dort Sys.Audit, liefert es KOMPLETT nichts — ohne Logmeldung. # # Ausgabe im Prometheus-Format (data_format = "prometheus"): # proxmox_vm_cpu_usage{vmid="1001",name="tunnel",type="qemu",status="running"} 0.38 # # Getestet gegen die reale Ausgabe von pvesh get /cluster/resources: # - Top-Level ist ein ARRAY (kein {"data":...}-Wrapper) # - qemu nutzt "cpus", lxc zusätzlich "maxswap"/"swap" # - mem kann maxmem überschreiten (Ballooning/VirtIO) set -eu command -v pvesh >/dev/null 2>&1 || { echo "# pvesh nicht gefunden — kein PVE-Host" >&2; exit 0; } TMP="$(mktemp)"; trap 'rm -f "$TMP"' EXIT pvesh get /cluster/resources --output-format json > "$TMP" 2>/dev/null || true [ -s "$TMP" ] || exit 0 python3 - "$TMP" <<'PY' import json, sys def esc(s): return str(s).replace('\\', '\\\\').replace('"', '\\"').replace('\n', ' ') try: raw = json.load(open(sys.argv[1])) except Exception as e: print(f"# JSON-Fehler: {e}", file=sys.stderr); sys.exit(0) # pvesh liefert je nach Aufruf {"data":[...]} ODER direkt [...] rows = raw.get("data", raw) if isinstance(raw, dict) else raw if not isinstance(rows, list): sys.exit(0) def emit(name, labels, value): if value is None: return lab = ",".join(f'{k}="{esc(v)}"' for k, v in labels.items() if v is not None and v != "") print(f"{name}{{{lab}}} {value}") for r in rows: typ = r.get("type") if typ not in ("qemu", "lxc"): # node/storage/sdn überspringen continue if r.get("template"): # Templates nicht als Gast zählen continue if r.get("vmid") in (None, ""): continue # "pvenode" statt "node": den global_tag "node" setzt Telegraf (NODE_NAME). # So stimmt der Node-Filter im Dashboard mit den Host-Metriken ueberein, # auch wenn --name und PVE-Knotenname abweichen (z. B. pve5-siedlung / pve5). L = {"vmid": r.get("vmid"), "name": r.get("name"), "pvenode": r.get("node"), "type": typ, "status": r.get("status")} cpu = r.get("cpu") if cpu is not None: emit("proxmox_vm_cpu_usage_percent", L, round(float(cpu) * 100, 2)) # qemu: "cpus"; lxc: "cpus" oder "maxcpu" emit("proxmox_vm_cpu_cores", L, r.get("cpus") or r.get("maxcpu")) emit("proxmox_vm_mem_bytes", L, r.get("mem")) emit("proxmox_vm_mem_max_bytes", L, r.get("maxmem")) emit("proxmox_vm_swap_bytes", L, r.get("swap")) emit("proxmox_vm_swap_max_bytes", L, r.get("maxswap")) emit("proxmox_vm_disk_bytes", L, r.get("disk")) emit("proxmox_vm_disk_max_bytes", L, r.get("maxdisk")) emit("proxmox_vm_diskread_bytes_total", L, r.get("diskread")) emit("proxmox_vm_diskwrite_bytes_total", L, r.get("diskwrite")) emit("proxmox_vm_net_in_bytes_total", L, r.get("netin")) emit("proxmox_vm_net_out_bytes_total", L, r.get("netout")) emit("proxmox_vm_uptime_seconds", L, r.get("uptime")) emit("proxmox_vm_running", L, 1 if r.get("status") == "running" else 0) # RAM-Auslastung in Prozent, robust gegen mem > maxmem (Ballooning) mem, maxmem = r.get("mem"), r.get("maxmem") if mem is not None and maxmem: emit("proxmox_vm_mem_usage_percent", L, round(min(100.0, float(mem) / float(maxmem) * 100.0), 2)) PY