[cmd] 状态查看status命令实现
This commit is contained in:
parent
14e41d114e
commit
f553d6c2bc
|
|
@ -0,0 +1,87 @@
|
|||
package status
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sysmonitord/internal/config"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var StatusCmd = &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "显示系统状态",
|
||||
Long: "显示Sysmonitod的当前状态",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cfg, err := config.LoadConfig("./config.yaml")
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("加载配置失败: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
printStatus(cfg)
|
||||
},
|
||||
}
|
||||
|
||||
func printStatus(cfg *config.Config) {
|
||||
dataDir := cfg.Storage.DataDir
|
||||
|
||||
fmt.Println("Sysmonitord Status")
|
||||
fmt.Println("================")
|
||||
|
||||
// Todo: 显示运行时长
|
||||
runtimeInfo := "N/A"
|
||||
|
||||
fmt.Printf("Runtime: %s\n", runtimeInfo)
|
||||
fmt.Printf("Data Directory: %s\n", dataDir)
|
||||
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println("[白名单统计]")
|
||||
fileCount, err := countLines(filepath.Join(dataDir, cfg.Storage.FileSystemFile))
|
||||
if err != nil {
|
||||
fmt.Printf("无法统计文件系统白名单: %v\n", err)
|
||||
fileCount = 0
|
||||
}
|
||||
processCount, err := countLines(filepath.Join(dataDir, cfg.Storage.ProcessSystemFile))
|
||||
if err != nil {
|
||||
fmt.Printf("无法统计进程白名单: %v\n", err)
|
||||
processCount = 0
|
||||
}
|
||||
|
||||
fmt.Printf("文件系统白名单: %d 条\n", fileCount)
|
||||
fmt.Printf("进程白名单: %d 条\n", processCount)
|
||||
|
||||
dubFileCount, _ := countLines(filepath.Join(dataDir, cfg.Storage.DubiousFileListFile))
|
||||
dubProcCount, _ := countLines(filepath.Join(dataDir, cfg.Storage.DubiousProcessListFile))
|
||||
|
||||
fmt.Printf("可疑文件列表: %d 条\n", dubFileCount)
|
||||
fmt.Printf("可疑进程列表: %d 条\n", dubProcCount)
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
func countLines(filePath string) (int, error) {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
return 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
lineCount := 0
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if len(line) > 0 && line[0] != '#' {
|
||||
lineCount++
|
||||
}
|
||||
}
|
||||
|
||||
return lineCount, scanner.Err()
|
||||
}
|
||||
2
main.go
2
main.go
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"os"
|
||||
"sysmonitord/cmd/start"
|
||||
"sysmonitord/cmd/status"
|
||||
"sysmonitord/cmd/version"
|
||||
"sysmonitord/internal/config"
|
||||
"sysmonitord/pkg/logger"
|
||||
|
|
@ -30,6 +31,7 @@ func main() {
|
|||
|
||||
rootCmd.AddCommand(start.StartCmd)
|
||||
rootCmd.AddCommand(version.VersionCmd)
|
||||
rootCmd.AddCommand(status.StatusCmd)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
logger.Log.Error("命令执行失败", zap.Error(err))
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user