重构 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
|
||||
|
||||
Reference in New Issue
Block a user