-
Notifications
You must be signed in to change notification settings - Fork 313
/
Stats.go
87 lines (81 loc) · 2.55 KB
/
Stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
netstat "github.com/shirou/gopsutil/net"
"math"
URL "net/url"
"strings"
"time"
)
func showStat() {
initialNetCounter, _ := netstat.IOCounters(true)
iplist := ""
if customIP != nil && len(customIP) > 0 {
iplist = customIP.String()
} else {
u, _ := URL.Parse(*url)
iplist = strings.Join(nslookup(u.Hostname(), "8.8.8.8"), ",")
}
for true {
percent, _ := cpu.Percent(time.Second, false)
memStat, _ := mem.VirtualMemory()
netCounter, _ := netstat.IOCounters(true)
loadStat, _ := load.Avg()
fmt.Fprintf(TerminalWriter, "URL:%s\n", TargetUrl)
fmt.Fprintf(TerminalWriter, "IP:%s\n", iplist)
fmt.Fprintf(TerminalWriter, "CPU:%.3f%% \n", percent)
fmt.Fprintf(TerminalWriter, "Memory:%.3f%% \n", memStat.UsedPercent)
fmt.Fprintf(TerminalWriter, "Load:%.3f %.3f %.3f\n", loadStat.Load1, loadStat.Load5, loadStat.Load15)
for i := 0; i < len(netCounter); i++ {
if netCounter[i].BytesRecv == 0 && netCounter[i].BytesSent == 0 {
continue
}
RecvBytes := float64(netCounter[i].BytesRecv - initialNetCounter[i].BytesRecv)
SendBytes := float64(netCounter[i].BytesSent - initialNetCounter[i].BytesSent)
//if RecvBytes > 1000 {
// SpeedIndex++
// pair := speedPair{
// index: SpeedIndex,
// speed: RecvBytes,
// }
// SpeedQueue.PushBack(pair)
// if SpeedQueue.Len() > 60 {
// SpeedQueue.Remove(SpeedQueue.Front())
// }
// var x []float64
// var y []float64
// x = make([]float64, 60)
// y = make([]float64, 60)
// var point = 0
// for item := SpeedQueue.Front(); item != nil; item = item.Next() {
// spdPair := item.Value.(speedPair)
// x[point] = float64(spdPair.index)
// y[point] = spdPair.speed
// point++
// }
// _, b := LeastSquares(x, y)
// log.Printf("Speed Vertical:%.3f\n", b)
//}
fmt.Fprintf(TerminalWriter, "Nic:%v,Recv %s(%s/s),Send %s(%s/s)\n", netCounter[i].Name,
readableBytes(float64(netCounter[i].BytesRecv)),
readableBytes(RecvBytes),
readableBytes(float64(netCounter[i].BytesSent)),
readableBytes(SendBytes))
}
initialNetCounter = netCounter
TerminalWriter.Clear()
TerminalWriter.Print()
time.Sleep(1 * time.Millisecond)
}
}
func readableBytes(bytes float64) (expression string) {
if bytes == 0 {
return "0B"
}
var i = math.Floor(math.Log(bytes) / math.Log(1024))
var sizes = []string{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
return fmt.Sprintf("%.3f%s", bytes/math.Pow(1024, i), sizes[int(i)])
}