From fbf099e4376bbe55cd4916b12e0a64438da7be42 Mon Sep 17 00:00:00 2001 From: naiba Date: Tue, 22 Oct 2024 23:45:10 +0800 Subject: [PATCH] fix connect to dev dashboard --- .gitignore | 1 + cmd/agent/edit.go | 4 +++- cmd/agent/main.go | 2 +- model/config.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++ model/model.go | 58 ----------------------------------------------- 5 files changed, 63 insertions(+), 60 deletions(-) create mode 100644 model/config.go delete mode 100644 model/model.go diff --git a/.gitignore b/.gitignore index fc468e6..c9b7a9b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .DS_Store /agent /cmd/agent/agent +/cmd/agent/config.yml *.pprof dist \ No newline at end of file diff --git a/cmd/agent/edit.go b/cmd/agent/edit.go index bcd76a4..e2b8d88 100644 --- a/cmd/agent/edit.go +++ b/cmd/agent/edit.go @@ -12,7 +12,9 @@ import ( ) // 修改Agent要监控的网卡与硬盘分区 -func editAgentConfig() { +func editAgentConfig(configPath string) { + agentConfig.Read(configPath) + nc, err := psnet.IOCounters(true) if err != nil { panic(err) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index fb7f577..13269e0 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -133,7 +133,7 @@ func main() { } if isEditConfig { - editAgentConfig() + editAgentConfig(configPath) os.Exit(0) } diff --git a/model/config.go b/model/config.go new file mode 100644 index 0000000..8969644 --- /dev/null +++ b/model/config.go @@ -0,0 +1,58 @@ +package model + +import ( + "os" + + "github.com/spf13/viper" + "sigs.k8s.io/yaml" +) + +type AgentConfig struct { + Debug bool `mapstructure:"debug"` + + Server string `mapstructure:"server"` // 服务器地址 + ClientSecret string `mapstructure:"client_secret"` // 客户端密钥 + UUID string `mapstructure:"uuid"` + + HardDrivePartitionAllowlist []string `mapstructure:"hard_drive_partition_allowlist"` + NICAllowlist map[string]bool `mapstructure:"nic_allowlist"` + DNS []string `mapstructure:"dns"` + GPU bool `mapstructure:"gpu"` // 是否检查GPU + Temperature bool `mapstructure:"temperature"` // 是否检查温度 + SkipConnectionCount bool `mapstructure:"skip_connection_count"` // 跳过连接数检查 + SkipProcsCount bool `mapstructure:"skip_procs_count"` // 跳过进程数量检查 + DisableAutoUpdate bool `mapstructure:"disable_auto_update"` // 关闭自动更新 + DisableForceUpdate bool `mapstructure:"disable_force_update"` // 关闭强制更新 + DisableCommandExecute bool `mapstructure:"disable_command_execute"` // 关闭命令执行 + ReportDelay int `mapstructure:"report_delay"` // 报告间隔 + TLS bool `mapstructure:"tls"` // 是否使用TLS加密传输至服务端 + InsecureTLS bool `mapstructure:"insecure_tls"` // 是否禁用证书检查 + UseIPv6CountryCode bool `mapstructure:"use_i_pv_6_country_code"` // 默认优先展示IPv6旗帜 + UseGiteeToUpgrade bool `mapstructure:"use_gitee_to_upgrade"` // 强制从Gitee获取更新 + IPReportPeriod uint32 `mapstructure:"ip_report_period"` // IP上报周期 + + v *viper.Viper +} + +// Read 从给定的文件目录加载配置文件 +func (c *AgentConfig) Read(path string) error { + c.v = viper.New() + c.v.SetConfigFile(path) + err := c.v.ReadInConfig() + if err != nil { + return err + } + err = c.v.Unmarshal(c) + if err != nil { + return err + } + return nil +} + +func (c *AgentConfig) Save() error { + data, err := yaml.Marshal(c) + if err != nil { + return err + } + return os.WriteFile(c.v.ConfigFileUsed(), data, os.ModePerm) +} diff --git a/model/model.go b/model/model.go deleted file mode 100644 index e37715f..0000000 --- a/model/model.go +++ /dev/null @@ -1,58 +0,0 @@ -package model - -import ( - "os" - - "github.com/spf13/viper" - "sigs.k8s.io/yaml" -) - -type AgentConfig struct { - Debug bool `json:"debug,omitempty"` - - Server string `json:"server,omitempty"` // 服务器地址 - ClientSecret string `json:"client_secret,omitempty"` // 客户端密钥 - UUID string `json:"uuid,omitempty"` - - HardDrivePartitionAllowlist []string `json:"hard_drive_partition_allowlist,omitempty"` - NICAllowlist map[string]bool `json:"nic_allowlist,omitempty"` - DNS []string `json:"dns,omitempty"` - GPU bool `json:"gpu,omitempty"` // 是否检查GPU - Temperature bool `json:"temperature,omitempty"` // 是否检查温度 - SkipConnectionCount bool `json:"skip_connection_count,omitempty"` // 跳过连接数检查 - SkipProcsCount bool `json:"skip_procs_count,omitempty"` // 跳过进程数量检查 - DisableAutoUpdate bool `json:"disable_auto_update,omitempty"` // 关闭自动更新 - DisableForceUpdate bool `json:"disable_force_update,omitempty"` // 关闭强制更新 - DisableCommandExecute bool `json:"disable_command_execute,omitempty"` // 关闭命令执行 - ReportDelay int `json:"report_delay,omitempty"` // 报告间隔 - TLS bool `json:"tls,omitempty"` // 是否使用TLS加密传输至服务端 - InsecureTLS bool `json:"insecure_tls,omitempty"` // 是否禁用证书检查 - UseIPv6CountryCode bool `json:"use_i_pv_6_country_code,omitempty"` // 默认优先展示IPv6旗帜 - UseGiteeToUpgrade bool `json:"use_gitee_to_upgrade,omitempty"` // 强制从Gitee获取更新 - IPReportPeriod uint32 `json:"ip_report_period,omitempty"` // IP上报周期 - - v *viper.Viper -} - -// Read 从给定的文件目录加载配置文件 -func (c *AgentConfig) Read(path string) error { - c.v = viper.New() - c.v.SetConfigFile(path) - err := c.v.ReadInConfig() - if err != nil { - return err - } - err = c.v.Unmarshal(c) - if err != nil { - return err - } - return nil -} - -func (c *AgentConfig) Save() error { - data, err := yaml.Marshal(c) - if err != nil { - return err - } - return os.WriteFile(c.v.ConfigFileUsed(), data, os.ModePerm) -}