checker

package
v0.0.0-...-e3e1421 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2025 License: Apache-2.0 Imports: 17 Imported by: 1

Documentation

Overview

Package checker provides an easy way to talk to NTPD/Chronyd and get NTPCheckResult, which abstracts away Chrony/NTP control protocol implementation details and contains tons of information useful for NTP monitoring, like NTP server variables including offset, peers and their variables and statuses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSyncSource

func GetSyncSource(p *Peer, clockSource string) string

GetSyncSource returns human readable representation of sync source address for remote, 4 rune word for local

func NewNTPPeerStats

func NewNTPPeerStats(r *NTPCheckResult, noDNS bool) (map[string]any, error)

NewNTPPeerStats constructs NTPStats from NTPCheckResult

Types

type ChronyCheck

type ChronyCheck struct {
	Client chronyClient
	// contains filtered or unexported fields
}

ChronyCheck gathers NTP stats using chronyc/chronyd protocol client

func NewChronyCheck

func NewChronyCheck(conn io.ReadWriter) *ChronyCheck

NewChronyCheck is a constructor for ChronyCheck

func (*ChronyCheck) Run

func (n *ChronyCheck) Run() (*NTPCheckResult, error)

Run is the main method of ChronyCheck and it fetches all information to return NTPCheckResult. Essentially we request tracking info, num of peers, and then request source_data and ntp_data for each peer individually.

func (*ChronyCheck) ServerStats

func (n *ChronyCheck) ServerStats() (*ServerStats, error)

ServerStats return server stats

func (*ChronyCheck) Unix

func (n *ChronyCheck) Unix() bool

Unix returns true if connected via a unix socket

type NTPCheck

type NTPCheck struct {
	Client ntpClient
}

NTPCheck gathers NTP stats using UDP network client

func NewNTPCheck

func NewNTPCheck(conn io.ReadWriter) *NTPCheck

NewNTPCheck is a constructor for NTPCheck

func (*NTPCheck) ReadServerVariables

func (n *NTPCheck) ReadServerVariables() (*control.NTPControlMsg, error)

ReadServerVariables sends Read Variables packet asking for server vars and returns response packet

func (*NTPCheck) ReadStatus

func (n *NTPCheck) ReadStatus() (*control.NTPControlMsg, error)

ReadStatus sends Read Status packet and returns response packet

func (*NTPCheck) ReadVariables

func (n *NTPCheck) ReadVariables(associationID uint16) (*control.NTPControlMsg, error)

ReadVariables sends Read Variables packet for associationID. associationID set to 0 means 'read local system variables'

func (*NTPCheck) Run

func (n *NTPCheck) Run() (*NTPCheckResult, error)

Run is the main method of NTPCheck and it fetches all information to return NTPCheckResult. Essentially we request system status that contains list of peers, and then request variables for our server and each peer individually.

func (*NTPCheck) ServerStats

func (n *NTPCheck) ServerStats() (*ServerStats, error)

ServerStats return server stats

type NTPCheckResult

type NTPCheckResult struct {
	// parsed from SystemStatusWord
	LI          uint8
	LIDesc      string
	ClockSource string
	Correction  float64
	Event       string
	EventCount  uint8
	// data parsed from System Variables
	SysVars *SystemVariables
	// map of peers with data from PeerStatusWord and Peer Variables
	Peers map[uint16]*Peer
}

NTPCheckResult represents result of NTPCheck run populated with information about the server and its peers.

func NewNTPCheckResult

func NewNTPCheckResult() *NTPCheckResult

NewNTPCheckResult returns new instance of NewNTPCheckResult

func RunCheck

func RunCheck(address string) (*NTPCheckResult, error)

RunCheck is a simple wrapper to connect to address and run NTPCheck.Run()

func RunNTPData

func RunNTPData(address string) (*NTPCheckResult, error)

RunNTPData is a simple wrapper to connect to address and run NTPCheck.Run() If using chrony it gathers extra info about the peers using the unix socket

func (*NTPCheckResult) FindAcceptableNonSysPeers

func (r *NTPCheckResult) FindAcceptableNonSysPeers() ([]*Peer, error)

FindAcceptableNonSysPeers returns list of peers suitable for checking time with excluding sys.peer

func (*NTPCheckResult) FindGoodPeers

func (r *NTPCheckResult) FindGoodPeers() ([]*Peer, error)

FindGoodPeers returns list of peers suitable for synchronization

func (*NTPCheckResult) FindNoSelectPeers

func (r *NTPCheckResult) FindNoSelectPeers() ([]*Peer, error)

FindNoSelectPeers returns list of peers in noselect state

func (*NTPCheckResult) FindSysPeer

func (r *NTPCheckResult) FindSysPeer() (*Peer, error)

FindSysPeer returns sys.peer (main source of NTP information for server)

type NTPStats

type NTPStats struct {
	PeerDelay             float64 `json:"ntp.peer.delay"`                      // sys.peer delay in ms
	PeerPoll              int     `json:"ntp.peer.poll"`                       // sys.peer poll in seconds
	PeerJitter            float64 `json:"ntp.peer.jitter"`                     // sys.peer jitter in ms
	PeerOffset            float64 `json:"ntp.peer.offset"`                     // sys.peer offset in ms
	PeerStratum           int     `json:"ntp.peer.stratum"`                    // sys.peer stratum
	Frequency             float64 `json:"ntp.sys.frequency"`                   // clock frequency in PPM
	Offset                float64 `json:"ntp.sys.offset"`                      // tracking clock offset in MS
	RootDelay             float64 `json:"ntp.sys.root_delay"`                  // tracking root delay in MS
	StatError             bool    `json:"ntp.stat.error"`                      // error reported in Leap Status
	Correction            float64 `json:"ntp.correction"`                      // current correction
	PeerCount             int     `json:"ntp.peer.count"`                      // number of upstream peers
	OffsetComparedToPeers float64 `json:"ntp.sys.offset_selected_vs_peers_ms"` // sys peer offset vs median peer offset in ms
}

NTPStats are metrics for upstream reporting

func NewNTPStats

func NewNTPStats(r *NTPCheckResult) (*NTPStats, error)

NewNTPStats constructs NTPStats from NTPCheckResult

type Peer

type Peer struct {
	// from PeerStatusWord
	Configured   bool
	AuthPossible bool
	Authentic    bool
	Reachable    bool
	Broadcast    bool
	Selection    uint8
	Condition    string
	// from variables
	SRCAdr     string
	SRCPort    int
	Hostname   string
	DSTAdr     string
	DSTPort    int
	Leap       int
	Stratum    int
	Precision  int
	RootDelay  float64
	RootDisp   float64
	RefID      string
	RefTime    string
	Reach      uint8
	Unreach    int
	HMode      int
	PMode      int
	HPoll      int
	PPoll      int
	Headway    int
	Flash      uint16
	Flashers   []string
	Offset     float64
	Delay      float64
	Dispersion float64
	Jitter     float64
	Xleave     float64
	Rec        string
	FiltDelay  string
	FiltOffset string
	FiltDisp   string
	NoSelect   bool
}

Peer contains parsed information from Peer Variables and peer status word, as described in http://doc.ntp.org/current-stable/ntpq.html

func NewPeerFromChrony

func NewPeerFromChrony(s *chrony.ReplySourceData, p *chrony.NTPData, n *chrony.ReplyNTPSourceName) (*Peer, error)

NewPeerFromChrony constructs Peer from three chrony packets

func NewPeerFromNTP

func NewPeerFromNTP(p *control.NTPControlMsg) (*Peer, error)

NewPeerFromNTP constructs Peer from NTPControlMsg packet

type Runner

type Runner interface {
	Run() (*NTPCheckResult, error)
	ServerStats() (*ServerStats, error)
}

Runner is something that can produce NTPCheckResult

type ServerStats

type ServerStats struct {
	PacketsReceived uint64 `json:"ntp.server.packets_received"`
	PacketsDropped  uint64 `json:"ntp.server.packets_dropped"`
}

ServerStats holds NTP server operational stats

func NewServerStatsFromChrony

func NewServerStatsFromChrony(s *chrony.ReplyServerStats) *ServerStats

NewServerStatsFromChrony constructs ServerStats from chrony ServerStats packet

func NewServerStatsFromChrony2

func NewServerStatsFromChrony2(s *chrony.ReplyServerStats2) *ServerStats

NewServerStatsFromChrony2 constructs ServerStats from chrony ServerStats2 packet

func NewServerStatsFromChrony3

func NewServerStatsFromChrony3(s *chrony.ReplyServerStats3) *ServerStats

NewServerStatsFromChrony3 constructs ServerStats from chrony ServerStats3 packet

func NewServerStatsFromChrony4

func NewServerStatsFromChrony4(s *chrony.ReplyServerStats4) *ServerStats

NewServerStatsFromChrony4 constructs ServerStats from chrony ServerStats4 packet

func NewServerStatsFromNTP

func NewServerStatsFromNTP(p *control.NTPControlMsg) (*ServerStats, error)

NewServerStatsFromNTP constructs ServerStats from NTPControlMsg packet

func RunServerStats

func RunServerStats(address string) (*ServerStats, error)

RunServerStats is a simple wrapper to connect to address and run NTPCheck.ServerStats()

type SystemVariables

type SystemVariables struct {
	Version   string
	Processor string
	System    string
	Leap      int
	Stratum   int
	Precision int
	RootDelay float64
	RootDisp  float64
	Peer      int
	TC        int
	MinTC     int
	Clock     string
	RefID     string
	RefTime   string
	Offset    float64
	SysJitter float64
	Frequency float64
	ClkWander float64
	ClkJitter float64
	Tai       int
}

SystemVariables holds System Variables extracted from k=v pairs, as described in http://doc.ntp.org/current-stable/ntpq.html

func NewSystemVariablesFromChrony

func NewSystemVariablesFromChrony(p *chrony.ReplyTracking) *SystemVariables

NewSystemVariablesFromChrony returns initialized instance of SystemVariables

func NewSystemVariablesFromNTP

func NewSystemVariablesFromNTP(p *control.NTPControlMsg) (*SystemVariables, error)

NewSystemVariablesFromNTP constructs System from NTPControlMsg packet

Jump to

Keyboard shortcuts

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