From 86b6484194923fffe7c9aa692e881288a8b253d9 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Thu, 18 Jan 2024 15:56:07 +0800 Subject: [PATCH] [ Agent ] [ Status ] - accomplish agent info part --- agent-go/executor/BaseFunction.go | 4 +- agent-go/executor/InitFunction.go | 72 +++++++++- agent-go/status/Disk.go | 4 +- agent-go/status/Status.go | 136 ++++++++++++++++--- agent-go/status/Status_test.go | 11 ++ agent-go/status/tmp/AgentStatus.json | 188 +++++++++++++++++++++++++++ 6 files changed, 391 insertions(+), 24 deletions(-) create mode 100644 agent-go/status/tmp/AgentStatus.json diff --git a/agent-go/executor/BaseFunction.go b/agent-go/executor/BaseFunction.go index 7704a95..68b8c2a 100644 --- a/agent-go/executor/BaseFunction.go +++ b/agent-go/executor/BaseFunction.go @@ -23,7 +23,9 @@ type AgentOsOperator struct { CanAccessInternet bool `json:"can_access_internet",comment:"是否可以访问公网"` - IsOsTypeUbuntu bool `json:"is_os_type_ubuntu",comment:"主机操作系统是否为ubuntu系列"` + IsOsTypeUbuntu bool `json:"is_os_type_ubuntu",comment:"主机操作系统是否为debian系列"` + + IsOsTypeCentOS bool `json:"is_os_type_centos",comment:"主机操作系统是否为centos系列"` IsAgentInnerWall bool `json:"is_agent_inner_wall",comment:"主机是否身处国内"` diff --git a/agent-go/executor/InitFunction.go b/agent-go/executor/InitFunction.go index 2ae464e..847d39d 100644 --- a/agent-go/executor/InitFunction.go +++ b/agent-go/executor/InitFunction.go @@ -1,13 +1,13 @@ package executor import ( + "agent-go/status" "strconv" "strings" ) func BuildAgentOsOperator(osInfo string, ossOfflinePrefix string) *AgentOsOperator { - // todo build from env AgentOsOperatorCache = &AgentOsOperator{ InstallCommandPrefix: []string{ "apt-get", "install", "--allow-downgrades", "-y", @@ -23,7 +23,7 @@ func BuildAgentOsOperator(osInfo string, ossOfflinePrefix string) *AgentOsOperat } // os type - detectByOsType(AgentOsOperatorCache, osInfo) + detectByAgentStatusInfo(AgentOsOperatorCache) // internet detectByInternet(AgentOsOperatorCache) @@ -31,6 +31,74 @@ func BuildAgentOsOperator(osInfo string, ossOfflinePrefix string) *AgentOsOperat return AgentOsOperatorCache } +func detectByAgentStatusInfo(os *AgentOsOperator) { + agentMetric := status.ReportAgentMetric() + + if strings.Contains(agentMetric.HostInfo.PlatformFamily, "centos") { + // centos + os.IsOsTypeUbuntu = false + os.IsOsTypeCentOS = true + os.InstallCommandPrefix = []string{ + "yum", "install", "-y", + } + os.RemoveCommandPrefix = []string{ + "yum", "remove", + } + + } else if strings.Contains(agentMetric.HostInfo.PlatformFamily, "debian") { + // ubuntu + os.IsOsTypeUbuntu = true + os.RemoveCommandPrefix = []string{"apt", "remove", "-y"} + os.InstallCommandPrefix = []string{ + "apt-get", "install", "--allow-downgrades", "-y", + } + + // os release code + os.AgentOSReleaseCode = judgeUbuntuReleaseFromOsVersion(agentMetric.HostInfo.PlatformVersion) + } + + // agent cpu arch + os.AgentArch = judgeAgentCpuArchByKernelArch(agentMetric.HostInfo.KernelArch) + +} + +func judgeUbuntuReleaseFromOsVersion(osVersion string) string { + + switch osVersion { + case "16.04": + return "xenial" + case "18.04": + return "bionic" + case "20.04": + return "focal" + case "22.04": + return "jammy" + default: + return "ubuntu-unknown" + } +} + +func judgeAgentCpuArchByKernelArch(kernelArch string) string { + switch kernelArch { + case "x86_64": + return "amd64" + case "aarch64": + return "arm64" + case "armv8": + return "arm64" + case "armv6": + return "arm6" + case "armv7": + return "arm7" + case "i32": + return "386" + case "i86": + return "386" + default: + return "cpu-unknown" + } +} + func detectByOsType(os *AgentOsOperator, osInfo string) { ubuntuOsReleaseCode := [][]string{ diff --git a/agent-go/status/Disk.go b/agent-go/status/Disk.go index 4396918..eaad69b 100644 --- a/agent-go/status/Disk.go +++ b/agent-go/status/Disk.go @@ -8,9 +8,7 @@ import ( ) type DiskStatus struct { - Total uint64 - Used uint64 - //LogicalDisk []disk.PartitionStat + DiskInfo } type DiskInfo struct { diff --git a/agent-go/status/Status.go b/agent-go/status/Status.go index 9849e10..a8a5a7a 100644 --- a/agent-go/status/Status.go +++ b/agent-go/status/Status.go @@ -26,18 +26,26 @@ type AgentMetric struct { CPUMetric *CPUMetric MemoryMetric *MemoryMetric NetworkMetric []NetworkMetric - DiskInfo []DiskInfo - HostInfo *HostInfo + DiskMetric []DiskInfo DockerMetric *DockerMetric } +type AgentInfo struct { + CPUInfo *CPUInfo + MemoryInfo *MemoryInfo + NetworkInfo []NetworkInfo + DiskInfo []DiskInfo + HostInfo *HostInfo + DockerInfo *DockerMetric +} + func Ping() string { return "PONG" } func ReportAgentMetric() *AgentMetric { - lenOfAgentMetric := 6 + lenOfAgentMetric := 5 waitResultChan := make(chan string, lenOfAgentMetric) timeout := time.After(5 * time.Second) var err error @@ -81,19 +89,6 @@ func ReportAgentMetric() *AgentMetric { log.ErrorF("[ReportAgentMetric] - GetDiskInfo exec error => %v", err) } - var hostInfo *HostInfo - err = pool.Submit(func() { - hostInfo, err = GetHostInfo() - if err != nil { - log.ErrorF("获取Agent的状态出现错误! 请检查 => %v", err) - waitResultChan <- "GetHostInfo error !" - } - waitResultChan <- "hostInfo success !" - }) - if err != nil { - log.ErrorF("[ReportAgentMetric] - hostInfo exec error => %v", err) - } - var networkMetric []NetworkMetric err = pool.Submit(func() { networkMetric, err = GetNetworkMetric() @@ -133,13 +128,118 @@ func ReportAgentMetric() *AgentMetric { CPUMetric: cpuMetric, MemoryMetric: memoryMetric, NetworkMetric: networkMetric, - DiskInfo: diskInfoList, - HostInfo: hostInfo, + DiskMetric: diskInfoList, DockerMetric: dockerMetric, } } +func ReportAgentInfo() *AgentInfo { + + lenOfAgentMetric := 6 + waitResultChan := make(chan string, lenOfAgentMetric) + timeout := time.After(5 * time.Second) + var err error + + var cpuInfo *CPUInfo + err = pool.Submit(func() { + cpuInfo, err = GetCPUInfo() + if err != nil { + log.ErrorF("获取Agent的状态出现错误! 请检查 => %v", err) + waitResultChan <- "GetCPUInfo error !" + } + waitResultChan <- "GetCPUInfo success !" + }) + if err != nil { + log.ErrorF("[ReportAgentInfo] - GetCPUInfo exec error => %v", err) + } + + var memoryInfo *MemoryInfo + err = pool.Submit(func() { + memoryInfo, err = GetMemoryInfo() + if err != nil { + log.ErrorF("获取Agent的状态出现错误! 请检查 => %v", err) + waitResultChan <- "GetMemoryInfo error !" + } + waitResultChan <- "GetMemoryInfo success !" + }) + if err != nil { + log.ErrorF("[ReportAgentInfo] - GetMemoryInfo 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("[ReportAgentInfo] - GetDiskInfo exec error => %v", err) + } + + var hostInfo *HostInfo + err = pool.Submit(func() { + hostInfo, err = GetHostInfo() + if err != nil { + log.ErrorF("获取Agent的状态出现错误! 请检查 => %v", err) + waitResultChan <- "GetHostInfo error !" + } + waitResultChan <- "hostInfo success !" + }) + if err != nil { + log.ErrorF("[ReportAgentInfo] - hostInfo exec error => %v", err) + } + + var networkInfo []NetworkInfo + err = pool.Submit(func() { + networkInfo, err = GetNetworkInfo() + if err != nil { + log.ErrorF("获取Agent的状态出现错误! 请检查 => %v", err) + waitResultChan <- "GetNetworkInfo error !" + } + waitResultChan <- "GetNetworkInfo success !" + }) + if err != nil { + log.ErrorF("[ReportAgentInfo] - GetNetworkInfo 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("[ReportAgentInfo] - GetDockerMetric exec error => %v", err) + } + + for i := 0; i < lenOfAgentMetric; i++ { + select { + case result := <-waitResultChan: + log.DebugF("[ReportAgentInfo] - metric received => %s", result) + case <-timeout: + fmt.Println("[ReportAgentInfo] - Timeout! Not all results received.") + break + } + } + + return &AgentInfo{ + CPUInfo: cpuInfo, + MemoryInfo: memoryInfo, + NetworkInfo: networkInfo, + DiskInfo: diskInfoList, + HostInfo: hostInfo, + DockerInfo: dockerMetric, + } + +} + func ConvertToFormat(eventData float64) string { duration := time.Duration(eventData) * time.Second diff --git a/agent-go/status/Status_test.go b/agent-go/status/Status_test.go index 452f224..4968185 100644 --- a/agent-go/status/Status_test.go +++ b/agent-go/status/Status_test.go @@ -25,3 +25,14 @@ func TestReportAppStatus(t *testing.T) { utils.BeautifulPrint(agentStatus) } + +func TestReportAgentInfo(t *testing.T) { + startTime := time.Now() + agentInfo := ReportAgentInfo() + + endTime := time.Now() + elapsedTime := endTime.Sub(startTime) + fmt.Println("函数执行时间:", elapsedTime) + + utils.BeautifulPrint(agentInfo) +} diff --git a/agent-go/status/tmp/AgentStatus.json b/agent-go/status/tmp/AgentStatus.json new file mode 100644 index 0000000..724555f --- /dev/null +++ b/agent-go/status/tmp/AgentStatus.json @@ -0,0 +1,188 @@ +{ + "CPUMetric": { + "NumCores": 8, + "CPUPercent": 33.37515683815345, + "CPULoads": { + "load1": 3.94, + "load5": 2.64, + "load15": 2 + } + }, + "MemoryMetric": { + "TotalMemory": 16604577792, + "AvailableMemory": 10842226688, + "UsedMemory": 4859039744, + "usedPercent": 29.263253813903418, + "free": 6947397632 + }, + "NetworkMetric": [ + { + "name": "lo", + "bytesSent": 10833552, + "bytesRecv": 10833552, + "packetsSent": 134017, + "packetsRecv": 134017, + "errin": 0, + "errout": 0, + "dropin": 0, + "dropout": 0, + "fifoin": 0, + "fifoout": 0, + "SendSpeed": 7084.5, + "RecvSpeed": 7084.5 + }, + { + "name": "eno2", + "bytesSent": 42069937, + "bytesRecv": 9954190, + "packetsSent": 96154, + "packetsRecv": 114005, + "errin": 0, + "errout": 0, + "dropin": 0, + "dropout": 0, + "fifoin": 0, + "fifoout": 0, + "SendSpeed": 160668.5, + "RecvSpeed": 28342 + } + ], + "DiskInfo": [ + { + "device": "/dev/nvme0n1p7", + "mountpoint": "/", + "fstype": "ext4", + "total": 153682100224, + "free": 54671085568, + "used": 91129868288, + "usedPercent": 62.502930109774326 + }, + { + "device": "/dev/nvme0n1p1", + "mountpoint": "/boot/efi", + "fstype": "vfat", + "total": 520093696, + "free": 426680320, + "used": 93413376, + "usedPercent": 17.96087449596774 + } + ], + "HostInfo": { + "hostname": "wdd-Lap-Office", + "uptime": 1980, + "bootTime": 1705560863, + "procs": 358, + "os": "linux", + "platform": "ubuntu", + "platformFamily": "debian", + "platformVersion": "20.04", + "kernelVersion": "5.15.0-91-generic", + "kernelArch": "x86_64", + "virtualizationSystem": "kvm", + "virtualizationRole": "host", + "hostId": "4c4c4544-004e-4a10-8036-b7c04f4e3533" + }, + "DockerMetric": { + "DockerStats": [ + { + "containerID": "b256661bcab1626be7fba15c487db61082de68dca473e145f66db7b06f212e4a", + "name": "octopus-redis-1", + "image": "redis/redis-stack:6.2.6-v10", + "status": "Up 32 minutes", + "running": true + }, + { + "containerID": "04334154b90fabb2e5f595d4746641a75deb85b00848791492bc9742e8119da1", + "name": "octopus-rabbitmq-1", + "image": "docker.io/bitnami/rabbitmq:3.11.10-debian-11-r0", + "status": "Up 32 minutes", + "running": true + }, + { + "containerID": "48c759747ab0bc24c4411fcfd59f1f33a50e71986eab582cb92c3cfaa2c38b6b", + "name": "octopus-mysql-1", + "image": "bitnami/mysql:8.0.32-debian-11-r12", + "status": "Up 32 minutes (unhealthy)", + "running": true + }, + { + "containerID": "b1016d88ba47aacf1cccbbf90d6ac04b0ccdfbf6de15ca31303d5af8c7c1d897", + "name": "harbor-jobservice", + "image": "goharbor/harbor-jobservice:v2.9.0", + "status": "Up 32 minutes (healthy)", + "running": true + }, + { + "containerID": "0d2321d74fe8c7261a6a368d8ca7d0f6b3029eff16bdf490bdfd7c51d81f7ac1", + "name": "nginx", + "image": "goharbor/nginx-photon:v2.9.0", + "status": "Up 32 minutes (healthy)", + "running": true + }, + { + "containerID": "9b6ad191743d6a0422a62bfbf7fcc3477645f296df88479ff584a3032b7a3991", + "name": "harbor-core", + "image": "goharbor/harbor-core:v2.9.0", + "status": "Up 32 minutes (healthy)", + "running": true + }, + { + "containerID": "84c452be11a3b51bf82ccdd3b5e92044d230929f34141a3782fcc8b53e549d80", + "name": "harbor-db", + "image": "goharbor/harbor-db:v2.9.0", + "status": "Up 32 minutes (healthy)", + "running": true + }, + { + "containerID": "f5f1da78ea2ac4141571d426f7457cb9d16befd69443d353dbede27b085326fb", + "name": "registryctl", + "image": "goharbor/harbor-registryctl:v2.9.0", + "status": "Up 32 minutes (healthy)", + "running": true + }, + { + "containerID": "b2e97123f38b3c95ec58f4b494cc1f261cbef184834ec0584e50377ae3923bb0", + "name": "registry", + "image": "goharbor/registry-photon:v2.9.0", + "status": "Up 32 minutes (healthy)", + "running": true + }, + { + "containerID": "1ba12ec625c13be6ecb052330443d6748b0ab9e692302397fadfad085768906f", + "name": "harbor-portal", + "image": "goharbor/harbor-portal:v2.9.0", + "status": "Up 32 minutes (healthy)", + "running": true + }, + { + "containerID": "250afc54541ddc7318dc7ff3a62013ca1da538de089e676b1d44c1e487742e0d", + "name": "redis", + "image": "goharbor/redis-photon:v2.9.0", + "status": "Up 32 minutes (healthy)", + "running": true + }, + { + "containerID": "a2724d367906efd2c6277d405c5d3da00a80a206c4f9b10aa9a867b735c441f9", + "name": "harbor-log", + "image": "goharbor/harbor-log:v2.9.0", + "status": "Up 32 minutes (healthy)", + "running": true + }, + { + "containerID": "ea12324df45a77f1f073e88f11de7eae0a95ce5a32c0c30992cdf689d705a842", + "name": "wdd-minio1-1", + "image": "bitnami/minio:2022.5.4", + "status": "Up 32 minutes", + "running": true + }, + { + "containerID": "d2df045ccb00b8c7ad14635556217cec743be4d302091ff1bfaa1039277e6da6", + "name": "root-minio1-1", + "image": "bitnami/minio:2022.5.4", + "status": "Exited (1) 5 months ago", + "running": false + } + ] + } +} +