2016-02-12 18:02:16 +00:00
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2018-03-06 00:52:43 +00:00
|
|
|
"context"
|
2016-02-12 18:02:16 +00:00
|
|
|
"errors"
|
2017-02-02 06:41:29 +00:00
|
|
|
"fmt"
|
2021-03-30 18:47:33 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
2016-02-12 18:02:16 +00:00
|
|
|
"os/exec"
|
2017-02-02 06:41:29 +00:00
|
|
|
"regexp"
|
2016-02-12 18:02:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-02-02 06:41:29 +00:00
|
|
|
)
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2017-02-02 06:41:29 +00:00
|
|
|
var (
|
|
|
|
errNetstatHeader = errors.New("Can't parse header of netstat output")
|
|
|
|
netstatLinkRegexp = regexp.MustCompile(`^<Link#(\d+)>$`)
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
2017-02-02 06:41:29 +00:00
|
|
|
const endOfLine = "\n"
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
func parseNetstatLine(line string) (stat *IOCountersStat, linkID *uint, err error) {
|
2017-02-02 06:41:29 +00: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-07-28 16:46:44 +00:00
|
|
|
linkIDUint := uint(numericValue)
|
|
|
|
linkID = &linkIDUint
|
2017-02-02 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
base := 1
|
|
|
|
numberColumns := len(columns)
|
2020-03-15 08:36:59 +00:00
|
|
|
// sometimes Address is omitted
|
2017-02-02 06:41:29 +00:00
|
|
|
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-07-28 16:46:44 +00:00
|
|
|
linkID *uint
|
2017-02-02 06:41:29 +00: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-07-28 16:46:44 +00:00
|
|
|
if nsIface.stat, nsIface.linkID, err = parseNetstatLine(lines[index+1]); err != nil {
|
2017-02-02 06:41:29 +00: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-07-28 16:46:44 +00:00
|
|
|
if ifaces[index].linkID != nil {
|
2017-02-02 06:41:29 +00: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
|
2016-02-12 18:02:16 +00: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-05-09 15:19:04 +00:00
|
|
|
func IOCounters(pernic bool) ([]IOCountersStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return IOCountersWithContext(context.Background(), pernic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
|
2017-02-02 06:41:29 +00:00
|
|
|
var (
|
|
|
|
ret []IOCountersStat
|
|
|
|
retIndex int
|
|
|
|
)
|
|
|
|
|
2020-03-15 08:36:59 +00:00
|
|
|
netstat, err := exec.LookPath("netstat")
|
2016-05-09 15:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-02 06:41:29 +00:00
|
|
|
|
|
|
|
// try to get all interface metrics, and hope there won't be any truncated
|
2020-03-15 08:36:59 +00:00
|
|
|
out, err := invoke.CommandWithContext(ctx, netstat, "-ibdnW")
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-02 06:41:29 +00:00
|
|
|
nsInterfaces, err := parseNetstatOutput(string(out))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2017-02-02 06:41:29 +00:00
|
|
|
ifaceUsage := newMapInterfaceNameUsage(nsInterfaces)
|
|
|
|
notTruncated := ifaceUsage.notTruncated()
|
|
|
|
ret = make([]IOCountersStat, len(notTruncated))
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2017-02-02 06:41:29 +00:00
|
|
|
if !ifaceUsage.isTruncated() {
|
|
|
|
// no truncated interface name, return stats of all interface with <Link#...>
|
|
|
|
for index := range nsInterfaces {
|
2017-07-28 16:46:44 +00:00
|
|
|
if nsInterfaces[index].linkID != nil {
|
2017-02-02 06:41:29 +00:00
|
|
|
ret[retIndex] = *nsInterfaces[index].stat
|
|
|
|
retIndex++
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2017-02-02 06:41:29 +00:00
|
|
|
} else {
|
|
|
|
// duplicated interface, list all interfaces
|
2020-03-15 08:36:59 +00:00
|
|
|
ifconfig, err := exec.LookPath("ifconfig")
|
2017-02-02 06:41:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2020-03-15 08:36:59 +00:00
|
|
|
if out, err = invoke.CommandWithContext(ctx, ifconfig, "-l"); err != nil {
|
2017-02-02 06:41:29 +00:00
|
|
|
return nil, err
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2017-02-02 06:41:29 +00:00
|
|
|
interfaceNames := strings.Fields(strings.TrimRight(string(out), endOfLine))
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2017-02-02 06:41:29 +00: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-07-28 16:46:44 +00:00
|
|
|
if nsInterfaces[index].linkID != nil && nsInterfaces[index].stat.Name == interfaceName {
|
2017-02-02 06:41:29 +00:00
|
|
|
// handle the non truncated name to avoid execute netstat for them again
|
|
|
|
ret[retIndex] = *nsInterfaces[index].stat
|
|
|
|
retIndex++
|
|
|
|
truncated = false
|
|
|
|
break
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2017-02-02 06:41:29 +00:00
|
|
|
if truncated {
|
|
|
|
// run netstat with -I$ifacename
|
2020-03-15 08:36:59 +00:00
|
|
|
if out, err = invoke.CommandWithContext(ctx, netstat, "-ibdnWI"+interfaceName); err != nil {
|
2017-02-02 06:41:29 +00: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-07-28 16:46:44 +00:00
|
|
|
if parsedIfaces[index].linkID != nil {
|
2017-02-02 06:41:29 +00:00
|
|
|
ret = append(ret, *parsedIfaces[index].stat)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pernic == false {
|
2016-05-09 15:19:04 +00:00
|
|
|
return getIOCountersAll(ret)
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
// NetIOCountersByFile is an method which is added just a compatibility for linux.
|
|
|
|
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return IOCountersByFileWithContext(context.Background(), pernic, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
return IOCounters(pernic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterCounters() ([]FilterStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return FilterCountersWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
|
2021-03-30 18:47:33 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
2016-05-09 15:19:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 08:36:59 +00:00
|
|
|
func ConntrackStats(percpu bool) ([]ConntrackStat, error) {
|
|
|
|
return ConntrackStatsWithContext(context.Background(), percpu)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) {
|
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00: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-05-09 15:19:04 +00:00
|
|
|
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return ProtoCountersWithContext(context.Background(), protocols)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
|
2021-03-30 18:47:33 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|