Skip to content

Commit edafe5d

Browse files
afreyermuth98crohr
authored andcommitted
🔧 Add volumeID to outputs
1 parent c0e79b0 commit edafe5d

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎internal/monitoring/helpers.go‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package monitoring
22

33
import (
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
113169
func calculateMin(data []float64) float64 {
114170
if len(data) == 0 {

‎internal/monitoring/metrics.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,24 @@ func GenerateMetricsSummary(action *githubactions.Action, metrics []string, form
178178
networkInterface = getNetworkInterface(networkInterface)
179179
diskDevice = getDiskDevice(diskDevice)
180180

181+
// Get volume ID from disk device
182+
volumeID := ""
183+
if diskDevice != "" {
184+
var err error
185+
volumeID, err = getVolumeID(diskDevice)
186+
if err != nil {
187+
action.Warningf("Failed to get volume ID from disk device %s: %v", diskDevice, err)
188+
}
189+
}
190+
181191
action.Infof("## CloudWatch Metrics Summary\n")
182192
action.Infof("Enabled metrics: %s", strings.Join(metrics, ", "))
183193
action.Infof("Namespace: %s", NAMESPACE)
184194
action.Infof("Network interface: %s", networkInterface)
185195
action.Infof("Disk device: %s", diskDevice)
196+
if volumeID != "" {
197+
action.Infof("Volume ID: %s", volumeID)
198+
}
186199
action.Infof("")
187200
showLinks(action, metrics)
188201

@@ -225,6 +238,10 @@ func GenerateMetricsSummary(action *githubactions.Action, metrics []string, form
225238
Name: aws.String("path"),
226239
Value: aws.String("/"),
227240
})
241+
dimensions = append(dimensions, types.Dimension{
242+
Name: aws.String("volume_id"),
243+
Value: aws.String(volumeID),
244+
})
228245
}
229246
if metricType == "io" {
230247
dimensions = append(dimensions, types.Dimension{

0 commit comments

Comments
 (0)