-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazurestorage.go
97 lines (76 loc) · 2.42 KB
/
azurestorage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"fmt"
"os/exec"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func listStorageObjects(client *kubernetes.Clientset) error {
// List Persistent Volumes
persistentVolumes, err := client.CoreV1().PersistentVolumes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return fmt.Errorf("could not list Persistent Volumes: %v", err)
}
fmt.Println("Persistent Volumes in AKS:")
for _, pv := range persistentVolumes.Items {
if strings.HasPrefix(pv.Name, "pvc-") {
fmt.Printf("Name: %s, Capacity: %v, Status: %s\n", pv.Name, pv.Spec.Capacity.Storage().Value(), pv.Status.Phase)
}
}
fmt.Println()
// Wait for user input to select a persistent volume
var selection int
fmt.Print("View 12-hour R/W Data of Persistent Volume: ")
fmt.Scanln(&selection)
// Call getAzureDiskResourceGroup with the selected persistent volume name
persistentVolume := persistentVolumes.Items[selection-1]
azureName := "kubernetes-dynamic-" + persistentVolume.Name
resourceGroup := getAzureDiskResourceGroup(azureName)
resourceID := getAzureDiskID(azureName, resourceGroup)
fmt.Println(getAzureDiskMetrics(resourceID))
return nil
}
func getAzureDiskResourceGroup(diskName string) string {
cmd := exec.Command("az", "disk", "list",
"--query", fmt.Sprintf("[?name=='%s'].resourceGroup | [0]", diskName),
"-o", "tsv")
output, err := cmd.Output()
if err != nil {
return ""
}
resourceGroup := strings.TrimSpace(string(output))
return resourceGroup
}
func getAzureDiskID(diskName, resourceGroup string) string {
cmd := exec.Command("az", "disk", "show",
"--name", diskName,
"--resource-group", resourceGroup,
"--query", "id",
"-o", "tsv")
output, err := cmd.Output()
if err != nil {
return ""
}
diskID := strings.TrimSpace(string(output))
return diskID
}
func getAzureDiskMetrics(resourceID string) string {
startTime := time.Now().UTC().Add(-12 * time.Hour).Format("2006-01-02T15:04:00Z")
endTime := time.Now().UTC().Format("2006-01-02T15:04:00Z")
cmd := exec.Command("az", "monitor", "metrics", "list",
"--resource", resourceID,
"--metric", "Composite Disk Read Operations/sec,Composite Disk Write Operations/sec",
"--interval", "PT1H",
"--start-time", startTime,
"--end-time", endTime,
"-o", "table")
output, err := cmd.Output()
if err != nil {
return ""
}
metrics := strings.TrimSpace(string(output))
return metrics
}