@@ -2,8 +2,10 @@ package monitoring
22
33import (
44 "bufio"
5+ "fmt"
56 "os"
67 "os/exec"
8+ "regexp"
79 "strings"
810)
911
@@ -109,6 +111,60 @@ func getDiskDevice(diskDevice string) string {
109111 return diskDevice
110112}
111113
114+ // getVolumeID extracts the EBS volume ID from a disk device name
115+ // It reads the MODEL and SERIAL from /sys/block/${BASE}/device/ and converts
116+ // the serial to the volume ID format (vol-...)
117+ func getVolumeID (diskDevice string ) (string , error ) {
118+ // Extract base device name (remove partition suffix like p1, p2, etc.)
119+ base := diskDevice
120+ // Remove partition suffix pattern: p1, p2, etc. (e.g., nvme0n1p1 -> nvme0n1)
121+ re := regexp .MustCompile (`p\d+$` )
122+ base = re .ReplaceAllString (base , "" )
123+
124+ // Read MODEL
125+ modelPath := fmt .Sprintf ("/sys/block/%s/device/model" , base )
126+ modelBytes , err := os .ReadFile (modelPath )
127+ if err != nil {
128+ return "" , fmt .Errorf ("failed to read model: %w" , err )
129+ }
130+ model := strings .TrimSpace (string (modelBytes ))
131+
132+ // Validate MODEL contains "Amazon Elastic Block Store"
133+ if ! strings .Contains (strings .ToLower (model ), "amazon elastic block store" ) {
134+ return "" , fmt .Errorf ("%s does not look like an EBS NVMe device (model=%s)" , base , model )
135+ }
136+
137+ // Read SERIAL
138+ serialPath := fmt .Sprintf ("/sys/block/%s/device/serial" , base )
139+ serialBytes , err := os .ReadFile (serialPath )
140+ if err != nil {
141+ return "" , fmt .Errorf ("failed to read serial: %w" , err )
142+ }
143+ serial := strings .TrimSpace (string (serialBytes ))
144+ // Remove all whitespace
145+ serial = strings .ReplaceAll (serial , " " , "" )
146+ serial = strings .ReplaceAll (serial , "\t " , "" )
147+ serial = strings .ReplaceAll (serial , "\n " , "" )
148+
149+ // Convert SERIAL to VOLUME_ID format
150+ // Pattern: vol00bb... -> vol-00bb...
151+ volPattern := regexp .MustCompile (`^vol[0-9a-f]+$` )
152+ volWithDashPattern := regexp .MustCompile (`^vol-[0-9a-f]+$` )
153+
154+ var volumeID string
155+ if volPattern .MatchString (serial ) {
156+ // vol00bb... -> vol-00bb...
157+ volumeID = "vol-" + serial [3 :]
158+ } else if volWithDashPattern .MatchString (serial ) {
159+ // Already in correct format
160+ volumeID = serial
161+ } else {
162+ return "" , fmt .Errorf ("unexpected NVMe serial format: %s" , serial )
163+ }
164+
165+ return volumeID , nil
166+ }
167+
112168// calculateMin returns the minimum value in a slice
113169func calculateMin (data []float64 ) float64 {
114170 if len (data ) == 0 {
0 commit comments