monitor

package
v1.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package monitor 提供服务器资源监控组件(对标阿里云 ECS 资源监控):

  • 采集:内存/CPU/磁盘/负载/主机信息/进程与协程数,跨平台(Linux /proc + Windows x/sys)
  • 页面:内置自包含 HTML 监控页(进度条 + 趋势曲线 + 自动刷新)
  • API:JSON 数据接口,支持 token 校验(Auth)与限流(Limit),防接口轰炸

接入(在 EnableWeb 回调中注册路由):

builder.EnableWeb(appbox.TimeFormat, ":8080", "info", func(app *iris.Application) {
    monitor.Register(app, "/monitor", monitor.Config{
        Auth: webiris.Auth(webiris.AuthConfig{Whitelist: []string{"/monitor"}}), // 监控 API 要求登录
        RatePerSecond: 5, // 每 IP 5 QPS,防轰炸
    })
})

浏览器访问 http://host:8080/monitor 查看监控页;JSON 接口 /monitor/api/stats。

Index

Constants

View Source
const Version = "v1.13.0"

Version 组件版本标识(随页面展示)。

Variables

This section is empty.

Functions

func Fail

func Fail(ctx iris.Context, status int, code apperr.Code, message string)

Fail 输出统一失败响应(与 webiris 一致的错误格式)。

func Register

func Register(app *iris.Application, prefix string, config Config) iris.Party

Register 注册监控路由:

GET  <prefix>          监控页面(HTML,匿名可看,建议内网)
GET  <prefix>/api/stats 监控数据 JSON(挂 Auth + 限流)

prefix 建议 /monitor。返回注册后的 party。

monitor.Register(app, "/monitor", monitor.Config{
    Auth: webiris.Auth(webiris.AuthConfig{Whitelist: []string{"/monitor"}}),
})

Types

type CPUStats

type CPUStats struct {
	UsagePercent float64 `json:"usage_percent"` // 采样间隔内平均使用率(0-100)
}

CPUStats CPU 统计。

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector 资源采集器。CPU 使用率基于两次采样的差值,线程安全。

func NewCollector

func NewCollector() *Collector

NewCollector 创建采集器。

func (*Collector) Stats

func (c *Collector) Stats() (*Stats, error)

Stats 采集一次全量快照。

type Config

type Config struct {
	// Auth 监控 API 的身份校验中间件(推荐 webiris.Auth,白名单放行页面)。
	// 为 nil 时不校验(仅限内网/Admin 端口场景)。
	Auth iris.Handler
	// RatePerSecond 监控 API 每 IP 限流速率;非正数时默认 5 QPS(防接口轰炸)。
	RatePerSecond float64
	// Burst 突发容量;非正数时取 RatePerSecond。
	Burst int
}

Config 监控组件配置。

type DiskStats

type DiskStats struct {
	Total        uint64  `json:"total"`
	Used         uint64  `json:"used"`
	Free         uint64  `json:"free"`
	UsagePercent float64 `json:"usage_percent"`
}

DiskStats 磁盘统计(字节,根分区)。

type LoadStats

type LoadStats struct {
	Load1  float64 `json:"load1"`
	Load5  float64 `json:"load5"`
	Load15 float64 `json:"load15"`
}

LoadStats 系统负载(1/5/15 分钟)。

type MemoryStats

type MemoryStats struct {
	Total        uint64  `json:"total"`
	Used         uint64  `json:"used"`
	Free         uint64  `json:"free"`
	UsagePercent float64 `json:"usage_percent"`
}

MemoryStats 内存统计(字节)。

type Stats

type Stats struct {
	Hostname      string      `json:"hostname"`               // 主机名
	Platform      string      `json:"platform"`               // 平台,如 linux/amd64
	GoVersion     string      `json:"go_version"`             // Go 版本
	Version       string      `json:"version"`                // go-blackbox monitor 组件版本
	Uptime        int64       `json:"uptime_seconds"`         // 系统运行时长(秒)
	ProcessUptime int64       `json:"process_uptime_seconds"` // 进程运行时长(秒)
	Goroutines    int         `json:"goroutines"`             // 当前协程数
	Time          int64       `json:"time"`                   // 采集时间戳(Unix 秒)
	Memory        MemoryStats `json:"memory"`                 // 内存
	CPU           CPUStats    `json:"cpu"`                    // CPU
	Disk          DiskStats   `json:"disk"`                   // 磁盘(根分区)
	Load          LoadStats   `json:"load"`                   // 系统负载(非 Linux 为 0)
}

Stats 一次采集快照。

Jump to

Keyboard shortcuts

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