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 (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2015-11-20 05:19:43 +08:00
|
|
|
"errors"
|
2016-09-04 22:36:41 +08:00
|
|
|
"fmt"
|
2014-08-08 22:09:28 +08:00
|
|
|
"os/exec"
|
2016-09-04 22:36:41 +08:00
|
|
|
"regexp"
|
2014-09-20 09:17:00 +08:00
|
|
|
"strconv"
|
2014-08-08 22:09:28 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-09-04 22:36:41 +08:00
|
|
|
var (
|
|
|
|
errNetstatHeader = errors.New("Can't parse header of netstat output")
|
|
|
|
netstatLinkRegexp = regexp.MustCompile(`^<Link#(\d+)>$`)
|
|
|
|
)
|
|
|
|
|
|
|
|
const endOfLine = "\n"
|
|
|
|
|
2017-04-28 05:21:34 +08:00
|
|
|
func parseNetstatLine(line string) (stat *IOCountersStat, linkID *uint, err error) {
|
2016-09-04 22:36:41 +08:00
|
|
|
var (
|
|
|
|
numericValue uint64
|
|
|
|
columns = strings.Fields(line)
|
|
|
|
)
|
|
|
|
|
|
|
|
if columns[0] == "Name" {
|
|
|
|
err = errNetstatHeader
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to extract the numeric value from <Link#123>
|
|
|
|
if subMatch := netstatLinkRegexp.FindStringSubmatch(columns[2]); len(subMatch) == 2 {
|
|
|
|
numericValue, err = strconv.ParseUint(subMatch[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-28 05:21:34 +08:00
|
|
|
linkIDUint := uint(numericValue)
|
|
|
|
linkID = &linkIDUint
|
2016-09-04 22:36:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
base := 1
|
|
|
|
numberColumns := len(columns)
|
|
|
|
// sometimes Address is ommitted
|
|
|
|
if numberColumns < 12 {
|
|
|
|
base = 0
|
|
|
|
}
|
|
|
|
if numberColumns < 11 || numberColumns > 13 {
|
|
|
|
err = fmt.Errorf("Line %q do have an invalid number of columns %d", line, numberColumns)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed := make([]uint64, 0, 7)
|
|
|
|
vv := []string{
|
|
|
|
columns[base+3], // Ipkts == PacketsRecv
|
|
|
|
columns[base+4], // Ierrs == Errin
|
|
|
|
columns[base+5], // Ibytes == BytesRecv
|
|
|
|
columns[base+6], // Opkts == PacketsSent
|
|
|
|
columns[base+7], // Oerrs == Errout
|
|
|
|
columns[base+8], // Obytes == BytesSent
|
|
|
|
}
|
|
|
|
if len(columns) == 12 {
|
|
|
|
vv = append(vv, columns[base+10])
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, target := range vv {
|
|
|
|
if target == "-" {
|
|
|
|
parsed = append(parsed, 0)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if numericValue, err = strconv.ParseUint(target, 10, 64); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
parsed = append(parsed, numericValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = &IOCountersStat{
|
|
|
|
Name: strings.Trim(columns[0], "*"), // remove the * that sometimes is on right on interface
|
|
|
|
PacketsRecv: parsed[0],
|
|
|
|
Errin: parsed[1],
|
|
|
|
BytesRecv: parsed[2],
|
|
|
|
PacketsSent: parsed[3],
|
|
|
|
Errout: parsed[4],
|
|
|
|
BytesSent: parsed[5],
|
|
|
|
}
|
|
|
|
if len(parsed) == 7 {
|
|
|
|
stat.Dropout = parsed[6]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type netstatInterface struct {
|
2017-04-28 05:21:34 +08:00
|
|
|
linkID *uint
|
2016-09-04 22:36:41 +08:00
|
|
|
stat *IOCountersStat
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNetstatOutput(output string) ([]netstatInterface, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
lines = strings.Split(strings.Trim(output, endOfLine), endOfLine)
|
|
|
|
)
|
|
|
|
|
|
|
|
// number of interfaces is number of lines less one for the header
|
|
|
|
numberInterfaces := len(lines) - 1
|
|
|
|
|
|
|
|
interfaces := make([]netstatInterface, numberInterfaces)
|
|
|
|
// no output beside header
|
|
|
|
if numberInterfaces == 0 {
|
|
|
|
return interfaces, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for index := 0; index < numberInterfaces; index++ {
|
|
|
|
nsIface := netstatInterface{}
|
2017-04-28 05:21:34 +08:00
|
|
|
if nsIface.stat, nsIface.linkID, err = parseNetstatLine(lines[index+1]); err != nil {
|
2016-09-04 22:36:41 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
interfaces[index] = nsIface
|
|
|
|
}
|
|
|
|
return interfaces, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// map that hold the name of a network interface and the number of usage
|
|
|
|
type mapInterfaceNameUsage map[string]uint
|
|
|
|
|
|
|
|
func newMapInterfaceNameUsage(ifaces []netstatInterface) mapInterfaceNameUsage {
|
|
|
|
output := make(mapInterfaceNameUsage)
|
|
|
|
for index := range ifaces {
|
2017-04-28 05:21:34 +08:00
|
|
|
if ifaces[index].linkID != nil {
|
2016-09-04 22:36:41 +08:00
|
|
|
ifaceName := ifaces[index].stat.Name
|
|
|
|
usage, ok := output[ifaceName]
|
|
|
|
if ok {
|
|
|
|
output[ifaceName] = usage + 1
|
|
|
|
} else {
|
|
|
|
output[ifaceName] = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (min mapInterfaceNameUsage) isTruncated() bool {
|
|
|
|
for _, usage := range min {
|
|
|
|
if usage > 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (min mapInterfaceNameUsage) notTruncated() []string {
|
|
|
|
output := make([]string, 0)
|
|
|
|
for ifaceName, usage := range min {
|
|
|
|
if usage == 1 {
|
|
|
|
output = append(output, ifaceName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
// example of `netstat -ibdnW` output on yosemite
|
2015-09-11 10:24:03 +08:00
|
|
|
// 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 - -
|
2016-03-22 22:09:12 +08:00
|
|
|
func IOCounters(pernic bool) ([]IOCountersStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return IOCountersWithContext(context.Background(), pernic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
|
2016-09-04 22:36:41 +08:00
|
|
|
var (
|
|
|
|
ret []IOCountersStat
|
|
|
|
retIndex int
|
|
|
|
)
|
|
|
|
|
|
|
|
netstat, err := exec.LookPath("/usr/sbin/netstat")
|
2016-04-01 20:34:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-30 23:40:19 +08:00
|
|
|
|
2016-09-04 22:36:41 +08:00
|
|
|
// try to get all interface metrics, and hope there won't be any truncated
|
2018-03-31 20:35:53 +08:00
|
|
|
out, err := invoke.CommandWithContext(ctx, netstat, "-ibdnW")
|
2014-08-08 22:09:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-04 22:36:41 +08:00
|
|
|
nsInterfaces, err := parseNetstatOutput(string(out))
|
2016-08-30 23:40:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
|
2016-09-04 22:36:41 +08:00
|
|
|
ifaceUsage := newMapInterfaceNameUsage(nsInterfaces)
|
|
|
|
notTruncated := ifaceUsage.notTruncated()
|
|
|
|
ret = make([]IOCountersStat, len(notTruncated))
|
2016-08-30 23:40:19 +08:00
|
|
|
|
2016-09-04 22:36:41 +08:00
|
|
|
if !ifaceUsage.isTruncated() {
|
|
|
|
// no truncated interface name, return stats of all interface with <Link#...>
|
|
|
|
for index := range nsInterfaces {
|
2017-04-28 05:21:34 +08:00
|
|
|
if nsInterfaces[index].linkID != nil {
|
2016-09-04 22:36:41 +08:00
|
|
|
ret[retIndex] = *nsInterfaces[index].stat
|
|
|
|
retIndex++
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
2016-09-04 22:36:41 +08:00
|
|
|
} else {
|
|
|
|
// duplicated interface, list all interfaces
|
|
|
|
ifconfig, err := exec.LookPath("/sbin/ifconfig")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-09-20 09:17:00 +08:00
|
|
|
}
|
2018-03-31 20:35:53 +08:00
|
|
|
if out, err = invoke.CommandWithContext(ctx, ifconfig, "-l"); err != nil {
|
2016-09-04 22:36:41 +08:00
|
|
|
return nil, err
|
2015-09-11 11:18:33 +08:00
|
|
|
}
|
2016-09-04 22:36:41 +08:00
|
|
|
interfaceNames := strings.Fields(strings.TrimRight(string(out), endOfLine))
|
2015-09-11 11:18:33 +08:00
|
|
|
|
2016-09-04 22:36:41 +08:00
|
|
|
// for each of the interface name, run netstat if we don't have any stats yet
|
|
|
|
for _, interfaceName := range interfaceNames {
|
|
|
|
truncated := true
|
|
|
|
for index := range nsInterfaces {
|
2017-04-28 05:21:34 +08:00
|
|
|
if nsInterfaces[index].linkID != nil && nsInterfaces[index].stat.Name == interfaceName {
|
2016-09-04 22:36:41 +08:00
|
|
|
// handle the non truncated name to avoid execute netstat for them again
|
|
|
|
ret[retIndex] = *nsInterfaces[index].stat
|
|
|
|
retIndex++
|
|
|
|
truncated = false
|
|
|
|
break
|
|
|
|
}
|
2014-09-20 09:17:00 +08:00
|
|
|
}
|
2016-09-04 22:36:41 +08:00
|
|
|
if truncated {
|
|
|
|
// run netstat with -I$ifacename
|
2018-03-31 20:35:53 +08:00
|
|
|
if out, err = invoke.CommandWithContext(ctx, netstat, "-ibdnWI"+interfaceName); err != nil {
|
2016-09-04 22:36:41 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parsedIfaces, err := parseNetstatOutput(string(out))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(parsedIfaces) == 0 {
|
|
|
|
// interface had been removed since `ifconfig -l` had been executed
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for index := range parsedIfaces {
|
2017-04-28 05:21:34 +08:00
|
|
|
if parsedIfaces[index].linkID != nil {
|
2016-09-04 22:36:41 +08:00
|
|
|
ret = append(ret, *parsedIfaces[index].stat)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-09-20 09:17:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
2015-01-01 20:29:25 +08:00
|
|
|
if pernic == false {
|
2016-03-22 22:09:12 +08:00
|
|
|
return getIOCountersAll(ret)
|
2015-01-01 20:29:25 +08:00
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
2015-11-20 05:19:43 +08:00
|
|
|
|
2016-01-21 16:31:05 +08:00
|
|
|
// NetIOCountersByFile is an method which is added just a compatibility for linux.
|
2016-03-22 22:09:12 +08:00
|
|
|
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return IOCountersByFileWithContext(context.Background(), pernic, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
|
2016-03-22 22:09:12 +08:00
|
|
|
return IOCounters(pernic)
|
2016-01-21 16:31:05 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func FilterCounters() ([]FilterStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return FilterCountersWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
|
2016-01-21 16:31:05 +08:00
|
|
|
return nil, errors.New("NetFilterCounters not implemented for darwin")
|
|
|
|
}
|
|
|
|
|
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
|
2016-03-22 22:09:12 +08:00
|
|
|
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return ProtoCountersWithContext(context.Background(), protocols)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
|
2015-11-20 05:19:43 +08:00
|
|
|
return nil, errors.New("NetProtoCounters not implemented for darwin")
|
|
|
|
}
|