1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,106 @@
# OpenNTPD Input Plugin
Get standard NTP query metrics from [OpenNTPD][] using the ntpctl command.
[OpenNTPD]: http://www.openntpd.org/
Below is the documentation of the various headers returned from the NTP query
command when running `ntpctl -s peers`.
- remote The remote peer or server being synced to.
- wt the peer weight
- tl the peer trust level
- st (stratum) The remote peer or server Stratum
- next number of seconds until the next poll
- poll polling interval in seconds
- delay Round trip communication delay to the remote peer
or server (milliseconds);
- offset Mean offset (phase) in the times reported between this local host and
the remote peer or server (RMS, milliseconds);
- jitter Mean deviation (jitter) in the time reported for that remote peer or
server (RMS of difference of multiple time samples, milliseconds);
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
In addition to the plugin-specific configuration settings, plugins support
additional global and plugin configuration settings. These settings are used to
modify metrics, tags, and field or create aliases and configure ordering, etc.
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
## Configuration
```toml @sample.conf
# Get standard NTP query metrics from OpenNTPD.
[[inputs.openntpd]]
## Run ntpctl binary with sudo.
# use_sudo = false
## Location of the ntpctl binary.
# binary = "/usr/sbin/ntpctl"
## Maximum time the ntpctl binary is allowed to run.
# timeout = "5s"
```
## Metrics
- ntpctl
- tags:
- remote
- stratum
- fields:
- delay (float, milliseconds)
- jitter (float, milliseconds)
- offset (float, milliseconds)
- poll (int, seconds)
- next (int, seconds)
- wt (int)
- tl (int)
## Permissions
It's important to note that this plugin references ntpctl, which may require
additional permissions to execute successfully.
Depending on the user/group permissions of the telegraf user executing this
plugin, you may need to alter the group membership, set facls, or use sudo.
**Group membership (Recommended)**:
```bash
$ groups telegraf
telegraf : telegraf
$ usermod -a -G ntpd telegraf
$ groups telegraf
telegraf : telegraf ntpd
```
**Sudo privileges**:
If you use this method, you will need the following in your telegraf config:
```toml
[[inputs.openntpd]]
use_sudo = true
```
You will also need to update your sudoers file:
```bash
$ visudo
# Add the following lines:
Cmnd_Alias NTPCTL = /usr/sbin/ntpctl
telegraf ALL=(ALL) NOPASSWD: NTPCTL
Defaults!NTPCTL !logfile, !syslog, !pam_session
```
Please use the solution you see as most appropriate.
## Example Output
```text
openntpd,remote=194.57.169.1,stratum=2,host=localhost tl=10i,poll=1007i,
offset=2.295,jitter=3.896,delay=53.766,next=266i,wt=1i 1514454299000000000
```

View file

@ -0,0 +1,201 @@
//go:generate ../../../tools/readme_config_includer/generator
package openntpd
import (
"bufio"
"bytes"
_ "embed"
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
var (
defaultBinary = "/usr/sbin/ntpctl"
defaultTimeout = config.Duration(5 * time.Second)
// Mapping of the ntpctl tag key to the index in the command output
tagI = map[string]int{
"stratum": 2,
}
// Mapping of float metrics to their index in the command output
floatI = map[string]int{
"offset": 5,
"delay": 6,
"jitter": 7,
}
// Mapping of int metrics to their index in the command output
intI = map[string]int{
"wt": 0,
"tl": 1,
"next": 3,
"poll": 4,
}
)
type Openntpd struct {
Binary string `toml:"binary"`
Timeout config.Duration `toml:"timeout"`
UseSudo bool `toml:"use_sudo"`
run runner
}
type runner func(cmdName string, timeout config.Duration, useSudo bool) (*bytes.Buffer, error)
func (*Openntpd) SampleConfig() string {
return sampleConfig
}
func (n *Openntpd) Gather(acc telegraf.Accumulator) error {
out, err := n.run(n.Binary, n.Timeout, n.UseSudo)
if err != nil {
return fmt.Errorf("error gathering metrics: %w", err)
}
lineCounter := 0
scanner := bufio.NewScanner(out)
for scanner.Scan() {
// skip first (peer) and second (field list) line
if lineCounter < 2 {
lineCounter++
continue
}
line := scanner.Text()
fields := strings.Fields(line)
mFields := make(map[string]interface{})
tags := make(map[string]string)
// Even line ---> ntp server info
if lineCounter%2 == 0 {
// DNS resolution error ---> keep DNS name as remote name
if fields[0] != "not" {
tags["remote"] = fields[0]
} else {
tags["remote"] = fields[len(fields)-1]
}
}
// Read next line - Odd line ---> ntp server stats
scanner.Scan()
line = scanner.Text()
lineCounter++
fields = strings.Fields(line)
// if there is an ntpctl state prefix, remove it and make it it's own tag
if strings.ContainsAny(fields[0], "*") {
tags["state_prefix"] = fields[0]
fields = fields[1:]
}
// Get tags from output
for key, index := range tagI {
if index >= len(fields) {
continue
}
tags[key] = fields[index]
}
// Get integer metrics from output
for key, index := range intI {
if index >= len(fields) {
continue
}
if fields[index] == "-" {
continue
}
if key == "next" || key == "poll" {
m, err := strconv.ParseInt(strings.TrimSuffix(fields[index], "s"), 10, 64)
if err != nil {
acc.AddError(fmt.Errorf("integer value expected, got: %s", fields[index]))
continue
}
mFields[key] = m
} else {
m, err := strconv.ParseInt(fields[index], 10, 64)
if err != nil {
acc.AddError(fmt.Errorf("integer value expected, got: %s", fields[index]))
continue
}
mFields[key] = m
}
}
// get float metrics from output
for key, index := range floatI {
if len(fields) <= index {
continue
}
if fields[index] == "-" || fields[index] == "----" || fields[index] == "peer" || fields[index] == "not" || fields[index] == "valid" {
continue
}
if key == "offset" || key == "delay" || key == "jitter" {
m, err := strconv.ParseFloat(strings.TrimSuffix(fields[index], "ms"), 64)
if err != nil {
acc.AddError(fmt.Errorf("float value expected, got: %s", fields[index]))
continue
}
mFields[key] = m
} else {
m, err := strconv.ParseFloat(fields[index], 64)
if err != nil {
acc.AddError(fmt.Errorf("float value expected, got: %s", fields[index]))
continue
}
mFields[key] = m
}
}
acc.AddFields("openntpd", mFields, tags)
lineCounter++
}
return nil
}
// Shell out to ntpctl and return the output
func openntpdRunner(cmdName string, timeout config.Duration, useSudo bool) (*bytes.Buffer, error) {
cmdArgs := []string{"-s", "peers"}
cmd := exec.Command(cmdName, cmdArgs...)
if useSudo {
cmdArgs = append([]string{cmdName}, cmdArgs...)
cmd = exec.Command("sudo", cmdArgs...)
}
var out bytes.Buffer
cmd.Stdout = &out
err := internal.RunTimeout(cmd, time.Duration(timeout))
if err != nil {
return &out, fmt.Errorf("error running ntpctl: %w", err)
}
return &out, nil
}
func init() {
inputs.Add("openntpd", func() telegraf.Input {
return &Openntpd{
run: openntpdRunner,
Binary: defaultBinary,
Timeout: defaultTimeout,
UseSudo: false,
}
})
}

View file

@ -0,0 +1,327 @@
package openntpd
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
)
func openntpdCTL(output string) func(string, config.Duration, bool) (*bytes.Buffer, error) {
return func(string, config.Duration, bool) (*bytes.Buffer, error) {
return bytes.NewBufferString(output), nil
}
}
func TestParseSimpleOutput(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Openntpd{
run: openntpdCTL(simpleOutput),
}
err := v.Gather(acc)
require.NoError(t, err)
require.True(t, acc.HasMeasurement("openntpd"))
require.Equal(t, uint64(1), acc.NMetrics())
require.Equal(t, 7, acc.NFields())
firstpeerfields := map[string]interface{}{
"wt": int64(1),
"tl": int64(10),
"next": int64(56),
"poll": int64(63),
"offset": float64(9.271),
"delay": float64(44.662),
"jitter": float64(2.678),
}
firstpeertags := map[string]string{
"remote": "212.129.9.36",
"stratum": "3",
}
acc.AssertContainsTaggedFields(t, "openntpd", firstpeerfields, firstpeertags)
}
func TestParseSimpleOutputwithStatePrefix(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Openntpd{
run: openntpdCTL(simpleOutputwithStatePrefix),
}
err := v.Gather(acc)
require.NoError(t, err)
require.True(t, acc.HasMeasurement("openntpd"))
require.Equal(t, uint64(1), acc.NMetrics())
require.Equal(t, 7, acc.NFields())
firstpeerfields := map[string]interface{}{
"wt": int64(1),
"tl": int64(10),
"next": int64(45),
"poll": int64(980),
"offset": float64(-9.901),
"delay": float64(67.573),
"jitter": float64(29.350),
}
firstpeertags := map[string]string{
"remote": "92.243.6.5",
"stratum": "2",
"state_prefix": "*",
}
acc.AssertContainsTaggedFields(t, "openntpd", firstpeerfields, firstpeertags)
}
func TestParseSimpleOutputInvalidPeer(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Openntpd{
run: openntpdCTL(simpleOutputInvalidPeer),
}
err := v.Gather(acc)
require.NoError(t, err)
require.True(t, acc.HasMeasurement("openntpd"))
require.Equal(t, uint64(1), acc.NMetrics())
require.Equal(t, 4, acc.NFields())
firstpeerfields := map[string]interface{}{
"wt": int64(1),
"tl": int64(2),
"next": int64(203),
"poll": int64(300),
}
firstpeertags := map[string]string{
"remote": "178.33.111.49",
"stratum": "-",
}
acc.AssertContainsTaggedFields(t, "openntpd", firstpeerfields, firstpeertags)
}
func TestParseSimpleOutputServersDNSError(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Openntpd{
run: openntpdCTL(simpleOutputServersDNSError),
}
err := v.Gather(acc)
require.NoError(t, err)
require.True(t, acc.HasMeasurement("openntpd"))
require.Equal(t, uint64(1), acc.NMetrics())
require.Equal(t, 4, acc.NFields())
firstpeerfields := map[string]interface{}{
"next": int64(2),
"poll": int64(15),
"wt": int64(1),
"tl": int64(2),
}
firstpeertags := map[string]string{
"remote": "pool.nl.ntp.org",
"stratum": "-",
}
acc.AssertContainsTaggedFields(t, "openntpd", firstpeerfields, firstpeertags)
secondpeerfields := map[string]interface{}{
"next": int64(2),
"poll": int64(15),
"wt": int64(1),
"tl": int64(2),
}
secondpeertags := map[string]string{
"remote": "pool.nl.ntp.org",
"stratum": "-",
}
acc.AssertContainsTaggedFields(t, "openntpd", secondpeerfields, secondpeertags)
}
func TestParseSimpleOutputServerDNSError(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Openntpd{
run: openntpdCTL(simpleOutputServerDNSError),
}
err := v.Gather(acc)
require.NoError(t, err)
require.True(t, acc.HasMeasurement("openntpd"))
require.Equal(t, uint64(1), acc.NMetrics())
require.Equal(t, 4, acc.NFields())
firstpeerfields := map[string]interface{}{
"next": int64(12),
"poll": int64(15),
"wt": int64(1),
"tl": int64(2),
}
firstpeertags := map[string]string{
"remote": "pool.fr.ntp.org",
"stratum": "-",
}
acc.AssertContainsTaggedFields(t, "openntpd", firstpeerfields, firstpeertags)
}
func TestParseFullOutput(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Openntpd{
run: openntpdCTL(fullOutput),
}
err := v.Gather(acc)
require.NoError(t, err)
require.True(t, acc.HasMeasurement("openntpd"))
require.Equal(t, uint64(20), acc.NMetrics())
require.Equal(t, 113, acc.NFields())
firstpeerfields := map[string]interface{}{
"wt": int64(1),
"tl": int64(10),
"next": int64(56),
"poll": int64(63),
"offset": float64(9.271),
"delay": float64(44.662),
"jitter": float64(2.678),
}
firstpeertags := map[string]string{
"remote": "212.129.9.36",
"stratum": "3",
}
acc.AssertContainsTaggedFields(t, "openntpd", firstpeerfields, firstpeertags)
secondpeerfields := map[string]interface{}{
"wt": int64(1),
"tl": int64(10),
"next": int64(21),
"poll": int64(64),
"offset": float64(-0.103),
"delay": float64(53.199),
"jitter": float64(9.046),
}
secondpeertags := map[string]string{
"remote": "163.172.25.19",
"stratum": "2",
}
acc.AssertContainsTaggedFields(t, "openntpd", secondpeerfields, secondpeertags)
thirdpeerfields := map[string]interface{}{
"wt": int64(1),
"tl": int64(10),
"next": int64(45),
"poll": int64(980),
"offset": float64(-9.901),
"delay": float64(67.573),
"jitter": float64(29.350),
}
thirdpeertags := map[string]string{
"remote": "92.243.6.5",
"stratum": "2",
"state_prefix": "*",
}
acc.AssertContainsTaggedFields(t, "openntpd", thirdpeerfields, thirdpeertags)
fourthpeerfields := map[string]interface{}{
"wt": int64(1),
"tl": int64(2),
"next": int64(203),
"poll": int64(300),
}
fourthpeertags := map[string]string{
"remote": "178.33.111.49",
"stratum": "-",
}
acc.AssertContainsTaggedFields(t, "openntpd", fourthpeerfields, fourthpeertags)
}
var simpleOutput = `peer
wt tl st next poll offset delay jitter
212.129.9.36 from pool 0.debian.pool.ntp.org
1 10 3 56s 63s 9.271ms 44.662ms 2.678ms`
var simpleOutputwithStatePrefix = `peer
wt tl st next poll offset delay jitter
92.243.6.5 from pool 0.debian.pool.ntp.org
* 1 10 2 45s 980s -9.901ms 67.573ms 29.350ms`
var simpleOutputInvalidPeer = `peer
wt tl st next poll offset delay jitter
178.33.111.49 from pool 0.debian.pool.ntp.org
1 2 - 203s 300s ---- peer not valid ----`
var simpleOutputServersDNSError = `peer
wt tl st next poll offset delay jitter
not resolved from pool pool.nl.ntp.org
1 2 - 2s 15s ---- peer not valid ----
`
var simpleOutputServerDNSError = `peer
wt tl st next poll offset delay jitter
not resolved pool.fr.ntp.org
1 2 - 12s 15s ---- peer not valid ----
`
var fullOutput = `peer
wt tl st next poll offset delay jitter
212.129.9.36 from pool 0.debian.pool.ntp.org
1 10 3 56s 63s 9.271ms 44.662ms 2.678ms
163.172.25.19 from pool 0.debian.pool.ntp.org
1 10 2 21s 64s -0.103ms 53.199ms 9.046ms
92.243.6.5 from pool 0.debian.pool.ntp.org
* 1 10 2 45s 980s -9.901ms 67.573ms 29.350ms
178.33.111.49 from pool 0.debian.pool.ntp.org
1 2 - 203s 300s ---- peer not valid ----
62.210.122.129 from pool 1.debian.pool.ntp.org
1 10 3 4s 60s 5.372ms 53.690ms 14.700ms
163.172.225.159 from pool 1.debian.pool.ntp.org
1 10 3 38s 61s 12.276ms 40.631ms 1.282ms
5.196.192.58 from pool 1.debian.pool.ntp.org
1 2 - 0s 300s ---- peer not valid ----
129.250.35.250 from pool 1.debian.pool.ntp.org
1 10 2 28s 63s 11.236ms 43.874ms 1.381ms
2001:41d0:a:5a7::1 from pool 2.debian.pool.ntp.org
1 2 - 5s 15s ---- peer not valid ----
2001:41d0:8:188d::16 from pool 2.debian.pool.ntp.org
1 2 - 3s 15s ---- peer not valid ----
2001:4b98:dc0:41:216:3eff:fe69:46e3 from pool 2.debian.pool.ntp.org
1 2 - 14s 15s ---- peer not valid ----
2a01:e0d:1:3:58bf:fa61:0:1 from pool 2.debian.pool.ntp.org
1 2 - 9s 15s ---- peer not valid ----
163.172.179.38 from pool 2.debian.pool.ntp.org
1 10 2 51s 65s -19.229ms 85.404ms 48.734ms
5.135.3.88 from pool 2.debian.pool.ntp.org
1 2 - 173s 300s ---- peer not valid ----
195.154.41.195 from pool 2.debian.pool.ntp.org
1 10 2 84s 1004s -3.956ms 54.549ms 13.658ms
62.210.81.130 from pool 2.debian.pool.ntp.org
1 10 2 158s 1043s -42.593ms 124.353ms 94.230ms
149.202.97.123 from pool 3.debian.pool.ntp.org
1 2 - 205s 300s ---- peer not valid ----
51.15.175.224 from pool 3.debian.pool.ntp.org
1 10 2 9s 64s 8.861ms 46.640ms 0.668ms
37.187.5.167 from pool 3.debian.pool.ntp.org
1 2 - 105s 300s ---- peer not valid ----
194.57.169.1 from pool 3.debian.pool.ntp.org
1 10 2 32s 63s 6.589ms 52.051ms 2.057ms`

View file

@ -0,0 +1,10 @@
# Get standard NTP query metrics from OpenNTPD.
[[inputs.openntpd]]
## Run ntpctl binary with sudo.
# use_sudo = false
## Location of the ntpctl binary.
# binary = "/usr/sbin/ntpctl"
## Maximum time the ntpctl binary is allowed to run.
# timeout = "5s"