Created
March 26, 2020 10:57
-
-
Save xiaotangyuan/e22ce4a18bb4c5e096e86ee475089678 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/prometheus/procfs" | |
) | |
func main() { | |
//监听协议 | |
http.HandleFunc("/", HelloWorldHandler) | |
http.HandleFunc("/cpuinfo", CpuInfoHandler) | |
//监听服务 | |
err := http.ListenAndServe("0.0.0.0:7777",nil) | |
if err != nil { | |
fmt.Println("服务器错误") | |
} | |
} | |
func HelloWorldHandler(w http.ResponseWriter,r *http.Request) { | |
fmt.Fprintf(w,"HelloWorld!") | |
} | |
func CpuInfoHandler(w http.ResponseWriter,r *http.Request) { | |
p, err := procfs.Self() | |
if err != nil { | |
log.Fatalf("could not get process: %s", err) | |
} | |
stat, err := p.NewStat() | |
if err != nil { | |
log.Fatalf("could not get process stat: %s", err) | |
} | |
// fmt.Printf("command: %s\n", stat.Comm) | |
// fmt.Printf("cpu time: %fs\n", stat.CPUTime()) | |
// fmt.Printf("vsize: %dB\n", stat.VirtualMemory()) | |
// fmt.Printf("rss: %dB\n", stat.ResidentMemory()) | |
cpuinfostring := fmt.Sprintf("command: %s\n, cpu time: %fs\n, vsize: %dB\n, rss: %dB\n", stat.Comm, stat.CPUTime(), stat.VirtualMemory(), stat.ResidentMemory()) | |
fmt.Fprintf(w, cpuinfostring) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment