大量的更新

This commit is contained in:
zeaslity
2025-02-26 17:44:03 +08:00
parent b8170e00d4
commit c751c21871
8 changed files with 323 additions and 42 deletions

View File

@@ -3,9 +3,11 @@ package config
import (
"agent-wdd/log"
"agent-wdd/utils"
"os"
"runtime"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
"os"
)
var WddConfigFilePath = "/usr/local/etc/wdd/agent-wdd-config.yaml"
@@ -14,9 +16,12 @@ var ConfigCache *Config
func init() {
// 根据运行的操作系统不同, 修改 WddConfigFilePath 的位置
if runtime.GOOS == "windows" {
homedir, _ := os.UserHomeDir()
WddConfigFilePath = homedir + "\\agent-wdd\\agent-wdd-config.yaml"
}
}
// 配置结构体定义
type Config struct {
TimeStamp string `yaml:"timestamp"`
ModifiedTimes int `yaml:"modifiedTimes"`

View File

@@ -3,12 +3,10 @@ package config
import (
"agent-wdd/log"
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
)
var CommonDiskPath = []string{
@@ -82,32 +80,31 @@ func DiskListGather() {
}
func (disk *Disk) calculateDiskUsage() {
var stat syscall.Statfs_t
err := syscall.Statfs(disk.Path, &stat)
if err != nil {
log.Error("disk syscall error of %v", err)
// var stat unix.Statfs_t
// err := unix.Statfs(disk.Path, &stat)
// if err != nil {
// log.Error("disk syscall error: %v", err)
// disk.Size = "0B"
// disk.Usage = "0B"
// disk.Percent = "0.00%"
// return
// }
disk.Size = "0B"
disk.Usage = "0B"
disk.Percent = "0.00%"
return
}
// // 计算存储空间大小
// totalBytes := stat.Blocks * uint64(stat.Bsize)
// availBytes := stat.Bavail * uint64(stat.Bsize)
// usedBytes := totalBytes - availBytes
// 计算存储空间大小
totalBytes := stat.Blocks * uint64(stat.Bsize)
availBytes := stat.Bavail * uint64(stat.Bsize)
usedBytes := totalBytes - availBytes
// // 格式化输出
// disk.Size = formatDiskSize(totalBytes)
// disk.Usage = formatDiskSize(usedBytes)
// 格式化输出
disk.Size = formatDiskSize(totalBytes)
disk.Usage = formatDiskSize(usedBytes)
if totalBytes == 0 {
disk.Percent = "0.00%"
} else {
percent := float64(usedBytes) / float64(totalBytes) * 100
disk.Percent = fmt.Sprintf("%.2f%%", percent)
}
// if totalBytes == 0 {
// disk.Percent = "0.00%"
// } else {
// percent := float64(usedBytes) / float64(totalBytes) * 100
// disk.Percent = fmt.Sprintf("%.2f%%", percent)
// }
}
func formatDiskSize(bytes uint64) string {

View File

@@ -41,6 +41,23 @@ func (network *Network) Gather() {
// CanConnectInternet 判定主机能够上网
func CanConnectInternet() int {
// 初始化 ConfigCache
if ConfigCache == nil {
ConfigCache = &Config{
TimeStamp: "",
ModifiedTimes: 0,
Agent: Agent{
OS: OS{},
Network: Network{},
CPU: CPU{},
Mem: Memory{},
Swap: Swap{},
Disks: []Disk{},
},
}
}
// 读取 config 文件,判定能否上网
internetCode := ConfigCache.Agent.Network.Internet