2014-08-08 22:09:28 +08:00
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
package gopsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
2014-09-20 09:17:00 +08:00
|
|
|
"strconv"
|
2014-08-08 22:09:28 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
|
|
|
|
out, err := exec.Command("/usr/sbin/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" {
|
2014-09-20 09:17:00 +08:00
|
|
|
// skip first line
|
2014-08-08 22:09:28 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
base := 1
|
|
|
|
// sometimes Address is ommitted
|
2014-09-20 09:16:30 +08:00
|
|
|
if len(values) < 11 {
|
2014-08-08 22:09:28 +08:00
|
|
|
base = 0
|
|
|
|
}
|
|
|
|
|
2014-09-20 09:17:00 +08:00
|
|
|
parsed := make([]uint64, 0, 3)
|
|
|
|
vv := []string{
|
|
|
|
values[base+3], // PacketsRecv
|
|
|
|
values[base+4], // Errin
|
|
|
|
values[base+5], // Dropin
|
|
|
|
}
|
|
|
|
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-08-08 22:09:28 +08:00
|
|
|
n := NetIOCountersStat{
|
|
|
|
Name: values[0],
|
2014-09-20 09:17:00 +08:00
|
|
|
PacketsRecv: parsed[0],
|
|
|
|
Errin: parsed[1],
|
|
|
|
Dropin: parsed[2],
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
ret = append(ret, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|