59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"runtime"
|
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
)
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"summary": map[string]interface{}{
|
|
// get the health of the cpu, memory and disk
|
|
},
|
|
})
|
|
})
|
|
|
|
http.HandleFunc("/health/cpu", func(w http.ResponseWriter, r *http.Request) {
|
|
percent, _ := cpu.Percent(0, false)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"go_os": runtime.GOOS,
|
|
"num_cpu": runtime.NumCPU(),
|
|
"cpu_usage": percent[0],
|
|
})
|
|
})
|
|
|
|
http.HandleFunc("/health/memory", func(w http.ResponseWriter, r *http.Request) {
|
|
v, _ := mem.VirtualMemory()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"total": v.Total,
|
|
"free": v.Available,
|
|
"used": v.Used,
|
|
"used_percent": v.UsedPercent,
|
|
})
|
|
})
|
|
|
|
http.HandleFunc("/health/disk", func(w http.ResponseWriter, r *http.Request) {
|
|
d, _ := disk.Usage("/")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"total": d.Total,
|
|
"used": d.Used,
|
|
"free": d.Free,
|
|
"percent": d.UsedPercent,
|
|
})
|
|
})
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|