重构 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:
张威33321
2026-08-11 20:58:19 +08:00
parent 19f6050147
commit 700ba91883
17 changed files with 867 additions and 516 deletions

View File

@@ -11,20 +11,22 @@ import (
// PhaseResult 单阶段结果
type PhaseResult struct {
Phase string `json:"phase" yaml:"phase"`
Status string `json:"status" yaml:"status"` // success / partial / failed
Phase string `json:"phase" yaml:"phase"`
Status string `json:"status" yaml:"status"` // success / partial / failed
Hosts []HostResult `json:"hosts" yaml:"hosts"`
Summary string `json:"summary" yaml:"summary"`
Summary string `json:"summary" yaml:"summary"`
}
// HostResult 单台主机结果
// HostResult 单台设备结果MAC 为唯一标识)
type HostResult struct {
IP string `json:"ip" yaml:"ip"`
Alive bool `json:"alive" yaml:"alive"`
OpenPort int `json:"open_port,omitempty" yaml:"open_port,omitempty"`
SSHLogin bool `json:"ssh_login" yaml:"ssh_login"`
SSHErr string `json:"ssh_error,omitempty" yaml:"ssh_error,omitempty"`
Stress []StressResult `json:"stress,omitempty" yaml:"stress,omitempty"`
MAC string `json:"mac" yaml:"mac"`
IP string `json:"ip" yaml:"ip"`
Alive bool `json:"alive" yaml:"alive"`
OpenPort int `json:"open_port,omitempty" yaml:"open_port,omitempty"`
SSHLogin bool `json:"ssh_login" yaml:"ssh_login"`
SSHErr string `json:"ssh_error,omitempty" yaml:"ssh_error,omitempty"`
Stress []StressResult `json:"stress,omitempty" yaml:"stress,omitempty"`
Curves map[string][]float64 `json:"curves,omitempty" yaml:"curves,omitempty"` // 指标名 → 时间序列(渲染为 sparkline
}
// StressResult 单项压力测试结果
@@ -56,8 +58,8 @@ type ConfigSummary struct {
// Builder 报告构建器
type Builder struct {
report *Report
phase *PhaseResult
report *Report
phase *PhaseResult
}
// NewBuilder 创建报告构建器
@@ -84,13 +86,13 @@ func (b *Builder) AddHost(h HostResult) {
}
}
// AddStressResult 为指定 IP 添加压力测试结果
func (b *Builder) AddStressResult(ip string, sr StressResult) {
// AddStressResult 为指定设备MAC添加压力测试结果
func (b *Builder) AddStressResult(mac string, sr StressResult) {
if b.phase == nil {
return
}
for i := range b.phase.Hosts {
if b.phase.Hosts[i].IP == ip {
if b.phase.Hosts[i].MAC == mac {
b.phase.Hosts[i].Stress = append(b.phase.Hosts[i].Stress, sr)
return
}
@@ -188,14 +190,10 @@ func (r *Report) ToText() string {
continue
}
if phase.Phase == "SSH 登录" && !h.SSHLogin {
sb.WriteString(fmt.Sprintf(" ✗ %-15s %s\n", h.IP, h.SSHErr))
sb.WriteString(fmt.Sprintf(" ✗ %-19s %-15s %s\n", h.MAC, h.IP, h.SSHErr))
continue
}
icon := "✓"
if phase.Phase == "SSH 登录" {
icon = "✓"
}
sb.WriteString(fmt.Sprintf(" %s %-15s\n", icon, h.IP))
sb.WriteString(fmt.Sprintf(" ✓ %-19s %-15s\n", h.MAC, h.IP))
}
}
@@ -204,7 +202,7 @@ func (r *Report) ToText() string {
if len(h.Stress) == 0 {
continue
}
sb.WriteString(fmt.Sprintf(" ┌─ %s\n", h.IP))
sb.WriteString(fmt.Sprintf(" ┌─ %s (%s)\n", h.MAC, h.IP))
for _, s := range h.Stress {
icon := "✓"
if s.Status != "pass" {
@@ -260,3 +258,22 @@ func DefaultReportPath() string {
timestamp := time.Now().Format("20060102-150405")
return filepath.Join(".", "reports", fmt.Sprintf("report-%s.txt", timestamp))
}
// ============================
// 渲染 + 打印便捷方法
// ============================
// PrintTo 使用 80mm 模板渲染并打印
// printer 为 nil 时仅返回渲染文本
func (r *Report) PrintTo(printer Printer) (string, error) {
text, err := r.Render80mm()
if err != nil {
return "", err
}
if printer != nil {
if err := printer.Print(text); err != nil {
return text, fmt.Errorf("打印失败: %w", err)
}
}
return text, nil
}