2014-08-08 22:09:28 +08:00
|
|
|
// +build darwin
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package net
|
2014-08-08 22:09:28 +08:00
|
|
|
|
|
|
|
import (
|
2015-11-20 05:19:43 +08:00
|
|
|
"errors"
|
2014-08-08 22:09:28 +08:00
|
|
|
"os/exec"
|
2014-09-20 09:17:00 +08:00
|
|
|
"strconv"
|
2014-08-08 22:09:28 +08:00
|
|
|
"strings"
|
2015-01-01 20:54:45 +08:00
|
|
|
|
2015-10-19 23:04:57 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-08-08 22:09:28 +08:00
|
|
|
)
|
|
|
|
|
2015-09-11 10:24:03 +08:00
|
|
|
// example of netstat -idbn output on yosemite
|
|
|
|
// Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll Drop
|
|
|
|
// lo0 16384 <Link#1> 869107 0 169411755 869107 0 169411755 0 0
|
|
|
|
// lo0 16384 ::1/128 ::1 869107 - 169411755 869107 - 169411755 - -
|
|
|
|
// lo0 16384 127 127.0.0.1 869107 - 169411755 869107 - 169411755 - -
|
2014-08-08 22:09:28 +08:00
|
|
|
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)
|
2015-01-01 20:54:45 +08:00
|
|
|
exists := make([]string, 0, len(ret))
|
2014-08-08 22:09:28 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-07-21 14:25:04 +08:00
|
|
|
if common.StringsHas(exists, values[0]) {
|
2015-01-01 20:54:45 +08:00
|
|
|
// skip if already get
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
exists = append(exists, values[0])
|
|
|
|
|
2014-08-08 22:09:28 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-11 11:18:33 +08:00
|
|
|
parsed := make([]uint64, 0, 7)
|
2014-09-20 09:17:00 +08:00
|
|
|
vv := []string{
|
2015-09-11 03:53:53 +08:00
|
|
|
values[base+3], // Ipkts == PacketsRecv
|
|
|
|
values[base+4], // Ierrs == Errin
|
|
|
|
values[base+5], // Ibytes == BytesRecv
|
|
|
|
values[base+6], // Opkts == PacketsSent
|
|
|
|
values[base+7], // Oerrs == Errout
|
|
|
|
values[base+8], // Obytes == BytesSent
|
2014-09-20 09:17:00 +08:00
|
|
|
}
|
2015-09-11 11:18:33 +08:00
|
|
|
if len(values) == 12 {
|
|
|
|
vv = append(vv, values[base+10])
|
|
|
|
}
|
|
|
|
|
2014-09-20 09:17:00 +08:00
|
|
|
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],
|
2015-09-11 03:53:53 +08:00
|
|
|
BytesRecv: parsed[2],
|
|
|
|
PacketsSent: parsed[3],
|
|
|
|
Errout: parsed[4],
|
|
|
|
BytesSent: parsed[5],
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
2015-09-11 11:18:33 +08:00
|
|
|
if len(parsed) == 7 {
|
|
|
|
n.Dropout = parsed[6]
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
ret = append(ret, n)
|
|
|
|
}
|
|
|
|
|
2015-01-01 20:29:25 +08:00
|
|
|
if pernic == false {
|
|
|
|
return getNetIOCountersAll(ret)
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:09:28 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
2015-11-20 05:19:43 +08:00
|
|
|
|
|
|
|
// NetProtoCounters returns network statistics for the entire system
|
|
|
|
// If protocols is empty then all protocols are returned, otherwise
|
|
|
|
// just the protocols in the list are returned.
|
|
|
|
// Not Implemented for Darwin
|
|
|
|
func NetProtoCounters(protocols []string) ([]NetProtoCountersStat, error) {
|
|
|
|
return nil, errors.New("NetProtoCounters not implemented for darwin")
|
|
|
|
}
|