Skip to content

Commit

Permalink
implements NetIOCounters() on FreeBSD.
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed May 18, 2014
1 parent b1d963d commit ea83f56
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Current Status
- disk_partitions (linux, freebsd, windows)
- disk_io_counters (linux)
- disk_usage (linux, freebsd, windows)
- net_io_counters (linux, windows)
- net_io_counters (linux, freebsd, windows)
- boot_time (linux, freebsd, windows(but little broken))
- users (linux, freebsd)
- pids (linux, freebsd)
Expand Down
38 changes: 36 additions & 2 deletions net_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,43 @@
package gopsutil

import (
"errors"
"os/exec"
"strings"
)

func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
return nil, errors.New("not implemented yet")
out, err := exec.Command("/usr/bin/netstat", "-ibdn").Output()
if err != nil {
return nil, err
}

lines := strings.Split(string(out), "\n")
ret := make([]NetIOCountersStat, 0, len(lines)-1)

for _, line := range lines {
values := strings.Fields(line)
if len(values) < 1 || values[0] == "Name" {
continue
}
base := 1
// sometimes Address is ommitted
if len(values) < 13 {
base = 0
}

n := NetIOCountersStat{
Name: values[0],
PacketsRecv: mustParseUint64(values[base+3]),
Errin: mustParseUint64(values[base+4]),
Dropin: mustParseUint64(values[base+5]),
BytesRecv: mustParseUint64(values[base+6]),
PacketsSent: mustParseUint64(values[base+7]),
Errout: mustParseUint64(values[base+8]),
BytesSent: mustParseUint64(values[base+9]),
Dropout: mustParseUint64(values[base+11]),
}
ret = append(ret, n)
}

return ret, nil
}

0 comments on commit ea83f56

Please sign in to comment.