From 6c590817104a8780665645d982808c27e0a757c0 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Mon, 28 Apr 2014 10:16:38 +0900 Subject: [PATCH] implements net_io_counters on linux. --- net.go | 1 + net_linux.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 net_linux.go diff --git a/net.go b/net.go index ca7f9ee..4ee40c4 100644 --- a/net.go +++ b/net.go @@ -1,6 +1,7 @@ package gopsutil type Net_io_countersStat struct { + Name string `json:"name""` // interface name Bytes_sent uint64 `json:"bytes_sent""` // number of bytes sent Bytes_recv uint64 `json:"bytes_recv"` // number of bytes received Packets_sent uint64 `json:"packets_sent"` // number of packets sent diff --git a/net_linux.go b/net_linux.go new file mode 100644 index 0000000..5faed61 --- /dev/null +++ b/net_linux.go @@ -0,0 +1,38 @@ +// +build linux + +package gopsutil + +import ( + "strings" +) + +func Net_io_counters() ([]Net_io_countersStat, error) { + filename := "/proc/net/dev" + lines, err := ReadLines(filename) + if err != nil{ + return make([]Net_io_countersStat, 0), err + } + + statlen := len(lines) - 1 + + ret := make([]Net_io_countersStat, 0, statlen) + + for _, line := range lines[2:] { + fields := strings.Fields(line) + if fields[0] == ""{ + continue + } + nic := Net_io_countersStat{ + Name: strings.Trim(fields[0], ":"), + Bytes_recv: parseUint64(fields[1]), + Errin: parseUint64(fields[2]), + Dropin: parseUint64(fields[3]), + Bytes_sent: parseUint64(fields[9]), + Packets_sent: parseUint64(fields[10]), + Errout: parseUint64(fields[11]), + Dropout: parseUint64(fields[12]), + } + ret = append(ret, nic) + } + return ret, nil +}