|
|
|
@ -17,7 +17,7 @@ func init() {
|
|
|
|
|
invoke = common.Invoke{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type NetIOCountersStat struct {
|
|
|
|
|
type IOCountersStat struct {
|
|
|
|
|
Name string `json:"name"` // interface name
|
|
|
|
|
BytesSent uint64 `json:"bytes_sent"` // number of bytes sent
|
|
|
|
|
BytesRecv uint64 `json:"bytes_recv"` // number of bytes received
|
|
|
|
@ -35,7 +35,7 @@ type Addr struct {
|
|
|
|
|
Port uint32 `json:"port"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type NetConnectionStat struct {
|
|
|
|
|
type ConnectionStat struct {
|
|
|
|
|
Fd uint32 `json:"fd"`
|
|
|
|
|
Family uint32 `json:"family"`
|
|
|
|
|
Type uint32 `json:"type"`
|
|
|
|
@ -46,25 +46,25 @@ type NetConnectionStat struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// System wide stats about different network protocols
|
|
|
|
|
type NetProtoCountersStat struct {
|
|
|
|
|
type ProtoCountersStat struct {
|
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
|
Stats map[string]int64 `json:"stats"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NetInterfaceAddr is designed for represent interface addresses
|
|
|
|
|
type NetInterfaceAddr struct {
|
|
|
|
|
type InterfaceAddr struct {
|
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type NetInterfaceStat 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 []NetInterfaceAddr `json:"addrs"`
|
|
|
|
|
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"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type NetFilterStat struct {
|
|
|
|
|
type FilterStat struct {
|
|
|
|
|
ConnTrackCount int64 `json:"conntrack_count"`
|
|
|
|
|
ConnTrackMax int64 `json:"conntrack_max"`
|
|
|
|
|
}
|
|
|
|
@ -76,17 +76,17 @@ var constMap = map[string]int{
|
|
|
|
|
"IPv6": syscall.AF_INET6,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n NetIOCountersStat) String() string {
|
|
|
|
|
func (n IOCountersStat) String() string {
|
|
|
|
|
s, _ := json.Marshal(n)
|
|
|
|
|
return string(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n NetConnectionStat) String() string {
|
|
|
|
|
func (n ConnectionStat) String() string {
|
|
|
|
|
s, _ := json.Marshal(n)
|
|
|
|
|
return string(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n NetProtoCountersStat) String() string {
|
|
|
|
|
func (n ProtoCountersStat) String() string {
|
|
|
|
|
s, _ := json.Marshal(n)
|
|
|
|
|
return string(s)
|
|
|
|
|
}
|
|
|
|
@ -96,22 +96,22 @@ func (a Addr) String() string {
|
|
|
|
|
return string(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n NetInterfaceStat) String() string {
|
|
|
|
|
func (n InterfaceStat) String() string {
|
|
|
|
|
s, _ := json.Marshal(n)
|
|
|
|
|
return string(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n NetInterfaceAddr) String() string {
|
|
|
|
|
func (n InterfaceAddr) String() string {
|
|
|
|
|
s, _ := json.Marshal(n)
|
|
|
|
|
return string(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NetInterfaces() ([]NetInterfaceStat, error) {
|
|
|
|
|
func Interfaces() ([]InterfaceStat, error) {
|
|
|
|
|
is, err := net.Interfaces()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ret := make([]NetInterfaceStat, 0, len(is))
|
|
|
|
|
ret := make([]InterfaceStat, 0, len(is))
|
|
|
|
|
for _, ifi := range is {
|
|
|
|
|
|
|
|
|
|
var flags []string
|
|
|
|
@ -131,7 +131,7 @@ func NetInterfaces() ([]NetInterfaceStat, error) {
|
|
|
|
|
flags = append(flags, "multicast")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r := NetInterfaceStat{
|
|
|
|
|
r := InterfaceStat{
|
|
|
|
|
Name: ifi.Name,
|
|
|
|
|
MTU: ifi.MTU,
|
|
|
|
|
HardwareAddr: ifi.HardwareAddr.String(),
|
|
|
|
@ -139,9 +139,9 @@ func NetInterfaces() ([]NetInterfaceStat, error) {
|
|
|
|
|
}
|
|
|
|
|
addrs, err := ifi.Addrs()
|
|
|
|
|
if err == nil {
|
|
|
|
|
r.Addrs = make([]NetInterfaceAddr, 0, len(addrs))
|
|
|
|
|
r.Addrs = make([]InterfaceAddr, 0, len(addrs))
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
|
r.Addrs = append(r.Addrs, NetInterfaceAddr{
|
|
|
|
|
r.Addrs = append(r.Addrs, InterfaceAddr{
|
|
|
|
|
Addr: addr.String(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
@ -153,8 +153,8 @@ func NetInterfaces() ([]NetInterfaceStat, error) {
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getNetIOCountersAll(n []NetIOCountersStat) ([]NetIOCountersStat, error) {
|
|
|
|
|
r := NetIOCountersStat{
|
|
|
|
|
func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) {
|
|
|
|
|
r := IOCountersStat{
|
|
|
|
|
Name: "all",
|
|
|
|
|
}
|
|
|
|
|
for _, nic := range n {
|
|
|
|
@ -168,38 +168,38 @@ func getNetIOCountersAll(n []NetIOCountersStat) ([]NetIOCountersStat, error) {
|
|
|
|
|
r.Dropout += nic.Dropout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return []NetIOCountersStat{r}, nil
|
|
|
|
|
return []IOCountersStat{r}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseNetLine(line string) (NetConnectionStat, error) {
|
|
|
|
|
func parseNetLine(line string) (ConnectionStat, error) {
|
|
|
|
|
f := strings.Fields(line)
|
|
|
|
|
if len(f) < 9 {
|
|
|
|
|
return NetConnectionStat{}, fmt.Errorf("wrong line,%s", line)
|
|
|
|
|
return ConnectionStat{}, fmt.Errorf("wrong line,%s", line)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(f[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return NetConnectionStat{}, err
|
|
|
|
|
return ConnectionStat{}, err
|
|
|
|
|
}
|
|
|
|
|
fd, err := strconv.Atoi(strings.Trim(f[3], "u"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return NetConnectionStat{}, fmt.Errorf("unknown fd, %s", f[3])
|
|
|
|
|
return ConnectionStat{}, fmt.Errorf("unknown fd, %s", f[3])
|
|
|
|
|
}
|
|
|
|
|
netFamily, ok := constMap[f[4]]
|
|
|
|
|
if !ok {
|
|
|
|
|
return NetConnectionStat{}, fmt.Errorf("unknown family, %s", f[4])
|
|
|
|
|
return ConnectionStat{}, fmt.Errorf("unknown family, %s", f[4])
|
|
|
|
|
}
|
|
|
|
|
netType, ok := constMap[f[7]]
|
|
|
|
|
if !ok {
|
|
|
|
|
return NetConnectionStat{}, fmt.Errorf("unknown type, %s", f[7])
|
|
|
|
|
return ConnectionStat{}, fmt.Errorf("unknown type, %s", f[7])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
laddr, raddr, err := parseNetAddr(f[8])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return NetConnectionStat{}, fmt.Errorf("failed to parse netaddr, %s", f[8])
|
|
|
|
|
return ConnectionStat{}, fmt.Errorf("failed to parse netaddr, %s", f[8])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n := NetConnectionStat{
|
|
|
|
|
n := ConnectionStat{
|
|
|
|
|
Fd: uint32(fd),
|
|
|
|
|
Family: uint32(netFamily),
|
|
|
|
|
Type: uint32(netType),
|
|
|
|
|