Explorar el Código

系统信息CPU监控新增温度显示

niujiuru hace 1 mes
padre
commit
bf684dfef2
Se han modificado 2 ficheros con 26 adiciones y 1 borrados
  1. 25 0
      web-ui.service/page1_handler.go
  2. 1 1
      web-ui.service/web/static/js/page1.js

+ 25 - 0
web-ui.service/page1_handler.go

@@ -32,6 +32,7 @@ func systemInfoHandler(w http.ResponseWriter, r *http.Request) {
 		"cpu_usage": getCPUUsage(),
 		"cpu_cores": getCPUCore(),
 		"cpu_freq":  getCPUFreq(),
+		"cpu_temp":  getCPUTemperature(),
 
 		"mem_percent": memPercent,
 		"mem_used":    memUsed,
@@ -136,6 +137,30 @@ func getCPUFreq() string {
 	return noValue
 }
 
+func getCPUTemperature() string {
+	files, _ := filepath.Glob("/sys/class/thermal/thermal_zone*/temp")
+
+	max := 0
+
+	for _, f := range files {
+		data, err := os.ReadFile(f)
+		if err != nil {
+			continue
+		}
+
+		temp, err := strconv.Atoi(strings.TrimSpace(string(data)))
+		if err == nil && temp > max {
+			max = temp
+		}
+	}
+
+	if max == 0 {
+		return noValue
+	}
+
+	return fmt.Sprintf("%d℃", max/1000)
+}
+
 func getMemInfo() (int, string, string) {
 	data, err := os.ReadFile("/proc/meminfo")
 	if err != nil {

+ 1 - 1
web-ui.service/web/static/js/page1.js

@@ -17,7 +17,7 @@ function getSystemInfo(){
         left:"center",
         top:"75%",
         style:{
-          text:`${d.cpu_cores||"--"}核 ${d.cpu_freq||"--"}MHz`,
+          text:`${d.cpu_cores||"--"}核 ${d.cpu_freq||"--"}MHz  温度${d.cpu_temp||"--"}`,
           fill:"#aaa",
           fontSize:15
         }