[agent-wdd] 完成自定义log部分;完成info network部分; 项目结构基本完成

This commit is contained in:
zeaslity
2025-02-11 17:27:41 +08:00
parent 66dca6a080
commit e826b55240
38 changed files with 39009 additions and 33930 deletions

120
agent-wdd/config/Config.go Normal file
View File

@@ -0,0 +1,120 @@
package config
import (
"agent-wdd/log"
"agent-wdd/utils"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
"os"
)
var WddConfigFilePath = "/usr/local/etc/wdd/agent-wdd-config.yaml"
var ConfigCache *Config
func init() {
// 根据运行的操作系统不同, 修改 WddConfigFilePath 的位置
}
// 配置结构体定义
type Config struct {
TimeStamp string `yaml:"timestamp"`
Agent Agent `yaml:"agent"`
}
type Agent struct {
Hostname string `yaml:"hostname"`
Network Network `yaml:"network"`
CPU CPU `yaml:"cpu"`
Mem Memory `yaml:"mem"`
Swap Swap `yaml:"swap"`
Disk []Disk `yaml:"disk"`
}
type Network struct {
Internet int `yaml:"internet"`
Public PublicInfo `yaml:"public"`
Interfaces []Interface `yaml:"interfaces"`
}
type PublicInfo struct {
IPv4 string `yaml:"ipv4"`
IPv6 string `yaml:"ipv6"`
Country string `yaml:"country"`
City string `yaml:"city"`
ASN string `yaml:"asn"`
}
type Interface struct {
Name string `yaml:"name"`
IPv4 string `yaml:"ipv4"`
IPv6 string `yaml:"ipv6"`
MAC string `yaml:"mac"`
}
type CPU struct {
Cores int `yaml:"cores"`
Brand string `yaml:"brand"`
Mhz int `yaml:"mhz"`
}
type Memory struct {
Size string `yaml:"size"`
Type string `yaml:"type"`
Speed int `yaml:"speed"`
}
type Swap struct {
On bool `yaml:"on"`
Size string `yaml:"size"`
}
type Disk struct {
Path string `yaml:"path"`
Size string `yaml:"size"`
Usage string `yaml:"usage"`
Percent string `yaml:"percent"`
VG string `yaml:"vg"`
LV string `yaml:"lv"`
}
func InitConfig() {
// 检查配置文件是否存在
if !utils.FileOrFolderExists(WddConfigFilePath) {
utils.AppendContentToFile("", WddConfigFilePath)
}
v := viper.New()
v.SetConfigFile(WddConfigFilePath)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
log.Error("读取配置文件失败: %w", err)
panic(err)
}
if err := v.Unmarshal(&ConfigCache); err != nil {
log.Error("解析配置失败: %w", err)
panic(err)
}
}
// 写入配置文件
func SaveConfig() {
// 每次写入新的时间
ConfigCache.TimeStamp = utils.CurrentTimeString()
data, err := yaml.Marshal(&ConfigCache)
if err != nil {
log.Error("YAML序列化失败: %w", err)
}
if err := os.WriteFile(WddConfigFilePath, data, 0644); err != nil {
log.Error("写入文件失败: %w", err)
}
}