Stats.go 2.6 KB

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