2018-02-19 15:25:02 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-02-20 07:02:01 +08:00
|
|
|
"fmt"
|
2018-12-05 13:44:25 +08:00
|
|
|
"log"
|
2018-02-19 15:25:02 +08:00
|
|
|
"time"
|
|
|
|
|
2018-05-11 12:23:20 +08:00
|
|
|
"github.com/cjbassi/gotop/src/utils"
|
2018-03-30 06:48:43 +08:00
|
|
|
ui "github.com/cjbassi/termui"
|
2018-03-04 09:05:52 +08:00
|
|
|
psNet "github.com/shirou/gopsutil/net"
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Net struct {
|
|
|
|
*ui.Sparklines
|
2018-02-23 16:42:39 +08:00
|
|
|
interval time.Duration
|
|
|
|
// used to calculate recent network activity
|
2018-04-11 12:34:11 +08:00
|
|
|
prevRecvTotal uint64
|
|
|
|
prevSentTotal uint64
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNet() *Net {
|
|
|
|
recv := ui.NewSparkline()
|
|
|
|
recv.Data = []int{0}
|
|
|
|
|
|
|
|
sent := ui.NewSparkline()
|
|
|
|
sent.Data = []int{0}
|
|
|
|
|
|
|
|
spark := ui.NewSparklines(recv, sent)
|
2018-03-28 05:27:23 +08:00
|
|
|
self := &Net{
|
2018-03-04 09:05:52 +08:00
|
|
|
Sparklines: spark,
|
|
|
|
interval: time.Second,
|
|
|
|
}
|
2018-03-28 05:27:23 +08:00
|
|
|
self.Label = "Network Usage"
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-04-13 11:00:34 +08:00
|
|
|
self.update()
|
|
|
|
|
2018-02-19 15:25:02 +08:00
|
|
|
go func() {
|
2018-11-30 10:17:13 +08:00
|
|
|
ticker := time.NewTicker(self.interval)
|
2018-02-19 15:25:02 +08:00
|
|
|
for range ticker.C {
|
2018-03-28 05:27:23 +08:00
|
|
|
self.update()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-03-28 05:27:23 +08:00
|
|
|
return self
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-28 05:27:23 +08:00
|
|
|
func (self *Net) update() {
|
2018-12-11 06:41:35 +08:00
|
|
|
interfaces, err := psNet.IOCounters(true)
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to get network activity from gopsutil: %v", err)
|
|
|
|
}
|
2018-12-11 06:41:35 +08:00
|
|
|
var curRecvTotal uint64
|
|
|
|
var curSentTotal uint64
|
|
|
|
for _, _interface := range interfaces {
|
|
|
|
// ignore VPN interface
|
|
|
|
if _interface.Name != "tun0" {
|
|
|
|
curRecvTotal += _interface.BytesRecv
|
|
|
|
curSentTotal += _interface.BytesSent
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
var recvRecent uint64
|
|
|
|
var sentRecent uint64
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-04-11 12:34:11 +08:00
|
|
|
if self.prevRecvTotal != 0 { // if this isn't the first update
|
2018-08-24 02:43:21 +08:00
|
|
|
recvRecent = curRecvTotal - self.prevRecvTotal
|
|
|
|
sentRecent = curSentTotal - self.prevSentTotal
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-12-05 13:44:25 +08:00
|
|
|
if int(recvRecent) < 0 {
|
|
|
|
log.Printf("error: negative value for recently received network data from gopsutil. recvRecent: %v", recvRecent)
|
|
|
|
// recover from error
|
|
|
|
recvRecent = 0
|
|
|
|
}
|
|
|
|
if int(sentRecent) < 0 {
|
|
|
|
log.Printf("error: negative value for recently sent network data from gopsutil. sentRecent: %v", sentRecent)
|
|
|
|
// recover from error
|
|
|
|
sentRecent = 0
|
|
|
|
}
|
|
|
|
|
2018-03-28 05:27:23 +08:00
|
|
|
self.Lines[0].Data = append(self.Lines[0].Data, int(recvRecent))
|
|
|
|
self.Lines[1].Data = append(self.Lines[1].Data, int(sentRecent))
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
// used in later calls to update
|
2018-04-11 12:34:11 +08:00
|
|
|
self.prevRecvTotal = curRecvTotal
|
|
|
|
self.prevSentTotal = curSentTotal
|
2018-02-20 07:02:01 +08:00
|
|
|
|
2018-05-22 07:02:31 +08:00
|
|
|
// render widget titles
|
2018-02-20 07:02:01 +08:00
|
|
|
for i := 0; i < 2; i++ {
|
2018-08-24 02:43:21 +08:00
|
|
|
total, label, recent := func() (uint64, string, uint64) {
|
|
|
|
if i == 0 {
|
|
|
|
return curRecvTotal, "RX", recvRecent
|
|
|
|
}
|
|
|
|
return curSentTotal, "Tx", sentRecent
|
|
|
|
}()
|
|
|
|
|
|
|
|
recentConv, unitRecent := utils.ConvertBytes(uint64(recent))
|
|
|
|
totalConv, unitTotal := utils.ConvertBytes(uint64(total))
|
|
|
|
|
|
|
|
self.Lines[i].Title1 = fmt.Sprintf(" Total %s: %5.1f %s", label, totalConv, unitTotal)
|
|
|
|
self.Lines[i].Title2 = fmt.Sprintf(" %s/s: %9.1f %2s/s", label, recentConv, unitRecent)
|
2018-02-20 07:02:01 +08:00
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|