[agent-go] [status] add status module
This commit is contained in:
44
agent-go/status/Memory.go
Normal file
44
agent-go/status/Memory.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
)
|
||||
|
||||
type MemoryStatus struct {
|
||||
TotalMemory uint64
|
||||
UsedMemory uint64
|
||||
AvailableMemory uint64
|
||||
TotalVirtualMemory uint64
|
||||
UsedVirtualMemory uint64
|
||||
}
|
||||
|
||||
func GetMemoryStatus() (MemoryStatus, error) {
|
||||
var memoryStatus MemoryStatus
|
||||
|
||||
virtualMemoryStat, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
return memoryStatus, err
|
||||
}
|
||||
|
||||
memoryStatus.TotalMemory = virtualMemoryStat.Total
|
||||
memoryStatus.UsedMemory = virtualMemoryStat.Used
|
||||
memoryStatus.AvailableMemory = virtualMemoryStat.Available
|
||||
memoryStatus.TotalVirtualMemory = virtualMemoryStat.Total
|
||||
memoryStatus.UsedVirtualMemory = virtualMemoryStat.Used
|
||||
|
||||
return memoryStatus, nil
|
||||
}
|
||||
|
||||
func FormatMemorySize(size uint64) string {
|
||||
const unit = 1024
|
||||
if size < unit {
|
||||
return fmt.Sprintf("%d B", size)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := size / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.1f %cB", float64(size)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
Reference in New Issue
Block a user