Categorygithub.com/bar-counter/monitor

# README

golang-ci

go mod version GoDoc goreportcard

GitHub license GitHub latest SemVer tag) GitHub release)

for what

  • this project used to gin api server status monitor

support check

  • health
  • Hardware
    • disk
    • cpu
    • ram
  • debug
    • run vars
    • pprof

env

  • minimum go version: go 1.21
  • change go 1.21, ^1.21, 1.23.4 to new go version

libs

liburlversion
ginhttps://github.com/gin-gonic/ginv1.9.1
gopsutilhttps://github.com/shirou/gopsutil/v3.23.5

Contributing

Contributor Covenant GitHub contributors

We welcome community contributions to this project.

Please read Contributor Guide for more information on how to get started.

请阅读有关 贡献者指南 以获取更多如何入门的信息

demo

make init
make dep
# ensure right then
make exampleDebug
# and open url
# health http://127.0.0.1:38000/status/health
# pprof http://127.0.0.1:38000/debug/pprof/

make exampleStatus
# status http://127.0.0.1:38000/status/hardware/disk
# status http://127.0.0.1:38000/status/hardware/ram
# status http://127.0.0.1:38000/status/hardware/cpu
# status http://127.0.0.1:38000/status/hardware/cpu_info

make examplePprof
# pprof http://127.0.0.1:38000/debug/vars
# pprof http://127.0.0.1:38000/debug/pprof/

use middleware lib

import

# go get
go get -v github.com/bar-counter/monitor/v3

# go mod find out version
go list -mod readonly -m -versions github.com/bar-counter/monitor/v3
# all use awk to get script
echo "go mod edit -require=$(go list -m -versions github.com/bar-counter/monitor | awk '{print $1 "@" $NF}')"
# then use your want version like v2.1.0
go mod edit -require=github.com/bar-counter/monitor/[email protected]
go mod download -x

gin server status

package main

import (
	"fmt"

	"github.com/bar-counter/monitor/v3"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	monitorCfg := &monitor.Cfg{
		Status: true,
		//StatusPrefix: "/status",
		StatusHardware: true,
		//StatusHardwarePrefix: "/hardware",
	}
	err := monitor.Register(r, monitorCfg)
	if err != nil {
		fmt.Printf("monitor register err %v\n", err)
		return
	}
	err = r.Run(":38000")
	if err != nil {
		fmt.Printf("run err %v\n", err)
		return
	}
}

and you can use to get status of server or run make exampleStatus

curl 'http://127.0.0.1:38000/status/health' \
  -X GET

curl 'http://127.0.0.1:38000/status/hardware/disk' \
  -X GET

curl 'http://127.0.0.1:38000/status/hardware/ram' \
  -X GET

curl 'http://127.0.0.1:38000/status/hardware/cpu' \
  -X GET

curl 'http://127.0.0.1:38000/status/hardware/cpu_info' \
  -X GET

StatusPrefix default is /status you can change by your self StatusHardwarePrefix default is /hardware

gin server debug

vars

package main

import (
	"fmt"

	"github.com/bar-counter/monitor/v3"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	monitorCfg := &monitor.Cfg{
		Status: true,
		//StatusPrefix: "/status",
		StatusHardware: true,
		//StatusHardwarePrefix: "/hardware",
		Debug: true,
		//DebugPrefix: "/debug",
		DebugMiddleware: gin.BasicAuth(gin.Accounts{
			"admin": "admin",
			"user":  "user",
		}),
	}
	err := monitor.Register(r, monitorCfg)
	if err != nil {
		fmt.Printf("monitor register err %v\n", err)
		return
	}

	err = r.Run(":38000")
	if err != nil {
		fmt.Printf("run err %v\n", err)
		return
	}
}

or run make exampleDebug because use gin.BasicAuth must add --user user:user

curl 'http://127.0.0.1:38000/debug/vars' \
 --user user:user \
 -X GET

DebugPrefix default is /debug

{
  "cgo": 6,
  "cmdline": [
    "/var/folders/79/dw7nb8rx7kgcqty_9qq2nv640000gn/T/go-build2348802398/b001/exe/debugdemo"
  ],
  "gc_pause": 0,
  "go_version": "go1.18.2",
  "goroutine": 5,
  "memstats": {
  },
  "os": "darwin",
  "os_cores": 8,
  "run_time": "8.211807521s"
}
itemdocdesc
cgogo doc runtime.NumCgoCall
cmdlineserver run cmd
coresgo doc runtime.NumCPU
gc_pausecount last gc time
goroutinego doc runtime.NumGoroutine
memstatsgo doc runtime.MemStats
osgo doc runtime.GOOS
os_coresgo doc runtime.NumCPU
run_timecount server run time

more info see go doc expvar

DebugMiddleware can use BasicAuth or other Middleware

pprof

package main

import (
	"fmt"

	"github.com/bar-counter/monitor/v3"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	monitorCfg := &monitor.Cfg{
		Status: true,
		//StatusPrefix: "/status",
		StatusHardware: true,
		//StatusHardwarePrefix: "/hardware",
		Debug: true,
		//DebugPrefix: "/debug",
		//DebugMiddleware: gin.BasicAuth(gin.Accounts{
		//	"admin": "admin",
		//	"user":  "user",
		//}),
		PProf: true,
		//PProfPrefix: "/pprof",
	}
	err := monitor.Register(r, monitorCfg)
	if err != nil {
		fmt.Printf("monitor register err %v\n", err)
		return
	}

	err = r.Run(":38000")
	if err != nil {
		fmt.Printf("run err %v\n", err)
		return
	}
}

or run make examplePprof

then see at http://127.0.0.1:38000/debug/pprof/ or use cli to

# cpu
go tool pprof http://localhost:38000/debug/pprof/profile
# mem
go tool pprof http://localhost:38000/debug/pprof/heap
# block
go tool pprof http://localhost:38000/debug/pprof/block

License

FOSSA Status

# Packages

No description provided by the author