Skip to content

Instantly share code, notes, and snippets.

@xiaotangyuan
Created March 26, 2020 10:57
Show Gist options
  • Save xiaotangyuan/e22ce4a18bb4c5e096e86ee475089678 to your computer and use it in GitHub Desktop.
Save xiaotangyuan/e22ce4a18bb4c5e096e86ee475089678 to your computer and use it in GitHub Desktop.
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