chore: code style (#44)
* some code refactor * service * chore: code style --------- Co-authored-by: naiba <hi@nai.ba>
This commit is contained in:
parent
a99bf7066b
commit
c0288971d8
@ -38,6 +38,7 @@ import (
|
|||||||
pb "github.com/nezhahq/agent/proto"
|
pb "github.com/nezhahq/agent/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Agent 运行时参数。如需添加新参数,记得同时在 service.go 中添加
|
||||||
type AgentCliParam struct {
|
type AgentCliParam struct {
|
||||||
SkipConnectionCount bool // 跳过连接数检查
|
SkipConnectionCount bool // 跳过连接数检查
|
||||||
SkipProcsCount bool // 跳过进程数量检查
|
SkipProcsCount bool // 跳过进程数量检查
|
||||||
@ -432,7 +433,9 @@ func reportState() {
|
|||||||
|
|
||||||
// doSelfUpdate 执行更新检查 如果更新成功则会结束进程
|
// doSelfUpdate 执行更新检查 如果更新成功则会结束进程
|
||||||
func doSelfUpdate(useLocalVersion bool) {
|
func doSelfUpdate(useLocalVersion bool) {
|
||||||
<-monitor.Sync
|
if monitor.CachedCountry == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
v := semver.MustParse("0.1.0")
|
v := semver.MustParse("0.1.0")
|
||||||
if useLocalVersion {
|
if useLocalVersion {
|
||||||
v = semver.MustParse(version)
|
v = semver.MustParse(version)
|
||||||
|
@ -86,6 +86,7 @@ func serviceActions(cmd *cobra.Command, args []string) {
|
|||||||
{agentCliParam.DisableForceUpdate, "--disable-force-update", ""},
|
{agentCliParam.DisableForceUpdate, "--disable-force-update", ""},
|
||||||
{agentCliParam.UseIPv6CountryCode, "--use-ipv6-countrycode", ""},
|
{agentCliParam.UseIPv6CountryCode, "--use-ipv6-countrycode", ""},
|
||||||
{agentConfig.GPU, "--gpu", ""},
|
{agentConfig.GPU, "--gpu", ""},
|
||||||
|
{agentCliParam.UseGiteeToUpgrade, "--gitee", ""},
|
||||||
{agentCliParam.IPReportPeriod != 30*60, "-u", fmt.Sprint(agentCliParam.IPReportPeriod)},
|
{agentCliParam.IPReportPeriod != 30*60, "-u", fmt.Sprint(agentCliParam.IPReportPeriod)},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,17 +83,14 @@ func GetHost() *model.Host {
|
|||||||
} else {
|
} else {
|
||||||
if hi.VirtualizationRole == "guest" {
|
if hi.VirtualizationRole == "guest" {
|
||||||
cpuType = "Virtual"
|
cpuType = "Virtual"
|
||||||
|
ret.Virtualization = hi.VirtualizationSystem
|
||||||
} else {
|
} else {
|
||||||
cpuType = "Physical"
|
cpuType = "Physical"
|
||||||
|
ret.Virtualization = ""
|
||||||
}
|
}
|
||||||
ret.Platform = hi.Platform
|
ret.Platform = hi.Platform
|
||||||
ret.PlatformVersion = hi.PlatformVersion
|
ret.PlatformVersion = hi.PlatformVersion
|
||||||
ret.Arch = hi.KernelArch
|
ret.Arch = hi.KernelArch
|
||||||
if cpuType == "Physical" {
|
|
||||||
ret.Virtualization = ""
|
|
||||||
} else {
|
|
||||||
ret.Virtualization = hi.VirtualizationSystem
|
|
||||||
}
|
|
||||||
ret.BootTime = hi.BootTime
|
ret.BootTime = hi.BootTime
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,40 +215,6 @@ func GetState(skipConnectionCount bool, skipProcsCount bool) *model.HostState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var tcpConnCount, udpConnCount uint64
|
|
||||||
if !skipConnectionCount {
|
|
||||||
ss_err := true
|
|
||||||
if runtime.GOOS == "linux" {
|
|
||||||
tcpStat, err_tcp := goss.ConnectionsWithProtocol(goss.AF_INET, syscall.IPPROTO_TCP)
|
|
||||||
udpStat, err_udp := goss.ConnectionsWithProtocol(goss.AF_INET, syscall.IPPROTO_UDP)
|
|
||||||
if err_tcp == nil && err_udp == nil {
|
|
||||||
ss_err = false
|
|
||||||
tcpConnCount = uint64(len(tcpStat))
|
|
||||||
udpConnCount = uint64(len(udpStat))
|
|
||||||
}
|
|
||||||
if strings.Contains(CachedIP, ":") {
|
|
||||||
tcpStat6, err_tcp := goss.ConnectionsWithProtocol(goss.AF_INET6, syscall.IPPROTO_TCP)
|
|
||||||
udpStat6, err_udp := goss.ConnectionsWithProtocol(goss.AF_INET6, syscall.IPPROTO_UDP)
|
|
||||||
if err_tcp == nil && err_udp == nil {
|
|
||||||
ss_err = false
|
|
||||||
tcpConnCount += uint64(len(tcpStat6))
|
|
||||||
udpConnCount += uint64(len(udpStat6))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ss_err {
|
|
||||||
conns, _ := net.Connections("all")
|
|
||||||
for i := 0; i < len(conns); i++ {
|
|
||||||
switch conns[i].Type {
|
|
||||||
case syscall.SOCK_STREAM:
|
|
||||||
tcpConnCount++
|
|
||||||
case syscall.SOCK_DGRAM:
|
|
||||||
udpConnCount++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if agentConfig.Temperature {
|
if agentConfig.Temperature {
|
||||||
go updateTemperatureStat()
|
go updateTemperatureStat()
|
||||||
ret.Temperatures = temperatureStat
|
ret.Temperatures = temperatureStat
|
||||||
@ -265,7 +228,7 @@ func GetState(skipConnectionCount bool, skipProcsCount bool) *model.HostState {
|
|||||||
ret.NetInTransfer, ret.NetOutTransfer = netInTransfer, netOutTransfer
|
ret.NetInTransfer, ret.NetOutTransfer = netInTransfer, netOutTransfer
|
||||||
ret.NetInSpeed, ret.NetOutSpeed = netInSpeed, netOutSpeed
|
ret.NetInSpeed, ret.NetOutSpeed = netInSpeed, netOutSpeed
|
||||||
ret.Uptime = uint64(time.Since(cachedBootTime).Seconds())
|
ret.Uptime = uint64(time.Since(cachedBootTime).Seconds())
|
||||||
ret.TcpConnCount, ret.UdpConnCount = tcpConnCount, udpConnCount
|
ret.TcpConnCount, ret.UdpConnCount = getConns(skipConnectionCount)
|
||||||
|
|
||||||
return &ret
|
return &ret
|
||||||
}
|
}
|
||||||
@ -352,6 +315,42 @@ func getDiskTotalAndUsed() (total uint64, used uint64) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getConns(skipConnectionCount bool) (tcpConnCount, udpConnCount uint64) {
|
||||||
|
if !skipConnectionCount {
|
||||||
|
ss_err := true
|
||||||
|
if runtime.GOOS == "linux" {
|
||||||
|
tcpStat, err_tcp := goss.ConnectionsWithProtocol(goss.AF_INET, syscall.IPPROTO_TCP)
|
||||||
|
udpStat, err_udp := goss.ConnectionsWithProtocol(goss.AF_INET, syscall.IPPROTO_UDP)
|
||||||
|
if err_tcp == nil && err_udp == nil {
|
||||||
|
ss_err = false
|
||||||
|
tcpConnCount = uint64(len(tcpStat))
|
||||||
|
udpConnCount = uint64(len(udpStat))
|
||||||
|
}
|
||||||
|
if strings.Contains(CachedIP, ":") {
|
||||||
|
tcpStat6, err_tcp := goss.ConnectionsWithProtocol(goss.AF_INET6, syscall.IPPROTO_TCP)
|
||||||
|
udpStat6, err_udp := goss.ConnectionsWithProtocol(goss.AF_INET6, syscall.IPPROTO_UDP)
|
||||||
|
if err_tcp == nil && err_udp == nil {
|
||||||
|
ss_err = false
|
||||||
|
tcpConnCount += uint64(len(tcpStat6))
|
||||||
|
udpConnCount += uint64(len(udpStat6))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ss_err {
|
||||||
|
conns, _ := net.Connections("all")
|
||||||
|
for i := 0; i < len(conns); i++ {
|
||||||
|
switch conns[i].Type {
|
||||||
|
case syscall.SOCK_STREAM:
|
||||||
|
tcpConnCount++
|
||||||
|
case syscall.SOCK_DGRAM:
|
||||||
|
udpConnCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tcpConnCount, udpConnCount
|
||||||
|
}
|
||||||
|
|
||||||
func updateGPUStat(gpuStat *uint64) {
|
func updateGPUStat(gpuStat *uint64) {
|
||||||
if !atomic.CompareAndSwapInt32(&updateGPUStatus, 0, 1) {
|
if !atomic.CompareAndSwapInt32(&updateGPUStatus, 0, 1) {
|
||||||
return
|
return
|
||||||
|
@ -49,7 +49,6 @@ var (
|
|||||||
// "https://freegeoip.app/json/", // 需要 Key
|
// "https://freegeoip.app/json/", // 需要 Key
|
||||||
}
|
}
|
||||||
CachedIP, CachedCountry string
|
CachedIP, CachedCountry string
|
||||||
Sync = make(chan bool)
|
|
||||||
httpClientV4 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
|
httpClientV4 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
|
||||||
httpClientV6 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
|
httpClientV6 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
|
||||||
)
|
)
|
||||||
@ -58,16 +57,10 @@ var (
|
|||||||
func UpdateIP(useIPv6CountryCode bool, period uint32) {
|
func UpdateIP(useIPv6CountryCode bool, period uint32) {
|
||||||
for {
|
for {
|
||||||
util.Println(agentConfig.Debug, "正在更新本地缓存IP信息")
|
util.Println(agentConfig.Debug, "正在更新本地缓存IP信息")
|
||||||
var primaryIP, secondaryIP geoIP
|
ipv4 := fetchGeoIP(geoIPApiList, false)
|
||||||
if useIPv6CountryCode {
|
ipv6 := fetchGeoIP(geoIPApiList, true)
|
||||||
primaryIP = fetchGeoIP(geoIPApiList, true)
|
|
||||||
secondaryIP = fetchGeoIP(geoIPApiList, false)
|
|
||||||
} else {
|
|
||||||
primaryIP = fetchGeoIP(geoIPApiList, false)
|
|
||||||
secondaryIP = fetchGeoIP(geoIPApiList, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
if primaryIP.IP == "" && secondaryIP.IP == "" {
|
if ipv4.IP == "" && ipv6.IP == "" {
|
||||||
if period > 60 {
|
if period > 60 {
|
||||||
time.Sleep(time.Minute)
|
time.Sleep(time.Minute)
|
||||||
} else {
|
} else {
|
||||||
@ -75,21 +68,17 @@ func UpdateIP(useIPv6CountryCode bool, period uint32) {
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if primaryIP.IP == "" || secondaryIP.IP == "" {
|
if ipv4.IP == "" || ipv6.IP == "" {
|
||||||
CachedIP = fmt.Sprintf("%s%s", primaryIP.IP, secondaryIP.IP)
|
CachedIP = fmt.Sprintf("%s%s", ipv4.IP, ipv6.IP)
|
||||||
} else {
|
} else {
|
||||||
CachedIP = fmt.Sprintf("%s/%s", primaryIP.IP, secondaryIP.IP)
|
CachedIP = fmt.Sprintf("%s/%s", ipv4.IP, ipv6.IP)
|
||||||
}
|
}
|
||||||
|
|
||||||
if primaryIP.CountryCode != "" {
|
if ipv4.CountryCode != "" {
|
||||||
CachedCountry = primaryIP.CountryCode
|
CachedCountry = ipv4.CountryCode
|
||||||
} else if secondaryIP.CountryCode != "" {
|
|
||||||
CachedCountry = secondaryIP.CountryCode
|
|
||||||
}
|
}
|
||||||
|
if ipv6.CountryCode != "" && (useIPv6CountryCode || CachedCountry == "") {
|
||||||
select {
|
CachedCountry = ipv6.CountryCode
|
||||||
case Sync <- true:
|
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Second * time.Duration(period))
|
time.Sleep(time.Second * time.Duration(period))
|
||||||
|
@ -58,9 +58,8 @@ func VersionCheck() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return version >= 17763
|
return version >= 17763
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func DownloadDependency() {
|
func DownloadDependency() {
|
||||||
@ -131,11 +130,10 @@ func Start() (Pty, error) {
|
|||||||
if !isWin10 {
|
if !isWin10 {
|
||||||
tty, err := winpty.OpenDefault(path, shellPath)
|
tty, err := winpty.OpenDefault(path, shellPath)
|
||||||
return &winPTY{tty: tty}, err
|
return &winPTY{tty: tty}, err
|
||||||
} else {
|
}
|
||||||
tty, err := conpty.Start(shellPath, conpty.ConPtyWorkDir(path))
|
tty, err := conpty.Start(shellPath, conpty.ConPtyWorkDir(path))
|
||||||
return &conPty{tty: tty}, err
|
return &conPty{tty: tty}, err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (w *winPTY) Write(p []byte) (n int, err error) {
|
func (w *winPTY) Write(p []byte) (n int, err error) {
|
||||||
return w.tty.StdIn.Write(p)
|
return w.tty.StdIn.Write(p)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user