Skip to content

Commit b75a5a6

Browse files
committed
refactor: remove traffic deque
1 parent 2bba354 commit b75a5a6

File tree

4 files changed

+45
-138
lines changed

4 files changed

+45
-138
lines changed

cmd/client/client.go

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
jsoniter "github.com/json-iterator/go"
88
"log"
99
"net"
10-
"os"
1110
"strconv"
1211
"strings"
1312
"time"
@@ -25,22 +24,22 @@ var (
2524

2625
var json = jsoniter.ConfigCompatibleWithStandardLibrary
2726

28-
type serverStatus struct {
29-
Uptime uint64 `json:"uptime"`
27+
type ServerStatus struct {
28+
Uptime uint64 `json:"uptime"`
3029
Load jsoniter.Number `json:"load"`
31-
MemoryTotal uint64 `json:"memory_total"`
32-
MemoryUsed uint64 `json:"memory_used"`
33-
SwapTotal uint64 `json:"swap_total"`
34-
SwapUsed uint64 `json:"swap_used"`
35-
HddTotal uint64 `json:"hdd_total"`
36-
HddUsed uint64 `json:"hdd_used"`
30+
MemoryTotal uint64 `json:"memory_total"`
31+
MemoryUsed uint64 `json:"memory_used"`
32+
SwapTotal uint64 `json:"swap_total"`
33+
SwapUsed uint64 `json:"swap_used"`
34+
HddTotal uint64 `json:"hdd_total"`
35+
HddUsed uint64 `json:"hdd_used"`
3736
CPU jsoniter.Number `json:"cpu"`
38-
NetworkTx uint64 `json:"network_tx"`
39-
NetworkRx uint64 `json:"network_rx"`
40-
NetworkIn uint64 `json:"network_in"`
41-
NetworkOut uint64 `json:"network_out"`
42-
Online4 bool `json:"online4"`
43-
Online6 bool `json:"online6"`
37+
NetworkTx uint64 `json:"network_tx"`
38+
NetworkRx uint64 `json:"network_rx"`
39+
NetworkIn uint64 `json:"network_in"`
40+
NetworkOut uint64 `json:"network_out"`
41+
Online4 bool `json:"online4"`
42+
Online6 bool `json:"online6"`
4443
}
4544

4645
func connect() {
@@ -86,22 +85,21 @@ func connect() {
8685
} else {
8786
return
8887
}
89-
item := &serverStatus{}
90-
traffic := status.NewNetwork()
88+
item := ServerStatus{}
9189
for {
92-
CPU := status.Cpu(INTERVAL)
93-
netRx, netTx := traffic.Speed()
94-
var netIn, netOut uint64
90+
CPU := status.Cpu(*INTERVAL)
91+
var netIn, netOut, netRx, netTx uint64
9592
if !*isVnstat {
96-
netIn, netOut = traffic.Traffic()
93+
netIn, netOut, netRx, netTx = status.Traffic(*INTERVAL)
9794
} else {
95+
_, _, netRx, netTx = status.Traffic(*INTERVAL)
9896
netIn, netOut, err = status.TrafficVnstat()
9997
if err != nil {
10098
log.Println("Please check if the installation of vnStat is correct")
10199
}
102100
}
103101
memoryTotal, memoryUsed, swapTotal, swapUsed := status.Memory()
104-
hddTotal, hddUsed := status.Disk(INTERVAL)
102+
hddTotal, hddUsed := status.Disk(*INTERVAL)
105103
uptime := status.Uptime()
106104
load := status.Load()
107105
item.CPU = jsoniter.Number(fmt.Sprintf("%.1f", CPU))
@@ -125,7 +123,7 @@ func connect() {
125123
}
126124
timer = 150.0
127125
}
128-
timer -= 1 * *INTERVAL
126+
timer -= *INTERVAL
129127
data, _ := json.Marshal(item)
130128
_, err = conn.Write(status.StringToBytes("update " + status.BytesToString(data) + "\n"))
131129
if err != nil {
@@ -149,12 +147,10 @@ func main() {
149147
}
150148
}
151149
if *PORT < 1 || *PORT > 65535 {
152-
log.Println("Check the port you input")
153-
os.Exit(1)
150+
log.Fatal("Check the port you input")
154151
}
155152
if *SERVER == "" || *USER == "" || *PASSWORD == "" {
156-
log.Println("HOST, USERNAME, PASSWORD must not be blank!")
157-
os.Exit(1)
153+
log.Fatal("HOST, USERNAME, PASSWORD must not be blank!")
158154
}
159155
for {
160156
connect()

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
1919
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2020
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
2121
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
22-
github.com/shirou/gopsutil/v3 v3.22.3 h1:UebRzEomgMpv61e3hgD1tGooqX5trFbdU/ehphbHd00=
23-
github.com/shirou/gopsutil/v3 v3.22.3/go.mod h1:D01hZJ4pVHPpCTZ3m3T2+wDF2YAGfd+H4ifUguaQzHM=
2422
github.com/shirou/gopsutil/v3 v3.22.4 h1:srAQaiX6jX/cYL6q29aE0m8lOskT9CurZ9N61YR3yoI=
2523
github.com/shirou/gopsutil/v3 v3.22.4/go.mod h1:D01hZJ4pVHPpCTZ3m3T2+wDF2YAGfd+H4ifUguaQzHM=
2624
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

pkg/status/deque.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

pkg/status/status.go

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/shirou/gopsutil/v3/disk"
66
"github.com/shirou/gopsutil/v3/host"
77
"github.com/shirou/gopsutil/v3/load"
8-
psutilNet "github.com/shirou/gopsutil/v3/net"
8+
pNet "github.com/shirou/gopsutil/v3/net"
99
"math"
1010
"net"
1111
"os/exec"
@@ -16,19 +16,8 @@ import (
1616

1717
var cachedFs = make(map[string]struct{})
1818
var timer = 0.0
19-
20-
type network struct {
21-
rx *deque
22-
tx *deque
23-
}
24-
25-
func NewNetwork() *network {
26-
instance := &network{
27-
newDeque(10),
28-
newDeque(10),
29-
}
30-
return instance
31-
}
19+
var prevNetIn uint64
20+
var prevNetOut uint64
3221

3322
func Uptime() uint64 {
3423
bootTime, _ := host.BootTime()
@@ -40,22 +29,23 @@ func Load() float64 {
4029
return theLoad.Load1
4130
}
4231

43-
func Disk(INTERVAL *float64) (uint64, uint64) {
32+
func Disk(INTERVAL float64) (uint64, uint64) {
4433
var (
4534
size, used uint64
4635
)
4736
if timer <= 0 {
4837
diskList, _ := disk.Partitions(false)
49-
devices := make(map[string]bool)
38+
devices := make(map[string]struct{})
5039
for _, d := range diskList {
51-
if !devices[d.Device] && checkValidFs(d.Fstype) {
40+
_, ok := devices[d.Device]
41+
if !ok && checkValidFs(d.Fstype) {
5242
cachedFs[d.Mountpoint] = struct{}{}
53-
devices[d.Device] = true
43+
devices[d.Device] = struct{}{}
5444
}
5545
}
5646
timer = 300.0
5747
}
58-
timer -= *INTERVAL
48+
timer -= INTERVAL
5949
for k := range cachedFs {
6050
usage, err := disk.Usage(k)
6151
if err != nil {
@@ -68,8 +58,8 @@ func Disk(INTERVAL *float64) (uint64, uint64) {
6858
return size, used
6959
}
7060

71-
func Cpu(INTERVAL *float64) float64 {
72-
cpuInfo, _ := cpu.Percent(time.Duration(*INTERVAL*float64(time.Second)), false)
61+
func Cpu(INTERVAL float64) float64 {
62+
cpuInfo, _ := cpu.Percent(time.Duration(INTERVAL*float64(time.Second)), false)
7363
return math.Round(cpuInfo[0]*10) / 10
7464
}
7565

@@ -86,30 +76,28 @@ func Network(checkIP int) bool {
8676
if err != nil {
8777
return false
8878
}
89-
err = conn.Close()
90-
if err != nil {
79+
if conn.Close() != nil {
9180
return false
9281
}
9382
return true
9483
}
9584

96-
func (net *network) getTraffic() {
85+
func Traffic(INTERVAL float64) (uint64, uint64, uint64, uint64) {
9786
var (
9887
netIn, netOut uint64
9988
)
100-
netInfo, _ := psutilNet.IOCounters(true)
89+
netInfo, _ := pNet.IOCounters(true)
10190
for _, v := range netInfo {
10291
if checkInterface(v.Name) {
10392
netIn += v.BytesRecv
10493
netOut += v.BytesSent
10594
}
10695
}
107-
net.rx.push(netIn)
108-
net.tx.push(netOut)
109-
}
110-
111-
func (net *network) Traffic() (uint64, uint64) {
112-
return net.rx.tail.value, net.tx.tail.value
96+
rx := uint64(float64(netIn-prevNetIn) / INTERVAL)
97+
tx := uint64(float64(netOut-prevNetOut) / INTERVAL)
98+
prevNetIn = netIn
99+
prevNetOut = netOut
100+
return netIn, netOut, rx, tx
113101
}
114102

115103
func TrafficVnstat() (uint64, uint64, error) {
@@ -122,18 +110,13 @@ func TrafficVnstat() (uint64, uint64, error) {
122110
// Not enough data available yet.
123111
return 0, 0, nil
124112
}
125-
rx, err := strconv.ParseUint(vData[8], 10, 64)
113+
netIn, err := strconv.ParseUint(vData[8], 10, 64)
126114
if err != nil {
127115
return 0, 0, err
128116
}
129-
tx, err := strconv.ParseUint(vData[9], 10, 64)
117+
netOut, err := strconv.ParseUint(vData[9], 10, 64)
130118
if err != nil {
131119
return 0, 0, err
132120
}
133-
return rx, tx, nil
134-
}
135-
136-
func (net *network) Speed() (uint64, uint64) {
137-
net.getTraffic()
138-
return uint64(net.rx.avg()), uint64(net.tx.avg())
121+
return netIn, netOut, nil
139122
}

0 commit comments

Comments
 (0)