[Agent] [Status] agent metric accomplish

This commit is contained in:
zeaslity
2023-12-21 11:39:19 +08:00
parent 8becc27472
commit 53f4f526cf
9 changed files with 246 additions and 49 deletions

View File

@@ -174,7 +174,7 @@ func statusOMHandler(octopusMessage *OctopusMessage) {
statusRes = status.Ping() statusRes = status.Ping()
} else if strings.HasPrefix(statusMessage.StatusType, "METRIC") { } else if strings.HasPrefix(statusMessage.StatusType, "METRIC") {
// metric info // metric info
agentStatusString, _ := json.Marshal(status.ReportAppStatus()) agentStatusString, _ := json.Marshal(status.ReportAgentMetric())
statusRes = string(agentStatusString) statusRes = string(agentStatusString)
} else if strings.HasPrefix(statusMessage.StatusType, "INFO") { } else if strings.HasPrefix(statusMessage.StatusType, "INFO") {

View File

@@ -1,9 +1,9 @@
package status package status
import ( import (
"agent-go/utils"
"fmt" "fmt"
"github.com/shirou/gopsutil/v3/disk" "github.com/shirou/gopsutil/v3/disk"
"regexp"
"time" "time"
) )
@@ -14,7 +14,13 @@ type DiskStatus struct {
} }
type DiskInfo struct { type DiskInfo struct {
DiskPartition []disk.PartitionStat Device string `json:"device"`
Mountpoint string `json:"mountpoint"`
Fstype string `json:"fstype"`
Total uint64 `json:"total"`
Free uint64 `json:"free"`
Used uint64 `json:"used"`
UsedPercent float64 `json:"usedPercent"`
} }
func GetDiskStatus() *DiskStatus { func GetDiskStatus() *DiskStatus {
@@ -36,20 +42,43 @@ func GetDiskStatus() *DiskStatus {
return ds return ds
} }
func GetDiskInfo() (*DiskInfo, error) { func GetDiskInfo() ([]DiskInfo, error) {
partitionStats, err := disk.Partitions(true) partitionStats, err := disk.Partitions(false)
if err != nil { if err != nil {
log.ErrorF("[GetDiskInfo] - get disk partition info error ! => %v", err) log.ErrorF("[GetDiskInfo] - get disk partition info error ! => %v", err)
return nil, err return nil, err
} }
d := &DiskInfo{} var result []DiskInfo
for _, partitionStat := range partitionStats { for _, partitionStat := range partitionStats {
utils.BeautifulPrint(partitionStat) if !MatchNeededDisk(partitionStat.Device) {
continue
} }
return d, nil usageStat, err := disk.Usage(partitionStat.Mountpoint)
if err != nil {
log.ErrorF("[GetDiskInfo] - device [%s] get mount point [%s] usage error => %v", partitionStat.Device, partitionStat.Mountpoint, err)
return nil, err
}
// new disk info
diskInfo := DiskInfo{
Device: partitionStat.Device,
Mountpoint: partitionStat.Mountpoint,
Fstype: partitionStat.Fstype,
Total: usageStat.Total,
Free: usageStat.Free,
Used: usageStat.Used,
UsedPercent: usageStat.UsedPercent,
}
// assign
result = append(result, diskInfo)
}
return result, nil
} }
func CalculateDiskIO() { func CalculateDiskIO() {
@@ -67,3 +96,24 @@ func CalculateDiskIO() {
fmt.Printf("%v: read %vKB/s, write %vKB/s\n", device, readSpeed, writeSpeed) fmt.Printf("%v: read %vKB/s, write %vKB/s\n", device, readSpeed, writeSpeed)
} }
} }
func MatchNeededDisk(deviceName string) bool {
match, _ := regexp.MatchString(`^(/dev/loop)\d+`, deviceName)
if match {
return false
}
//if strings.HasPrefix(deviceName, "/sys") {
// return false
//}
//if strings.HasPrefix(deviceName, "/run") {
// return false
//}
//if strings.HasPrefix(deviceName, "/snap") {
// return false
//}
//if strings.HasPrefix(deviceName, "tracefs") {
// return false
//}
return true
}

View File

@@ -0,0 +1,17 @@
package status
import (
"testing"
)
func TestGetDockerInfo(t *testing.T) {
GetDockerInfo()
//diskInfo, err :=
//if err != nil {
// t.Errorf("get docker info error %v", err)
//}
//
//utils.BeautifulPrint(diskInfo)
}

56
agent-go/status/Dokcer.go Normal file
View File

@@ -0,0 +1,56 @@
package status
import (
"agent-go/utils"
"github.com/shirou/gopsutil/v3/docker"
"strings"
)
type DockerMetric struct {
DockerStats []docker.CgroupDockerStat
}
func GetDockerMetric() (*DockerMetric, error) {
dockerStats, err := docker.GetDockerStat()
if err != nil {
log.ErrorF("[GetDockerMetric] - get docker stats list error %v", err)
return nil, err
}
return &DockerMetric{
DockerStats: dockerStats,
}, nil
}
func GetDockerInfo() {
//dockerIDList, err := docker.GetDockerIDList()
//if err != nil {
// log.ErrorF("[GetDockerInfo] - get docker id list error %v", err)
//}
//utils.BeautifulPrint(dockerIDList)
dockerStats, err := docker.GetDockerStat()
if err != nil {
log.ErrorF("[GetDockerInfo] - get docker stats list error %v", err)
}
utils.BeautifulPrint(dockerStats)
for _, dockerStat := range dockerStats {
if strings.Contains(dockerStat.Name, "mysql") {
cpuDocker, _ := docker.CgroupCPUDocker(dockerStat.ContainerID)
utils.BeautifulPrint(cpuDocker)
usageDocker, _ := docker.CgroupCPUUsageDocker(dockerStat.ContainerID)
utils.BeautifulPrint(usageDocker)
mem, _ := docker.CgroupMemDocker(dockerStat.ContainerID)
utils.BeautifulPrint(mem)
}
}
}

View File

@@ -45,7 +45,7 @@ type MemoryInfo struct {
HugePageSize uint64 `json:"hugePageSize"` HugePageSize uint64 `json:"hugePageSize"`
} }
func GetMemoryStatus() (*MemoryMetric, error) { func GetMemoryMetric() (*MemoryMetric, error) {
virtualMemoryStat, err := mem.VirtualMemory() virtualMemoryStat, err := mem.VirtualMemory()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -8,7 +8,7 @@ import (
func TestGetMemoryStatus(t *testing.T) { func TestGetMemoryStatus(t *testing.T) {
memoryStatus, err := GetMemoryStatus() memoryStatus, err := GetMemoryMetric()
if err != nil { if err != nil {
return return
} }

View File

@@ -1,6 +1,7 @@
package status package status
import ( import (
"agent-go/utils"
"encoding/json" "encoding/json"
"fmt" "fmt"
"testing" "testing"
@@ -37,10 +38,7 @@ func TestGetNetworkInfo(t *testing.T) {
} }
for _, networkInfo := range networkInfos { for _, networkInfo := range networkInfos {
fmt.Println() utils.BeautifulPrint(networkInfo)
marshal, _ := json.MarshalIndent(networkInfo, "", " ")
fmt.Println(string(marshal))
fmt.Println()
} }
} }

View File

@@ -1,12 +1,14 @@
package status package status
import ( import (
"agent-go/g"
logger2 "agent-go/logger" logger2 "agent-go/logger"
"fmt" "fmt"
"time" "time"
) )
var log = logger2.Log var log = logger2.Log
var pool = g.G.P
type StatusMessage struct { type StatusMessage struct {
/** /**
@@ -20,11 +22,107 @@ type StatusMessage struct {
metricRepeatPinch int `json:"metricRepeatPinch,omitempty"` metricRepeatPinch int `json:"metricRepeatPinch,omitempty"`
} }
type AgentStatus struct { type AgentMetric struct {
CPUStatus *CPUMetric CPUMetric *CPUMetric
MemoryStatus *MemoryMetric MemoryMetric *MemoryMetric
NetworkStatus *NetworkStatus NetworkMetric []NetworkMetric
DiskStatus *DiskStatus DiskInfo []DiskInfo
DockerMetric *DockerMetric
}
func Ping() string {
return "PONG"
}
func ReportAgentMetric() *AgentMetric {
lenOfAgentMetric := 5
waitResultChan := make(chan string, lenOfAgentMetric)
timeout := time.After(5 * time.Second)
var err error
var cpuMetric *CPUMetric
err = pool.Submit(func() {
cpuMetric, err = GetCPUMetric()
if err != nil {
log.ErrorF("获取Agent的状态出现错误 请检查 => %v", err)
waitResultChan <- "GetCPUMetric error !"
}
waitResultChan <- "GetCPUMetric success !"
})
if err != nil {
log.ErrorF("[ReportAgentMetric] - GetCPUMetric exec error => %v", err)
}
var memoryMetric *MemoryMetric
err = pool.Submit(func() {
memoryMetric, err = GetMemoryMetric()
if err != nil {
log.ErrorF("获取Agent的状态出现错误 请检查 => %v", err)
waitResultChan <- "GetMemoryMetric error !"
}
waitResultChan <- "GetMemoryMetric success !"
})
if err != nil {
log.ErrorF("[ReportAgentMetric] - GetMemoryMetric exec error => %v", err)
}
var diskInfoList []DiskInfo
err = pool.Submit(func() {
diskInfoList, err = GetDiskInfo()
if err != nil {
log.ErrorF("获取Agent的状态出现错误 请检查 => %v", err)
waitResultChan <- "GetDiskInfo error !"
}
waitResultChan <- "GetDiskInfo success !"
})
if err != nil {
log.ErrorF("[ReportAgentMetric] - GetDiskInfo exec error => %v", err)
}
var networkMetric []NetworkMetric
err = pool.Submit(func() {
networkMetric, err = GetNetworkMetric()
if err != nil {
log.ErrorF("获取Agent的状态出现错误 请检查 => %v", err)
waitResultChan <- "GetNetworkMetric error !"
}
waitResultChan <- "GetNetworkMetric success !"
})
if err != nil {
log.ErrorF("[ReportAgentMetric] - GetNetworkMetric exec error => %v", err)
}
var dockerMetric *DockerMetric
err = pool.Submit(func() {
dockerMetric, err = GetDockerMetric()
if err != nil {
log.ErrorF("获取Agent的状态出现错误 请检查 => %v", err)
waitResultChan <- "GetDockerMetric error !"
}
waitResultChan <- "GetDockerMetric success !"
})
if err != nil {
log.ErrorF("[ReportAgentMetric] - GetDockerMetric exec error => %v", err)
}
for i := 0; i < lenOfAgentMetric; i++ {
select {
case result := <-waitResultChan:
log.DebugF("[ReportAgentMetric] - metric received => %s", result)
case <-timeout:
fmt.Println("[ReportAgentMetric] - Timeout! Not all results received.")
break
}
}
return &AgentMetric{
CPUMetric: cpuMetric,
MemoryMetric: memoryMetric,
NetworkMetric: networkMetric,
DiskInfo: diskInfoList,
DockerMetric: dockerMetric,
}
} }
func ConvertToFormat(eventData float64) string { func ConvertToFormat(eventData float64) string {
@@ -38,26 +136,3 @@ func ConvertToFormat(eventData float64) string {
milliseconds := duration.Milliseconds() % 1000 milliseconds := duration.Milliseconds() % 1000
return fmt.Sprintf("%02d:%02d:%02d,%03d", hours, minutes, seconds, milliseconds) return fmt.Sprintf("%02d:%02d:%02d,%03d", hours, minutes, seconds, milliseconds)
} }
func Ping() string {
return "PONG"
}
func ReportAppStatus() *AgentStatus {
cpuStatus, cpuerr := GetCPUMetric()
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,
}
}

View File

@@ -1,9 +1,10 @@
package status package status
import ( import (
"encoding/json" "agent-go/utils"
"fmt" "fmt"
"testing" "testing"
"time"
) )
func TestConvertToFormat(t *testing.T) { func TestConvertToFormat(t *testing.T) {
@@ -15,12 +16,12 @@ func TestConvertToFormat(t *testing.T) {
func TestReportAppStatus(t *testing.T) { func TestReportAppStatus(t *testing.T) {
agentStatus := ReportAppStatus() startTime := time.Now()
agentStatus := ReportAgentMetric()
marshal, err := json.Marshal(agentStatus) endTime := time.Now()
if err != nil { elapsedTime := endTime.Sub(startTime)
return fmt.Println("函数执行时间:", elapsedTime)
}
fmt.Printf(string(marshal)) utils.BeautifulPrint(agentStatus)
} }