fix disk info collection

This commit is contained in:
illustris
2026-03-12 16:03:36 +05:30
parent 3808e3b672
commit 0ba7c190f8
4 changed files with 146 additions and 37 deletions

View File

@@ -3,6 +3,7 @@ package qmmonitor
import (
"encoding/json"
"fmt"
"log/slog"
"regexp"
"strings"
)
@@ -95,13 +96,7 @@ func ParseBlockInfo(raw string) map[string]DiskInfo {
info.Labels["attached_to"] = val
} else if strings.HasPrefix(line, "Cache mode:") {
val := strings.TrimSpace(strings.TrimPrefix(line, "Cache mode:"))
for _, mode := range strings.Split(val, ", ") {
mode = strings.TrimSpace(mode)
if mode != "" {
key := "cache_mode_" + strings.ReplaceAll(mode, " ", "_")
info.Labels[key] = "true"
}
}
info.Labels["cache_mode"] = val
} else if strings.HasPrefix(line, "Detect zeroes:") {
info.Labels["detect_zeroes"] = "on"
}
@@ -110,6 +105,9 @@ func ParseBlockInfo(raw string) map[string]DiskInfo {
result[diskName] = info
}
if len(result) == 0 && raw != "" {
slog.Debug("ParseBlockInfo found no disks", "rawLen", len(raw))
}
return result
}

View File

@@ -27,11 +27,8 @@ func TestParseBlockInfo_Qcow2(t *testing.T) {
if d.Labels["detect_zeroes"] != "on" {
t.Errorf("detect_zeroes = %q", d.Labels["detect_zeroes"])
}
if d.Labels["cache_mode_writeback"] != "true" {
t.Errorf("cache_mode_writeback missing")
}
if d.Labels["cache_mode_direct"] != "true" {
t.Errorf("cache_mode_direct missing")
if d.Labels["cache_mode"] != "writeback, direct" {
t.Errorf("cache_mode = %q, want %q", d.Labels["cache_mode"], "writeback, direct")
}
}