chrony

package
v0.0.0-...-5e75ab2 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2025 License: Apache-2.0 Imports: 8 Imported by: 6

README

Chrony network protocol used for command and monitoring of the timeserver

GoDoc

Native Go implementation of Chrony communication protocol v6.

As of now, only monitoring part of protocol that is used to communicate between chronyc and chronyd is implemented.

Documentation

Overview

Package chrony implements Chrony (https://chrony.tuxfamily.org) network protocol v6 used for monitoring of the timeserver.

As of now, only monitoring part of protocol that is used to communicate between `chronyc` and `chronyd` is implemented. Chronyc/chronyd protocol is not documented (https://chrony.tuxfamily.org/faq.html#_is_the_code_chronyc_code_code_chronyd_code_protocol_documented_anywhere).

Library allows communicating with Chrony NTP server, and get various information, for example: current server status; server variables like offset; peers with their statuses and variables; server counters.

Example usage can be found in ntpcheck project - https://github.com/facebook/time/ntp/ntpcheck

Index

Constants

View Source
const (
	FlagNoselect uint16 = 0x1
	FlagPrefer   uint16 = 0x2
	FlagTrust    uint16 = 0x4
	FlagRequire  uint16 = 0x8
)

source data flags

View Source
const (
	FlagSDOptionNoSelect uint16 = 0x1
	FlagSDOptionPrefer   uint16 = 0x2
	FlagSDOptionTrust    uint16 = 0x4
	FlagSDOptionRequire  uint16 = 0x8
)

select data flags

View Source
const (
	NTPFlagsTests        uint16 = 0x3ff
	NTPFlagInterleaved   uint16 = 0x4000
	NTPFlagAuthenticated uint16 = 0x8000
)

ntpdata flags

View Source
const (
	ClockSourceUnspec     = "unspec"
	ClockSourcePPS        = "pps"
	ClockSourceLFRadio    = "lf_radio"
	ClockSourceHFRadio    = "hf_radio"
	ClockSourceUHFRadio   = "uhf_radio"
	ClockSourceLocal      = "local"
	ClockSourceNTP        = "ntp"
	ClockSourceOther      = "other"
	ClockSourceWristWatch = "wristwatch"
	ClockSourceTelephone  = "telephone"
)

possible clock sources

View Source
const ChronyPortV6Regexp = "[0-9]+: [0-9A-Z]+:0143 .*"

ChronyPortV6Regexp is a regexp to find anything that listens on port 323 hex(323) = '0x143'

View Source
const ChronySocketPath = "/var/run/chrony/chronyd.sock"

ChronySocketPath is the default path to chronyd socket

Variables

ClockSourceDesc stores human-readable descriptions of ClockSource field

View Source
var ModeTypeDesc = [3]string{
	"client",
	"peer",
	"reference clock",
}

ModeTypeDesc provides mapping from ModeType to string

View Source
var NTPTestDescMap = map[uint16]string{
	0x0001: "pkt_dup",
	0x0002: "pkt_bogus",
	0x0004: "pkt_invalid",
	0x0008: "pkt_auth",
	0x0010: "pkt_stratum",
	0x0020: "pkt_header",
	0x0040: "tst_max_delay",
	0x0080: "tst_delay_ratio",
	0x0100: "tst_delay_dev_ration",
	0x0200: "tst_sync_loop",
}

NTPTestDescMap maps bit mask with corresponding flash status

View Source
var SourceStateDesc = [6]string{
	"sync",
	"unreach",
	"falseticker",
	"jittery",
	"candidate",
	"outlier",
}

SourceStateDesc provides mapping from SourceStateType to string

View Source
var StatusDesc = [20]string{
	"SUCCESS",
	"FAILED",
	"UNAUTH",
	"INVALID",
	"NOSUCHSOURCE",
	"INVALIDTS",
	"NOTENABLED",
	"BADSUBNET",
	"ACCESSALLOWED",
	"ACCESSDENIED",
	"NOHOSTACCESS",
	"SOURCEALREADYKNOWN",
	"TOOMANYSOURCES",
	"NORTC",
	"BADRTCFILE",
	"INACTIVE",
	"BADSAMPLE",
	"INVALIDAF",
	"BADPKTVERSION",
	"BADPKTLENGTH",
}

StatusDesc provides mapping from ResponseStatusType to string

Functions

func ReadNTPTestFlags

func ReadNTPTestFlags(flags uint16) []string

ReadNTPTestFlags returns list of failed ntp test flags (as strings)

func RefidAsHEX

func RefidAsHEX(refID uint32) string

RefidAsHEX prints ref id as hex

func RefidToString

func RefidToString(refID uint32) string

RefidToString decodes ASCII string encoded as uint32

Types

type Activity

type Activity struct {
	Online       int32
	Offline      int32
	BurstOnline  int32
	BurstOffline int32
	Unresolved   int32
}

Activity contains parsed version of 'activity' reply

type Client

type Client struct {
	Connection io.ReadWriter
	Sequence   uint32
}

Client talks to chronyd

func (*Client) Communicate

func (n *Client) Communicate(packet RequestPacket) (ResponsePacket, error)

Communicate sends the packet to chronyd, parse response into something usable

type CommandType

type CommandType uint16

CommandType identifies command type in both request and repy

type LoggerInterface

type LoggerInterface interface {
	Printf(format string, v ...interface{})
}

LoggerInterface is an interface for debug logging.

var Logger LoggerInterface = &noopLogger{}

Logger is a default debug logger which simply discards all messages. It can be overridden by setting the global variable to a different implementation, like std log

chrony.Logger = log.New(os.Stderr, "", 0)

or logrus

chrony.Logger = logrus.StandardLogger()

type ModeType

type ModeType uint16

ModeType identifies source (peer) mode

const (
	SourceModeClient ModeType = 0
	SourceModePeer   ModeType = 1
	SourceModeRef    ModeType = 2
)

source modes

func (ModeType) String

func (m ModeType) String() string

type NTPData

type NTPData struct {
	RemoteAddr      net.IP
	LocalAddr       net.IP
	RemotePort      uint16
	Leap            uint8
	Version         uint8
	Mode            uint8
	Stratum         uint8
	Poll            int8
	Precision       int8
	RootDelay       float64
	RootDispersion  float64
	RefID           uint32
	RefTime         time.Time
	Offset          float64
	PeerDelay       float64
	PeerDispersion  float64
	ResponseTime    float64
	JitterAsymmetry float64
	Flags           uint16
	TXTssChar       uint8
	RXTssChar       uint8
	TotalTXCount    uint32
	TotalRXCount    uint32
	TotalValidCount uint32
}

NTPData contains parsed version of 'ntpdata' reply

type NTPData2

type NTPData2 struct {
	NTPData

	TotalKernelTXts uint32
	TotalKernelRXts uint32
	TotalHWTXts     uint32
	TotalHWRXts     uint32
}

NTPData2 contains parsed version of a new 'ntpdata' reply

type NTPSourceName

type NTPSourceName struct {
	Name string
}

NTPSourceName contains parsed version of 'sourcename' reply

type PacketType

type PacketType uint8

PacketType - request or reply

func (PacketType) String

func (t PacketType) String() string

type ReplyActivity

type ReplyActivity struct {
	ReplyHead
	Activity
}

ReplyActivity is a usable version of 'activity' response

type ReplyHead

type ReplyHead struct {
	Version  uint8
	PKTType  PacketType
	Res1     uint8
	Res2     uint8
	Command  CommandType
	Reply    ReplyType
	Status   ResponseStatusType
	Pad1     uint16
	Pad2     uint16
	Pad3     uint16
	Sequence uint32
	Pad4     uint32
	Pad5     uint32
}

ReplyHead is the first (common) part of the reply packet, in a format that can be directly passed to binary.Read

func (*ReplyHead) GetCommand

func (r *ReplyHead) GetCommand() CommandType

GetCommand returns reply packet command

func (*ReplyHead) GetStatus

func (r *ReplyHead) GetStatus() ResponseStatusType

GetStatus returns reply packet status

func (*ReplyHead) GetType

func (r *ReplyHead) GetType() ReplyType

GetType returns reply packet type

type ReplyNTPData

type ReplyNTPData struct {
	ReplyHead
	NTPData
}

ReplyNTPData is a what end user will get in 'ntp data' response

type ReplyNTPData2

type ReplyNTPData2 struct {
	ReplyHead
	NTPData2
}

ReplyNTPData2 is a what end user will get in 'ntp data' response

type ReplyNTPSourceName

type ReplyNTPSourceName struct {
	ReplyHead
	NTPSourceName
}

ReplyNTPSourceName is a what end user will get in 'sourcename' response

type ReplySelectData

type ReplySelectData struct {
	ReplyHead
	SelectData
}

ReplySelectData is a usable version of 'selectdata' response

type ReplyServerStats

type ReplyServerStats struct {
	ReplyHead
	ServerStats
}

ReplyServerStats is a usable version of 'serverstats' response

type ReplyServerStats2

type ReplyServerStats2 struct {
	ReplyHead
	ServerStats2
}

ReplyServerStats2 is a usable version of 'serverstats2' response

type ReplyServerStats3

type ReplyServerStats3 struct {
	ReplyHead
	ServerStats3
}

ReplyServerStats3 is a usable version of 'serverstats3' response

type ReplyServerStats4

type ReplyServerStats4 struct {
	ReplyHead
	ServerStats4
}

ReplyServerStats4 is a usable version of 'serverstats4' response

type ReplySourceData

type ReplySourceData struct {
	ReplyHead
	SourceData
}

ReplySourceData is a usable version of 'source data' reply for given source id

type ReplySourceStats

type ReplySourceStats struct {
	ReplyHead
	SourceStats
}

ReplySourceStats has usable 'sourcestats' response

type ReplySources

type ReplySources struct {
	ReplyHead
	NSources int
}

ReplySources is a usable version of a reply to 'sources' command

type ReplyTracking

type ReplyTracking struct {
	ReplyHead
	Tracking
}

ReplyTracking has usable 'tracking' response

type ReplyType

type ReplyType uint16

ReplyType identifies reply packet type

const (
	RpyNSources      ReplyType = 2
	RpySourceData    ReplyType = 3
	RpyTracking      ReplyType = 5
	RpySourceStats   ReplyType = 6
	RpyActivity      ReplyType = 12
	RpyServerStats   ReplyType = 14
	RpyNTPData       ReplyType = 16
	RpyNTPSourceName ReplyType = 19
	RpyServerStats2  ReplyType = 22
	RpySelectData    ReplyType = 23
	RpyServerStats3  ReplyType = 24
	RpyServerStats4  ReplyType = 25
	RpyNTPData2      ReplyType = 26
)

reply types

type RequestActivity

type RequestActivity struct {
	RequestHead
	// contains filtered or unexported fields
}

RequestActivity - packet to request 'activity' data

func NewActivityPacket

func NewActivityPacket() *RequestActivity

NewActivityPacket creates new packet to request 'activity' information

type RequestHead

type RequestHead struct {
	Version  uint8
	PKTType  PacketType
	Res1     uint8
	Res2     uint8
	Command  CommandType
	Attempt  uint16
	Sequence uint32
	Pad1     uint32
	Pad2     uint32
}

RequestHead is the first (common) part of the request, in a format that can be directly passed to binary.Write

func (*RequestHead) GetCommand

func (r *RequestHead) GetCommand() CommandType

GetCommand returns request packet command

func (*RequestHead) SetSequence

func (r *RequestHead) SetSequence(n uint32)

SetSequence sets request packet sequence number

type RequestNTPData

type RequestNTPData struct {
	RequestHead
	IPAddr ipAddr
	EOR    int32
	// contains filtered or unexported fields
}

RequestNTPData - packet to request NTP data for peer IP. As of now, it's only allowed by Chrony over unix socket connection.

func NewNTPDataPacket

func NewNTPDataPacket(ip net.IP) *RequestNTPData

NewNTPDataPacket creates new packet to request 'ntp data' information for given peer IP

type RequestNTPSourceName

type RequestNTPSourceName struct {
	RequestHead
	IPAddr ipAddr
	EOR    int32
	// contains filtered or unexported fields
}

RequestNTPSourceName - packet to request source name for peer IP.

func NewNTPSourceNamePacket

func NewNTPSourceNamePacket(ip net.IP) *RequestNTPSourceName

NewNTPSourceNamePacket creates new packet to request 'source name' information for given peer IP

type RequestPacket

type RequestPacket interface {
	GetCommand() CommandType
	SetSequence(n uint32)
}

RequestPacket is an interface to abstract all different outgoing packets

type RequestSelectData

type RequestSelectData struct {
	RequestHead
	Index int32
	EOR   int32
	// contains filtered or unexported fields
}

RequestSelectData - packet to request 'selectdata' data

func NewSelectDataPacket

func NewSelectDataPacket(sourceID int32) *RequestSelectData

NewSelectDataPacket creates new packet to request 'selectdata' information

type RequestServerStats

type RequestServerStats struct {
	RequestHead
	// contains filtered or unexported fields
}

RequestServerStats - packet to request server stats

func NewServerStatsPacket

func NewServerStatsPacket() *RequestServerStats

NewServerStatsPacket creates new packet to request 'serverstats' information

type RequestSourceData

type RequestSourceData struct {
	RequestHead
	Index int32
	EOR   int32
	// contains filtered or unexported fields
}

RequestSourceData - packet to request source data for source id

func NewSourceDataPacket

func NewSourceDataPacket(sourceID int32) *RequestSourceData

NewSourceDataPacket creates new packet to request 'source data' information about source with given ID

type RequestSourceStats

type RequestSourceStats struct {
	RequestHead
	Index int32
	EOR   int32
	// contains filtered or unexported fields
}

RequestSourceStats - packet to request 'sourcestats' data for source id

func NewSourceStatsPacket

func NewSourceStatsPacket(sourceID int32) *RequestSourceStats

NewSourceStatsPacket creates a new packet to request 'sourcestats' information

type RequestSources

type RequestSources struct {
	RequestHead
	// contains filtered or unexported fields
}

RequestSources - packet to request number of sources (peers)

func NewSourcesPacket

func NewSourcesPacket() *RequestSources

NewSourcesPacket creates new packet to request number of sources (peers)

type RequestTracking

type RequestTracking struct {
	RequestHead
	// contains filtered or unexported fields
}

RequestTracking - packet to request 'tracking' data

func NewTrackingPacket

func NewTrackingPacket() *RequestTracking

NewTrackingPacket creates new packet to request 'tracking' information

type ResponsePacket

type ResponsePacket interface {
	GetCommand() CommandType
	GetType() ReplyType
	GetStatus() ResponseStatusType
}

ResponsePacket is an interface to abstract all different incoming packets

type ResponseStatusType

type ResponseStatusType uint16

ResponseStatusType identifies response status

func (ResponseStatusType) String

func (r ResponseStatusType) String() string

type SelectData

type SelectData struct {
	RefID          uint32
	IPAddr         net.IP
	StateChar      uint8
	Authentication uint8
	Leap           uint8
	ConfOptions    uint16
	EFFOptions     uint16
	LastSampleAgo  uint32
	Score          float64
	LoLimit        float64
	HiLimit        float64
}

SelectData contains parsed version of 'selectdata' reply

type ServerStats

type ServerStats struct {
	NTPHits  uint32
	CMDHits  uint32
	NTPDrops uint32
	CMDDrops uint32
	LogDrops uint32
}

ServerStats contains parsed version of 'serverstats' reply

type ServerStats2

type ServerStats2 struct {
	NTPHits     uint32
	NKEHits     uint32
	CMDHits     uint32
	NTPDrops    uint32
	NKEDrops    uint32
	CMDDrops    uint32
	LogDrops    uint32
	NTPAuthHits uint32
}

ServerStats2 contains parsed version of 'serverstats2' reply

type ServerStats3

type ServerStats3 struct {
	NTPHits            uint32
	NKEHits            uint32
	CMDHits            uint32
	NTPDrops           uint32
	NKEDrops           uint32
	CMDDrops           uint32
	LogDrops           uint32
	NTPAuthHits        uint32
	NTPInterleavedHits uint32
	NTPTimestamps      uint32
	NTPSpanSeconds     uint32
}

ServerStats3 contains parsed version of 'serverstats3' reply

type ServerStats4

type ServerStats4 struct {
	NTPHits               uint64
	NKEHits               uint64
	CMDHits               uint64
	NTPDrops              uint64
	NKEDrops              uint64
	CMDDrops              uint64
	LogDrops              uint64
	NTPAuthHits           uint64
	NTPInterleavedHits    uint64
	NTPTimestamps         uint64
	NTPSpanSeconds        uint64
	NTPDaemonRxtimestamps uint64
	NTPDaemonTxtimestamps uint64
	NTPKernelRxtimestamps uint64
	NTPKernelTxtimestamps uint64
	NTPHwRxTimestamps     uint64
	NTPHwTxTimestamps     uint64
}

ServerStats4 contains parsed version of 'serverstats4' reply

type SourceData

type SourceData struct {
	IPAddr         net.IP
	Poll           int16
	Stratum        uint16
	State          SourceStateType
	Mode           ModeType
	Flags          uint16
	Reachability   uint16
	SinceSample    uint32
	OrigLatestMeas float64
	LatestMeas     float64
	LatestMeasErr  float64
}

SourceData contains parsed version of 'source data' reply

type SourceStateType

type SourceStateType uint16

SourceStateType identifies source (peer) state

const (
	SourceStateSync        SourceStateType = 0
	SourceStateUnreach     SourceStateType = 1
	SourceStateFalseTicker SourceStateType = 2
	SourceStateJittery     SourceStateType = 3
	SourceStateCandidate   SourceStateType = 4
	SourceStateOutlier     SourceStateType = 5
)

source state

func (SourceStateType) String

func (s SourceStateType) String() string

type SourceStats

type SourceStats struct {
	RefID              uint32
	IPAddr             net.IP
	NSamples           uint32
	NRuns              uint32
	SpanSeconds        uint32
	StandardDeviation  float64
	ResidFreqPPM       float64
	SkewPPM            float64
	EstimatedOffset    float64
	EstimatedOffsetErr float64
}

SourceStats contains stats about the source

type Tracking

type Tracking struct {
	RefID              uint32
	IPAddr             net.IP
	Stratum            uint16
	LeapStatus         uint16
	RefTime            time.Time
	CurrentCorrection  float64
	LastOffset         float64
	RMSOffset          float64
	FreqPPM            float64
	ResidFreqPPM       float64
	SkewPPM            float64
	RootDelay          float64
	RootDispersion     float64
	LastUpdateInterval float64
}

Tracking contains parsed version of 'tracking' reply

Jump to

Keyboard shortcuts

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