[agent-wdd] 完成自定义log部分;完成info network部分; 项目结构基本完成
This commit is contained in:
120
agent-wdd/config/Config.go
Normal file
120
agent-wdd/config/Config.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type AgentServerInfo struct {
|
||||
NetworkInfo
|
||||
CPUInfo
|
||||
}
|
||||
|
||||
type NetworkInfo struct {
|
||||
Interfaces []InterfaceInfo
|
||||
}
|
||||
|
||||
type InterfaceInfo struct {
|
||||
Name string
|
||||
HardwareAddr string
|
||||
Addrs []string
|
||||
}
|
||||
|
||||
func GetNetworkInfo() (*NetworkInfo, error) {
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var networkInfo NetworkInfo
|
||||
for _, iface := range interfaces {
|
||||
var addrs []string
|
||||
addresses, err := iface.Addrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, addr := range addresses {
|
||||
addrs = append(addrs, addr.String())
|
||||
}
|
||||
|
||||
interfaceInfo := InterfaceInfo{
|
||||
Name: iface.Name,
|
||||
HardwareAddr: iface.HardwareAddr.String(),
|
||||
Addrs: addrs,
|
||||
}
|
||||
networkInfo.Interfaces = append(networkInfo.Interfaces, interfaceInfo)
|
||||
}
|
||||
|
||||
return &networkInfo, nil
|
||||
}
|
||||
|
||||
type CPUInfo struct {
|
||||
ModelName string
|
||||
Cores int
|
||||
Mhz float64
|
||||
}
|
||||
|
||||
func GetCpuInfo() CPUInfo {
|
||||
info := CPUInfo{}
|
||||
info.ModelName = runtime.GOARCH
|
||||
info.Cores = runtime.NumCPU()
|
||||
info.Mhz = 0.0
|
||||
return info
|
||||
}
|
||||
|
||||
func (info *AgentServerInfo) GetAgentInfo() {
|
||||
|
||||
}
|
||||
@@ -1,25 +1,149 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"agent-wdd/log"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CanConnectInternet 判定主机能否上网 请求 www.google.com 如果请求正常 返回2 如果超时三秒 请求baidu.com,如果没有错误返回1 如果错误返回0
|
||||
// 能够联网,就大于这个数字 外网9 国内7 不能联网1 未知0
|
||||
const InternetBaseLine = 1
|
||||
|
||||
// 定义响应数据结构体
|
||||
type IPInfo struct {
|
||||
IP string `json:"ip"`
|
||||
City string `json:"city"`
|
||||
Region string `json:"region"`
|
||||
Country string `json:"country"`
|
||||
Loc string `json:"loc"`
|
||||
Org string `json:"org"`
|
||||
Postal string `json:"postal"`
|
||||
Timezone string `json:"timezone"`
|
||||
}
|
||||
|
||||
// Gather 获取网络相关的信息
|
||||
func (network *Network) Gather() {
|
||||
// 能够联网
|
||||
network.Internet = CanConnectInternet()
|
||||
|
||||
// 获取公网的相关信息
|
||||
pub := PublicInfo{}
|
||||
pub.GetPublicInfo()
|
||||
|
||||
network.Public = pub
|
||||
|
||||
//获取本机网卡相关的内容
|
||||
}
|
||||
|
||||
// CanConnectInternet 判定主机能够上网
|
||||
func CanConnectInternet() int {
|
||||
// 读取 config 文件,判定能否上网
|
||||
internetCode := ConfigCache.Agent.Network.Internet
|
||||
|
||||
if internetCode == 0 {
|
||||
// 没有相关的信息,需要重新判定
|
||||
internetCode = judgeCanConnectInternet()
|
||||
|
||||
// 持久化保存
|
||||
ConfigCache.Agent.Network.Internet = internetCode
|
||||
SaveConfig()
|
||||
}
|
||||
|
||||
return internetCode
|
||||
}
|
||||
|
||||
// judgeCanConnectInternet 请求网址判定主机能否上网 请求 www.google.com 如果请求正常 返回9 如果超时三秒 请求baidu.com,如果没有错误返回7 如果错误返回1
|
||||
func judgeCanConnectInternet() int {
|
||||
client := http.Client{
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
|
||||
_, err := client.Get("https://www.google.com")
|
||||
if err == nil {
|
||||
return 2
|
||||
return 9
|
||||
}
|
||||
|
||||
_, err = client.Get("https://www.baidu.com")
|
||||
if err == nil {
|
||||
return 1
|
||||
return 7
|
||||
}
|
||||
|
||||
return 0
|
||||
return 1
|
||||
}
|
||||
|
||||
// GetPublicInfo 获取服务器的公网信息
|
||||
func (p PublicInfo) GetPublicInfo() {
|
||||
|
||||
if CanConnectInternet() == InternetBaseLine {
|
||||
// 无法联网, 假信息
|
||||
|
||||
fakePublicInfo := PublicInfo{
|
||||
IPv4: "1.1.1.1",
|
||||
IPv6: "2400::1",
|
||||
Country: "CN",
|
||||
City: "Shanghai",
|
||||
ASN: "Wdd Inc",
|
||||
}
|
||||
|
||||
ConfigCache.Agent.Network.Public = fakePublicInfo
|
||||
|
||||
SaveConfig()
|
||||
return
|
||||
}
|
||||
|
||||
// 可以联网
|
||||
// 创建带有超时的HTTP客户端
|
||||
client := &http.Client{
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
|
||||
// 创建新的请求对象
|
||||
req, err := http.NewRequest("GET", "https://ipinfo.io", nil)
|
||||
if err != nil {
|
||||
log.Error("创建请求失败: %v", err)
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
req.Header.Add("Authorization", "Bearer 6ecb0db9bd8f19")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Error("请求PublicInfo失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查响应状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Error("非200状态码: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// 读取响应体
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Error("读取响应失败: %v", err)
|
||||
}
|
||||
|
||||
// 解析JSON数据
|
||||
var info IPInfo
|
||||
if err := json.Unmarshal(body, &info); err != nil {
|
||||
log.Error("JSON解析失败: %v", err)
|
||||
}
|
||||
|
||||
// 打印解析结果
|
||||
log.Info("IP信息:\n%+v\n", info)
|
||||
|
||||
// 保存信息
|
||||
p.IPv4 = info.IP
|
||||
p.ASN = info.Org
|
||||
p.Country = info.Country
|
||||
p.City = info.City
|
||||
|
||||
ConfigCache.Agent.Network.Public = p
|
||||
|
||||
SaveConfig()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user