- discovery: ping 探测存活 + ARP 解析 MAC,包级 Devices map,串行扫描 - stress: 脚本生成模式(BuildScript/ParseResults),ToolSet 工具检测,包级 Results map - report: 包级 Printed map,SaveAndPrintDeviceReport 保存+打印 - sshclient: 包级 RunCommand - workflow: 仅持有 config,Start() 固定循环,直接调用各包方法 - config: 移除 ScanConfig.Concurrency(串行扫描无需并发数)
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
package stress
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"auto-check/pkg/sshclient"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
// ============================
|
|
// 远程工具检测
|
|
// ============================
|
|
|
|
// ToolInfo 远程工具信息
|
|
type ToolInfo struct {
|
|
Name string
|
|
Path string
|
|
Version string
|
|
}
|
|
|
|
// ToolSet 远程可用工具集合
|
|
type ToolSet struct {
|
|
Tools map[string]ToolInfo // name → info
|
|
}
|
|
|
|
// Has 工具是否可用
|
|
func (ts ToolSet) Has(name string) bool {
|
|
_, ok := ts.Tools[name]
|
|
return ok
|
|
}
|
|
|
|
// Get 获取工具信息
|
|
func (ts ToolSet) Get(name string) (ToolInfo, bool) {
|
|
t, ok := ts.Tools[name]
|
|
return t, ok
|
|
}
|
|
|
|
// DetectTools 检测远程所有相关工具是否可用
|
|
func DetectTools(client *ssh.Client) ToolSet {
|
|
ts := ToolSet{Tools: make(map[string]ToolInfo)}
|
|
for _, name := range []string{"stress-ng", "stressapptest", "iperf3", "fio", "lm-sensors"} {
|
|
if info, ok := detectOne(client, name); ok {
|
|
ts.Tools[name] = info
|
|
}
|
|
}
|
|
return ts
|
|
}
|
|
|
|
// detectOne 检测单个工具
|
|
func detectOne(client *ssh.Client, name string) (ToolInfo, bool) {
|
|
out, err := sshclient.RunCommand(client, "which "+name+" 2>/dev/null")
|
|
path := strings.TrimSpace(out)
|
|
if err != nil || path == "" {
|
|
return ToolInfo{}, false
|
|
}
|
|
|
|
var version string
|
|
switch name {
|
|
case "stress-ng":
|
|
out, _ = sshclient.RunCommand(client, path+" --version 2>&1 | head -1")
|
|
version = strings.TrimSpace(out)
|
|
case "stressapptest":
|
|
out, _ = sshclient.RunCommand(client, path+" --help 2>&1 | head -1")
|
|
version = strings.TrimSpace(out)
|
|
case "iperf3":
|
|
out, _ = sshclient.RunCommand(client, path+" --version 2>&1 | head -1")
|
|
version = strings.TrimSpace(out)
|
|
case "fio":
|
|
out, _ = sshclient.RunCommand(client, path+" --version 2>&1 | head -1")
|
|
version = strings.TrimSpace(out)
|
|
case "lm-sensors":
|
|
out, _ = sshclient.RunCommand(client, path+" -v 2>&1 | head -1")
|
|
version = strings.TrimSpace(out)
|
|
}
|
|
|
|
return ToolInfo{Name: name, Path: path, Version: version}, true
|
|
}
|