Skip to content

Instantly share code, notes, and snippets.

@shirou
Created March 11, 2018 04:50
Show Gist options
  • Save shirou/97cdcb2175215d64751490532d8e3299 to your computer and use it in GitHub Desktop.
Save shirou/97cdcb2175215d64751490532d8e3299 to your computer and use it in GitHub Desktop.
gopsutil netcounter
package main
import (
"fmt"
"time"
"github.com/shirou/gopsutil/net"
)
func run() error {
nic_name := "enp0s3"
iocounters, err := net.IOCounters(true)
if err != nil {
fmt.Println(err)
return nil
}
now := time.Now().UTC()
for _, iocounter := range iocounters {
if iocounter.Name == nic_name {
fmt.Printf("%s,%d,%d,%d,%d\n", now.Format(time.RFC3339),
iocounter.BytesRecv,
iocounter.BytesSent,
iocounter.PacketsRecv,
iocounter.PacketsSent,
)
}
}
return nil
}
func main() {
fmt.Println("timestamp,brecv,bsent,precv,psent")
for {
run()
time.Sleep(time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment