Created
March 11, 2018 04:50
-
-
Save shirou/97cdcb2175215d64751490532d8e3299 to your computer and use it in GitHub Desktop.
gopsutil netcounter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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