[agent-go] [status] basically accomplished the status module
This commit is contained in:
47
agent-go/status/Disk.go
Normal file
47
agent-go/status/Disk.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
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 {
|
||||||
|
var 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
14
agent-go/status/Disk_test.go
Normal file
14
agent-go/status/Disk_test.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package status
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetDiskStatus(t *testing.T) {
|
||||||
|
|
||||||
|
ds := GetDiskStatus()
|
||||||
|
fmt.Printf("Total: %v, Used: %v\n", ds.Total, ds.Used)
|
||||||
|
fmt.Printf("Logical Disks: %v\n", ds.LogicalDisk)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -13,8 +13,8 @@ type MemoryStatus struct {
|
|||||||
UsedVirtualMemory uint64
|
UsedVirtualMemory uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMemoryStatus() (MemoryStatus, error) {
|
func GetMemoryStatus() (*MemoryStatus, error) {
|
||||||
var memoryStatus MemoryStatus
|
var memoryStatus *MemoryStatus
|
||||||
|
|
||||||
virtualMemoryStat, err := mem.VirtualMemory()
|
virtualMemoryStat, err := mem.VirtualMemory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NetworkInfo struct {
|
type NetworkStatus struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
InternalIPv4 []string `json:"internal_ip_v4,omitempty"`
|
InternalIPv4 []string `json:"internal_ip_v4,omitempty"`
|
||||||
InternalIPv6 []string `json:"internal_ip_v6,omitempty"`
|
InternalIPv6 []string `json:"internal_ip_v6,omitempty"`
|
||||||
@@ -21,7 +21,7 @@ type NetworkInfo struct {
|
|||||||
RecvRate string `json:"recv_rate,omitempty"`
|
RecvRate string `json:"recv_rate,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNetworkInfo() (*NetworkInfo, error) {
|
func GetNetworkStatus() (*NetworkStatus, error) {
|
||||||
interfaces, err := net.Interfaces()
|
interfaces, err := net.Interfaces()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -57,21 +57,25 @@ func GetNetworkInfo() (*NetworkInfo, error) {
|
|||||||
|
|
||||||
// 休眠3秒
|
// 休眠3秒
|
||||||
|
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
var sentAfter uint64
|
var sentAfter uint64
|
||||||
var recvAfter uint64
|
var recvAfter uint64
|
||||||
for _, counter := range counters {
|
countersAfter, err := net.IOCounters(true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, counter := range countersAfter {
|
||||||
if counter.Name == mainInterface.Name {
|
if counter.Name == mainInterface.Name {
|
||||||
sentAfter = counter.BytesSent
|
sentAfter = counter.BytesSent
|
||||||
recvAfter = counter.BytesRecv
|
recvAfter = counter.BytesRecv
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendRate := fmt.Sprintf(formatBytes((sentAfter-sent)/10) + "/s")
|
sendRate := fmt.Sprintf(formatBytes((sentAfter-sent)/3) + "/s")
|
||||||
recvRate := fmt.Sprintf(formatBytes((recvAfter-recv)/10) + "/s")
|
recvRate := fmt.Sprintf(formatBytes((recvAfter-recv)/3) + "/s")
|
||||||
|
|
||||||
info := &NetworkInfo{
|
info := &NetworkStatus{
|
||||||
Name: mainInterface.Name,
|
Name: mainInterface.Name,
|
||||||
InternalIPv4: ipv4List,
|
InternalIPv4: ipv4List,
|
||||||
InternalIPv6: ipv6List,
|
InternalIPv6: ipv6List,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
func TestGetNetworkInfo(t *testing.T) {
|
func TestGetNetworkInfo(t *testing.T) {
|
||||||
|
|
||||||
networkInfo, err := GetNetworkInfo()
|
networkInfo, err := GetNetworkStatus()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,3 +5,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var log = logger2.Log
|
var log = logger2.Log
|
||||||
|
|
||||||
|
type AgentStatus struct {
|
||||||
|
CPUStatus *CPUStatus
|
||||||
|
MemoryStatus *MemoryStatus
|
||||||
|
NetworkStatus *NetworkStatus
|
||||||
|
DiskStatus *DiskStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
func Ping() string {
|
||||||
|
return "PONG"
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReportAppStatus() *AgentStatus {
|
||||||
|
|
||||||
|
cpuStatus, cpuerr := GetCPUStatus()
|
||||||
|
memoryStatus, memerr := GetMemoryStatus()
|
||||||
|
networkStatus, neterr := GetNetworkStatus()
|
||||||
|
if cpuerr != nil || memerr != nil || neterr != nil {
|
||||||
|
log.ErrorF("获取Agent的状态出现错误! 请检查")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
diskStatus := GetDiskStatus()
|
||||||
|
return &AgentStatus{
|
||||||
|
CPUStatus: cpuStatus,
|
||||||
|
MemoryStatus: memoryStatus,
|
||||||
|
NetworkStatus: networkStatus,
|
||||||
|
DiskStatus: diskStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user