sysmonitord/cmd/start/start.go
2026-04-01 09:06:56 +08:00

107 lines
2.9 KiB
Go

package start
import (
"fmt"
"os"
"sysmonitord/internal/config"
"sysmonitord/internal/scanner/file"
"sysmonitord/internal/scanner/hash"
"sysmonitord/internal/scanner/process"
"sysmonitord/internal/storage"
"sysmonitord/pkg/logger"
"time"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var StartCmd = &cobra.Command{
Use: "start",
Short: "启动系统监控守护服务",
Long: "sysmonitord start 命令用于启动系统监控守护服务,首次启动会进行全量扫描建立白名单。",
Run: func(cmd *cobra.Command, args []string) {
logger.Log.Info("正在启动系统监控守护服务...")
cfg, err := config.LoadConfig("./config.yaml")
if err != nil {
logger.Log.Error("加载配置文件失败", zap.Error(err))
os.Exit(1)
}
logger.Log.Info("配置文件加载成功",
zap.String("审计服务器地址", fmt.Sprintf("%s:%d", cfg.Audit.Server, cfg.Audit.Port)),
)
hashCfg := &hash.Config{
UseFastHash: cfg.Scanner.File.FastHash,
Threshold: cfg.Scanner.File.FastHashSize,
ChunkSize: cfg.Scanner.File.FastHashChunk,
}
storageCfg := &storage.Storage{
DataDir: cfg.Storage.DataDir,
ProcessSystemFile: cfg.Storage.ProcessSystemFile,
FileSystemFile: cfg.Storage.FileSystemFile,
}
// ====== 进程扫描和存储 ======
procs, err := process.ScanAllProcesses(hashCfg)
if err != nil {
logger.Log.Error("扫描进程失败", zap.Error(err))
os.Exit(1)
} else {
if err := storage.SaveProcessSystem(procs, storageCfg.DataDir, storageCfg.ProcessSystemFile); err != nil {
logger.Log.Error("保存进程白名单失败", zap.Error(err))
}
}
logger.Log.Info("进程列表:")
for i, p := range procs {
if i >= 10 {
logger.Log.Info("... (仅显示前10个进程)")
break
}
logger.Log.Info(
"进程信息",
zap.Int32("pid", p.PID),
zap.String("name", p.Name),
zap.String("path", p.Path),
zap.String("cmdline", p.Cmdline),
zap.Stringer("data", p),
)
}
// ====== 文件扫描和存储 ======
logger.Log.Info("正在扫描文件系统...")
startTime := time.Now()
fileCfg := &config.FileScannerConfig{
IncludePaths: cfg.Scanner.File.IncludePaths,
ExcludePaths: cfg.Scanner.File.ExcludePaths,
FastHash: cfg.Scanner.File.FastHash,
FastHashSize: cfg.Scanner.File.FastHashSize,
FastHashChunk: cfg.Scanner.File.FastHashChunk,
}
fileScanner := file.NewScanner(fileCfg)
files, err := fileScanner.Scan()
if err != nil {
logger.Log.Error("扫描文件系统失败", zap.Error(err))
os.Exit(1)
} else {
if err := storage.SaveFileSystem(files, storageCfg.DataDir, storageCfg.FileSystemFile); err != nil {
logger.Log.Error("保存文件系统白名单失败", zap.Error(err))
os.Exit(1)
}
}
duration := time.Since(startTime)
logger.Log.Info("文件系统扫描完成",
zap.Int("文件数量", len(files)),
zap.Duration("扫描耗时", duration),
)
},
}