Files
ProjectOctopus/agent-go/status/Disk.go
2023-04-23 14:15:12 +08:00

49 lines
980 B
Go

package status
import (
"fmt"
"github.com/shirou/gopsutil/v3/disk"
"runtime"
"time"
)
type DiskStatus struct {
Total uint64
Used uint64
LogicalDisk []disk.PartitionStat
}
func GetDiskStatus() *DiskStatus {
ds := &DiskStatus{}
// Get disk usage
du, _ := disk.Usage("/")
ds.Total = du.Total
ds.Used = du.Used
// Get logical disk info for Linux systems
if runtime.GOOS == "linux" {
ld, _ := disk.Partitions(true)
ds.LogicalDisk = ld
}
return ds
}
func CalculateDiskIO() {
// Get initial disk IO counters
counters1, _ := disk.IOCounters()
time.Sleep(time.Second)
// Get disk IO counters after 1 second
counters2, _ := disk.IOCounters()
for device, counter1 := range counters1 {
counter2 := counters2[device]
readSpeed := float64(counter2.ReadBytes-counter1.ReadBytes) / 1024
writeSpeed := float64(counter2.WriteBytes-counter1.WriteBytes) / 1024
fmt.Printf("%v: read %vKB/s, write %vKB/s\n", device, readSpeed, writeSpeed)
}
}