pinger

package
v0.0.0-...-22022a5 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package pinger implements ICMP ping functionality for the agent

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRawSocketUnsupported is sent when the pinger is configured to use raw sockets
	// when raw socket based pings are not supported on the system
	ErrRawSocketUnsupported = errors.New("raw socket cannot be used with this OS")
	// ErrUDPSocketUnsupported is sent when the pinger is configured to use UDP sockets
	// when UDP socket based pings are not supported on the system
	ErrUDPSocketUnsupported = errors.New("udp socket cannot be used with this OS")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// UseRawSocket determines the socket type to use
	// RAW or UDP
	UseRawSocket bool
	// Interval is the amount of time to wait between
	// sending ICMP packets, default is 1 second
	Interval time.Duration
	// Timeout is the total time to wait for all pings
	// to complete
	Timeout time.Duration
	// Count is the number of ICMP packets, pings, to send
	Count int
}

Config defines how pings should be run across all hosts

type LinuxPinger

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

LinuxPinger implements the Pinger interface for Linux users

func (*LinuxPinger) Ping

func (p *LinuxPinger) Ping(host string) (*Result, error)

Ping takes a host and depending on the config will either directly ping the host sending packets over a UDP socket or a raw socket

type MockPinger

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

MockPinger is a pinger used for testing

func NewMockPinger

func NewMockPinger(res *Result, err error) *MockPinger

NewMockPinger returns a pinger that

func (*MockPinger) Ping

func (m *MockPinger) Ping(_ string) (*Result, error)

Ping ignores the passed in host and returns the result and error set in the constructor

type Pinger

type Pinger interface {
	Ping(host string) (*Result, error)
}

Pinger is an interface for sending an ICMP ping to a host

func New

func New(cfg Config) (Pinger, error)

New creates a LinuxPinger using the passed in config

type Result

type Result struct {
	// CanConnect is true if we receive a response from any
	// of the packets on the host
	CanConnect bool `json:"can_connect"`
	// PacketLoss indicates the percentage of packets lost
	PacketLoss float64 `json:"packet_loss"`
	// AvgRtt is the average round trip time
	AvgRtt time.Duration `json:"avg_rtt"`
}

Result encapsulates the results of a single run of ping

func RunPing

func RunPing(cfg *Config, host string) (*Result, error)

RunPing creates a pinger for the requested host and sends the requested number of packets to it

Jump to

Keyboard shortcuts

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