mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 11:56:53 +00:00
Db file change
This commit is contained in:
@@ -3,9 +3,24 @@ package tool
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
"github.com/shirou/gopsutil/net"
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ports []int
|
||||
var (
|
||||
ports []int
|
||||
ServerStatus []map[string]interface{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
ServerStatus = make([]map[string]interface{}, 0, 1500)
|
||||
go getSeverStatus()
|
||||
}
|
||||
|
||||
func InitAllowPort() {
|
||||
p := beego.AppConfig.String("allow_ports")
|
||||
@@ -28,3 +43,48 @@ func TestServerPort(p int, m string) (b bool) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getSeverStatus() {
|
||||
for {
|
||||
if len(ServerStatus) < 10 {
|
||||
time.Sleep(time.Second)
|
||||
} else {
|
||||
time.Sleep(time.Minute)
|
||||
}
|
||||
cpuPercet, _ := cpu.Percent(0, true)
|
||||
var cpuAll float64
|
||||
for _, v := range cpuPercet {
|
||||
cpuAll += v
|
||||
}
|
||||
m := make(map[string]interface{})
|
||||
loads, _ := load.Avg()
|
||||
m["load1"] = loads.Load1
|
||||
m["load5"] = loads.Load5
|
||||
m["load15"] = loads.Load15
|
||||
m["cpu"] = math.Round(cpuAll / float64(len(cpuPercet)))
|
||||
swap, _ := mem.SwapMemory()
|
||||
m["swap_mem"] = math.Round(swap.UsedPercent)
|
||||
vir, _ := mem.VirtualMemory()
|
||||
m["virtual_mem"] = math.Round(vir.UsedPercent)
|
||||
conn, _ := net.ProtoCounters(nil)
|
||||
io1, _ := net.IOCounters(false)
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
io2, _ := net.IOCounters(false)
|
||||
if len(io2) > 0 && len(io1) > 0 {
|
||||
m["io_send"] = (io2[0].BytesSent - io1[0].BytesSent) * 2
|
||||
m["io_recv"] = (io2[0].BytesRecv - io1[0].BytesRecv) * 2
|
||||
}
|
||||
t := time.Now()
|
||||
m["time"] = strconv.Itoa(t.Hour()) + ":" + strconv.Itoa(t.Minute()) + ":" + strconv.Itoa(t.Second())
|
||||
|
||||
for _, v := range conn {
|
||||
m[v.Protocol] = v.Stats["CurrEstab"]
|
||||
}
|
||||
if len(ServerStatus) >= 1440 {
|
||||
ServerStatus = ServerStatus[1:]
|
||||
}
|
||||
ServerStatus = append(ServerStatus, m)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user