Add container/VM autodiscovery, optional username in config, and management scripts
All checks were successful
Build-Publish / build (linux/amd64) (push) Successful in 4s
Build-Publish / build (linux/arm64) (push) Successful in 13s
Build-Publish / create-manifest (push) Successful in 1s
Build-Publish / publish-template (push) Successful in 15s

This commit is contained in:
j
2026-03-08 09:57:53 +13:00
parent ce55d6acc7
commit 8747209181
7 changed files with 270 additions and 6 deletions

View File

@@ -156,4 +156,115 @@ else
echo "installed=false"
fi
# --- Container / VM autodiscovery ---
# Helper: collect basic stats by execing into a container
# Usage: gather_container_stats <exec_prefix>
# e.g. gather_container_stats "pct exec 100 --"
gather_container_stats() {
local exec_cmd="$1"
# Memory
local mem_total mem_avail mem_used mem_pct
mem_total=$($exec_cmd cat /proc/meminfo 2>/dev/null | awk '/MemTotal/{print int($2/1024)}')
mem_avail=$($exec_cmd cat /proc/meminfo 2>/dev/null | awk '/MemAvailable/{print int($2/1024)}')
if [ -n "$mem_total" ] && [ -n "$mem_avail" ] && [ "$mem_total" -gt 0 ] 2>/dev/null; then
mem_used=$((mem_total - mem_avail))
mem_pct=$((mem_used * 100 / mem_total))
echo "mem_total_mb=$mem_total"
echo "mem_used_mb=$mem_used"
echo "mem_percent=$mem_pct"
fi
# Disk (rootfs)
local disk_info
disk_info=$($exec_cmd df -B1 / 2>/dev/null | tail -1)
if [ -n "$disk_info" ]; then
echo "disk_total=$(echo "$disk_info" | awk '{print $2}')"
echo "disk_used=$(echo "$disk_info" | awk '{print $3}')"
echo "disk_percent=$(echo "$disk_info" | awk '{gsub(/%/,""); print $5}')"
fi
# IP
local ip
ip=$($exec_cmd hostname -I 2>/dev/null | awk '{print $1}')
[ -n "$ip" ] && echo "ip=$ip"
# Uptime
local uptime_s
uptime_s=$($exec_cmd cut -d' ' -f1 /proc/uptime 2>/dev/null | cut -d. -f1)
[ -n "$uptime_s" ] && echo "uptime_seconds=$uptime_s"
}
# Proxmox LXC (pct)
if command -v pct &>/dev/null; then
pct list 2>/dev/null | tail -n +2 | while read -r vmid status _ name _; do
[ -z "$vmid" ] && continue
echo "[container:pct-${vmid}]"
echo "type=lxc"
echo "platform=proxmox"
echo "id=$vmid"
echo "name=${name:-$vmid}"
echo "status=$status"
if [ "$status" = "running" ]; then
gather_container_stats "pct exec $vmid --"
fi
done
fi
# Proxmox VMs (qm)
if command -v qm &>/dev/null; then
qm list 2>/dev/null | tail -n +2 | while read -r vmid name status _ mem _; do
[ -z "$vmid" ] && continue
echo "[container:qm-${vmid}]"
echo "type=vm"
echo "platform=proxmox"
echo "id=$vmid"
echo "name=${name:-$vmid}"
echo "status=$status"
[ -n "$mem" ] && echo "mem_allocated_mb=$mem"
# VM stats require guest agent - best effort
if [ "$status" = "running" ]; then
agent_test=$(qm guest exec "$vmid" -- cat /proc/meminfo 2>/dev/null)
if [ -n "$agent_test" ]; then
gather_container_stats "qm guest exec $vmid --"
fi
fi
done
fi
# Plain LXC (lxc/lxd)
if command -v lxc &>/dev/null && ! command -v pct &>/dev/null; then
lxc list --format csv -c nsN 2>/dev/null | while IFS=',' read -r name status network; do
[ -z "$name" ] && continue
echo "[container:lxc-${name}]"
echo "type=lxc"
echo "platform=lxd"
echo "name=$name"
echo "status=$status"
if [ "$status" = "RUNNING" ]; then
gather_container_stats "lxc exec $name --"
lxd_ip=$(echo "$network" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1)
[ -n "$lxd_ip" ] && echo "ip=$lxd_ip"
fi
done
fi
# libvirt VMs (virsh)
if command -v virsh &>/dev/null; then
virsh list --all --name 2>/dev/null | while read -r name; do
[ -z "$name" ] && continue
state=$(virsh domstate "$name" 2>/dev/null | head -1)
echo "[container:virsh-${name}]"
echo "type=vm"
echo "platform=libvirt"
echo "name=$name"
echo "status=$state"
if [ "$state" = "running" ]; then
virsh_ip=$(virsh domifaddr "$name" --source agent 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1)
[ -n "$virsh_ip" ] && echo "ip=$virsh_ip"
fi
done
fi
echo "[end]"