package net
import (
"context"
"encoding/json"
"net"
"github.com/gofiber/fiber/v2/internal/gopsutil/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 InterfaceStat struct {
Index int `json:"index"`
MTU int `json:"mtu"`
Name string `json:"name"`
HardwareAddr string `json:"hardwareaddr"`
Flags []string `json:"flags"`
Addrs []InterfaceAddr `json:"addrs"`
}
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:"delete_list"`
Insert uint32 `json:"insert"`
InsertFailed uint32 `json:"insert_failed"`
Drop uint32 `json:"drop"`
EarlyDrop uint32 `json:"early_drop"`
IcmpError uint32 `json:"icmp_error"`
ExpectNew uint32 `json:"expect_new"`
ExpectCreate uint32 `json:"expect_create"`
ExpectDelete uint32 `json:"expect_delete"`
SearchRestart uint32 `json:"search_restart"`
}
func NewConntrackStat (e , s , f , n , inv , ign , del , dlst , ins , insfail , drop , edrop , ie , en , ec , ed , 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 (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 () ([]InterfaceStat , error ) {
return InterfacesWithContext (context .Background ())
}
func InterfacesWithContext (ctx context .Context ) ([]InterfaceStat , error ) {
is , err := net .Interfaces ()
if err != nil {
return nil , err
}
ret := make ([]InterfaceStat , 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 ([]InterfaceAddr , 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.6.7 . (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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds .