From b3211b9c40ef0ed444de1cf0b87c59f352902039 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Tue, 1 Oct 2024 19:09:32 +0800 Subject: [PATCH] improve: sort sensor names & add an ignore list for sensors (#71) * improve: sort sensor names * add an ignore list for sensors --- pkg/monitor/monitor.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/monitor/monitor.go b/pkg/monitor/monitor.go index ff4a0ae..9b5daea 100644 --- a/pkg/monitor/monitor.go +++ b/pkg/monitor/monitor.go @@ -4,6 +4,7 @@ import ( "fmt" "os/exec" "runtime" + "sort" "strconv" "strings" "sync/atomic" @@ -35,6 +36,10 @@ var ( excludeNetInterfaces = []string{ "lo", "tun", "docker", "veth", "br-", "vmbr", "vnet", "kube", } + sensorIgnoreList = []string{ + "PMU tcal", // the calibration sensor on arm macs, value is fixed + "noname", + } agentConfig *model.AgentConfig ) @@ -62,7 +67,7 @@ var statDataFetchAttempts = map[string]int{ } var ( - updateTempStatus int32 + updateTempStatus = new(atomic.Int32) ) func InitConfig(cfg *model.AgentConfig) { @@ -237,7 +242,7 @@ func TrackNetworkSpeed() { continue } } else { - if isListContainsStr(excludeNetInterfaces, v.Name) { + if util.ContainsStr(excludeNetInterfaces, v.Name) { continue } } @@ -270,7 +275,7 @@ func getDiskTotalAndUsed() (total uint64, used uint64) { for _, d := range diskList { fsType := strings.ToLower(d.Fstype) // 不统计 K8s 的虚拟挂载点:https://github.com/shirou/gopsutil/issues/1007 - if devices[d.Device] == "" && isListContainsStr(expectDiskFsTypes, fsType) && !strings.Contains(d.Mountpoint, "/var/lib/kubelet") { + if devices[d.Device] == "" && util.ContainsStr(expectDiskFsTypes, fsType) && !strings.Contains(d.Mountpoint, "/var/lib/kubelet") { devices[d.Device] = d.Mountpoint } } @@ -362,10 +367,10 @@ func updateGPUStat() float64 { } func updateTemperatureStat() { - if !atomic.CompareAndSwapInt32(&updateTempStatus, 0, 1) { + if !updateTempStatus.CompareAndSwap(0, 1) { return } - defer atomic.StoreInt32(&updateTempStatus, 0) + defer updateTempStatus.Store(0) if statDataFetchAttempts["Temperatures"] < maxDeviceDataFetchAttempts { temperatures, err := sensors.SensorsTemperatures() @@ -376,7 +381,7 @@ func updateTemperatureStat() { statDataFetchAttempts["Temperatures"] = 0 tempStat := []model.SensorTemperature{} for _, t := range temperatures { - if t.Temperature > 0 { + if t.Temperature > 0 && !util.ContainsStr(sensorIgnoreList, t.SensorKey) { tempStat = append(tempStat, model.SensorTemperature{ Name: t.SensorKey, Temperature: t.Temperature, @@ -384,20 +389,15 @@ func updateTemperatureStat() { } } + sort.Slice(tempStat, func(i, j int) bool { + return tempStat[i].Name < tempStat[j].Name + }) + temperatureStat = tempStat } } } -func isListContainsStr(list []string, str string) bool { - for i := 0; i < len(list); i++ { - if strings.Contains(str, list[i]) { - return true - } - } - return false -} - func printf(format string, v ...interface{}) { util.Printf(agentConfig.Debug, format, v...) }