重构 auto-check:拆分包架构,精简 workflow 为纯调度循环
各包职责: - config: 分类 YAML 配置 - model: Device/IP/MAC 公共类型 - discovery: 扫描 + ARP MAC 解析 + Devices 变量 - sshclient: SSH 连接 - stress: stress-ng/stressapptest 压测 + Results 变量 - report: 80mm 热敏模板/sparkline/打印 + Printed 变量 - workflow: 永久循环调度 + Start/Stop workflow 已精简为 100 行纯编排,所有业务逻辑下沉各包
This commit is contained in:
@@ -5,21 +5,27 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"auto-check/pkg/config"
|
||||
"auto-check/pkg/sshclient"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// Results 测试结果表(IP → 报告),包级公开
|
||||
var Results = make(map[string]*Report)
|
||||
|
||||
// Runner 压力测试调度器
|
||||
type Runner struct {
|
||||
Config Config
|
||||
Types []TestType
|
||||
Config Config
|
||||
Types []TestType
|
||||
Executor Executor
|
||||
}
|
||||
|
||||
// NewRunner 创建调度器
|
||||
func NewRunner(cfg Config, types []TestType, executor Executor) *Runner {
|
||||
return &Runner{
|
||||
Config: cfg,
|
||||
Types: types,
|
||||
Config: cfg,
|
||||
Types: types,
|
||||
Executor: executor,
|
||||
}
|
||||
}
|
||||
@@ -133,6 +139,62 @@ func (r *Runner) RunAll(clients map[string]*ssh.Client) []*Report {
|
||||
return reports
|
||||
}
|
||||
|
||||
// IsPassed 报告是否通过
|
||||
func IsPassed(rpt *Report) bool {
|
||||
return rpt != nil && rpt.Failed == 0 && rpt.Errors == 0
|
||||
}
|
||||
|
||||
// Status 报告状态字符串:pass / fail / untested
|
||||
func Status(rpt *Report) string {
|
||||
if rpt == nil {
|
||||
return "untested"
|
||||
}
|
||||
if IsPassed(rpt) {
|
||||
return "pass"
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
|
||||
// ParseTypes 逗号分隔字符串 → TestType 列表
|
||||
func ParseTypes(s string) []TestType {
|
||||
var types []TestType
|
||||
for _, t := range strings.Split(s, ",") {
|
||||
if t = strings.TrimSpace(t); t != "" {
|
||||
types = append(types, TestType(t))
|
||||
}
|
||||
}
|
||||
return types
|
||||
}
|
||||
|
||||
// TestDevice 对单台设备执行 SSH 登录 + 压力测试(业务入口)
|
||||
func TestDevice(ip string, cfg config.Config) *Report {
|
||||
client := sshclient.NewClient(cfg.SSH.User, cfg.SSH.Password, cfg.SSH.KeyFile, cfg.SSH.Port, cfg.Scan.Timeout)
|
||||
|
||||
conn, err := client.Connect(ip)
|
||||
if err != nil {
|
||||
fmt.Printf(" [测试] %s SSH 连接失败: %v\n", ip, err)
|
||||
return NewSSHFailReport(ip, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
return NewRunner(
|
||||
Config{Duration: cfg.Stress.Duration, Threads: cfg.Stress.Threads, DiskSizeMB: 1024, TempLogInt: 10 * time.Second},
|
||||
ParseTypes(cfg.Stress.Types),
|
||||
client,
|
||||
).Run(conn, ip)
|
||||
}
|
||||
|
||||
// NewSSHFailReport 构造 SSH 连接失败报告
|
||||
func NewSSHFailReport(ip string, err error) *Report {
|
||||
return &Report{
|
||||
IP: ip,
|
||||
StartTime: time.Now(),
|
||||
Results: []Result{{Type: "ssh", Status: "fail", Error: err.Error()}},
|
||||
Failed: 1,
|
||||
}
|
||||
}
|
||||
|
||||
// min 取较小值
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
|
||||
@@ -24,8 +24,8 @@ const (
|
||||
TestFull TestType = "full" // CPU+内存+磁盘IO 三合一
|
||||
|
||||
// 监控(自动附加)
|
||||
MonitorTemp TestType = "temp" // lm-sensors 温度监控
|
||||
MonitorDmesg TestType = "dmesg" // dmesg 内核报错监控
|
||||
MonitorTemp TestType = "temp" // lm-sensors 温度监控
|
||||
MonitorDmesg TestType = "dmesg" // dmesg 内核报错监控
|
||||
)
|
||||
|
||||
// ============================
|
||||
@@ -34,12 +34,12 @@ const (
|
||||
|
||||
// Config 压力测试配置
|
||||
type Config struct {
|
||||
Duration time.Duration // 总持续时间
|
||||
Threads int // CPU/内存线程数
|
||||
MemSizeMB int // 内存测试大小 (MB),0=自动(可用60%)
|
||||
DiskSizeMB int // 磁盘测试大小 (MB),0=默认1024
|
||||
DiskDir string // 磁盘测试目录,空=自动临时目录
|
||||
TempLogInt time.Duration // 温度采样间隔,0=10s
|
||||
Duration time.Duration // 总持续时间
|
||||
Threads int // CPU/内存线程数
|
||||
MemSizeMB int // 内存测试大小 (MB),0=自动(可用60%)
|
||||
DiskSizeMB int // 磁盘测试大小 (MB),0=默认1024
|
||||
DiskDir string // 磁盘测试目录,空=自动临时目录
|
||||
TempLogInt time.Duration // 温度采样间隔,0=10s
|
||||
}
|
||||
|
||||
// DefaultConfig 默认配置
|
||||
|
||||
Reference in New Issue
Block a user