common

package
v0.0.0-...-1d39891 Latest Latest
Warning

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

Go to latest
Published: May 12, 2020 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BPFCapability the probe is able to handle bpf filters
	BPFCapability ProbeCapability = 1 << 0
	// RawPacketsCapability the probe can capture raw packets
	RawPacketsCapability = 1 << 1
	// ExtraTCPMetricCapability the probe can report TCP metrics
	ExtraTCPMetricCapability = 1 << 2
	// MultipleOnSameNodeCapability is defined on probes that support multiple captures of the same type on one node
	MultipleOnSameNodeCapability = 1 << 3
)
View Source
const (
	AllPackets    = syscall.ETH_P_ALL
	OnlyIPPackets = syscall.ETH_P_IP
)

Protocols to receive

Variables

View Source
var (
	// ProbeTypes returns a list of all the capture probes
	ProbeTypes = []string{"ovssflow", "pcapsocket", "ovsmirror", "dpdk", "afpacket", "pcap", "ebpf", "sflow", "ovsnetflow"}

	// CaptureTypes contains all registered capture type and associated probes
	CaptureTypes = map[string]CaptureType{}

	// ProbeCapabilities defines capability per probes
	ProbeCapabilities = map[string]ProbeCapability{}
)
View Source
var (
	// ErrInvalidPortRange invalid port range
	ErrInvalidPortRange = errors.New("Invalid port range")
	// ErrNoPortLeft no port left in the range
	ErrNoPortLeft = errors.New("No free port left")
)
View Source
var (
	// ErrCantCompareInterface error can't compare interface
	ErrCantCompareInterface = errors.New("Can't compare interface")
	// ErrFieldNotFound error field not found
	ErrFieldNotFound = errors.New("Field not found")
	// ErrFieldWrongType error field has wrong type
	ErrFieldWrongType = errors.New("Field has wrong type")
	// ErrNotFound error no result was found
	ErrNotFound = errors.New("No result found")
	// ErrTimeout network timeout
	ErrTimeout = errors.New("Timeout")
	// ErrNotImplemented unimplemented feature
	ErrNotImplemented = errors.New("Not implemented")
)

Functions

func CheckProbeCapabilities

func CheckProbeCapabilities(probeType string, capability ProbeCapability) bool

CheckProbeCapabilities checks that a probe supports given capabilities

func DelField

func DelField(obj map[string]interface{}, k string) bool

DelField deletes a value in a tree based on dot key

func GetMapField

func GetMapField(obj map[string]interface{}, k string) (interface{}, error)

GetMapField retrieves a value from a tree from the dot key like "a.b.c.d"

func GetMapFieldKeys

func GetMapFieldKeys(obj map[string]interface{}) []string

GetMapFieldKeys returns all the keys using dot notation

func IPStrToUint32

func IPStrToUint32(ipAddr string) (uint32, error)

IPStrToUint32 converts IP string to 32bits

func IPV4CIDRToRegex

func IPV4CIDRToRegex(cidr string) (string, error)

IPV4CIDRToRegex returns a regex matching IPs belonging to a given cidr

func IsCaptureAllowed

func IsCaptureAllowed(nodeType string) bool

IsCaptureAllowed returns true if the node capture type exist

func IsIPv6

func IsIPv6(addr string) bool

IsIPv6 returns whether is a IPV6 addresses or not

func JSONDecode

func JSONDecode(r io.Reader, i interface{}) error

JSONDecode wrapper to UseNumber during JSON decoding

func LookupPath

func LookupPath(obj interface{}, path string, kind reflect.Kind) (reflect.Value, bool)

LookupPath lookup through the given obj according to the given path return the value found if the kind matches

func MaxInt64

func MaxInt64(a, b int64) int64

MaxInt64 returns the biggest value

func MinInt64

func MinInt64(a, b int64) int64

MinInt64 returns the lowest value

func NormalizeAddrForURL

func NormalizeAddrForURL(addr string) string

NormalizeAddrForURL format the given address to be used in URL. For IPV6 addresses the brackets will be added.

func NormalizeIP

func NormalizeIP(ip, ipFamily string) string

NormalizeIP adds a default netmask

func NormalizeIPForURL

func NormalizeIPForURL(ip net.IP) string

NormalizeIPForURL returns a string normalized that can be used in URL. Brackets will be used for IPV6 addresses.

func NormalizeValue

func NormalizeValue(obj interface{}) interface{}

NormalizeValue returns a version of the passed value that can be safely marshalled to JSON

func ParseAddr

func ParseAddr(address string) (protocol string, target string, err error)

ParseAddr parses an address of the form protocol://target such as unix:////var/run/service/program.sock or tcp://my.domain:2134. It also handles addresses of the form address:port and assumes it uses TCP.

func ProbeTypeForNode

func ProbeTypeForNode(nodeType string, captureType string) (string, error)

ProbeTypeForNode returns the appropriate probe type for the given node type and capture type.

func Profile

func Profile()

Profile start profiling loop

func RandString

func RandString(n int) string

RandString generates random string

func RangeToRegex

func RangeToRegex(min, max int) string

RangeToRegex returns a regular expression matching number in the given range Golang version of https://github.com/dimka665/range-regex

func Retry

func Retry(fnc func() error, try int, delay time.Duration) error

Retry tries to execute the given function until a success applying a delay between each try

func RetryExponential

func RetryExponential(fnc func() error, try int, baseDelay time.Duration) error

RetryExponential tries to execute the given function until a success applying a delay between each try. The delay is doubled after each try. Its initial value is baseDelay.

func SetMapField

func SetMapField(obj map[string]interface{}, k string, v interface{}) bool

SetMapField set a value in a tree based on dot key ("a.b.c.d" = "ok")

func SetupTLSClientConfig

func SetupTLSClientConfig(certPEM string, keyPEM string) (*tls.Config, error)

SetupTLSClientConfig creates a client X509 certificate from public and private key

func SetupTLSLoadCA

func SetupTLSLoadCA(certPEM string) (*x509.CertPool, error)

SetupTLSLoadCA creates an X509 certificate from file

func SetupTLSServerConfig

func SetupTLSServerConfig(certPEM string, keyPEM string) (*tls.Config, error)

SetupTLSServerConfig creates a server X509 certificate from public and private key

func StructFieldKeys

func StructFieldKeys(i interface{}) []string

StructFieldKeys returns field names of a structure

func ToInt64

func ToInt64(i interface{}) (int64, error)

ToInt64 Convert all number like type to int64

func UnixMillis

func UnixMillis(t time.Time) int64

UnixMillis returns the current time in miliseconds

Types

type BoolPredicate

type BoolPredicate func(b bool) bool

BoolPredicate is a function that applies a test against a boolean

type CaptureType

type CaptureType struct {
	Allowed []string
	Default string
}

CaptureType describes a list of allowed and default captures probes

type Debouncer

type Debouncer struct {
	// contains filtered or unexported fields
}

Debouncer defines a debouncer object

func NewDebouncer

func NewDebouncer(interval time.Duration, callback func()) *Debouncer

NewDebouncer returns a Debouncer calling the given callback after a certain amount of time.

func (*Debouncer) Call

func (d *Debouncer) Call()

Call calls the callback of the Debouncer object

func (*Debouncer) Start

func (d *Debouncer) Start()

Start the debouncer

func (*Debouncer) Stop

func (d *Debouncer) Stop()

Stop stops the debouncer

type Getter

type Getter interface {
	GetField(field string) (interface{}, error)
	GetFieldKeys() []string
	GetFieldBool(field string) (bool, error)
	GetFieldInt64(field string) (int64, error)
	GetFieldString(field string) (string, error)
	MatchBool(field string, predicate BoolPredicate) bool
	MatchInt64(field string, predicate Int64Predicate) bool
	MatchString(field string, predicate StringPredicate) bool
}

Getter describes filter getter fields

type Int64Predicate

type Int64Predicate func(i int64) bool

Int64Predicate is a function that applies a test against an integer

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator describes a int64 iterator

func NewIterator

func NewIterator(values ...int64) (it *Iterator)

NewIterator creates a new iterator based on (at, from, to) parameters

func (*Iterator) Done

func (it *Iterator) Done() bool

Done returns true when the Iterator is at the end or uninitialized properly

func (*Iterator) Next

func (it *Iterator) Next() bool

Next returns true if we can continue to iterate

type MasterElection

type MasterElection interface {
	Start()
	StartAndWait()
	Stop()
	IsMaster() bool
	AddEventListener(listener MasterElectionListener)
	TTL() time.Duration
}

MasterElection describes the master election mechanism

type MasterElectionListener

type MasterElectionListener interface {
	OnStartAsMaster()
	OnStartAsSlave()
	OnSwitchToMaster()
	OnSwitchToSlave()
}

MasterElectionListener describes the multi election mechanism

type MasterElectionService

type MasterElectionService interface {
	NewElection(key string) MasterElection
}

MasterElectionService describes the election service mechanism

type Metric

type Metric interface {
	// part of the Getter interface
	GetFieldInt64(field string) (int64, error)
	GetFieldKeys() []string

	Add(m Metric) Metric
	Sub(m Metric) Metric
	Split(cut int64) (Metric, Metric)
	GetStart() int64
	SetStart(start int64)
	GetLast() int64
	SetLast(last int64)
	IsZero() bool
}

Metric defines a common metric interface

type NetNSContext

type NetNSContext struct {
	// contains filtered or unexported fields
}

NetNSContext describes a NameSpace Context switch API

func NewNetNsContext

func NewNetNsContext(path string) (*NetNSContext, error)

NewNetNsContext creates a new NameSpace context base on path

func (*NetNSContext) Close

func (n *NetNSContext) Close()

Close the NameSpace

func (*NetNSContext) Quit

func (n *NetNSContext) Quit() error

Quit the NameSpace and go back to the original one

type PortAllocator

type PortAllocator struct {
	RWMutex
	MinPort int
	MaxPort int
	PortMap map[int]bool
}

PortAllocator describes a threads safe port list that can be allocated

func NewPortAllocator

func NewPortAllocator(min, max int) (*PortAllocator, error)

NewPortAllocator creates a new port allocator range

func (*PortAllocator) Allocate

func (p *PortAllocator) Allocate(fnc func(int) error) (int, error)

Allocate returns a new port between min and max ports. Call the given function that will validate the allocation

func (*PortAllocator) Release

func (p *PortAllocator) Release(i int) error

Release a port

func (*PortAllocator) ReleaseAll

func (p *PortAllocator) ReleaseAll()

ReleaseAll ports

type ProbeCapability

type ProbeCapability int

ProbeCapability defines probe capability

type ProcessInfo

type ProcessInfo struct {
	Process  string
	Pid      int64
	Name     string
	PPid     int64
	PGrp     int64
	Session  int64
	Utime    int64
	Stime    int64
	CUtime   int64
	CStime   int64
	Priority int64
	Nice     int64
	Threads  int64
	Start    int64
	Vsize    int64
	RSS      int64
	State    ProcessState
}

ProcessInfo describes the information of a running process

func GetProcessInfo

func GetProcessInfo(pid int) (*ProcessInfo, error)

GetProcessInfo retrieve process info from /proc

type ProcessState

type ProcessState rune

ProcessState describes the state of a process

const (
	// Running state
	Running ProcessState = 'R'
	// Sleeping state
	Sleeping ProcessState = 'S'
	// Waiting state
	Waiting ProcessState = 'D'
	// Zombie state
	Zombie ProcessState = 'Z'
	// Stopped state
	Stopped ProcessState = 'T'
	// TracingStop state
	TracingStop ProcessState = 't'
	// Dead state
	Dead ProcessState = 'X'
)

type RWMutex

type RWMutex struct {
	sync.RWMutex
}

RWMutex is a wrapper around sync.RWMutex

type RawSocket

type RawSocket struct {
	// contains filtered or unexported fields
}

RawSocket describes a raw socket C implemenation

func NewRawSocket

func NewRawSocket(ifName string, protocol int) (*RawSocket, error)

NewRawSocket creates a raw socket for the network interface ifName

func NewRawSocketInNs

func NewRawSocketInNs(nsPath string, ifName string, protocol int) (*RawSocket, error)

NewRawSocketInNs create/open a socket in the namespace nsPath for the network interface ifName

func (*RawSocket) Close

func (s *RawSocket) Close() error

Close the file descriptor

func (*RawSocket) GetFd

func (s *RawSocket) GetFd() int

GetFd returns the file descriptor

func (*RawSocket) Write

func (s *RawSocket) Write(data []byte) (int, error)

Write outputs some bytes to the file

type Service

type Service struct {
	Type ServiceType
	ID   string
}

Service describes a service identified by its type and identifier

type ServiceAddress

type ServiceAddress struct {
	Addr string
	Port int
}

ServiceAddress describes the service listening address and port

func ServiceAddressFromString

func ServiceAddressFromString(addressPort string) (ServiceAddress, error)

ServiceAddressFromString returns a service address from a string, could be IPv4 or IPv6

func (ServiceAddress) String

func (sa ServiceAddress) String() string

type ServiceState

type ServiceState int64

ServiceState describes the state of a service.

const (
	// StoppedState service stopped
	StoppedState ServiceState = iota + 1
	// StartingState service starting
	StartingState
	// RunningState service running
	RunningState
	// StoppingState service stopping
	StoppingState
)

func (*ServiceState) CompareAndSwap

func (s *ServiceState) CompareAndSwap(old, new ServiceState) bool

CompareAndSwap executes the compare-and-swap operation for a state

func (*ServiceState) Load

func (s *ServiceState) Load() ServiceState

Load atomatically loads and returns the state

func (*ServiceState) MarshalJSON

func (s *ServiceState) MarshalJSON() ([]byte, error)

MarshalJSON marshal the connection state to JSON

func (*ServiceState) Store

func (s *ServiceState) Store(state ServiceState)

Store atomatically stores the state

type ServiceType

type ServiceType string

ServiceType describes the service type (analyzer or agent)

const (
	// UnknownService for unknown client types
	UnknownService ServiceType = "unknown"
	// AnalyzerService analyzer
	AnalyzerService ServiceType = "analyzer"
	// AgentService agent
	AgentService ServiceType = "agent"
)

func (ServiceType) String

func (st ServiceType) String() string

type SortOrder

type SortOrder string

SortOrder describes ascending or descending order

const (
	// SortAscending sorting order
	SortAscending SortOrder = "ASC"
	// SortDescending sorting order
	SortDescending SortOrder = "DESC"
)

type StringPredicate

type StringPredicate func(s string) bool

StringPredicate is a function that applies a test against a string

type TimeSlice

type TimeSlice struct {
	Start int64 `json:"Start"`
	Last  int64 `json:"Last"`
}

TimeSlice defines a time boudary values

func NewTimeSlice

func NewTimeSlice(s, l int64) *TimeSlice

NewTimeSlice creates a new TimeSlice based on Start and Last

Jump to

Keyboard shortcuts

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