70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package status
|
|
|
|
import (
|
|
"agent-go/utils"
|
|
"fmt"
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
"time"
|
|
)
|
|
|
|
type DiskStatus struct {
|
|
Total uint64
|
|
Used uint64
|
|
//LogicalDisk []disk.PartitionStat
|
|
}
|
|
|
|
type DiskInfo struct {
|
|
DiskPartition []disk.PartitionStat
|
|
}
|
|
|
|
func GetDiskStatus() *DiskStatus {
|
|
|
|
ds := &DiskStatus{}
|
|
|
|
// Get disk usage
|
|
du, _ := disk.Usage("/")
|
|
ds.Total = du.Total
|
|
ds.Used = du.Used
|
|
|
|
// 2023年7月14日 去除此部分 没什么用
|
|
// Get logical disk info for Linux systems
|
|
//if runtime.GOOS == "linux" {
|
|
// ld, _ := disk.Partitions(true)
|
|
// ds.LogicalDisk = ld
|
|
//}
|
|
|
|
return ds
|
|
}
|
|
|
|
func GetDiskInfo() (*DiskInfo, error) {
|
|
|
|
partitionStats, err := disk.Partitions(true)
|
|
if err != nil {
|
|
log.ErrorF("[GetDiskInfo] - get disk partition info error ! => %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
d := &DiskInfo{}
|
|
for _, partitionStat := range partitionStats {
|
|
utils.BeautifulPrint(partitionStat)
|
|
}
|
|
|
|
return d, nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|