@@ -651,6 +651,8 @@ func (m *Master) handleInfo(w http.ResponseWriter, r *http.Request) {
651651 info := map [string ]any {
652652 "os" : runtime .GOOS ,
653653 "arch" : runtime .GOARCH ,
654+ "cpu" : - 1 ,
655+ "ram" : - 1 ,
654656 "ver" : m .version ,
655657 "name" : m .hostname ,
656658 "uptime" : uint64 (time .Since (m .startTime ).Seconds ()),
@@ -660,9 +662,49 @@ func (m *Master) handleInfo(w http.ResponseWriter, r *http.Request) {
660662 "key" : m .keyPath ,
661663 }
662664
665+ if runtime .GOOS == "linux" {
666+ cpu , ram := getLinuxSysInfo ()
667+ info ["cpu" ] = cpu
668+ info ["ram" ] = ram
669+ }
670+
663671 writeJSON (w , http .StatusOK , info )
664672}
665673
674+ // getLinuxSysInfo 获取Linux系统信息
675+ func getLinuxSysInfo () (cpu , ram int ) {
676+ if runtime .GOOS != "linux" {
677+ return 0 , 0
678+ }
679+
680+ // CPU使用率:解析/proc/loadavg
681+ if data , err := os .ReadFile ("/proc/loadavg" ); err == nil {
682+ if fields := strings .Fields (string (data )); len (fields ) > 0 {
683+ if load , err := strconv .ParseFloat (fields [0 ], 64 ); err == nil {
684+ cpu = min (int (load * 100 ), 100 )
685+ }
686+ }
687+ }
688+
689+ // RAM使用率:解析/proc/meminfo
690+ if data , err := os .ReadFile ("/proc/meminfo" ); err == nil {
691+ var memTotal , memFree uint64
692+ for line := range strings .FieldsSeq (string (data )) {
693+ switch {
694+ case strings .HasPrefix (line , "MemTotal:" ):
695+ memTotal , _ = strconv .ParseUint (strings .TrimSuffix (line [9 :], "kB" ), 10 , 64 )
696+ case strings .HasPrefix (line , "MemFree:" ):
697+ memFree , _ = strconv .ParseUint (strings .TrimSuffix (line [8 :], "kB" ), 10 , 64 )
698+ }
699+ }
700+ if memTotal > 0 {
701+ ram = min (int ((memTotal - memFree )* 100 / memTotal ), 100 )
702+ }
703+ }
704+
705+ return cpu , ram
706+ }
707+
666708// handleInstances 处理实例集合请求
667709func (m * Master ) handleInstances (w http.ResponseWriter , r * http.Request ) {
668710 switch r .Method {
@@ -1481,6 +1523,8 @@ func generateOpenAPISpec() string {
14811523 "properties": {
14821524 "os": {"type": "string", "description": "Operating system"},
14831525 "arch": {"type": "string", "description": "System architecture"},
1526+ "cpu": {"type": "integer", "description": "CPU usage percentage"},
1527+ "ram": {"type": "integer", "description": "RAM usage percentage"},
14841528 "ver": {"type": "string", "description": "NodePass version"},
14851529 "name": {"type": "string", "description": "Hostname"},
14861530 "uptime": {"type": "integer", "format": "int64", "description": "Uptime in seconds"},
0 commit comments