Gestire Proxmox da script e applicazioni
L'API REST di Proxmox permette di automatizzare qualsiasi operazione: creare VM, gestire backup, monitorare risorse.
1. Autenticazione
# Ottenere ticket e CSRF token
RESPONSE=$(curl -s -k -d "username=root@pam&password=PASSWORD" https://192.168.1.81:8006/api2/json/access/ticket)
TICKET=$(echo $RESPONSE | jq -r .data.ticket)
CSRF=$(echo $RESPONSE | jq -r .data.CSRFPreventionToken)
2. Elencare VM
curl -s -k -b "PVEAuthCookie=$TICKET" https://192.168.1.81:8006/api2/json/nodes/server3/qemu | jq ".data[] | {vmid, name, status}"
3. Avviare/fermare VM
# Avviare
curl -s -k -b "PVEAuthCookie=$TICKET" -H "CSRFPreventionToken: $CSRF" -X POST https://192.168.1.81:8006/api2/json/nodes/server3/qemu/200/status/start
# Shutdown
curl -s -k -b "PVEAuthCookie=$TICKET" -H "CSRFPreventionToken: $CSRF" -X POST https://192.168.1.81:8006/api2/json/nodes/server3/qemu/200/status/shutdown
4. Creare VM da API
curl -s -k -b "PVEAuthCookie=$TICKET" -H "CSRFPreventionToken: $CSRF" -X POST \
-d "vmid=201&name=test-vm&memory=2048&cores=2&net0=virtio,bridge=vmbr0" \
https://192.168.1.81:8006/api2/json/nodes/server3/qemu
5. Script PHP per dashboard
function proxmox_api($path) {
$ch = curl_init("https://192.168.1.81:8006/api2/json$path");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=$ticket");
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
return $result["data"];
}
L'API Proxmox è alla base della dashboard homelab del sito, che mostra in tempo reale lo stato di tutte le VM.