xmtop/src/widgets/net.go

104 lines
2.4 KiB
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package widgets
import (
"fmt"
"log"
2018-02-19 15:25:02 +08:00
"time"
2019-01-01 08:55:50 +08:00
ui "github.com/cjbassi/gotop/src/termui"
2018-05-11 12:23:20 +08:00
"github.com/cjbassi/gotop/src/utils"
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,
}
2019-01-01 08:55:50 +08:00
self.Title = " 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() {
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() {
interfaces, err := psNet.IOCounters(true)
if err != nil {
log.Printf("failed to get network activity from gopsutil: %v", err)
2018-12-14 13:59:45 +08:00
return
}
var curRecvTotal uint64
var curSentTotal uint64
for _, _interface := range interfaces {
// ignore VPN interface
if _interface.Name != "tun0" {
curRecvTotal += _interface.BytesRecv
curSentTotal += _interface.BytesSent
}
}
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
recvRecent = curRecvTotal - self.prevRecvTotal
sentRecent = curSentTotal - self.prevSentTotal
2018-02-19 15:25:02 +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-05-22 07:02:31 +08:00
// render widget titles
for i := 0; i < 2; i++ {
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-19 15:25:02 +08:00
}