fix: info_monitor从用户配置中读取配置
This commit is contained in:
parent
553ce79ba2
commit
27d067b155
|
|
@ -125,7 +125,9 @@ func main() {
|
|||
// 状态监控
|
||||
if userCfg.Modules.SystemMonitor {
|
||||
metricsChan := make(chan monitor.ServerMetrics, 100)
|
||||
infoMon = monitor.NewInfoMonitor(nil, metricsChan)
|
||||
|
||||
SystemMonitorCfg := &userCfg.MonitorConfig.SystemMonitorConfig
|
||||
infoMon = monitor.NewInfoMonitor(SystemMonitorCfg, metricsChan)
|
||||
|
||||
go func() {
|
||||
for metrics := range metricsChan {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type OfficialConfig struct {
|
||||
Version string `json:"version"`
|
||||
WhitelistFiles map[string][]string `json:"whitelist_files"`
|
||||
|
|
@ -31,11 +33,14 @@ type SSHMonitorConfig struct {
|
|||
}
|
||||
|
||||
type SystemMonitorConfig struct {
|
||||
CollectInterval string `json:"collect_interval"`
|
||||
CollectNetwork bool `json:"collect_network"`
|
||||
CollectProcess bool `json:"collect_process"`
|
||||
ProcessLimit int `json:"process_limit"`
|
||||
ScanCPUThreshold int `json:"scan_cpu_threshold"`
|
||||
Enabled bool `json:"enable"`
|
||||
Interval time.Duration `json:"collect_interval"`
|
||||
LogFilePath string `yaml:"log_file_path"`
|
||||
MaxLogSize int64 `yaml:"max_log_size"`
|
||||
ProcessLimit int `json:"process_limit"`
|
||||
CollectNetwork bool `json:"collect_network"`
|
||||
CollectProcess bool `json:"collect_process"`
|
||||
ScanCPUThreshold int `json:"scan_cpu_threshold"`
|
||||
}
|
||||
|
||||
type MonitorConfig struct {
|
||||
|
|
@ -68,7 +73,10 @@ func NewDefaultUserConfig() UserConfig {
|
|||
MonitorConfig: MonitorConfig{
|
||||
SSHMonitorConfig: SSHMonitorConfig{Enabled: true},
|
||||
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,
|
||||
CollectProcess: true,
|
||||
ProcessLimit: 10,
|
||||
|
|
|
|||
|
|
@ -15,28 +15,17 @@ import (
|
|||
"github.com/shirou/gopsutil/v4/mem"
|
||||
"github.com/shirou/gopsutil/v4/net"
|
||||
"github.com/shirou/gopsutil/v4/process"
|
||||
"github.com/wuko233/sysmonitord/internal/config"
|
||||
)
|
||||
|
||||
// InfoMonitor 服务器信息监控器
|
||||
type InfoMonitor struct {
|
||||
config *InfoMonitorConfig
|
||||
config *config.SystemMonitorConfig
|
||||
logFile *os.File
|
||||
stopChan chan struct{}
|
||||
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 服务器指标
|
||||
type ServerMetrics struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
|
|
@ -155,9 +144,9 @@ type QuickMetrics struct {
|
|||
}
|
||||
|
||||
// NewInfoMonitor 创建信息监控器
|
||||
func NewInfoMonitor(cfg *InfoMonitorConfig, metricsChan chan ServerMetrics) *InfoMonitor {
|
||||
func NewInfoMonitor(cfg *config.SystemMonitorConfig, metricsChan chan ServerMetrics) *InfoMonitor {
|
||||
if cfg == nil {
|
||||
cfg = &InfoMonitorConfig{
|
||||
cfg = &config.SystemMonitorConfig{
|
||||
Enabled: true,
|
||||
Interval: 30 * time.Second,
|
||||
ProcessLimit: 10,
|
||||
|
|
@ -175,7 +164,7 @@ func NewInfoMonitor(cfg *InfoMonitorConfig, metricsChan chan ServerMetrics) *Inf
|
|||
}
|
||||
|
||||
if cfg.MaxLogSize == 0 {
|
||||
cfg.MaxLogSize = 100 * 1024 * 1024 // 100MB
|
||||
cfg.MaxLogSize = 10 * 1024 * 1024 // 10MB
|
||||
}
|
||||
|
||||
if cfg.LogFilePath == "" {
|
||||
|
|
|
|||
Reference in New Issue
Block a user