Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Harden Proxmox guest fsinfo parsing
rcourtman69ea9e59406b
diff --git a/pkg/proxmox/client.go b/pkg/proxmox/client.goindex f58458593b..f8801ac4c0 100644--- a/pkg/proxmox/client.go+++ b/pkg/proxmox/client.go@@ -1589,23 +1589,25 @@ func (c *Client) GetVMAgentVersion(ctx context.Context, node string, vmid int) (// VMFileSystem represents filesystem information from QEMU guest agenttype VMFileSystem struct {- Name string `json:"name"`- Type string `json:"type"`- Mountpoint string `json:"mountpoint"`- TotalBytes uint64 `json:"total-bytes"`- UsedBytes uint64 `json:"used-bytes"`- Disk string // Extracted disk device name for duplicate detection- DiskRaw []interface{} `json:"disk"` // Raw disk device info from API+ Name string `json:"name"`+ Type string `json:"type"`+ Mountpoint string `json:"mountpoint"`+ TotalBytes uint64 `json:"total-bytes"`+ TotalBytesPrivileged uint64 `json:"total-bytes-privileged"`+ UsedBytes uint64 `json:"used-bytes"`+ Disk string // Extracted disk device name for duplicate detection+ DiskRaw []interface{} `json:"disk"` // Raw disk device info from API}func (fs *VMFileSystem) UnmarshalJSON(data []byte) error {type rawVMFileSystem struct {- Name string `json:"name"`- Type string `json:"type"`- Mountpoint string `json:"mountpoint"`- TotalBytes interface{} `json:"total-bytes"`- UsedBytes interface{} `json:"used-bytes"`- DiskRaw []interface{} `json:"disk"`+ Name string `json:"name"`+ Type string `json:"type"`+ Mountpoint string `json:"mountpoint"`+ TotalBytes interface{} `json:"total-bytes"`+ TotalBytesPrivileged interface{} `json:"total-bytes-privileged"`+ UsedBytes interface{} `json:"used-bytes"`+ DiskRaw interface{} `json:"disk"`}var raw rawVMFileSystem@@ -1617,21 +1619,69 @@ func (fs *VMFileSystem) UnmarshalJSON(data []byte) error {if err != nil {return err}+ totalPrivileged, err := parseUint64Flexible(raw.TotalBytesPrivileged)+ if err != nil {+ return err+ }used, err := parseUint64Flexible(raw.UsedBytes)if err != nil {return err}+ if total == 0 && totalPrivileged > 0 {+ total = totalPrivileged+ }fs.Name = raw.Namefs.Type = raw.Typefs.Mountpoint = raw.Mountpoint+ if normalized, ok := normalizeWindowsDriveMountpoint(raw.Name); ok {+ if fs.Mountpoint == "" || isWindowsVolumeGUIDMountpoint(fs.Mountpoint) {+ fs.Mountpoint = normalized+ }+ }fs.TotalBytes = total+ fs.TotalBytesPrivileged = totalPrivilegedfs.UsedBytes = used- fs.DiskRaw = raw.DiskRaw+ fs.DiskRaw = normalizeVMFilesystemDiskRaw(raw.DiskRaw)fs.Disk = ""return nil}+func normalizeVMFilesystemDiskRaw(value interface{}) []interface{} {+ switch v := value.(type) {+ case nil:+ return nil+ case []interface{}:+ return v+ case map[string]interface{}:+ return []interface{}{v}+ default:+ return nil+ }+}++func normalizeWindowsDriveMountpoint(value string) (string, bool) {+ value = strings.TrimSpace(value)+ if len(value) < 2 || value[1] != ':' {+ return "", false+ }+ drive := strings.ToUpper(value[:2])+ if len(value) == 2 {+ return drive, true+ }+ switch value[2] {+ case '\\', '/':+ return drive + value[2:], true+ default:+ return "", false+ }+}++func isWindowsVolumeGUIDMountpoint(value string) bool {+ value = strings.TrimSpace(strings.ToLower(value))+ return strings.HasPrefix(value, `\\?\volume{`) && strings.HasSuffix(value, `}\`)+}+func parseUint64Flexible(value interface{}) (uint64, error) {switch v := value.(type) {case nil:@@ -1879,15 +1929,30 @@ func (c *Client) GetVMFSInfo(ctx context.Context, node string, vmid int) ([]VMFiStr("response", string(bodyBytes)).Msg("Raw response from guest agent get-fsinfo")- // Try to unmarshal as an array first (expected format)+ // Decode array payloads entry-by-entry so one malformed filesystem record+ // does not wipe valid guest-agent disk data for the whole VM.var arrayResult struct {Data struct {- Result []VMFileSystem `json:"result"`+ Result []json.RawMessage `json:"result"`} `json:"data"`}if err := json.Unmarshal(bodyBytes, &arrayResult); err == nil && arrayResult.Data.Result != nil {- postProcessVMFilesystems(node, vmid, arrayResult.Data.Result)- return arrayResult.Data.Result, nil+ filesystems := make([]VMFileSystem, 0, len(arrayResult.Data.Result))+ for idx, rawFS := range arrayResult.Data.Result {+ var fs VMFileSystem+ if err := json.Unmarshal(rawFS, &fs); err != nil {+ log.Warn().+ Err(err).+ Str("node", node).+ Int("vmid", vmid).+ Int("filesystem_index", idx).+ Msg("Skipping malformed guest agent filesystem entry")+ continue+ }+ filesystems = append(filesystems, fs)+ }+ postProcessVMFilesystems(node, vmid, filesystems)+ return filesystems, nil}// If that fails, try as an object (might be an error response or different format)diff --git a/pkg/proxmox/client_api_more3_test.go b/pkg/proxmox/client_api_more3_test.goindex 11080c45b7..dd9cd678b8 100644--- a/pkg/proxmox/client_api_more3_test.go+++ b/pkg/proxmox/client_api_more3_test.go@@ -54,6 +54,125 @@ func TestClientVMFSInfoParsing(t *testing.T) {}}+func TestClientVMFSInfoParsingPrivilegedCapacityFallback(t *testing.T) {+ client := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {+ switch r.URL.Path {+ case "/api2/json/nodes/node1/qemu/100/agent/get-fsinfo":+ writeJSON(t, w, map[string]interface{}{+ "data": map[string]interface{}{+ "result": []map[string]interface{}{+ {+ "name": "windows",+ "type": "ntfs",+ "mountpoint": "C:\\Windows",+ "total-bytes": 0,+ "total-bytes-privileged": 500,+ "used-bytes": 200,+ },+ },+ },+ })+ default:+ http.NotFound(w, r)+ }+ })++ ctx := context.Background()+ filesystems, err := client.GetVMFSInfo(ctx, "node1", 100)+ if err != nil {+ t.Fatalf("GetVMFSInfo error: %v", err)+ }+ if len(filesystems) != 1 {+ t.Fatalf("expected 1 filesystem, got %d", len(filesystems))+ }+ if filesystems[0].TotalBytes != 500 {+ t.Fatalf("expected privileged total-bytes fallback, got %d", filesystems[0].TotalBytes)+ }+ if filesystems[0].Disk != "C:" {+ t.Fatalf("expected windows drive disk, got %q", filesystems[0].Disk)+ }+}++func TestClientVMFSInfoParsingWindowsVolumeGUIDMountpointFallback(t *testing.T) {+ client := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {+ switch r.URL.Path {+ case "/api2/json/nodes/node1/qemu/100/agent/get-fsinfo":+ writeJSON(t, w, map[string]interface{}{+ "data": map[string]interface{}{+ "result": []map[string]interface{}{+ {+ "name": "C:\\",+ "type": "ntfs",+ "mountpoint": "\\\\?\\Volume{1234-5678}\\",+ "total-bytes": 500,+ "used-bytes": 200,+ },+ },+ },+ })+ default:+ http.NotFound(w, r)+ }+ })++ ctx := context.Background()+ filesystems, err := client.GetVMFSInfo(ctx, "node1", 100)+ if err != nil {+ t.Fatalf("GetVMFSInfo error: %v", err)+ }+ if len(filesystems) != 1 {+ t.Fatalf("expected 1 filesystem, got %d", len(filesystems))+ }+ if filesystems[0].Mountpoint != "C:\\" {+ t.Fatalf("expected windows drive-letter mountpoint fallback, got %q", filesystems[0].Mountpoint)+ }+ if filesystems[0].Disk != "C:" {+ t.Fatalf("expected windows drive disk, got %q", filesystems[0].Disk)+ }+}++func TestClientVMFSInfoSkipsMalformedEntries(t *testing.T) {+ client := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {+ switch r.URL.Path {+ case "/api2/json/nodes/node1/qemu/100/agent/get-fsinfo":+ writeJSON(t, w, map[string]interface{}{+ "data": map[string]interface{}{+ "result": []interface{}{+ map[string]interface{}{+ "name": "root",+ "type": "ext4",+ "mountpoint": "/",+ "total-bytes": 100,+ "used-bytes": 10,+ },+ map[string]interface{}{+ "name": "broken",+ "type": "ext4",+ "mountpoint": "/broken",+ "total-bytes": true,+ "used-bytes": 20,+ },+ },+ },+ })+ default:+ http.NotFound(w, r)+ }+ })++ ctx := context.Background()+ filesystems, err := client.GetVMFSInfo(ctx, "node1", 100)+ if err != nil {+ t.Fatalf("GetVMFSInfo error: %v", err)+ }+ if len(filesystems) != 1 {+ t.Fatalf("expected malformed entry to be skipped, got %+v", filesystems)+ }+ if filesystems[0].Disk != "root-filesystem" {+ t.Fatalf("expected valid filesystem to survive malformed peer entry, got %+v", filesystems[0])+ }+}+func TestClientVMFSInfoObjectResult(t *testing.T) {client := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {switch r.URL.Path {diff --git a/pkg/proxmox/client_test.go b/pkg/proxmox/client_test.goindex 470fe10225..556d5a4ada 100644--- a/pkg/proxmox/client_test.go+++ b/pkg/proxmox/client_test.go@@ -239,6 +239,53 @@ func TestVMFileSystemUnmarshalFlexibleNumbers(t *testing.T) {t.Fatalf("unexpected float string values: got total=%d used=%d", fs.TotalBytes, fs.UsedBytes)}})++ t.Run("falls back to total-bytes-privileged when total-bytes is missing", func(t *testing.T) {+ payload := `{"name":"windows","type":"ntfs","mountpoint":"C:\\\\","total-bytes-privileged":536870912000,"used-bytes":214748364800}`+ var fs VMFileSystem+ if err := json.Unmarshal([]byte(payload), &fs); err != nil {+ t.Fatalf("unexpected error unmarshalling privileged total-bytes: %v", err)+ }+ if fs.TotalBytes != 536870912000 || fs.TotalBytesPrivileged != 536870912000 || fs.UsedBytes != 214748364800 {+ t.Fatalf("unexpected privileged values: got total=%d privileged=%d used=%d", fs.TotalBytes, fs.TotalBytesPrivileged, fs.UsedBytes)+ }+ })++ t.Run("uses windows name as mountpoint fallback when mountpoint is empty", func(t *testing.T) {+ payload := `{"name":"C:\\Windows","type":"ntfs","mountpoint":"","total-bytes":536870912000,"used-bytes":214748364800}`+ var fs VMFileSystem+ if err := json.Unmarshal([]byte(payload), &fs); err != nil {+ t.Fatalf("unexpected error unmarshalling windows name fallback: %v", err)+ }+ if fs.Mountpoint != "C:\\Windows" {+ t.Fatalf("expected windows mountpoint fallback from name, got %q", fs.Mountpoint)+ }+ })++ t.Run("replaces windows volume GUID mountpoint with normalized name", func(t *testing.T) {+ payload := `{"name":"C:\\","type":"ntfs","mountpoint":"\\\\?\\Volume{1234-5678}\\","total-bytes":536870912000,"used-bytes":214748364800}`+ var fs VMFileSystem+ if err := json.Unmarshal([]byte(payload), &fs); err != nil {+ t.Fatalf("unexpected error unmarshalling volume GUID mountpoint: %v", err)+ }+ if fs.Mountpoint != "C:\\" {+ t.Fatalf("expected windows drive mountpoint fallback, got %q", fs.Mountpoint)+ }+ })++ t.Run("accepts disk metadata as an object", func(t *testing.T) {+ payload := `{"name":"rootfs","type":"ext4","mountpoint":"/","total-bytes":1000,"used-bytes":200,"disk":{"dev":"/dev/sda"}}`+ var fs VMFileSystem+ if err := json.Unmarshal([]byte(payload), &fs); err != nil {+ t.Fatalf("unexpected error unmarshalling disk object: %v", err)+ }+ if len(fs.DiskRaw) != 1 {+ t.Fatalf("expected disk metadata object to normalize to one entry, got %d", len(fs.DiskRaw))+ }+ if diskMap, ok := fs.DiskRaw[0].(map[string]interface{}); !ok || diskMap["dev"] != "/dev/sda" {+ t.Fatalf("expected normalized disk metadata to preserve dev, got %#v", fs.DiskRaw[0])+ }+ })}func TestVMFileSystemUnmarshalJSON_InvalidValues(t *testing.T) {