Added disk space usage for partitions in use by the system

pull/13/head
Paulo Truta 2021-06-07 09:16:04 +00:00
parent 97c268200e
commit ca9c2cc910
3 changed files with 61 additions and 26 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.15
require ( require (
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect 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-ole/go-ole v1.2.5 // indirect
github.com/go-sql-driver/mysql v1.5.0 github.com/go-sql-driver/mysql v1.5.0
github.com/joho/godotenv v1.3.0 github.com/joho/godotenv v1.3.0

2
go.sum
View File

@ -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/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.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/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 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= 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= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=

View File

@ -3,9 +3,11 @@ package storage
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/edgebox-iot/edgeboxctl/internal/utils" "github.com/edgebox-iot/edgeboxctl/internal/utils"
"github.com/shirou/gopsutil/disk"
) )
// Device : Struct representing a storage device in the system // Device : Struct representing a storage device in the system
@ -35,31 +37,28 @@ type MaybeDevice struct {
Valid bool `json:"valid"` 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) // Partition : Struct representing a partition / filesystem (Empty Mountpoint means it is not mounted)
type Partition struct { type Partition struct {
ID string `json:"id"` ID string `json:"id"`
Size string `json:"size"` Size string `json:"size"`
MAJ string `json:"maj"` MAJ string `json:"maj"`
MIN string `json:"min"` MIN string `json:"min"`
RM string `json:"rm"` RM string `json:"rm"`
RO string `json:"ro"` RO string `json:"ro"`
Filesystem string `json:"filesystem"` Filesystem string `json:"filesystem"`
Mountpoint string `json:"mountpoint"` Mountpoint string `json:"mountpoint"`
UsageStat UsageStat `json:"usage_stat"`
} }
const mainDiskID = "mmcblk0" 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 // GetDevices : Returns a list of all available sotrage devices in structs filled with information
func GetDevices() []Device { func GetDevices() []Device {
@ -89,20 +88,22 @@ func GetDevices() []Device {
} }
if isDevice { 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 // The first device found should not run the cleanup lines below
fmt.Println("Processing Device")
if !firstDevice { if !firstDevice {
fmt.Println("Appending finalized device info to list")
currentDevice.Partitions = currentPartitions currentDevice.Partitions = currentPartitions
if !currentDeviceInUseFlag {
currentDevice.Status.ID = 0
currentDevice.Status.Description = "not configured"
}
currentDevice.InUse = currentDeviceInUseFlag currentDevice.InUse = currentDeviceInUseFlag
currentDeviceInUseFlag = false currentDeviceInUseFlag = false
currentPartitions = []Partition{} currentPartitions = []Partition{}
devices = append(devices, currentDevice) devices = append(devices, currentDevice)
} else { } else {
fmt.Println("First device, not appending to list")
firstDevice = false firstDevice = false
} }
@ -128,8 +129,6 @@ func GetDevices() []Device {
} else { } else {
fmt.Println("Processing Partition")
mountpoint := "" mountpoint := ""
if len(deviceRawInfo) >= 7 { if len(deviceRawInfo) >= 7 {
mountpoint = deviceRawInfo[6] mountpoint = deviceRawInfo[6]
@ -155,8 +154,41 @@ func GetDevices() []Device {
} }
currentDevice.Partitions = currentPartitions currentDevice.Partitions = currentPartitions
if !currentDeviceInUseFlag {
currentDevice.Status.ID = 0
currentDevice.Status.Description = "Not configured"
}
currentDevice.InUse = currentDeviceInUseFlag currentDevice.InUse = currentDeviceInUseFlag
devices = append([]Device{currentDevice}, devices...) // Prepending the first device... 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 return devices
} }