Add openntpd input plugin (#3627)

This commit is contained in:
aromeyer 2019-08-21 00:14:11 +02:00 committed by Daniel Nelson
parent 7bbed9e2a3
commit 153dd585af
5 changed files with 650 additions and 0 deletions

View File

@ -240,6 +240,7 @@ For documentation on the latest development code see the [documentation index][d
* [ntpq](./plugins/inputs/ntpq)
* [nvidia_smi](./plugins/inputs/nvidia_smi)
* [openldap](./plugins/inputs/openldap)
* [openntpd](./plugins/inputs/openntpd)
* [opensmtpd](./plugins/inputs/opensmtpd)
* [openweathermap](./plugins/inputs/openweathermap)
* [pf](./plugins/inputs/pf)

View File

@ -104,6 +104,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/ntpq"
_ "github.com/influxdata/telegraf/plugins/inputs/nvidia_smi"
_ "github.com/influxdata/telegraf/plugins/inputs/openldap"
_ "github.com/influxdata/telegraf/plugins/inputs/openntpd"
_ "github.com/influxdata/telegraf/plugins/inputs/opensmtpd"
_ "github.com/influxdata/telegraf/plugins/inputs/openweathermap"
_ "github.com/influxdata/telegraf/plugins/inputs/passenger"

View File

@ -0,0 +1,96 @@
# OpenNTPD Input Plugin
Get standard NTP query metrics from OpenNTPD ([OpenNTPD - a FREE, easy to use
implementation of the Network Time Protocol](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);
### Configuration:
```toml
# Get standard NTP query metrics, requires ntpctls executable
# provided by openntpd packages
[[inputs.openntpd]]
## If running as a restricted user you can prepend sudo for additional access:
#use_sudo = false
## The default location of the ntpctl binary can be overridden with:
binary = "/usr/sbin/ntpctl"
## The default timeout of 1000ms can be overriden with (in milliseconds):
#timeout = 1000
```
### Measurements & Fields:
- ntpctl
- delay (float, milliseconds)
- jitter (float, milliseconds)
- offset (float, milliseconds)
- poll (int, seconds)
- next (int,,seconds)
- wt (int)
- tl (int)
### Tags:
- All measurements have the following tags:
- remote
- stratum
### 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 line:
telegraf ALL=(ALL) NOPASSWD: /usr/sbin/ntpctl
```
Please use the solution you see as most appropriate.
### Example Output:
```
$ telegraf --config ~/ws/telegraf.conf --input-filter openntpd --test
* Plugin: openntpd, Collection 1
> 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,223 @@
package openntpd
import (
"bufio"
"bytes"
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
)
// Mapping of ntpctl header names to tag keys
var tagHeaders = map[string]string{
"st": "stratum",
}
// Mapping of the ntpctl tag key to the index in the command output
var tagI = map[string]int{
"stratum": 2,
}
// Mapping of float metrics to their index in the command output
var floatI = map[string]int{
"offset": 5,
"delay": 6,
"jitter": 7,
}
// Mapping of int metrics to their index in the command output
var intI = map[string]int{
"wt": 0,
"tl": 1,
"next": 3,
"poll": 4,
}
type runner func(cmdName string, Timeout internal.Duration, UseSudo bool) (*bytes.Buffer, error)
// Openntpd is used to store configuration values
type Openntpd struct {
Binary string
Timeout internal.Duration
UseSudo bool
filter filter.Filter
run runner
}
var defaultBinary = "/usr/sbin/ntpctl"
var defaultTimeout = internal.Duration{Duration: time.Second}
func (n *Openntpd) Description() string {
return "Get standard NTP query metrics from OpenNTPD."
}
func (n *Openntpd) SampleConfig() string {
return `
## If running as a restricted user you can prepend sudo for additional access:
#use_sudo = false
## The default location of the ntpctl binary can be overridden with:
binary = "/usr/sbin/ntpctl"
## The default timeout of 1000ms can be overriden with (in milliseconds):
timeout = 1000
`
}
// Shell out to ntpctl and return the output
func openntpdRunner(cmdName string, Timeout internal.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, Timeout.Duration)
if err != nil {
return &out, fmt.Errorf("error running ntpctl: %s", err)
}
return &out, nil
}
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: %s", 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(string(fields[0]), "*") {
tags["state_prefix"] = string(fields[0])
fields = append(fields[:0], fields[1:]...)
}
// Get tags from output
for key, index := range tagI {
if len(fields) < index {
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
}
func init() {
inputs.Add("openntpd", func() telegraf.Input {
return &Openntpd{
run: openntpdRunner,
Binary: defaultBinary,
Timeout: defaultTimeout,
UseSudo: false,
}
})
}

View File

@ -0,0 +1,329 @@
package openntpd
import (
"bytes"
"testing"
"time"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
)
var TestTimeout = internal.Duration{Duration: time.Second}
func OpenntpdCTL(output string, Timeout internal.Duration, useSudo bool) func(string, internal.Duration, bool) (*bytes.Buffer, error) {
return func(string, internal.Duration, bool) (*bytes.Buffer, error) {
return bytes.NewBuffer([]byte(output)), nil
}
}
func TestParseSimpleOutput(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Openntpd{
run: OpenntpdCTL(simpleOutput, TestTimeout, false),
}
err := v.Gather(acc)
assert.NoError(t, err)
assert.True(t, acc.HasMeasurement("openntpd"))
assert.Equal(t, acc.NMetrics(), uint64(1))
assert.Equal(t, acc.NFields(), 7)
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, TestTimeout, false),
}
err := v.Gather(acc)
assert.NoError(t, err)
assert.True(t, acc.HasMeasurement("openntpd"))
assert.Equal(t, acc.NMetrics(), uint64(1))
assert.Equal(t, acc.NFields(), 7)
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 TestParseSimpleOutputInavlidPeer(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Openntpd{
run: OpenntpdCTL(simpleOutputInvalidPeer, TestTimeout, false),
}
err := v.Gather(acc)
assert.NoError(t, err)
assert.True(t, acc.HasMeasurement("openntpd"))
assert.Equal(t, acc.NMetrics(), uint64(1))
assert.Equal(t, acc.NFields(), 4)
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, TestTimeout, false),
}
err := v.Gather(acc)
assert.NoError(t, err)
assert.True(t, acc.HasMeasurement("openntpd"))
assert.Equal(t, acc.NMetrics(), uint64(1))
assert.Equal(t, acc.NFields(), 4)
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, TestTimeout, false),
}
err := v.Gather(acc)
assert.NoError(t, err)
assert.True(t, acc.HasMeasurement("openntpd"))
assert.Equal(t, acc.NMetrics(), uint64(1))
assert.Equal(t, acc.NFields(), 4)
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, TestTimeout, false),
}
err := v.Gather(acc)
assert.NoError(t, err)
assert.True(t, acc.HasMeasurement("openntpd"))
assert.Equal(t, acc.NMetrics(), uint64(20))
assert.Equal(t, acc.NFields(), 113)
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`