From 0242086adb02d27c7aeb94ea6eee111ee143a309 Mon Sep 17 00:00:00 2001 From: spiritlhl <103393591+spiritLHLS@users.noreply.github.com> Date: Tue, 25 Jun 2024 02:05:55 +0000 Subject: [PATCH] =?UTF-8?q?v0.0.1=20-=20=E8=A7=A3=E8=80=A6=E6=96=B9?= =?UTF-8?q?=E4=BE=BF=E8=9E=8D=E5=90=88=E6=80=AA=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yaml | 31 ++++++++++++++++ README.md | 20 ++++++++++ basics_install.sh | 3 +- cmd/main.go | 8 ++++ cmd/main_test.go | 7 ++++ defaultset/defaultset.go | 35 ------------------ go.mod | 14 ++++--- go.sum | 13 +++++-- {network/model => model}/model.go | 61 +++++++++++++++++++++++++++++++ network/baseinfo/baseinfo.go | 5 ++- network/baseinfo/baseinfo_test.go | 4 +- network/network.go | 22 +++++++++-- network/utils/merge.go | 10 ++--- network/utils/parse.go | 10 ++--- system/cpu.go | 2 +- system/disk.go | 16 +++++--- system/model/model.go | 57 ----------------------------- system/system.go | 18 +++++++-- system/utils/utils_windows.go | 2 +- 19 files changed, 206 insertions(+), 132 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 cmd/main_test.go delete mode 100644 defaultset/defaultset.go rename {network/model => model}/model.go (72%) delete mode 100644 system/model/model.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..0c53bbb --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,31 @@ +name: CI + +on: + workflow_dispatch: + +jobs: + test: + strategy: + matrix: + go: [ '1.22.x' ] + os: [ ubuntu-latest ] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go }} + - name: Test + run: go test ./... -coverprofile=coverage.txt + - name: Create Tag + if: success() # 仅在测试成功时运行 + run: | + git config --global user.name 'github-actions' + git config --global user.email 'github-actions@github.com' + TAG="v0.0.1-$(date +'%Y%m%d%H%M%S')" + git tag $TAG + git push origin $TAG + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/README.md b/README.md index 938e36e..4cfb2c9 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ Use ```-l``` to specify the output language, currently ```en``` and ```zh``` are ## Usage +下载及安装 + ``` curl https://raw.githubusercontent.com/oneclickvirt/basics/main/basics_install.sh -sSf | bash ``` @@ -27,8 +29,26 @@ curl https://raw.githubusercontent.com/oneclickvirt/basics/main/basics_install.s curl https://cdn.spiritlhl.net/https://raw.githubusercontent.com/oneclickvirt/basics/main/basics_install.sh -sSf | bash ``` +使用 + +``` +basics +``` + +或 + +``` +./basics +``` + +进行测试 + 无环境依赖,理论上适配所有系统和主流架构,更多架构请查看 https://github.com/oneclickvirt/basics/releases/tag/output +``` + +``` + ![图片](https://github.com/oneclickvirt/basics/assets/103393591/634064de-17a6-485f-b401-dc3a159a18c4) ![图片](https://github.com/oneclickvirt/basics/assets/103393591/49404a18-1717-4875-b50d-26a930238248) diff --git a/basics_install.sh b/basics_install.sh index 1fcbe04..b1ea54d 100644 --- a/basics_install.sh +++ b/basics_install.sh @@ -2,6 +2,7 @@ #From https://github.com/oneclickvirt/basics #2024.05.21 +rm -rf /usr/bin/basics rm -rf basics os=$(uname -s) arch=$(uname -m) @@ -106,4 +107,4 @@ FreeBSD) esac chmod 777 basics -./basics +cp basics /usr/bin/basics diff --git a/cmd/main.go b/cmd/main.go index e633fb8..317c1b5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,13 +6,21 @@ import ( "net/http" "strings" + "github.com/oneclickvirt/basics/model" "github.com/oneclickvirt/basics/network" "github.com/oneclickvirt/basics/system" ) func main() { + var showVersion bool + flag.BoolVar(&showVersion, "v", false, "show version") + flag.BoolVar(&model.EnableLoger, "e", false, "Enable logging") languagePtr := flag.String("l", "", "Language parameter (en or zh)") flag.Parse() + if showVersion { + fmt.Println(model.BasicsVersion) + return + } var language string if *languagePtr == "" { language = "zh" diff --git a/cmd/main_test.go b/cmd/main_test.go new file mode 100644 index 0000000..f540234 --- /dev/null +++ b/cmd/main_test.go @@ -0,0 +1,7 @@ +package main + +import "testing" + +func Test_main(t *testing.T) { + main() +} \ No newline at end of file diff --git a/defaultset/defaultset.go b/defaultset/defaultset.go deleted file mode 100644 index d75a8fb..0000000 --- a/defaultset/defaultset.go +++ /dev/null @@ -1,35 +0,0 @@ -package defaultset - -import "fmt" - -func Red(text string) string { - return fmt.Sprintf("\033[31m\033[01m%s\033[0m", text) -} - -func Green(text string) string { - return fmt.Sprintf("\033[32m\033[01m%s\033[0m", text) -} - -func DarkGreen(text string) string { - return fmt.Sprintf("\033[32m\033[02m%s\033[0m", text) -} - -func Yellow(text string) string { - return fmt.Sprintf("\033[33m\033[01m%s\033[0m", text) -} - -func Blue(text string) string { - return fmt.Sprintf("\033[36m\033[01m%s\033[0m", text) -} - -func Purple(text string) string { - return fmt.Sprintf("\033[35m\033[01m%s\033[0m", text) -} - -func Cyan(text string) string { - return fmt.Sprintf("\033[36m\033[01m%s\033[0m", text) -} - -func White(text string) string { - return fmt.Sprintf("\033[37m\033[01m%s\033[0m", text) -} diff --git a/go.mod b/go.mod index fe40a28..3b7a652 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,14 @@ module github.com/oneclickvirt/basics -go 1.21.5 +go 1.22.4 require ( github.com/libp2p/go-nat v0.2.0 + github.com/pion/logging v0.2.2 + github.com/pion/stun v0.6.1 github.com/shirou/gopsutil v3.21.11+incompatible + github.com/yusufpapurcu/wmi v1.2.4 + golang.org/x/sys v0.21.0 ) require ( @@ -14,16 +18,14 @@ require ( github.com/jackpal/go-nat-pmp v1.0.2 // indirect github.com/koron/go-ssdp v0.0.4 // indirect github.com/libp2p/go-netroute v0.2.1 // indirect + github.com/oneclickvirt/defaultset v0.0.2-20240624082446 // indirect github.com/pion/dtls/v2 v2.2.7 // indirect - github.com/pion/logging v0.2.2 // indirect - github.com/pion/stun v0.6.1 // indirect github.com/pion/transport/v2 v2.2.1 // indirect - github.com/stretchr/testify v1.9.0 // indirect github.com/tklauser/go-sysconf v0.3.14 // indirect github.com/tklauser/numcpus v0.8.0 // indirect - github.com/yusufpapurcu/wmi v1.2.4 // indirect + go.uber.org/multierr v1.10.0 // indirect + go.uber.org/zap v1.27.0 // indirect golang.org/x/crypto v0.8.0 // indirect golang.org/x/net v0.10.0 // indirect golang.org/x/sync v0.2.0 // indirect - golang.org/x/sys v0.19.0 // indirect ) diff --git a/go.sum b/go.sum index dc64b4c..e071c9b 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/libp2p/go-nat v0.2.0 h1:Tyz+bUFAYqGyJ/ppPPymMGbIgNRH+WqC5QrT5fKrrGk= github.com/libp2p/go-nat v0.2.0/go.mod h1:3MJr+GRpRkyT65EpVPBstXLvOlAPzUVlG6Pwg9ohLJk= github.com/libp2p/go-netroute v0.2.1 h1:V8kVrpD8GK0Riv15/7VN6RbUQ3URNZVosw7H2v9tksU= github.com/libp2p/go-netroute v0.2.1/go.mod h1:hraioZr0fhBjG0ZRXJJ6Zj2IVEVNx6tDTFQfSmcq7mQ= +github.com/oneclickvirt/defaultset v0.0.2-20240624082446 h1:5Pg3mK/u/vQvSz7anu0nxzrNdELi/AcDAU1mMsmPzyc= +github.com/oneclickvirt/defaultset v0.0.2-20240624082446/go.mod h1:e9Jt4tf2sbemCtc84/XgKcHy9EZ2jkc5x2sW1NiJS+E= github.com/pion/dtls/v2 v2.2.7 h1:cSUBsETxepsCSFSxC3mc/aDo14qQLMSL+O6IjG28yV8= github.com/pion/dtls/v2 v2.2.7/go.mod h1:8WiMkebSHFD0T+dIU+UeBaoV7kDhOW5oDCzZ7WZ/F9s= github.com/pion/logging v0.2.2 h1:M9+AIj/+pxNsDfAT64+MAVgJO0rsyLnoJKCqf//DoeY= @@ -33,9 +35,8 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU= github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY= github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY= @@ -43,6 +44,10 @@ github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8t github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -75,8 +80,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= diff --git a/network/model/model.go b/model/model.go similarity index 72% rename from network/model/model.go rename to model/model.go index 3d03913..1e89dba 100644 --- a/network/model/model.go +++ b/model/model.go @@ -1,5 +1,9 @@ package model +const BasicsVersion = "v0.0.1" + +var EnableLoger bool + type IpInfo struct { Ip string ASN string @@ -88,3 +92,60 @@ var TranslationMap = map[string]string{ "IsBogon": "是否Bogon", "IsBot": "是否机器人", } + +type CpuInfo struct { + CpuModel string + CpuCores string + CpuCache string + CpuAesNi string + CpuVAH string +} + +type MemoryInfo struct { + MemoryUsage string + MemoryTotal string + SwapUsage string + SwapTotal string +} + +type DiskInfo struct { + DiskUsage string + DiskTotal string + Percentage string + BootPath string +} + +type SystemInfo struct { + CpuInfo + MemoryInfo + DiskInfo + Platform string // 系统名字 Distro1 + PlatformVersion string // 系统版本 Distro2 + Kernel string // 系统内核 + Arch string // + Uptime string // 正常运行时间 + TimeZone string // 系统时区 + VmType string // 虚拟化架构 + Load string // load1 load2 load3 + NatType string // stun + VirtioBalloon string // 气球驱动 + KSM string // 内存合并 + TcpAccelerationMethod string // TCP拥塞控制 +} + +type Win32_Processor struct { + L2CacheSize uint32 + L3CacheSize uint32 +} + +type Win32_ComputerSystem struct { + SystemType string +} + +type Win32_OperatingSystem struct { + BuildType string +} + +type Win32_TimeZone struct { + Caption string +} diff --git a/network/baseinfo/baseinfo.go b/network/baseinfo/baseinfo.go index 42d9229..0b15746 100644 --- a/network/baseinfo/baseinfo.go +++ b/network/baseinfo/baseinfo.go @@ -2,11 +2,12 @@ package baseinfo import ( "fmt" - "github.com/oneclickvirt/basics/network/model" - "github.com/oneclickvirt/basics/network/utils" "strconv" "strings" "sync" + + "github.com/oneclickvirt/basics/model" + "github.com/oneclickvirt/basics/network/utils" ) // FetchIPInfoIo 从 ipinfo.io 获取 IP 信息 diff --git a/network/baseinfo/baseinfo_test.go b/network/baseinfo/baseinfo_test.go index b4f933e..722a2d2 100644 --- a/network/baseinfo/baseinfo_test.go +++ b/network/baseinfo/baseinfo_test.go @@ -2,13 +2,13 @@ package baseinfo import ( "fmt" - networkModel "github.com/oneclickvirt/basics/network/model" + "github.com/oneclickvirt/basics/model" "testing" "time" ) // printIPInfo 重构输出函数 -func printIPInfo(ipInfo *networkModel.IpInfo, securityInfo *networkModel.SecurityInfo, err error) { +func printIPInfo(ipInfo *model.IpInfo, securityInfo *model.SecurityInfo, err error) { if err != nil { fmt.Println("获取 IP 信息时出错:", err) return diff --git a/network/network.go b/network/network.go index 3bc10e4..191a23a 100644 --- a/network/network.go +++ b/network/network.go @@ -4,8 +4,9 @@ import ( "fmt" "strings" + "github.com/oneclickvirt/basics/model" "github.com/oneclickvirt/basics/network/baseinfo" - "github.com/oneclickvirt/basics/network/model" + . "github.com/oneclickvirt/defaultset" ) // sortAndTranslateText 对原始文本进行排序和翻译 @@ -70,9 +71,16 @@ func processPrintIPInfo(headASNString string, headLocationString string, ipResul // checkType 可选 both ipv4 ipv6 // language 暂时仅支持 en 或 zh func NetworkCheck(checkType string, enableSecurityCheck bool, language string) (string, string, error) { + if model.EnableLoger { + InitLogger() + defer Logger.Sync() + } var ipInfo string if checkType == "both" { - ipInfoV4Result, _, ipInfoV6Result, _, _ := baseinfo.RunIpCheck("both") + ipInfoV4Result, _, ipInfoV6Result, _, err := baseinfo.RunIpCheck("both") + if err != nil && model.EnableLoger { + Logger.Info(err.Error()) + } if ipInfoV4Result != nil { ipInfo += processPrintIPInfo(" IPV4 ASN : ", " IPV4 Location : ", ipInfoV4Result) } @@ -81,13 +89,19 @@ func NetworkCheck(checkType string, enableSecurityCheck bool, language string) ( } return ipInfo, "", nil } else if checkType == "ipv4" { - ipInfoV4Result, _, _, _, _ := baseinfo.RunIpCheck("ipv4") + ipInfoV4Result, _, _, _, err := baseinfo.RunIpCheck("ipv4") + if err != nil && model.EnableLoger { + Logger.Info(err.Error()) + } if ipInfoV4Result != nil { ipInfo += processPrintIPInfo(" IPV4 ASN : ", " IPV4 Location : ", ipInfoV4Result) } return ipInfo, "", nil } else if checkType == "ipv6" { - _, _, ipInfoV6Result, _, _ := baseinfo.RunIpCheck("ipv6") + _, _, ipInfoV6Result, _, err := baseinfo.RunIpCheck("ipv6") + if err != nil && model.EnableLoger { + Logger.Info(err.Error()) + } if ipInfoV6Result != nil { ipInfo += processPrintIPInfo(" IPV6 ASN : ", " IPV6 Location : ", ipInfoV6Result) } diff --git a/network/utils/merge.go b/network/utils/merge.go index 58313bf..9a780bc 100644 --- a/network/utils/merge.go +++ b/network/utils/merge.go @@ -3,7 +3,7 @@ package utils import ( "fmt" - networkModel "github.com/oneclickvirt/basics/network/model" + "github.com/oneclickvirt/basics/model" ) // chooseString 用于选择非空字符串 @@ -15,12 +15,12 @@ func chooseString(src, dst string) string { } // CompareAndMergeIpInfo 用于比较和合并两个 IpInfo 结构体 -func CompareAndMergeIpInfo(dst, src *networkModel.IpInfo) (res *networkModel.IpInfo, err error) { +func CompareAndMergeIpInfo(dst, src *model.IpInfo) (res *model.IpInfo, err error) { if src == nil { return nil, fmt.Errorf("Error merge IpInfo") } if dst == nil { - dst = &networkModel.IpInfo{} + dst = &model.IpInfo{} } dst.Ip = chooseString(src.Ip, dst.Ip) dst.ASN = chooseString(src.ASN, dst.ASN) @@ -32,12 +32,12 @@ func CompareAndMergeIpInfo(dst, src *networkModel.IpInfo) (res *networkModel.IpI } // CompareAndMergeSecurityInfo 用于比较和合并两个 SecurityInfo 结构体 -func CompareAndMergeSecurityInfo(dst, src *networkModel.SecurityInfo) (res *networkModel.SecurityInfo, err error) { +func CompareAndMergeSecurityInfo(dst, src *model.SecurityInfo) (res *model.SecurityInfo, err error) { if src == nil { return nil, fmt.Errorf("Error merge SecurityInfo") } if dst == nil { - dst = &networkModel.SecurityInfo{} + dst = &model.SecurityInfo{} } dst.IsAbuser = chooseString(src.IsAbuser, dst.IsAbuser) dst.IsAttacker = chooseString(src.IsAttacker, dst.IsAttacker) diff --git a/network/utils/parse.go b/network/utils/parse.go index 934101e..17127a3 100644 --- a/network/utils/parse.go +++ b/network/utils/parse.go @@ -1,13 +1,13 @@ package utils import ( - networkModel "github.com/oneclickvirt/basics/network/model" + "github.com/oneclickvirt/basics/model" "strconv" "strings" ) -func ParseIpInfo(data map[string]interface{}) *networkModel.IpInfo { - ipInfo := &networkModel.IpInfo{} +func ParseIpInfo(data map[string]interface{}) *model.IpInfo { + ipInfo := &model.IpInfo{} if ip, ok := data["ip"].(string); ok { ipInfo.Ip = ip } @@ -37,8 +37,8 @@ func ParseIpInfo(data map[string]interface{}) *networkModel.IpInfo { return ipInfo } -func ParseSecurityInfo(data map[string]interface{}) *networkModel.SecurityInfo { - securityInfo := &networkModel.SecurityInfo{} +func ParseSecurityInfo(data map[string]interface{}) *model.SecurityInfo { + securityInfo := &model.SecurityInfo{} if security, ok := data["security"].(map[string]interface{}); ok { if isAbuser, ok := security["is_abuser"].(bool); ok { securityInfo.IsAbuser = strconv.FormatBool(isAbuser) diff --git a/system/cpu.go b/system/cpu.go index cd4ab8e..9b78813 100644 --- a/system/cpu.go +++ b/system/cpu.go @@ -9,7 +9,7 @@ import ( "strconv" "strings" - "github.com/oneclickvirt/basics/system/model" + "github.com/oneclickvirt/basics/model" "github.com/oneclickvirt/basics/system/utils" "github.com/shirou/gopsutil/cpu" ) diff --git a/system/disk.go b/system/disk.go index b915229..b6e70ad 100644 --- a/system/disk.go +++ b/system/disk.go @@ -10,8 +10,8 @@ import ( ) // getDiskInfo 获取硬盘信息 -func getDiskInfo() (string, string, string, error) { - var diskTotalStr, diskUsageStr, bootPath string +func getDiskInfo() (string, string, string, string, error) { + var diskTotalStr, diskUsageStr, percentageStr, bootPath string tempDiskTotal, tempDiskUsage := getDiskTotalAndUsed() diskTotalGB := float64(tempDiskTotal) / (1024 * 1024 * 1024) diskUsageGB := float64(tempDiskUsage) / (1024 * 1024 * 1024) @@ -69,9 +69,11 @@ func getDiskInfo() (string, string, string, error) { diskTotalGB = float64(tpDiskTotal) / (1024 * 1024) diskUsageGB = float64(tpDiskUsage) / (1024 * 1024) if diskTotalGB < 1 { - diskTotalStr = strconv.FormatFloat(diskTotalGB*1024, 'f', 2, 64) + " MB" + " [" + nonEmptyFields[4] + "]" + diskTotalStr = strconv.FormatFloat(diskTotalGB*1024, 'f', 2, 64) + " MB" + percentageStr = nonEmptyFields[4] } else { - diskTotalStr = strconv.FormatFloat(diskTotalGB, 'f', 2, 64) + " GB" + " [" + nonEmptyFields[4] + "]" + diskTotalStr = strconv.FormatFloat(diskTotalGB, 'f', 2, 64) + " GB" + percentageStr = nonEmptyFields[4] } if diskUsageGB < 1 { diskUsageStr = strconv.FormatFloat(diskUsageGB*1024, 'f', 2, 64) + " MB" @@ -84,7 +86,11 @@ func getDiskInfo() (string, string, string, error) { } } } - return diskTotalStr, diskUsageStr, bootPath, nil + // 两个%避免被转义 + if percentageStr != "" && strings.Contains(percentageStr, "%") { + percentageStr = strings.ReplaceAll(percentageStr, "%", "%%") + } + return diskTotalStr, diskUsageStr, percentageStr, bootPath, nil } func getDiskTotalAndUsed() (total uint64, used uint64) { diff --git a/system/model/model.go b/system/model/model.go deleted file mode 100644 index 588e813..0000000 --- a/system/model/model.go +++ /dev/null @@ -1,57 +0,0 @@ -package model - -type CpuInfo struct { - CpuModel string - CpuCores string - CpuCache string - CpuAesNi string - CpuVAH string -} - -type MemoryInfo struct { - MemoryUsage string - MemoryTotal string - SwapUsage string - SwapTotal string -} - -type DiskInfo struct { - DiskUsage string - DiskTotal string - BootPath string -} - -type SystemInfo struct { - CpuInfo - MemoryInfo - DiskInfo - Platform string // 系统名字 Distro1 - PlatformVersion string // 系统版本 Distro2 - Kernel string // 系统内核 - Arch string // - Uptime string // 正常运行时间 - TimeZone string // 系统时区 - VmType string // 虚拟化架构 - Load string // load1 load2 load3 - NatType string // stun - VirtioBalloon string // 气球驱动 - KSM string // 内存合并 - TcpAccelerationMethod string // TCP拥塞控制 -} - -type Win32_Processor struct { - L2CacheSize uint32 - L3CacheSize uint32 -} - -type Win32_ComputerSystem struct { - SystemType string -} - -type Win32_OperatingSystem struct { - BuildType string -} - -type Win32_TimeZone struct { - Caption string -} diff --git a/system/system.go b/system/system.go index 6bc23d5..bdbd15f 100644 --- a/system/system.go +++ b/system/system.go @@ -4,7 +4,7 @@ import ( "runtime" "strconv" - "github.com/oneclickvirt/basics/system/model" + "github.com/oneclickvirt/basics/model" "github.com/oneclickvirt/basics/system/utils" ) @@ -24,7 +24,7 @@ func GetSystemInfo() *model.SystemInfo { // CPU信息查询 ret, _ = getCpuInfo(ret, cpuType) // 硬盘信息查询 - ret.DiskTotal, ret.DiskUsage, ret.BootPath, _ = getDiskInfo() + ret.DiskTotal, ret.DiskUsage, ret.Percentage, ret.BootPath, _ = getDiskInfo() // 内存信息查询 ret.MemoryTotal, ret.MemoryUsage, ret.SwapTotal, ret.SwapUsage, ret.VirtioBalloon, ret.KSM = getMemoryInfo() // 获取负载信息 @@ -65,7 +65,12 @@ func CheckSystemInfo(language string) string { } else if ret.SwapTotal != "" && ret.SwapUsage != "" { res += " Swap : " + ret.SwapUsage + " / " + ret.SwapTotal + "\n" } - res += " Disk : " + ret.DiskUsage + " / " + ret.DiskTotal + "\n" + res += " Disk : " + ret.DiskUsage + " / " + ret.DiskTotal + if ret.Percentage != "" { + res += " [" + ret.Percentage + "] " + "\n" + } else { + res += "\n" + } res += " Boot Path : " + ret.BootPath + "\n" res += " OS Release : " + ret.Platform + " [" + ret.Arch + "] " + "\n" if ret.Kernel != "" { @@ -101,7 +106,12 @@ func CheckSystemInfo(language string) string { } else if ret.SwapTotal != "" && ret.SwapUsage != "" { res += " Swap : " + ret.SwapUsage + " / " + ret.SwapTotal + "\n" } - res += " 硬盘空间 : " + ret.DiskUsage + " / " + ret.DiskTotal + "\n" + res += " 硬盘空间 : " + ret.DiskUsage + " / " + ret.DiskTotal + if ret.Percentage != "" { + res += " [" + ret.Percentage + "] " + "\n" + } else { + res += "\n" + } res += " 启动盘路径 : " + ret.BootPath + "\n" res += " 系统 : " + ret.Platform + " [" + ret.Arch + "] " + "\n" if ret.Kernel != "" { diff --git a/system/utils/utils_windows.go b/system/utils/utils_windows.go index aef3015..b2e6e66 100644 --- a/system/utils/utils_windows.go +++ b/system/utils/utils_windows.go @@ -5,7 +5,7 @@ import ( "math" "strings" - "github.com/oneclickvirt/basics/system/model" + "github.com/oneclickvirt/basics/model" "github.com/yusufpapurcu/wmi" "golang.org/x/sys/windows/registry" )