数据调优
This commit is contained in:
@@ -31,15 +31,24 @@ type Config struct {
|
||||
|
||||
// NetworkInterfaceMetrics 网卡监控指标
|
||||
type NetworkInterfaceMetrics struct {
|
||||
BytesSent uint64 `json:"bytes_sent"`
|
||||
BytesReceived uint64 `json:"bytes_received"`
|
||||
BytesSent uint64 `json:"bytes_sent"` // 发送速率 (bytes/s)
|
||||
BytesReceived uint64 `json:"bytes_received"` // 接收速率 (bytes/s)
|
||||
TxBytes uint64 `json:"tx_bytes"` // 累计发送字节数
|
||||
RxBytes uint64 `json:"rx_bytes"` // 累计接收字节数
|
||||
}
|
||||
|
||||
// DiskMetrics 磁盘监控指标
|
||||
type DiskMetrics struct {
|
||||
UsedPercent float64 `json:"used_percent"` // 使用率百分比
|
||||
Total uint64 `json:"total"` // 总容量 (bytes)
|
||||
}
|
||||
|
||||
// Metrics 监控指标
|
||||
type Metrics struct {
|
||||
CPU float64 `json:"cpu"`
|
||||
CPUHz float64 `json:"cpu_hz"` // CPU频率 (MHz)
|
||||
Memory float64 `json:"memory"`
|
||||
Disk map[string]float64 `json:"disk"`
|
||||
Disk map[string]DiskMetrics `json:"disk"`
|
||||
Network map[string]NetworkInterfaceMetrics `json:"network"`
|
||||
}
|
||||
|
||||
@@ -271,14 +280,29 @@ func readConfigFile() {
|
||||
log.Printf("Config loaded from %s", configFile)
|
||||
}
|
||||
|
||||
// 采集CPU使用率
|
||||
func collectCPU() (float64, error) {
|
||||
// 采集CPU使用率和频率
|
||||
func collectCPU() (float64, float64, error) {
|
||||
// 采集CPU使用率
|
||||
percentages, err := cpu.Percent(0, false)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
return percentages[0], nil
|
||||
// 采集CPU频率(使用CPUInfo获取频率信息)
|
||||
cpus, err := cpu.Info()
|
||||
if err != nil {
|
||||
return percentages[0], 0, nil // 频率采集失败,返回使用率和0频率
|
||||
}
|
||||
|
||||
// 计算平均频率 (转换为MHz)
|
||||
var totalFreq float64
|
||||
for _, cpuInfo := range cpus {
|
||||
// CPUInfo返回的MHz,直接累加
|
||||
totalFreq += cpuInfo.Mhz
|
||||
}
|
||||
avgFreq := totalFreq / float64(len(cpus))
|
||||
|
||||
return percentages[0], avgFreq, nil
|
||||
}
|
||||
|
||||
// 采集内存使用率
|
||||
@@ -291,8 +315,8 @@ func collectMemory() (float64, error) {
|
||||
return vm.UsedPercent, nil
|
||||
}
|
||||
|
||||
// 采集磁盘使用率
|
||||
func collectDisk() (map[string]float64, error) {
|
||||
// 采集磁盘使用率和总容量
|
||||
func collectDisk() (map[string]DiskMetrics, error) {
|
||||
// 获取系统所有挂载点
|
||||
partitions, err := disk.Partitions(false)
|
||||
if err != nil {
|
||||
@@ -300,26 +324,32 @@ func collectDisk() (map[string]float64, error) {
|
||||
}
|
||||
|
||||
// 初始化返回值
|
||||
diskUsageMap := make(map[string]float64)
|
||||
diskMetricsMap := make(map[string]DiskMetrics)
|
||||
|
||||
// 遍历所有挂载点,采集磁盘使用率
|
||||
// 遍历所有挂载点,采集磁盘使用率和总容量
|
||||
for _, partition := range partitions {
|
||||
// 只处理本地文件系统,跳过网络文件系统
|
||||
if partition.Fstype == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// 采集磁盘使用率
|
||||
// 采集磁盘使用率和总容量
|
||||
usage, err := disk.Usage(partition.Mountpoint)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 保存磁盘使用率
|
||||
diskUsageMap[partition.Mountpoint] = usage.UsedPercent
|
||||
// 创建DiskMetrics结构体
|
||||
diskMetrics := DiskMetrics{
|
||||
UsedPercent: usage.UsedPercent,
|
||||
Total: usage.Total,
|
||||
}
|
||||
|
||||
// 保存磁盘指标
|
||||
diskMetricsMap[partition.Mountpoint] = diskMetrics
|
||||
}
|
||||
|
||||
return diskUsageMap, nil
|
||||
return diskMetricsMap, nil
|
||||
}
|
||||
|
||||
// 采集网络流量
|
||||
@@ -342,7 +372,7 @@ func collectNetwork() (map[string]NetworkInterfaceMetrics, error) {
|
||||
|
||||
// 遍历所有网卡
|
||||
for _, counter := range ioCounters {
|
||||
// 获取当前网卡的流量
|
||||
// 获取当前网卡的累计流量
|
||||
currentBytesSent := counter.BytesSent
|
||||
currentBytesReceived := counter.BytesRecv
|
||||
|
||||
@@ -372,17 +402,19 @@ func collectNetwork() (map[string]NetworkInterfaceMetrics, error) {
|
||||
BytesReceived: currentBytesReceived,
|
||||
}
|
||||
|
||||
// 保存当前网卡的速率
|
||||
// 保存当前网卡的速率和累计流量
|
||||
networkMetrics[counter.Name] = NetworkInterfaceMetrics{
|
||||
BytesSent: bytesSentRate,
|
||||
BytesReceived: bytesReceivedRate,
|
||||
TxBytes: currentBytesSent,
|
||||
RxBytes: currentBytesReceived,
|
||||
}
|
||||
}
|
||||
|
||||
// 更新上一次采集时间
|
||||
lastCollectTime = currentTime
|
||||
|
||||
// 返回所有网卡的速率
|
||||
// 返回所有网卡的速率和累计流量
|
||||
return networkMetrics, nil
|
||||
}
|
||||
|
||||
@@ -390,12 +422,13 @@ func collectNetwork() (map[string]NetworkInterfaceMetrics, error) {
|
||||
func collectMetrics() (*Metrics, error) {
|
||||
metrics := &Metrics{}
|
||||
|
||||
// 采集CPU使用率
|
||||
cpuUsage, err := collectCPU()
|
||||
// 采集CPU使用率和频率
|
||||
cpuUsage, cpuHz, err := collectCPU()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to collect CPU metrics: %w", err)
|
||||
}
|
||||
metrics.CPU = cpuUsage
|
||||
metrics.CPUHz = cpuHz
|
||||
|
||||
// 采集内存使用率
|
||||
memoryUsage, err := collectMemory()
|
||||
@@ -404,12 +437,12 @@ func collectMetrics() (*Metrics, error) {
|
||||
}
|
||||
metrics.Memory = memoryUsage
|
||||
|
||||
// 采集磁盘使用率
|
||||
diskUsageMap, err := collectDisk()
|
||||
// 采集磁盘使用率和总容量
|
||||
diskMetricsMap, err := collectDisk()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to collect disk metrics: %w", err)
|
||||
}
|
||||
metrics.Disk = diskUsageMap
|
||||
metrics.Disk = diskMetricsMap
|
||||
|
||||
// 采集网络流量
|
||||
networkMetrics, err := collectNetwork()
|
||||
@@ -517,7 +550,7 @@ func startHTTPServer() {
|
||||
log.Printf("API Request: %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
// 采集当前状态
|
||||
cpu, _ := collectCPU()
|
||||
cpu, cpuHz, _ := collectCPU()
|
||||
memory, _ := collectMemory()
|
||||
disk, _ := collectDisk()
|
||||
|
||||
@@ -529,6 +562,7 @@ func startHTTPServer() {
|
||||
"debug": config.Debug,
|
||||
"interval": config.Interval,
|
||||
"cpu": cpu,
|
||||
"cpu_hz": cpuHz,
|
||||
"memory": memory,
|
||||
"disk": disk,
|
||||
}
|
||||
@@ -607,7 +641,7 @@ func collectMetricsToBuffer() {
|
||||
totalDiskUsage := 0.0
|
||||
diskCount := 0
|
||||
for _, usage := range metrics.Disk {
|
||||
totalDiskUsage += usage
|
||||
totalDiskUsage += usage.UsedPercent
|
||||
diskCount++
|
||||
}
|
||||
averageDiskUsage := 0.0
|
||||
@@ -649,7 +683,7 @@ func collectAndSendMetrics() {
|
||||
totalMemory += metrics.Memory
|
||||
// 计算磁盘使用率
|
||||
for _, usage := range metrics.Disk {
|
||||
totalDiskUsage += usage
|
||||
totalDiskUsage += usage.UsedPercent
|
||||
diskCount++
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user