chrony

package
v0.0.0-...-49e71f9 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

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/facebookincubator/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 (
	NTPFlagsTests        uint16 = 0x3ff
	NTPFlagInterleaved   uint16 = 0x4000
	NTPFlagAuthenticated uint16 = 0x8000
)

ntpdata flags

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

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",
	"falseticket",
	"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 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 ModeType

type ModeType uint16

ModeType identifies source (peer) mode

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

source modes

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 PacketType

type PacketType uint8

PacketType - request or reply

func (PacketType) String

func (t PacketType) String() string

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() PacketType

GetType returns reply packet type

type ReplyNTPData

type ReplyNTPData struct {
	ReplyHead
	NTPData
}

ReplyNTPData is a what end user will get for of 'ntp data' 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 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

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 RequestPacket

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

RequestPacket is an iterface to abstract all different outgoing packets

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() PacketType
	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 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 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
	SourceStateFalseTicket 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
}

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