2014-05-12 11:03:20 +08:00
|
|
|
// +build freebsd
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package net
|
2014-05-12 11:03:20 +08:00
|
|
|
|
|
|
|
import (
|
2014-05-18 22:23:47 +08:00
|
|
|
"os/exec"
|
2014-09-19 23:15:35 +08:00
|
|
|
"strconv"
|
2014-05-18 22:23:47 +08:00
|
|
|
"strings"
|
2014-05-12 11:03:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
|
2014-05-18 22:23:47 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-19 23:15:35 +08:00
|
|
|
parsed := make([]uint64, 0, 8)
|
|
|
|
vv := []string{
|
|
|
|
values[base+3], // PacketsRecv
|
|
|
|
values[base+4], // Errin
|
|
|
|
values[base+5], // Dropin
|
|
|
|
values[base+6], // BytesRecvn
|
|
|
|
values[base+7], // PacketSent
|
|
|
|
values[base+8], // Errout
|
|
|
|
values[base+9], // BytesSent
|
|
|
|
values[base+11], // Dropout
|
|
|
|
}
|
|
|
|
for _, target := range vv {
|
|
|
|
if target == "-" {
|
|
|
|
parsed = append(parsed, 0)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := strconv.ParseUint(target, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parsed = append(parsed, t)
|
|
|
|
}
|
|
|
|
|
2014-05-18 22:23:47 +08:00
|
|
|
n := NetIOCountersStat{
|
|
|
|
Name: values[0],
|
2014-09-19 23:15:35 +08:00
|
|
|
PacketsRecv: parsed[0],
|
|
|
|
Errin: parsed[1],
|
|
|
|
Dropin: parsed[2],
|
|
|
|
BytesRecv: parsed[3],
|
|
|
|
PacketsSent: parsed[4],
|
|
|
|
Errout: parsed[5],
|
|
|
|
BytesSent: parsed[6],
|
|
|
|
Dropout: parsed[7],
|
2014-05-18 22:23:47 +08:00
|
|
|
}
|
|
|
|
ret = append(ret, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2014-05-12 11:03:20 +08:00
|
|
|
}
|