From ca9c2cc91034bf143a5d8ebf5c965f71d801af90 Mon Sep 17 00:00:00 2001 From: Paulo Truta Date: Mon, 7 Jun 2021 09:16:04 +0000 Subject: [PATCH] Added disk space usage for partitions in use by the system --- go.mod | 1 + go.sum | 2 + internal/storage/storage.go | 84 +++++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 175fc69..df74328 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect + github.com/dustin/go-humanize v1.0.0 // indirect github.com/go-ole/go-ole v1.2.5 // indirect github.com/go-sql-driver/mysql v1.5.0 github.com/joho/godotenv v1.3.0 diff --git a/go.sum b/go.sum index 3fe5772..85d8c22 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 h1:5sXbqlSomvdjl github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY= github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= diff --git a/internal/storage/storage.go b/internal/storage/storage.go index e48e1e5..e86e4cd 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -3,9 +3,11 @@ package storage import ( "bufio" "fmt" + "strconv" "strings" "github.com/edgebox-iot/edgeboxctl/internal/utils" + "github.com/shirou/gopsutil/disk" ) // Device : Struct representing a storage device in the system @@ -35,31 +37,28 @@ type MaybeDevice struct { Valid bool `json:"valid"` } +type UsageStat struct { + Total string `json:"total"` + Used string `json:"used"` + Free string `json:"free"` + Percent string `json:"percent"` +} + // Partition : Struct representing a partition / filesystem (Empty Mountpoint means it is not mounted) type Partition struct { - ID string `json:"id"` - Size string `json:"size"` - MAJ string `json:"maj"` - MIN string `json:"min"` - RM string `json:"rm"` - RO string `json:"ro"` - Filesystem string `json:"filesystem"` - Mountpoint string `json:"mountpoint"` + ID string `json:"id"` + Size string `json:"size"` + MAJ string `json:"maj"` + MIN string `json:"min"` + RM string `json:"rm"` + RO string `json:"ro"` + Filesystem string `json:"filesystem"` + Mountpoint string `json:"mountpoint"` + UsageStat UsageStat `json:"usage_stat"` } const mainDiskID = "mmcblk0" -func GetDevice() MaybeDevice { - - result := MaybeDevice{ - Device: Device{}, - Valid: false, - } - - return result - -} - // GetDevices : Returns a list of all available sotrage devices in structs filled with information func GetDevices() []Device { @@ -89,20 +88,22 @@ func GetDevices() []Device { } if isDevice { - // Clean up on the last device being prepared. Append all partitions found and delete the currentPartitions list afterwards. + // Clean up on the latest device being prepared. Append all partitions found and delete the currentPartitions list afterwards. // The first device found should not run the cleanup lines below - fmt.Println("Processing Device") - if !firstDevice { - fmt.Println("Appending finalized device info to list") currentDevice.Partitions = currentPartitions + + if !currentDeviceInUseFlag { + currentDevice.Status.ID = 0 + currentDevice.Status.Description = "not configured" + } + currentDevice.InUse = currentDeviceInUseFlag currentDeviceInUseFlag = false currentPartitions = []Partition{} devices = append(devices, currentDevice) } else { - fmt.Println("First device, not appending to list") firstDevice = false } @@ -128,8 +129,6 @@ func GetDevices() []Device { } else { - fmt.Println("Processing Partition") - mountpoint := "" if len(deviceRawInfo) >= 7 { mountpoint = deviceRawInfo[6] @@ -155,8 +154,41 @@ func GetDevices() []Device { } currentDevice.Partitions = currentPartitions + if !currentDeviceInUseFlag { + currentDevice.Status.ID = 0 + currentDevice.Status.Description = "Not configured" + } currentDevice.InUse = currentDeviceInUseFlag devices = append([]Device{currentDevice}, devices...) // Prepending the first device... + getDevicesSpaceUsage(devices) + + return devices +} + +func getDevicesSpaceUsage(devices []Device) []Device { + + for deviceIndex, device := range devices { + + if device.InUse { + + for partitionIndex, partition := range device.Partitions { + + s, _ := disk.Usage(partition.Mountpoint) + if s.Total == 0 { + continue + } + + partitionUsagePercent := fmt.Sprintf("%2.f%%", s.UsedPercent) + devices[deviceIndex].Partitions[partitionIndex].UsageStat = UsageStat{Total: strconv.FormatUint(s.Total, 10), Used: strconv.FormatUint(s.Used, 10), Free: strconv.FormatUint(s.Free, 10), Percent: partitionUsagePercent} + + } + + } + + fmt.Println(device) + + } + return devices }