monitor

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2021 License: MIT Imports: 4 Imported by: 0

README

golang-full TravisBuildStatus GoDoc GoReportCard codecov

for what

  • this project used to gin api server status monitor

support check

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

dependInfo

lib url version
gin https://github.com/gin-gonic/gin v1.6.3
gopsutil https://github.com/shirou/gopsutil v2.20.9+incompatible
go-ole https://github.com/go-ole/go-ole v1.2.5

demo

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

use middleware lib

import

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

# go mod find out verison
go list -m -versions github.com/bar-counter/monitor
# 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 verison like v1.1.0
GO111MODULE=on go mod edit -require=github.com/bar-counter/monitor@v1.1.0
GO111MODULE=on go mod vendor

# dep go 1.7 -> 1.11
dep ensure --add github.com/bar-counter/monitor@1.0.1
dep ensure -v

gin server status

import (
	"github.com/bar-counter/monitor"
)

	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
	}
	r.Run(":38000")

and you can use to get status of server

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

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

gin server debug

vars
import (
	"github.com/bar-counter/monitor"
)

	r := gin.Default()
	monitorCfg := &monitor.Cfg{
		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
	}
	r.Run(":38000")
curl 'http://127.0.0.1:38000/debug/vars' \
-X GET

DebugPrefix default is /debug

{
    "cgo": 6,
    "cmdline": [
        "/private/var/folders/3t/q6knzmrs09b0m5px2dljnpnr0000gn/T/___go_build_main_go__1_"
    ],
    "gc_pause": 0,
    "go_version": "go1.11.4",
    "goroutine": 3,
    "memstats": {
    },
    "os": "darwin",
    "os_cores": 8,
    "run_time": "8.211807521s"
}
item doc desc
cgo go doc runtime.NumCgoCall
cmdline server run cmd
cores go doc runtime.NumCPU
gc_pause count last gc time
goroutine go doc runtime.NumGoroutine
memstats go doc runtime.MemStats
os go doc runtime.GOOS
os_cores go doc runtime.NumCPU
run_time count server run time

more info see go doc expvar

DebugMiddleware can use BasicAuth or other Middleware

pprof
import (
	"github.com/bar-counter/monitor"
)

	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
	}
	r.Run(":38000")

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

Documentation

Index

Constants

View Source
const (
	DefaultStatusPrefix = "/status"
	// DefaultVarsPrefix url prefix of /status/hardware
	DefaultStatusHardwarePrefix = "/hardware"
	DefaultDebugPrefix          = "/debug"
	// DefaultVarsPrefix url prefix of /debug/vars
	DefaultVarsPrefix = "/vars"
	// DefaultPrefix url prefix of /debug/pprof
	DefaultPPROFPrefix = "/pprof"
)

Variables

This section is empty.

Functions

func Register

func Register(r *gin.Engine, cfg *Cfg) error

Types

type Cfg

type Cfg struct {
	APIBase              string
	Status               bool
	StatusPrefix         string
	StatusHardware       bool
	StatusHardwarePrefix string
	Debug                bool
	DebugPrefix          string
	DebugMiddleware      gin.HandlerFunc
	VarsPrefix           string
	PProf                bool
	PProfPrefix          string
}
var DefaultCfg *Cfg

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL