Skip to content

Commit

Permalink
add sysinfo 2022-08-03
Browse files Browse the repository at this point in the history
  • Loading branch information
hktalent committed Aug 3, 2022
1 parent bad7822 commit ba1c512
Show file tree
Hide file tree
Showing 84 changed files with 6,289 additions and 257 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ require (
github.com/projectdiscovery/nuclei/v2 v2.7.5
github.com/projectdiscovery/subfinder/v2 v2.5.2
github.com/satori/go.uuid v1.2.0
github.com/shirou/gopsutil/v3 v3.22.7
github.com/sijms/go-ora/v2 v2.4.25
github.com/simonnilsson/ask v0.2.0
github.com/spf13/viper v1.12.0
github.com/stacktitan/smb v0.0.0-20190531122847-da9a425dceb8
github.com/tj/go-update v2.2.5-0.20200519121640-62b4b798fd68+incompatible
Expand Down Expand Up @@ -241,8 +243,6 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/segmentio/ksuid v1.0.4 // indirect
github.com/shirou/gopsutil/v3 v3.22.6 // indirect
github.com/simonnilsson/ask v0.2.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,9 @@ github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil/v3 v3.21.7/go.mod h1:RGl11Y7XMTQPmHh8F0ayC6haKNBgH4PXMJuTAcMOlz4=
github.com/shirou/gopsutil/v3 v3.22.6 h1:FnHOFOh+cYAM0C30P+zysPISzlknLC5Z1G4EAElznfQ=
github.com/shirou/gopsutil/v3 v3.22.6/go.mod h1:EdIubSnZhbAvBS1yJ7Xi+AShB/hxwLHOMz4MCYz7yMs=
github.com/shirou/gopsutil/v3 v3.22.7 h1:flKnuCMfUUrO+oAvwAd6GKZgnPzr098VA/UJ14nhJd4=
github.com/shirou/gopsutil/v3 v3.22.7/go.mod h1:s648gW4IywYzUfE/KjXxUsqrqx/T2xO5VqOXxONeRfI=
github.com/sijms/go-ora/v2 v2.4.25 h1:PtyMsRGHd6FNttdaMEg4NdHuRYxg29NKwsxPuTmCEJ4=
github.com/sijms/go-ora/v2 v2.4.25/go.mod h1:EHxlY6x7y9HAsdfumurRfTd+v8NrEOTR3Xl4FWlH6xk=
github.com/simonnilsson/ask v0.2.0 h1:CpiQtuLyRlrPNVk6aQjaFL51XmBlCIxaqssxentXBBA=
Expand Down
171 changes: 171 additions & 0 deletions lib/util/sysinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package util

import (
"fmt"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
systemNet "net"
"strconv"
"time"
)

// 分区
type Part struct {
Path string `json:"path"`
FsType string `json:"fstype"`
Total float64 `json:"total"`
Free float64 `json:"free"`
Used float64 `json:"used"`
UsedPercent int `json:"usedPercent"`
}

// 分区集合
type Parts []Part

// CPU
type CpuSingle struct {
Num string `json:"num"`
Percent int `json:"percent"`
}

type CpuInfo struct {
CpuAvg float64 `json:"cpuAvg"`
CpuAll []CpuSingle `json:"cpuAll"`
}

const GB = 1024 * 1024 * 1024

func decimal(v string) float64 {
value, _ := strconv.ParseFloat(v, 64)
return value
}

// 1.主机IP
func GetLocalIP() (ip string) {
addresses, err := systemNet.InterfaceAddrs()
if err != nil {
return ""
}
for _, addr := range addresses {
ipAddr, ok := addr.(*systemNet.IPNet)
if !ok {
continue
}
if ipAddr.IP.IsLoopback() {
continue
}
if !ipAddr.IP.IsGlobalUnicast() {
continue
}
return ipAddr.IP.String()
}
return ""
}

// 2.主机信息
func GetHostInfo() (result *host.InfoStat, err error) {
result, err = host.Info()
return result, err
}

// 3.磁盘信息
func GetDiskInfo() (result Parts, err error) {
parts, err := disk.Partitions(true)
if err != nil {
return result, err
}
for _, part := range parts {
diskInfo, err := disk.Usage(part.Mountpoint)
if err == nil {
result = append(result, Part{
Path: diskInfo.Path,
FsType: diskInfo.Fstype,
Total: decimal(fmt.Sprintf("%.2f", float64(diskInfo.Total/GB))),
Free: decimal(fmt.Sprintf("%.2f", float64(diskInfo.Free/GB))),
Used: decimal(fmt.Sprintf("%.2f", float64(diskInfo.Used/GB))),
UsedPercent: int(diskInfo.UsedPercent),
})
} else {
return result, err
}
}
return result, err
}

// 4.CPU使用率
func GetCpuPercent() (result CpuInfo, err error) {
infos, err := cpu.Percent(1*time.Second, true)
if err != nil {
return result, err
}
var total float64 = 0
for index, value := range infos {
result.CpuAll = append(result.CpuAll, CpuSingle{
Num: fmt.Sprintf("#%d", index+1),
Percent: int(value),
})
total += value
}
result.CpuAvg = decimal(fmt.Sprintf("%.1f", total/float64(len(infos))))
return result, err
}

// 5.内存信息
func GetMemInfo() (float64, []map[string]interface{}) {
info, err := mem.VirtualMemory()
if err != nil {
fmt.Println(err)
return 0, nil
}
return decimal(fmt.Sprintf("%.1f", info.UsedPercent)), []map[string]interface{}{
{"key": "使用率[%]", "value": decimal(fmt.Sprintf("%.1f", info.UsedPercent))},
{"key": "总量[GB]", "value": int(info.Total / GB)},
{"key": "使用量[GB]", "value": int(info.Used / GB)},
{"key": "剩余量[GB]", "value": int(info.Free / GB)},
}
}

// 6.获取网卡信息
func GetNetInfo() (result []net.IOCountersStat, err error) {
info, err := net.IOCounters(true)
if err != nil {
return result, err
}
return info, err
}

// 7.计算上下行带宽
func GetNetSpeed() (speed map[string]map[string]uint64, err error) {
speed = map[string]map[string]uint64{}
info, err := net.IOCounters(true)
if err != nil {
return speed, err
}
for _, item := range info {
if item.BytesSent != 0 {
speed[item.Name] = map[string]uint64{
"send": item.BytesSent,
"recv": item.BytesRecv,
}
}
}

time.Sleep(1 * time.Second)

info, err = net.IOCounters(true)
if err != nil {
return speed, err
}
for _, item := range info {
if item.BytesSent != 0 {
speed[item.Name] = map[string]uint64{
"send": item.BytesSent - speed[item.Name]["send"],
"recv": item.BytesRecv - speed[item.Name]["recv"],
}
}
}
return speed, nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions vendor/github.com/shirou/gopsutil/v3/cpu/cpu.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions vendor/github.com/shirou/gopsutil/v3/disk/disk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/shirou/gopsutil/v3/disk/disk_aix.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ba1c512

Please sign in to comment.