package net
import (
"context"
"encoding/json"
"net"
"github.com/shirou/gopsutil/v3/internal/common"
)
var invoke common .Invoker = common .Invoke {}
type IOCountersStat struct {
Name string `json:"name"`
BytesSent uint64 `json:"bytesSent"`
BytesRecv uint64 `json:"bytesRecv"`
PacketsSent uint64 `json:"packetsSent"`
PacketsRecv uint64 `json:"packetsRecv"`
Errin uint64 `json:"errin"`
Errout uint64 `json:"errout"`
Dropin uint64 `json:"dropin"`
Dropout uint64 `json:"dropout"`
Fifoin uint64 `json:"fifoin"`
Fifoout uint64 `json:"fifoout"`
}
type Addr struct {
IP string `json:"ip"`
Port uint32 `json:"port"`
}
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"`
}
type ProtoCountersStat struct {
Protocol string `json:"protocol"`
Stats map [string ]int64 `json:"stats"`
}
type InterfaceAddr struct {
Addr string `json:"addr"`
}
type InterfaceAddrList []InterfaceAddr
type InterfaceStat struct {
Index int `json:"index"`
MTU int `json:"mtu"`
Name string `json:"name"`
HardwareAddr string `json:"hardwareAddr"`
Flags []string `json:"flags"`
Addrs InterfaceAddrList `json:"addrs"`
}
type InterfaceStatList []InterfaceStat
type FilterStat struct {
ConnTrackCount int64 `json:"connTrackCount"`
ConnTrackMax int64 `json:"connTrackMax"`
}
type ConntrackStat struct {
Entries uint32 `json:"entries"`
Searched uint32 `json:"searched"`
Found uint32 `json:"found"`
New uint32 `json:"new"`
Invalid uint32 `json:"invalid"`
Ignore uint32 `json:"ignore"`
Delete uint32 `json:"delete"`
DeleteList uint32 `json:"deleteList"`
Insert uint32 `json:"insert"`
InsertFailed uint32 `json:"insertFailed"`
Drop uint32 `json:"drop"`
EarlyDrop uint32 `json:"earlyDrop"`
IcmpError uint32 `json:"icmpError"`
ExpectNew uint32 `json:"expectNew"`
ExpectCreate uint32 `json:"expectCreate"`
ExpectDelete uint32 `json:"expectDelete"`
SearchRestart uint32 `json:"searchRestart"`
}
func NewConntrackStat (e uint32 , s uint32 , f uint32 , n uint32 , inv uint32 , ign uint32 , del uint32 , dlst uint32 , ins uint32 , insfail uint32 , drop uint32 , edrop uint32 , ie uint32 , en uint32 , ec uint32 , ed uint32 , sr uint32 ) *ConntrackStat {
return &ConntrackStat {
Entries : e ,
Searched : s ,
Found : f ,
New : n ,
Invalid : inv ,
Ignore : ign ,
Delete : del ,
DeleteList : dlst ,
Insert : ins ,
InsertFailed : insfail ,
Drop : drop ,
EarlyDrop : edrop ,
IcmpError : ie ,
ExpectNew : en ,
ExpectCreate : ec ,
ExpectDelete : ed ,
SearchRestart : sr ,
}
}
type ConntrackStatList struct {
items []*ConntrackStat
}
func NewConntrackStatList () *ConntrackStatList {
return &ConntrackStatList {
items : []*ConntrackStat {},
}
}
func (l *ConntrackStatList ) Append (c *ConntrackStat ) {
l .items = append (l .items , c )
}
func (l *ConntrackStatList ) Items () []ConntrackStat {
items := make ([]ConntrackStat , len (l .items ))
for i , el := range l .items {
items [i ] = *el
}
return items
}
func (l *ConntrackStatList ) Summary () []ConntrackStat {
summary := NewConntrackStat (0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
for _ , cs := range l .items {
summary .Entries += cs .Entries
summary .Searched += cs .Searched
summary .Found += cs .Found
summary .New += cs .New
summary .Invalid += cs .Invalid
summary .Ignore += cs .Ignore
summary .Delete += cs .Delete
summary .DeleteList += cs .DeleteList
summary .Insert += cs .Insert
summary .InsertFailed += cs .InsertFailed
summary .Drop += cs .Drop
summary .EarlyDrop += cs .EarlyDrop
summary .IcmpError += cs .IcmpError
summary .ExpectNew += cs .ExpectNew
summary .ExpectCreate += cs .ExpectCreate
summary .ExpectDelete += cs .ExpectDelete
summary .SearchRestart += cs .SearchRestart
}
return []ConntrackStat {*summary }
}
func (n IOCountersStat ) String () string {
s , _ := json .Marshal (n )
return string (s )
}
func (n ConnectionStat ) String () string {
s , _ := json .Marshal (n )
return string (s )
}
func (n ProtoCountersStat ) String () string {
s , _ := json .Marshal (n )
return string (s )
}
func (a Addr ) String () string {
s , _ := json .Marshal (a )
return string (s )
}
func (n InterfaceStat ) String () string {
s , _ := json .Marshal (n )
return string (s )
}
func (l InterfaceStatList ) String () string {
s , _ := json .Marshal (l )
return string (s )
}
func (n InterfaceAddr ) String () string {
s , _ := json .Marshal (n )
return string (s )
}
func (n ConntrackStat ) String () string {
s , _ := json .Marshal (n )
return string (s )
}
func Interfaces () (InterfaceStatList , error ) {
return InterfacesWithContext (context .Background ())
}
func InterfacesWithContext (ctx context .Context ) (InterfaceStatList , error ) {
is , err := net .Interfaces ()
if err != nil {
return nil , err
}
ret := make (InterfaceStatList , 0 , len (is ))
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" )
}
r := InterfaceStat {
Index : ifi .Index ,
Name : ifi .Name ,
MTU : ifi .MTU ,
HardwareAddr : ifi .HardwareAddr .String (),
Flags : flags ,
}
addrs , err := ifi .Addrs ()
if err == nil {
r .Addrs = make (InterfaceAddrList , 0 , len (addrs ))
for _ , addr := range addrs {
r .Addrs = append (r .Addrs , InterfaceAddr {
Addr : addr .String (),
})
}
}
ret = append (ret , r )
}
return ret , nil
}
func getIOCountersAll(n []IOCountersStat ) ([]IOCountersStat , error ) {
r := IOCountersStat {
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 []IOCountersStat {r }, nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .