open-nomad/vendor/github.com/shirou/gopsutil/net/net.go

253 lines
6.1 KiB
Go
Raw Normal View History

2016-02-12 18:02:16 +00:00
package net
import (
"context"
2016-02-12 18:02:16 +00:00
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
"syscall"
"github.com/shirou/gopsutil/internal/common"
)
var invoke common.Invoker
func init() {
invoke = common.Invoke{}
}
2016-05-09 15:19:04 +00:00
type IOCountersStat struct {
2016-06-17 14:31:00 +00:00
Name string `json:"name"` // interface name
2016-05-09 15:19:04 +00:00
BytesSent uint64 `json:"bytesSent"` // number of bytes sent
BytesRecv uint64 `json:"bytesRecv"` // number of bytes received
PacketsSent uint64 `json:"packetsSent"` // number of packets sent
PacketsRecv uint64 `json:"packetsRecv"` // number of packets received
2016-06-17 14:31:00 +00:00
Errin uint64 `json:"errin"` // total number of errors while receiving
Errout uint64 `json:"errout"` // total number of errors while sending
Dropin uint64 `json:"dropin"` // total number of incoming packets which were dropped
Dropout uint64 `json:"dropout"` // total number of outgoing packets which were dropped (always 0 on OSX and BSD)
Fifoin uint64 `json:"fifoin"` // total number of FIFO buffers errors while receiving
Fifoout uint64 `json:"fifoout"` // total number of FIFO buffers errors while sending
2016-02-12 18:02:16 +00:00
}
// Addr is implemented compatibility to psutil
type Addr struct {
IP string `json:"ip"`
Port uint32 `json:"port"`
}
2016-05-09 15:19:04 +00:00
type ConnectionStat struct {
Fd uint32 `json:"fd"`
Family uint32 `json:"family"`
Type uint32 `json:"type"`
Laddr Addr `json:"localaddr"`
Raddr Addr `json:"remoteaddr"`
Status string `json:"status"`
Uids []int32 `json:"uids"`
Pid int32 `json:"pid"`
2016-02-12 18:02:16 +00:00
}
// System wide stats about different network protocols
2016-05-09 15:19:04 +00:00
type ProtoCountersStat struct {
2016-02-12 18:02:16 +00:00
Protocol string `json:"protocol"`
Stats map[string]int64 `json:"stats"`
}
// NetInterfaceAddr is designed for represent interface addresses
2016-05-09 15:19:04 +00:00
type InterfaceAddr struct {
2016-02-12 18:02:16 +00:00
Addr string `json:"addr"`
}
2016-05-09 15:19:04 +00:00
type InterfaceStat struct {
MTU int `json:"mtu"` // maximum transmission unit
Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100"
HardwareAddr string `json:"hardwareaddr"` // IEEE MAC-48, EUI-48 and EUI-64 form
Flags []string `json:"flags"` // e.g., FlagUp, FlagLoopback, FlagMulticast
Addrs []InterfaceAddr `json:"addrs"`
2016-02-12 18:02:16 +00:00
}
2016-05-09 15:19:04 +00:00
type FilterStat struct {
ConnTrackCount int64 `json:"conntrackCount"`
ConnTrackMax int64 `json:"conntrackMax"`
2016-02-12 18:02:16 +00:00
}
var constMap = map[string]int{
"TCP": syscall.SOCK_STREAM,
"UDP": syscall.SOCK_DGRAM,
"IPv4": syscall.AF_INET,
"IPv6": syscall.AF_INET6,
}
2016-05-09 15:19:04 +00:00
func (n IOCountersStat) String() string {
2016-02-12 18:02:16 +00:00
s, _ := json.Marshal(n)
return string(s)
}
2016-05-09 15:19:04 +00:00
func (n ConnectionStat) String() string {
2016-02-12 18:02:16 +00:00
s, _ := json.Marshal(n)
return string(s)
}
2016-05-09 15:19:04 +00:00
func (n ProtoCountersStat) String() string {
2016-02-12 18:02:16 +00:00
s, _ := json.Marshal(n)
return string(s)
}
func (a Addr) String() string {
s, _ := json.Marshal(a)
return string(s)
}
2016-05-09 15:19:04 +00:00
func (n InterfaceStat) String() string {
2016-02-12 18:02:16 +00:00
s, _ := json.Marshal(n)
return string(s)
}
2016-05-09 15:19:04 +00:00
func (n InterfaceAddr) String() string {
2016-02-12 18:02:16 +00:00
s, _ := json.Marshal(n)
return string(s)
}
2016-05-09 15:19:04 +00:00
func Interfaces() ([]InterfaceStat, error) {
return InterfacesWithContext(context.Background())
}
func InterfacesWithContext(ctx context.Context) ([]InterfaceStat, error) {
2016-02-12 18:02:16 +00:00
is, err := net.Interfaces()
if err != nil {
return nil, err
}
2016-05-09 15:19:04 +00:00
ret := make([]InterfaceStat, 0, len(is))
2016-02-12 18:02:16 +00:00
for _, ifi := range is {
var flags []string
if ifi.Flags&net.FlagUp != 0 {
flags = append(flags, "up")
}
if ifi.Flags&net.FlagBroadcast != 0 {
flags = append(flags, "broadcast")
}
if ifi.Flags&net.FlagLoopback != 0 {
flags = append(flags, "loopback")
}
if ifi.Flags&net.FlagPointToPoint != 0 {
flags = append(flags, "pointtopoint")
}
if ifi.Flags&net.FlagMulticast != 0 {
flags = append(flags, "multicast")
}
2016-05-09 15:19:04 +00:00
r := InterfaceStat{
2016-02-12 18:02:16 +00:00
Name: ifi.Name,
MTU: ifi.MTU,
HardwareAddr: ifi.HardwareAddr.String(),
Flags: flags,
}
addrs, err := ifi.Addrs()
if err == nil {
2016-05-09 15:19:04 +00:00
r.Addrs = make([]InterfaceAddr, 0, len(addrs))
2016-02-12 18:02:16 +00:00
for _, addr := range addrs {
2016-05-09 15:19:04 +00:00
r.Addrs = append(r.Addrs, InterfaceAddr{
2016-02-12 18:02:16 +00:00
Addr: addr.String(),
})
}
}
ret = append(ret, r)
}
return ret, nil
}
2016-05-09 15:19:04 +00:00
func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) {
r := IOCountersStat{
2016-02-12 18:02:16 +00:00
Name: "all",
}
for _, nic := range n {
r.BytesRecv += nic.BytesRecv
r.PacketsRecv += nic.PacketsRecv
r.Errin += nic.Errin
r.Dropin += nic.Dropin
r.BytesSent += nic.BytesSent
r.PacketsSent += nic.PacketsSent
r.Errout += nic.Errout
r.Dropout += nic.Dropout
}
2016-05-09 15:19:04 +00:00
return []IOCountersStat{r}, nil
2016-02-12 18:02:16 +00:00
}
2016-05-09 15:19:04 +00:00
func parseNetLine(line string) (ConnectionStat, error) {
2016-02-12 18:02:16 +00:00
f := strings.Fields(line)
if len(f) < 9 {
2016-05-09 15:19:04 +00:00
return ConnectionStat{}, fmt.Errorf("wrong line,%s", line)
2016-02-12 18:02:16 +00:00
}
pid, err := strconv.Atoi(f[1])
if err != nil {
2016-05-09 15:19:04 +00:00
return ConnectionStat{}, err
2016-02-12 18:02:16 +00:00
}
fd, err := strconv.Atoi(strings.Trim(f[3], "u"))
if err != nil {
2016-05-09 15:19:04 +00:00
return ConnectionStat{}, fmt.Errorf("unknown fd, %s", f[3])
2016-02-12 18:02:16 +00:00
}
netFamily, ok := constMap[f[4]]
if !ok {
2016-05-09 15:19:04 +00:00
return ConnectionStat{}, fmt.Errorf("unknown family, %s", f[4])
2016-02-12 18:02:16 +00:00
}
netType, ok := constMap[f[7]]
if !ok {
2016-05-09 15:19:04 +00:00
return ConnectionStat{}, fmt.Errorf("unknown type, %s", f[7])
2016-02-12 18:02:16 +00:00
}
laddr, raddr, err := parseNetAddr(f[8])
if err != nil {
2016-05-09 15:19:04 +00:00
return ConnectionStat{}, fmt.Errorf("failed to parse netaddr, %s", f[8])
2016-02-12 18:02:16 +00:00
}
2016-05-09 15:19:04 +00:00
n := ConnectionStat{
2016-02-12 18:02:16 +00:00
Fd: uint32(fd),
Family: uint32(netFamily),
Type: uint32(netType),
Laddr: laddr,
Raddr: raddr,
Pid: int32(pid),
}
if len(f) == 10 {
n.Status = strings.Trim(f[9], "()")
}
return n, nil
}
func parseNetAddr(line string) (laddr Addr, raddr Addr, err error) {
parse := func(l string) (Addr, error) {
host, port, err := net.SplitHostPort(l)
if err != nil {
return Addr{}, fmt.Errorf("wrong addr, %s", l)
}
lport, err := strconv.Atoi(port)
if err != nil {
return Addr{}, err
}
return Addr{IP: host, Port: uint32(lport)}, nil
}
addrs := strings.Split(line, "->")
if len(addrs) == 0 {
return laddr, raddr, fmt.Errorf("wrong netaddr, %s", line)
}
laddr, err = parse(addrs[0])
if len(addrs) == 2 { // remote addr exists
raddr, err = parse(addrs[1])
if err != nil {
return laddr, raddr, err
}
}
return laddr, raddr, err
}