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 (
|
2015-11-20 05:19:43 +08:00
|
|
|
"errors"
|
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"
|
2015-01-01 20:54:45 +08:00
|
|
|
|
2015-10-19 23:04:57 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-05-12 11:03:20 +08:00
|
|
|
)
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func IOCounters(pernic bool) ([]IOCountersStat, error) {
|
2016-04-01 20:34:39 +08:00
|
|
|
netstat, err := exec.LookPath("/usr/bin/netstat")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-20 16:59:41 +08:00
|
|
|
out, err := invoke.Command(netstat, "-ibdnW")
|
2014-05-18 22:23:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(out), "\n")
|
2016-03-22 22:09:12 +08:00
|
|
|
ret := make([]IOCountersStat, 0, len(lines)-1)
|
2015-01-01 20:54:45 +08:00
|
|
|
exists := make([]string, 0, len(ret))
|
2014-05-18 22:23:47 +08:00
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
values := strings.Fields(line)
|
|
|
|
if len(values) < 1 || values[0] == "Name" {
|
|
|
|
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])
|
|
|
|
|
2015-12-11 19:47:48 +08:00
|
|
|
if len(values) < 12 {
|
|
|
|
continue
|
|
|
|
}
|
2014-05-18 22:23:47 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
n := IOCountersStat{
|
2014-05-18 22:23:47 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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-05-18 22:23:47 +08:00
|
|
|
return ret, nil
|
2014-05-12 11:03:20 +08:00
|
|
|
}
|
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) {
|
|
|
|
return IOCounters(pernic)
|
2016-01-21 16:31:05 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func FilterCounters() ([]FilterStat, error) {
|
2016-01-21 16:31:05 +08:00
|
|
|
return nil, errors.New("NetFilterCounters not implemented for freebsd")
|
|
|
|
}
|
|
|
|
|
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 FreeBSD
|
2016-03-22 22:09:12 +08:00
|
|
|
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
|
2015-11-20 05:19:43 +08:00
|
|
|
return nil, errors.New("NetProtoCounters not implemented for freebsd")
|
|
|
|
}
|