fix: info_monitor从用户配置中读取配置

This commit is contained in:
wuko233 2026-03-25 08:55:13 +08:00
parent 553ce79ba2
commit 27d067b155
3 changed files with 22 additions and 23 deletions

View File

@ -125,7 +125,9 @@ func main() {
// 状态监控 // 状态监控
if userCfg.Modules.SystemMonitor { if userCfg.Modules.SystemMonitor {
metricsChan := make(chan monitor.ServerMetrics, 100) metricsChan := make(chan monitor.ServerMetrics, 100)
infoMon = monitor.NewInfoMonitor(nil, metricsChan)
SystemMonitorCfg := &userCfg.MonitorConfig.SystemMonitorConfig
infoMon = monitor.NewInfoMonitor(SystemMonitorCfg, metricsChan)
go func() { go func() {
for metrics := range metricsChan { for metrics := range metricsChan {

View File

@ -1,5 +1,7 @@
package config package config
import "time"
type OfficialConfig struct { type OfficialConfig struct {
Version string `json:"version"` Version string `json:"version"`
WhitelistFiles map[string][]string `json:"whitelist_files"` WhitelistFiles map[string][]string `json:"whitelist_files"`
@ -31,11 +33,14 @@ type SSHMonitorConfig struct {
} }
type SystemMonitorConfig struct { type SystemMonitorConfig struct {
CollectInterval string `json:"collect_interval"` Enabled bool `json:"enable"`
CollectNetwork bool `json:"collect_network"` Interval time.Duration `json:"collect_interval"`
CollectProcess bool `json:"collect_process"` LogFilePath string `yaml:"log_file_path"`
ProcessLimit int `json:"process_limit"` MaxLogSize int64 `yaml:"max_log_size"`
ScanCPUThreshold int `json:"scan_cpu_threshold"` ProcessLimit int `json:"process_limit"`
CollectNetwork bool `json:"collect_network"`
CollectProcess bool `json:"collect_process"`
ScanCPUThreshold int `json:"scan_cpu_threshold"`
} }
type MonitorConfig struct { type MonitorConfig struct {
@ -68,7 +73,10 @@ func NewDefaultUserConfig() UserConfig {
MonitorConfig: MonitorConfig{ MonitorConfig: MonitorConfig{
SSHMonitorConfig: SSHMonitorConfig{Enabled: true}, SSHMonitorConfig: SSHMonitorConfig{Enabled: true},
SystemMonitorConfig: SystemMonitorConfig{ SystemMonitorConfig: SystemMonitorConfig{
CollectInterval: "30s", Enabled: true,
Interval: 30 * time.Second,
LogFilePath: "/var/log/sysmonitord/info_monitor.log",
MaxLogSize: 10 * 1024 * 1024, // 10 MB
CollectNetwork: true, CollectNetwork: true,
CollectProcess: true, CollectProcess: true,
ProcessLimit: 10, ProcessLimit: 10,

View File

@ -15,28 +15,17 @@ import (
"github.com/shirou/gopsutil/v4/mem" "github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net" "github.com/shirou/gopsutil/v4/net"
"github.com/shirou/gopsutil/v4/process" "github.com/shirou/gopsutil/v4/process"
"github.com/wuko233/sysmonitord/internal/config"
) )
// InfoMonitor 服务器信息监控器 // InfoMonitor 服务器信息监控器
type InfoMonitor struct { type InfoMonitor struct {
config *InfoMonitorConfig config *config.SystemMonitorConfig
logFile *os.File logFile *os.File
stopChan chan struct{} stopChan chan struct{}
metricsChan chan ServerMetrics metricsChan chan ServerMetrics
} }
// InfoMonitorConfig 信息监控配置
type InfoMonitorConfig struct {
Enabled bool `yaml:"enabled"`
Interval time.Duration `yaml:"interval"` // 采集间隔
LogFilePath string `yaml:"log_file_path"` // 日志文件路径
MaxLogSize int64 `yaml:"max_log_size"` // 最大日志大小(字节)
LogRetention int `yaml:"log_retention"` // 日志保留天数
ProcessLimit int `yaml:"process_limit"` // 显示进程数限制
CollectNetwork bool `yaml:"collect_network"` // 是否收集网络信息
CollectProcess bool `yaml:"collect_process"` // 是否收集进程信息
}
// ServerMetrics 服务器指标 // ServerMetrics 服务器指标
type ServerMetrics struct { type ServerMetrics struct {
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
@ -155,9 +144,9 @@ type QuickMetrics struct {
} }
// NewInfoMonitor 创建信息监控器 // NewInfoMonitor 创建信息监控器
func NewInfoMonitor(cfg *InfoMonitorConfig, metricsChan chan ServerMetrics) *InfoMonitor { func NewInfoMonitor(cfg *config.SystemMonitorConfig, metricsChan chan ServerMetrics) *InfoMonitor {
if cfg == nil { if cfg == nil {
cfg = &InfoMonitorConfig{ cfg = &config.SystemMonitorConfig{
Enabled: true, Enabled: true,
Interval: 30 * time.Second, Interval: 30 * time.Second,
ProcessLimit: 10, ProcessLimit: 10,
@ -175,7 +164,7 @@ func NewInfoMonitor(cfg *InfoMonitorConfig, metricsChan chan ServerMetrics) *Inf
} }
if cfg.MaxLogSize == 0 { if cfg.MaxLogSize == 0 {
cfg.MaxLogSize = 100 * 1024 * 1024 // 100MB cfg.MaxLogSize = 10 * 1024 * 1024 // 10MB
} }
if cfg.LogFilePath == "" { if cfg.LogFilePath == "" {