5
5
"github.com/shirou/gopsutil/v3/disk"
6
6
"github.com/shirou/gopsutil/v3/host"
7
7
"github.com/shirou/gopsutil/v3/load"
8
- psutilNet "github.com/shirou/gopsutil/v3/net"
8
+ pNet "github.com/shirou/gopsutil/v3/net"
9
9
"math"
10
10
"net"
11
11
"os/exec"
@@ -16,19 +16,8 @@ import (
16
16
17
17
var cachedFs = make (map [string ]struct {})
18
18
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
32
21
33
22
func Uptime () uint64 {
34
23
bootTime , _ := host .BootTime ()
@@ -40,22 +29,23 @@ func Load() float64 {
40
29
return theLoad .Load1
41
30
}
42
31
43
- func Disk (INTERVAL * float64 ) (uint64 , uint64 ) {
32
+ func Disk (INTERVAL float64 ) (uint64 , uint64 ) {
44
33
var (
45
34
size , used uint64
46
35
)
47
36
if timer <= 0 {
48
37
diskList , _ := disk .Partitions (false )
49
- devices := make (map [string ]bool )
38
+ devices := make (map [string ]struct {} )
50
39
for _ , d := range diskList {
51
- if ! devices [d .Device ] && checkValidFs (d .Fstype ) {
40
+ _ , ok := devices [d .Device ]
41
+ if ! ok && checkValidFs (d .Fstype ) {
52
42
cachedFs [d .Mountpoint ] = struct {}{}
53
- devices [d .Device ] = true
43
+ devices [d .Device ] = struct {}{}
54
44
}
55
45
}
56
46
timer = 300.0
57
47
}
58
- timer -= * INTERVAL
48
+ timer -= INTERVAL
59
49
for k := range cachedFs {
60
50
usage , err := disk .Usage (k )
61
51
if err != nil {
@@ -68,8 +58,8 @@ func Disk(INTERVAL *float64) (uint64, uint64) {
68
58
return size , used
69
59
}
70
60
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 )
73
63
return math .Round (cpuInfo [0 ]* 10 ) / 10
74
64
}
75
65
@@ -86,30 +76,28 @@ func Network(checkIP int) bool {
86
76
if err != nil {
87
77
return false
88
78
}
89
- err = conn .Close ()
90
- if err != nil {
79
+ if conn .Close () != nil {
91
80
return false
92
81
}
93
82
return true
94
83
}
95
84
96
- func ( net * network ) getTraffic ( ) {
85
+ func Traffic ( INTERVAL float64 ) ( uint64 , uint64 , uint64 , uint64 ) {
97
86
var (
98
87
netIn , netOut uint64
99
88
)
100
- netInfo , _ := psutilNet .IOCounters (true )
89
+ netInfo , _ := pNet .IOCounters (true )
101
90
for _ , v := range netInfo {
102
91
if checkInterface (v .Name ) {
103
92
netIn += v .BytesRecv
104
93
netOut += v .BytesSent
105
94
}
106
95
}
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
113
101
}
114
102
115
103
func TrafficVnstat () (uint64 , uint64 , error ) {
@@ -122,18 +110,13 @@ func TrafficVnstat() (uint64, uint64, error) {
122
110
// Not enough data available yet.
123
111
return 0 , 0 , nil
124
112
}
125
- rx , err := strconv .ParseUint (vData [8 ], 10 , 64 )
113
+ netIn , err := strconv .ParseUint (vData [8 ], 10 , 64 )
126
114
if err != nil {
127
115
return 0 , 0 , err
128
116
}
129
- tx , err := strconv .ParseUint (vData [9 ], 10 , 64 )
117
+ netOut , err := strconv .ParseUint (vData [9 ], 10 , 64 )
130
118
if err != nil {
131
119
return 0 , 0 , err
132
120
}
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
139
122
}
0 commit comments