2014-12-30 21:09:05 +08:00
|
|
|
package net
|
2014-04-23 11:51:28 +08:00
|
|
|
|
2014-05-01 17:43:11 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-06-02 23:33:55 +08:00
|
|
|
"net"
|
2015-09-16 15:12:41 +08:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/common"
|
2014-05-01 17:43:11 +08:00
|
|
|
)
|
|
|
|
|
2015-09-16 15:12:41 +08:00
|
|
|
var invoke common.Invoker
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
invoke = common.Invoke{}
|
|
|
|
}
|
|
|
|
|
2014-04-30 14:32:05 +08:00
|
|
|
type NetIOCountersStat struct {
|
2014-05-01 11:01:30 +08:00
|
|
|
Name string `json:"name"` // interface name
|
|
|
|
BytesSent uint64 `json:"bytes_sent"` // number of bytes sent
|
2014-04-30 14:32:05 +08:00
|
|
|
BytesRecv uint64 `json:"bytes_recv"` // number of bytes received
|
|
|
|
PacketsSent uint64 `json:"packets_sent"` // number of packets sent
|
|
|
|
PacketsRecv uint64 `json:"packets_recv"` // number of packets received
|
|
|
|
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)
|
2014-04-23 11:51:28 +08:00
|
|
|
}
|
|
|
|
|
2014-06-02 23:33:55 +08:00
|
|
|
// Addr is implemented compatibility to psutil
|
2014-04-23 11:51:28 +08:00
|
|
|
type Addr struct {
|
2014-04-30 15:42:08 +08:00
|
|
|
IP string `json:"ip"`
|
|
|
|
Port uint32 `json:"port"`
|
2014-04-23 11:51:28 +08:00
|
|
|
}
|
|
|
|
|
2014-04-30 14:32:05 +08:00
|
|
|
type NetConnectionStat struct {
|
2014-04-30 15:42:08 +08:00
|
|
|
Fd uint32 `json:"fd"`
|
|
|
|
Family uint32 `json:"family"`
|
|
|
|
Type uint32 `json:"type"`
|
2014-05-16 11:21:55 +08:00
|
|
|
Laddr Addr `json:"localaddr"`
|
|
|
|
Raddr Addr `json:"remoteaddr"`
|
2014-04-30 15:42:08 +08:00
|
|
|
Status string `json:"status"`
|
|
|
|
Pid int32 `json:"pid"`
|
2014-04-23 11:51:28 +08:00
|
|
|
}
|
2014-05-01 17:43:11 +08:00
|
|
|
|
2014-06-02 23:33:55 +08:00
|
|
|
// NetInterfaceAddr is designed for represent interface addresses
|
|
|
|
type NetInterfaceAddr struct {
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type NetInterfaceStat struct {
|
2014-06-02 23:39:47 +08:00
|
|
|
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
|
2014-06-02 23:33:55 +08:00
|
|
|
Addrs []NetInterfaceAddr `json:"addrs"`
|
|
|
|
}
|
|
|
|
|
2014-05-01 17:53:07 +08:00
|
|
|
func (n NetIOCountersStat) String() string {
|
2014-05-01 17:43:11 +08:00
|
|
|
s, _ := json.Marshal(n)
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
2014-05-01 17:53:07 +08:00
|
|
|
func (n NetConnectionStat) String() string {
|
2014-05-01 17:43:11 +08:00
|
|
|
s, _ := json.Marshal(n)
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Addr) String() string {
|
|
|
|
s, _ := json.Marshal(a)
|
|
|
|
return string(s)
|
|
|
|
}
|
2014-06-02 23:33:55 +08:00
|
|
|
|
|
|
|
func (n NetInterfaceStat) String() string {
|
|
|
|
s, _ := json.Marshal(n)
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
2014-06-02 23:39:47 +08:00
|
|
|
func (n NetInterfaceAddr) String() string {
|
2014-06-02 23:33:55 +08:00
|
|
|
s, _ := json.Marshal(n)
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NetInterfaces() ([]NetInterfaceStat, error) {
|
|
|
|
is, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := make([]NetInterfaceStat, 0, len(is))
|
|
|
|
for _, ifi := range is {
|
|
|
|
|
|
|
|
var flags []string
|
|
|
|
if ifi.Flags&net.FlagUp != 0 {
|
|
|
|
flags = append(flags, "up")
|
|
|
|
}
|
2014-06-02 23:39:47 +08:00
|
|
|
if ifi.Flags&net.FlagBroadcast != 0 {
|
2014-06-02 23:33:55 +08:00
|
|
|
flags = append(flags, "broadcast")
|
|
|
|
}
|
2014-06-02 23:39:47 +08:00
|
|
|
if ifi.Flags&net.FlagLoopback != 0 {
|
2014-06-02 23:33:55 +08:00
|
|
|
flags = append(flags, "loopback")
|
|
|
|
}
|
2014-06-02 23:39:47 +08:00
|
|
|
if ifi.Flags&net.FlagPointToPoint != 0 {
|
2014-06-02 23:33:55 +08:00
|
|
|
flags = append(flags, "pointtopoint")
|
|
|
|
}
|
2014-06-02 23:39:47 +08:00
|
|
|
if ifi.Flags&net.FlagMulticast != 0 {
|
2014-06-02 23:33:55 +08:00
|
|
|
flags = append(flags, "multicast")
|
|
|
|
}
|
|
|
|
|
|
|
|
r := NetInterfaceStat{
|
|
|
|
Name: ifi.Name,
|
|
|
|
MTU: ifi.MTU,
|
2014-06-02 23:39:47 +08:00
|
|
|
HardwareAddr: ifi.HardwareAddr.String(),
|
2014-06-02 23:33:55 +08:00
|
|
|
Flags: flags,
|
|
|
|
}
|
|
|
|
addrs, err := ifi.Addrs()
|
|
|
|
if err == nil {
|
|
|
|
r.Addrs = make([]NetInterfaceAddr, 0, len(addrs))
|
2014-06-02 23:39:47 +08:00
|
|
|
for _, addr := range addrs {
|
2014-06-02 23:33:55 +08:00
|
|
|
r.Addrs = append(r.Addrs, NetInterfaceAddr{
|
|
|
|
Addr: addr.String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ret = append(ret, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2015-01-01 20:29:25 +08:00
|
|
|
|
|
|
|
func getNetIOCountersAll(n []NetIOCountersStat) ([]NetIOCountersStat, error) {
|
|
|
|
r := NetIOCountersStat{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return []NetIOCountersStat{r}, nil
|
|
|
|
}
|