network

package
v1.10.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2023 License: MIT Imports: 8 Imported by: 0

README

Network package

This package is useful to retrieve network interface information and counter.

The information available are :

  • name of the interface (cannot be empty)
  • physical address (or hardware address) (can be empty for example virtual ip or bridge)
  • MTU (Maximu Transmission Unit) is the maximum size of packets could be transmit
  • Addr is the list of all network address (can be empty)
  • Index is the index for the current interface into the list all interface for current os
  • Flags are capabilities supported by the interface. You can find const for all flags available in golang/x/net package :
    • FlagUp: interface is up
    • FlagBroadcast: interface supports broadcast access capability
    • FlagLoopback: interface is a loopback interface
    • FlagPointToPoint: interface belongs to a point-to-point link
    • FlagMulticast: interface supports multicast access capability

Example of implement

You can get all interface with filter.

In this example we will search all interface who's have physical address and having at least on address =and match the list of flags composed here with only FlagUp :

// GetAllInterfaces(context.Context, onlyPhysical, hasAddr bool, atLeastMTU int, withFlags ...net.Flags)
list, err := network.GetAllInterfaces(context.Background(), true, true, 0, net.FlagUp)

You can also retrieve on interface based on his name or his phyisical address :

//NewInterface(context.Context, name, physical string)
i, e := NewInterface(context.Context, "eth0", "") 

When you have you interface variable, you can use it to print for example the counter of received data.

The available statistics are grouped by way : In or Out In each group, the stats are :

  • StatBytes : Traffic in Bytes
  • StatPackets : Number of Packets
  • StatFifo : Number of packet in FIFO
  • StatDrop : Number of packet dropped
  • StatErr : Number of packet in Error

Stats are given as map[Stats]Number :

  • Stats is a self defined to map constant with a statement.
  • Number are still self defined type to allow formatting.
  • Bytes is closed similar to Number but with different unit

This translation between bytes and number included into each type :

  • Number.AsBytes
  • Bytes.AsNumber

You can also convert this type as uint64 or as float64.

Now, we have all information to understand this example who's print all stat of sending traffic with unit :

		fmt.Print("\tOut: \n")
 		l = i.GetStatOut()
 		for _, k := range network.ListStatsSort() {
 			s := network.Stats(k)
 			if v, ok := l[s]; ok {
 				fmt.Printf("\t\t%s\n", s.FormatLabelUnitPadded(v))
 			} else {
 				fmt.Printf("\t\t%s\n", s.FormatLabelUnitPadded(0))
 			}
 		}

Example

You can find an example in test/test-network

There result of this example is like this :

iface 'eth0' [00:15:5d:66:60:dd] :
        Flags: up broadcast multicast
        Addrs: {"addr":"172.25.59.95/20"} {"addr":"fe80::215:5dff:fe66:60dd/64"}
        In:
                Traffic:   27.33 GB
                Packets:   20 M
                Fifo:       0
                Drop:       0
                Error:      0
        Out:
                Traffic:   28.03 GB
                Packets: 1118 K
                Fifo:       0
                Drop:       0
                Error:      0

Documentation

Index

Constants

View Source
const (
	ErrorParamsEmpty errors.CodeError = iota + errors.MinPkgNetwork
	ErrorNetCounter
	ErrorNetInterface
	ErrorNetNotFound
	ErrorNetReload
)

Variables

This section is empty.

Functions

func FindAllFlagInList

func FindAllFlagInList(list []string, flags []net.Flags) bool

func FindFlagInList

func FindFlagInList(list []string, flag net.Flags) bool

func IsCodeError

func IsCodeError() bool

func ListStatsSort

func ListStatsSort() []int

Types

type Bytes

type Bytes uint64

func (Bytes) AsFloat64

func (n Bytes) AsFloat64() float64

func (Bytes) AsNumber

func (n Bytes) AsNumber() Number

func (Bytes) AsUint64

func (n Bytes) AsUint64() uint64

func (Bytes) FormatUnitFloat

func (n Bytes) FormatUnitFloat(precision int) string

func (Bytes) FormatUnitInt

func (n Bytes) FormatUnitInt() string

func (Bytes) String

func (n Bytes) String() string

type Interface

type Interface interface {
	ReLoad(ctx context.Context) errors.Error

	GetIndex() int
	GetName() string
	GetHardwareAddr() string
	GetMTU() int
	GetAddr() []string

	IsUp() bool
	HasPhysical() bool

	GetFlags() []string

	GetStatIn() map[Stats]Number
	GetStatOut() map[Stats]Number
}

func GetAllInterfaces

func GetAllInterfaces(ctx context.Context, onlyPhysical, hasAddr bool, atLeastMTU int, withFlags ...net.Flags) ([]Interface, errors.Error)

func NewInterface

func NewInterface(ctx context.Context, name, physical string) (Interface, errors.Error)

type Number

type Number uint64

func (Number) AsBytes

func (n Number) AsBytes() Bytes

func (Number) AsFloat64

func (n Number) AsFloat64() float64

func (Number) AsUint64

func (n Number) AsUint64() uint64

func (Number) FormatUnitFloat

func (n Number) FormatUnitFloat(precision int) string

func (Number) FormatUnitInt

func (n Number) FormatUnitInt() string

func (Number) String

func (n Number) String() string

type Stats

type Stats uint8
const (
	StatBytes Stats = iota + 1
	StatPackets
	StatFifo
	StatDrop
	StatErr
)

func (Stats) FormatLabelUnit

func (s Stats) FormatLabelUnit(n Number) string

func (Stats) FormatLabelUnitPadded

func (s Stats) FormatLabelUnitPadded(n Number) string

func (Stats) FormatUnit

func (s Stats) FormatUnit(n Number) string

func (Stats) FormatUnitFloat

func (s Stats) FormatUnitFloat(n Number, precision int) string

func (Stats) FormatUnitInt

func (s Stats) FormatUnitInt(n Number) string

func (Stats) String

func (s Stats) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL