Skip to content

Commit

Permalink
refactor: remove traffic deque
Browse files Browse the repository at this point in the history
  • Loading branch information
cokemine committed May 12, 2022
1 parent 2bba354 commit b75a5a6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 138 deletions.
50 changes: 23 additions & 27 deletions cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
jsoniter "github.com/json-iterator/go"
"log"
"net"
"os"
"strconv"
"strings"
"time"
Expand All @@ -25,22 +24,22 @@ var (

var json = jsoniter.ConfigCompatibleWithStandardLibrary

type serverStatus struct {
Uptime uint64 `json:"uptime"`
type ServerStatus struct {
Uptime uint64 `json:"uptime"`
Load jsoniter.Number `json:"load"`
MemoryTotal uint64 `json:"memory_total"`
MemoryUsed uint64 `json:"memory_used"`
SwapTotal uint64 `json:"swap_total"`
SwapUsed uint64 `json:"swap_used"`
HddTotal uint64 `json:"hdd_total"`
HddUsed uint64 `json:"hdd_used"`
MemoryTotal uint64 `json:"memory_total"`
MemoryUsed uint64 `json:"memory_used"`
SwapTotal uint64 `json:"swap_total"`
SwapUsed uint64 `json:"swap_used"`
HddTotal uint64 `json:"hdd_total"`
HddUsed uint64 `json:"hdd_used"`
CPU jsoniter.Number `json:"cpu"`
NetworkTx uint64 `json:"network_tx"`
NetworkRx uint64 `json:"network_rx"`
NetworkIn uint64 `json:"network_in"`
NetworkOut uint64 `json:"network_out"`
Online4 bool `json:"online4"`
Online6 bool `json:"online6"`
NetworkTx uint64 `json:"network_tx"`
NetworkRx uint64 `json:"network_rx"`
NetworkIn uint64 `json:"network_in"`
NetworkOut uint64 `json:"network_out"`
Online4 bool `json:"online4"`
Online6 bool `json:"online6"`
}

func connect() {
Expand Down Expand Up @@ -86,22 +85,21 @@ func connect() {
} else {
return
}
item := &serverStatus{}
traffic := status.NewNetwork()
item := ServerStatus{}
for {
CPU := status.Cpu(INTERVAL)
netRx, netTx := traffic.Speed()
var netIn, netOut uint64
CPU := status.Cpu(*INTERVAL)
var netIn, netOut, netRx, netTx uint64
if !*isVnstat {
netIn, netOut = traffic.Traffic()
netIn, netOut, netRx, netTx = status.Traffic(*INTERVAL)
} else {
_, _, netRx, netTx = status.Traffic(*INTERVAL)
netIn, netOut, err = status.TrafficVnstat()
if err != nil {
log.Println("Please check if the installation of vnStat is correct")
}
}
memoryTotal, memoryUsed, swapTotal, swapUsed := status.Memory()
hddTotal, hddUsed := status.Disk(INTERVAL)
hddTotal, hddUsed := status.Disk(*INTERVAL)
uptime := status.Uptime()
load := status.Load()
item.CPU = jsoniter.Number(fmt.Sprintf("%.1f", CPU))
Expand All @@ -125,7 +123,7 @@ func connect() {
}
timer = 150.0
}
timer -= 1 * *INTERVAL
timer -= *INTERVAL
data, _ := json.Marshal(item)
_, err = conn.Write(status.StringToBytes("update " + status.BytesToString(data) + "\n"))
if err != nil {
Expand All @@ -149,12 +147,10 @@ func main() {
}
}
if *PORT < 1 || *PORT > 65535 {
log.Println("Check the port you input")
os.Exit(1)
log.Fatal("Check the port you input")
}
if *SERVER == "" || *USER == "" || *PASSWORD == "" {
log.Println("HOST, USERNAME, PASSWORD must not be blank!")
os.Exit(1)
log.Fatal("HOST, USERNAME, PASSWORD must not be blank!")
}
for {
connect()
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/shirou/gopsutil/v3 v3.22.3 h1:UebRzEomgMpv61e3hgD1tGooqX5trFbdU/ehphbHd00=
github.com/shirou/gopsutil/v3 v3.22.3/go.mod h1:D01hZJ4pVHPpCTZ3m3T2+wDF2YAGfd+H4ifUguaQzHM=
github.com/shirou/gopsutil/v3 v3.22.4 h1:srAQaiX6jX/cYL6q29aE0m8lOskT9CurZ9N61YR3yoI=
github.com/shirou/gopsutil/v3 v3.22.4/go.mod h1:D01hZJ4pVHPpCTZ3m3T2+wDF2YAGfd+H4ifUguaQzHM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
70 changes: 0 additions & 70 deletions pkg/status/deque.go

This file was deleted.

61 changes: 22 additions & 39 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/load"
psutilNet "github.com/shirou/gopsutil/v3/net"
pNet "github.com/shirou/gopsutil/v3/net"
"math"
"net"
"os/exec"
Expand All @@ -16,19 +16,8 @@ import (

var cachedFs = make(map[string]struct{})
var timer = 0.0

type network struct {
rx *deque
tx *deque
}

func NewNetwork() *network {
instance := &network{
newDeque(10),
newDeque(10),
}
return instance
}
var prevNetIn uint64
var prevNetOut uint64

func Uptime() uint64 {
bootTime, _ := host.BootTime()
Expand All @@ -40,22 +29,23 @@ func Load() float64 {
return theLoad.Load1
}

func Disk(INTERVAL *float64) (uint64, uint64) {
func Disk(INTERVAL float64) (uint64, uint64) {
var (
size, used uint64
)
if timer <= 0 {
diskList, _ := disk.Partitions(false)
devices := make(map[string]bool)
devices := make(map[string]struct{})
for _, d := range diskList {
if !devices[d.Device] && checkValidFs(d.Fstype) {
_, ok := devices[d.Device]
if !ok && checkValidFs(d.Fstype) {
cachedFs[d.Mountpoint] = struct{}{}
devices[d.Device] = true
devices[d.Device] = struct{}{}
}
}
timer = 300.0
}
timer -= *INTERVAL
timer -= INTERVAL
for k := range cachedFs {
usage, err := disk.Usage(k)
if err != nil {
Expand All @@ -68,8 +58,8 @@ func Disk(INTERVAL *float64) (uint64, uint64) {
return size, used
}

func Cpu(INTERVAL *float64) float64 {
cpuInfo, _ := cpu.Percent(time.Duration(*INTERVAL*float64(time.Second)), false)
func Cpu(INTERVAL float64) float64 {
cpuInfo, _ := cpu.Percent(time.Duration(INTERVAL*float64(time.Second)), false)
return math.Round(cpuInfo[0]*10) / 10
}

Expand All @@ -86,30 +76,28 @@ func Network(checkIP int) bool {
if err != nil {
return false
}
err = conn.Close()
if err != nil {
if conn.Close() != nil {
return false
}
return true
}

func (net *network) getTraffic() {
func Traffic(INTERVAL float64) (uint64, uint64, uint64, uint64) {
var (
netIn, netOut uint64
)
netInfo, _ := psutilNet.IOCounters(true)
netInfo, _ := pNet.IOCounters(true)
for _, v := range netInfo {
if checkInterface(v.Name) {
netIn += v.BytesRecv
netOut += v.BytesSent
}
}
net.rx.push(netIn)
net.tx.push(netOut)
}

func (net *network) Traffic() (uint64, uint64) {
return net.rx.tail.value, net.tx.tail.value
rx := uint64(float64(netIn-prevNetIn) / INTERVAL)
tx := uint64(float64(netOut-prevNetOut) / INTERVAL)
prevNetIn = netIn
prevNetOut = netOut
return netIn, netOut, rx, tx
}

func TrafficVnstat() (uint64, uint64, error) {
Expand All @@ -122,18 +110,13 @@ func TrafficVnstat() (uint64, uint64, error) {
// Not enough data available yet.
return 0, 0, nil
}
rx, err := strconv.ParseUint(vData[8], 10, 64)
netIn, err := strconv.ParseUint(vData[8], 10, 64)
if err != nil {
return 0, 0, err
}
tx, err := strconv.ParseUint(vData[9], 10, 64)
netOut, err := strconv.ParseUint(vData[9], 10, 64)
if err != nil {
return 0, 0, err
}
return rx, tx, nil
}

func (net *network) Speed() (uint64, uint64) {
net.getTraffic()
return uint64(net.rx.avg()), uint64(net.tx.avg())
return netIn, netOut, nil
}

0 comments on commit b75a5a6

Please sign in to comment.