大量的更新
This commit is contained in:
@@ -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"`
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user