增加更多采集指标
This commit is contained in:
12
agent/copyagent.sh
Executable file
12
agent/copyagent.sh
Executable file
@@ -0,0 +1,12 @@
|
||||
ssh root@10.35.10.130 killall agent monitor-agent
|
||||
ssh root@10.35.10.90 killall agent monitor-agent
|
||||
ssh root@10.35.10.70 killall agent monitor-agent
|
||||
|
||||
scp monitor-agent root@10.35.10.130:/root/monitor/
|
||||
scp monitor-agent root@10.35.10.90:/root/monitor/
|
||||
scp monitor-agent root@10.35.10.70:/root/monitor/
|
||||
|
||||
ssh root@10.35.10.90 cd /root/monitor/ && screen -S agent monitor-agent
|
||||
ssh root@10.35.10.70 cd /root/monitor/ && screen -S agent monitor-agent
|
||||
ssh root@10.35.10.130 cd /root/monitor/ && screen -S agent monitor-agent
|
||||
|
||||
247
agent/main.go
247
agent/main.go
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -61,7 +62,7 @@ type ProcessMetrics struct {
|
||||
|
||||
// DiskDetailMetrics 磁盘详细信息
|
||||
type DiskDetailMetrics struct {
|
||||
DeviceID string `json:"device_id"` // 设备ID
|
||||
Path string `json:"path"` // 设备路径
|
||||
Status string `json:"status"` // 设备状态
|
||||
Type string `json:"type"` // 设备类型
|
||||
SizeGB float64 `json:"size_gb"` // 设备大小(GB)
|
||||
@@ -78,6 +79,52 @@ type LogEntry struct {
|
||||
Message string `json:"message"` // 内容
|
||||
}
|
||||
|
||||
// OSInfo 操作系统信息
|
||||
type OSInfo struct {
|
||||
Name string `json:"name"` // 操作系统名称
|
||||
Version string `json:"version"` // 操作系统版本
|
||||
Arch string `json:"arch"` // 系统架构
|
||||
Fullname string `json:"fullname"` // 完整名称(Name+Version)
|
||||
}
|
||||
|
||||
// CPUInfo CPU详细信息
|
||||
type CPUInfo struct {
|
||||
Model string `json:"model"` // CPU型号
|
||||
Cores int `json:"cores"` // 核心数
|
||||
Threads int `json:"threads"` // 线程数
|
||||
MaxFreq float64 `json:"max_freq"` // 最大频率 (MHz)
|
||||
MinFreq float64 `json:"min_freq"` // 最小频率 (MHz)
|
||||
AvgFreq float64 `json:"avg_freq"` // 平均频率 (MHz)
|
||||
Usage float64 `json:"usage"` // 当前使用率
|
||||
}
|
||||
|
||||
// MemoryInfo 内存详细信息
|
||||
type MemoryInfo struct {
|
||||
Total uint64 `json:"total"` // 总容量 (bytes)
|
||||
Used uint64 `json:"used"` // 已使用 (bytes)
|
||||
Free uint64 `json:"free"` // 空闲 (bytes)
|
||||
UsedPercent float64 `json:"used_percent"` // 使用率百分比
|
||||
}
|
||||
|
||||
// NetworkHardwareInfo 网卡硬件信息
|
||||
type NetworkHardwareInfo struct {
|
||||
Name string `json:"name"` // 网卡名称
|
||||
MAC string `json:"mac"` // MAC地址
|
||||
IPAddresses []string `json:"ip_addresses"` // IP地址列表
|
||||
MTU int `json:"mtu"` // MTU值
|
||||
Speed int `json:"speed"` // 网卡速度 (Mbps)
|
||||
Up bool `json:"up"` // 是否启用
|
||||
}
|
||||
|
||||
// HardwareMetrics 硬件信息指标
|
||||
type HardwareMetrics struct {
|
||||
OS OSInfo `json:"os"` // 操作系统信息
|
||||
CPU CPUInfo `json:"cpu"` // CPU信息
|
||||
Memory MemoryInfo `json:"memory"` // 内存信息
|
||||
DiskDetails []DiskDetailMetrics `json:"disk_details"` // 磁盘硬件信息
|
||||
NetworkCards []NetworkHardwareInfo `json:"network_cards"` // 网卡硬件信息
|
||||
}
|
||||
|
||||
// Metrics 监控指标
|
||||
type Metrics struct {
|
||||
CPU float64 `json:"cpu"`
|
||||
@@ -96,6 +143,8 @@ type Metrics struct {
|
||||
AgentID string `json:"agent_id"` // Agent唯一标识
|
||||
Name string `json:"name"` // 设备名称
|
||||
IP string `json:"ip"` // 设备IP地址
|
||||
// 硬件信息
|
||||
Hardware HardwareMetrics `json:"hardware"` // 硬件详细信息
|
||||
}
|
||||
|
||||
// 全局配置
|
||||
@@ -517,6 +566,158 @@ func collectNetwork() (map[string]NetworkInterfaceMetrics, uint64, uint64, uint6
|
||||
return networkMetrics, totalRxBytes, totalTxBytes, totalRxRate, totalTxRate, nil
|
||||
}
|
||||
|
||||
// 采集操作系统信息
|
||||
func collectOSInfo() (OSInfo, error) {
|
||||
// 从/etc/os-release文件读取操作系统信息
|
||||
content, err := os.ReadFile("/etc/os-release")
|
||||
if err != nil {
|
||||
// 如果读取失败,使用runtime包的信息作为备选
|
||||
return OSInfo{
|
||||
Name: runtime.GOOS,
|
||||
Version: "",
|
||||
Arch: runtime.GOARCH,
|
||||
Fullname: runtime.GOOS,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 解析os-release文件
|
||||
osName := ""
|
||||
osVersion := ""
|
||||
lines := strings.Split(string(content), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasPrefix(line, "NAME=") {
|
||||
osName = strings.Trim(strings.TrimPrefix(line, "NAME="), `"`)
|
||||
} else if strings.HasPrefix(line, "VERSION=") {
|
||||
osVersion = strings.Trim(strings.TrimPrefix(line, "VERSION="), `"`)
|
||||
}
|
||||
}
|
||||
|
||||
// 构建完整名称
|
||||
fullname := osName
|
||||
if osVersion != "" {
|
||||
fullname += " " + osVersion
|
||||
}
|
||||
|
||||
return OSInfo{
|
||||
Name: osName,
|
||||
Version: osVersion,
|
||||
Arch: runtime.GOARCH,
|
||||
Fullname: fullname,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 采集CPU详细信息
|
||||
func collectCPUInfo() (CPUInfo, error) {
|
||||
// 获取CPU使用率
|
||||
percentages, err := cpu.Percent(0, false)
|
||||
cpuUsage := 0.0
|
||||
if err == nil && len(percentages) > 0 {
|
||||
cpuUsage = percentages[0]
|
||||
}
|
||||
|
||||
// 获取CPU信息
|
||||
cpuInfos, err := cpu.Info()
|
||||
if err != nil {
|
||||
return CPUInfo{
|
||||
Usage: cpuUsage,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 初始化频率值
|
||||
maxFreq := 0.0
|
||||
minFreq := 0.0
|
||||
avgFreq := 0.0
|
||||
|
||||
if len(cpuInfos) > 0 {
|
||||
cpuInfo := cpuInfos[0]
|
||||
// 使用CPUInfo中的频率信息,而不是cpu.Freq()
|
||||
avgFreq = cpuInfo.Mhz
|
||||
maxFreq = cpuInfo.Mhz // 简化处理,使用当前频率作为最大频率
|
||||
minFreq = cpuInfo.Mhz // 简化处理,使用当前频率作为最小频率
|
||||
|
||||
return CPUInfo{
|
||||
Model: cpuInfo.ModelName,
|
||||
Cores: int(cpuInfo.Cores),
|
||||
Threads: int(cpuInfo.Cores), // 简化处理,使用核心数作为线程数
|
||||
MaxFreq: maxFreq,
|
||||
MinFreq: minFreq,
|
||||
AvgFreq: avgFreq,
|
||||
Usage: cpuUsage,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return CPUInfo{
|
||||
Usage: cpuUsage,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 采集内存详细信息
|
||||
func collectMemoryInfo() (MemoryInfo, error) {
|
||||
vm, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
return MemoryInfo{}, err
|
||||
}
|
||||
|
||||
return MemoryInfo{
|
||||
Total: vm.Total,
|
||||
Used: vm.Used,
|
||||
Free: vm.Free,
|
||||
UsedPercent: vm.UsedPercent,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 采集网卡硬件信息
|
||||
func collectNetworkHardware() ([]NetworkHardwareInfo, error) {
|
||||
// 获取所有网络接口
|
||||
interfaces, err := stdnet.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
networkCards := make([]NetworkHardwareInfo, 0, len(interfaces))
|
||||
|
||||
for _, iface := range interfaces {
|
||||
// 获取接口的IP地址
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 转换IP地址格式
|
||||
ipAddresses := make([]string, 0, len(addrs))
|
||||
for _, addr := range addrs {
|
||||
ipStr := addr.String()
|
||||
// 移除CIDR后缀
|
||||
if slashIndex := strings.Index(ipStr, "/"); slashIndex > 0 {
|
||||
ipStr = ipStr[:slashIndex]
|
||||
}
|
||||
ipAddresses = append(ipAddresses, ipStr)
|
||||
}
|
||||
|
||||
// 检查网卡是否启用
|
||||
up := iface.Flags&stdnet.FlagUp != 0
|
||||
|
||||
// 获取网卡速度 (Mbps)
|
||||
speed := 0
|
||||
// gopsutil的net.IOCounters没有直接提供速度,这里简化处理
|
||||
// 实际应用中可能需要更复杂的逻辑
|
||||
|
||||
networkCard := NetworkHardwareInfo{
|
||||
Name: iface.Name,
|
||||
MAC: iface.HardwareAddr.String(),
|
||||
IPAddresses: ipAddresses,
|
||||
MTU: iface.MTU,
|
||||
Speed: speed,
|
||||
Up: up,
|
||||
}
|
||||
|
||||
networkCards = append(networkCards, networkCard)
|
||||
}
|
||||
|
||||
return networkCards, nil
|
||||
}
|
||||
|
||||
// 采集所有监控指标
|
||||
// 采集进程信息,返回CPU使用率较高的前N个进程
|
||||
func collectProcessMetrics() ([]ProcessMetrics, error) {
|
||||
@@ -682,11 +883,8 @@ func collectDiskDetails() ([]DiskDetailMetrics, error) {
|
||||
continue // 忽略无法访问的分区
|
||||
}
|
||||
|
||||
// 简单实现,获取设备ID
|
||||
deviceID := partition.Device
|
||||
if len(deviceID) > 0 && deviceID[0] == '/' {
|
||||
deviceID = deviceID[1:]
|
||||
}
|
||||
// 设备路径
|
||||
path := partition.Device
|
||||
|
||||
// 设备状态
|
||||
status := "online"
|
||||
@@ -722,7 +920,7 @@ func collectDiskDetails() ([]DiskDetailMetrics, error) {
|
||||
|
||||
// 创建磁盘详细信息
|
||||
diskDetail := DiskDetailMetrics{
|
||||
DeviceID: deviceID,
|
||||
Path: path,
|
||||
Status: status,
|
||||
Type: diskType,
|
||||
SizeGB: sizeGB,
|
||||
@@ -1015,6 +1213,41 @@ func collectMetrics() (*Metrics, error) {
|
||||
}
|
||||
metrics.Logs = logs
|
||||
|
||||
// 采集硬件信息
|
||||
// 采集操作系统信息
|
||||
osInfo, err := collectOSInfo()
|
||||
if err != nil {
|
||||
log.Printf("Failed to collect OS info: %v", err)
|
||||
}
|
||||
|
||||
// 采集CPU详细信息
|
||||
cpuInfo, err := collectCPUInfo()
|
||||
if err != nil {
|
||||
log.Printf("Failed to collect CPU info: %v", err)
|
||||
}
|
||||
|
||||
// 采集内存详细信息
|
||||
memoryInfo, err := collectMemoryInfo()
|
||||
if err != nil {
|
||||
log.Printf("Failed to collect memory info: %v", err)
|
||||
}
|
||||
|
||||
// 采集网卡硬件信息
|
||||
networkCards, err := collectNetworkHardware()
|
||||
if err != nil {
|
||||
log.Printf("Failed to collect network hardware info: %v", err)
|
||||
networkCards = make([]NetworkHardwareInfo, 0)
|
||||
}
|
||||
|
||||
// 设置硬件信息
|
||||
metrics.Hardware = HardwareMetrics{
|
||||
OS: osInfo,
|
||||
CPU: cpuInfo,
|
||||
Memory: memoryInfo,
|
||||
DiskDetails: diskDetails,
|
||||
NetworkCards: networkCards,
|
||||
}
|
||||
|
||||
return metrics, nil
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user