chore(inputs.ntpq): Cleanup existing plugin (#11575)
This commit is contained in:
parent
196abb74cf
commit
9f57f9408c
|
|
@ -19,6 +19,26 @@ import (
|
||||||
//go:embed sample.conf
|
//go:embed sample.conf
|
||||||
var sampleConfig string
|
var sampleConfig string
|
||||||
|
|
||||||
|
// Due to problems with a parsing, we have to use regexp expression in order
|
||||||
|
// to remove string that starts from '(' and ends with space
|
||||||
|
// see: https://github.com/influxdata/telegraf/issues/2386
|
||||||
|
var reBrackets = regexp.MustCompile(`\s+\([\S]*`)
|
||||||
|
|
||||||
|
type elementType int64
|
||||||
|
|
||||||
|
const (
|
||||||
|
None elementType = iota
|
||||||
|
Tag
|
||||||
|
FieldInt
|
||||||
|
FieldFloat
|
||||||
|
FieldDuration
|
||||||
|
)
|
||||||
|
|
||||||
|
type column struct {
|
||||||
|
name string
|
||||||
|
etype elementType
|
||||||
|
}
|
||||||
|
|
||||||
// Mapping of ntpq header names to tag keys
|
// Mapping of ntpq header names to tag keys
|
||||||
var tagHeaders = map[string]string{
|
var tagHeaders = map[string]string{
|
||||||
"remote": "remote",
|
"remote": "remote",
|
||||||
|
|
@ -27,13 +47,20 @@ var tagHeaders = map[string]string{
|
||||||
"t": "type",
|
"t": "type",
|
||||||
}
|
}
|
||||||
|
|
||||||
type NTPQ struct {
|
// Mapping of fields
|
||||||
runQ func() ([]byte, error)
|
var fieldElements = map[string]elementType{
|
||||||
tagI map[string]int
|
"delay": FieldFloat,
|
||||||
floatI map[string]int
|
"jitter": FieldFloat,
|
||||||
intI map[string]int
|
"offset": FieldFloat,
|
||||||
|
"reach": FieldInt,
|
||||||
|
"poll": FieldDuration,
|
||||||
|
"when": FieldDuration,
|
||||||
|
}
|
||||||
|
|
||||||
|
type NTPQ struct {
|
||||||
DNSLookup bool `toml:"dns_lookup"`
|
DNSLookup bool `toml:"dns_lookup"`
|
||||||
|
|
||||||
|
runQ func() ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*NTPQ) SampleConfig() string {
|
func (*NTPQ) SampleConfig() string {
|
||||||
|
|
@ -46,197 +73,141 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Due to problems with a parsing, we have to use regexp expression in order
|
|
||||||
// to remove string that starts from '(' and ends with space
|
|
||||||
// see: https://github.com/influxdata/telegraf/issues/2386
|
|
||||||
reg, err := regexp.Compile(`\s+\([\S]*`)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
lineCounter := 0
|
|
||||||
numColumns := 0
|
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(out))
|
scanner := bufio.NewScanner(bytes.NewReader(out))
|
||||||
|
|
||||||
|
// Look for the header
|
||||||
|
var columns []column
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
|
||||||
|
_, elements := processLine(line)
|
||||||
|
if len(elements) < 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, el := range elements {
|
||||||
|
// Check if the element is a tag
|
||||||
|
if name, isTag := tagHeaders[el]; isTag {
|
||||||
|
columns = append(columns, column{
|
||||||
|
name: name,
|
||||||
|
etype: Tag,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a field
|
||||||
|
if etype, isField := fieldElements[el]; isField {
|
||||||
|
columns = append(columns, column{
|
||||||
|
name: el,
|
||||||
|
etype: etype,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip the column if not found
|
||||||
|
columns = append(columns, column{etype: None})
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
|
||||||
|
prefix, elements := processLine(line)
|
||||||
|
if len(elements) != len(columns) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
tags := make(map[string]string)
|
tags := make(map[string]string)
|
||||||
// if there is an ntpq state prefix, remove it and make it it's own tag
|
fields := make(map[string]interface{})
|
||||||
// see https://github.com/influxdata/telegraf/issues/1161
|
|
||||||
if strings.ContainsAny(string(line[0]), "*#o+x.-") {
|
if prefix != "" {
|
||||||
tags["state_prefix"] = string(line[0])
|
tags["state_prefix"] = prefix
|
||||||
line = strings.TrimLeft(line, "*#o+x.-")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
line = reg.ReplaceAllString(line, "")
|
for i, raw := range elements {
|
||||||
|
col := columns[i]
|
||||||
|
|
||||||
fields := strings.Fields(line)
|
switch col.etype {
|
||||||
if len(fields) < 2 {
|
case None:
|
||||||
continue
|
continue
|
||||||
}
|
case Tag:
|
||||||
|
tags[col.name] = raw
|
||||||
// If lineCounter == 0, then this is the header line
|
case FieldInt:
|
||||||
if lineCounter == 0 {
|
value, err := strconv.ParseInt(raw, 10, 64)
|
||||||
numColumns = len(fields)
|
|
||||||
for i, field := range fields {
|
|
||||||
// Check if field is a tag:
|
|
||||||
if tagKey, ok := tagHeaders[field]; ok {
|
|
||||||
n.tagI[tagKey] = i
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if field is a float metric:
|
|
||||||
if _, ok := n.floatI[field]; ok {
|
|
||||||
n.floatI[field] = i
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if field is an int metric:
|
|
||||||
if _, ok := n.intI[field]; ok {
|
|
||||||
n.intI[field] = i
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if len(fields) != numColumns {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
mFields := make(map[string]interface{})
|
|
||||||
|
|
||||||
// Get tags from output
|
|
||||||
for key, index := range n.tagI {
|
|
||||||
if index == -1 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
tags[key] = fields[index]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get integer metrics from output
|
|
||||||
for key, index := range n.intI {
|
|
||||||
if index == -1 || index >= len(fields) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if fields[index] == "-" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if key == "when" || key == "poll" {
|
|
||||||
when := fields[index]
|
|
||||||
switch {
|
|
||||||
case strings.HasSuffix(when, "h"):
|
|
||||||
m, err := strconv.Atoi(strings.TrimSuffix(fields[index], "h"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
|
acc.AddError(fmt.Errorf("parsing %q (%v) as int failed: %w", col.name, raw, err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// seconds in an hour
|
fields[col.name] = value
|
||||||
mFields[key] = int64(m) * 3600
|
case FieldFloat:
|
||||||
continue
|
value, err := strconv.ParseFloat(raw, 64)
|
||||||
case strings.HasSuffix(when, "d"):
|
|
||||||
m, err := strconv.Atoi(strings.TrimSuffix(fields[index], "d"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
|
acc.AddError(fmt.Errorf("parsing %q (%v) as float failed: %w", col.name, raw, err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// seconds in a day
|
fields[col.name] = value
|
||||||
mFields[key] = int64(m) * 86400
|
case FieldDuration:
|
||||||
continue
|
factor := int64(1)
|
||||||
case strings.HasSuffix(when, "m"):
|
suffix := raw[len(raw)-1:]
|
||||||
m, err := strconv.Atoi(strings.TrimSuffix(fields[index], "m"))
|
switch suffix {
|
||||||
|
case "d":
|
||||||
|
factor = 24 * 3600
|
||||||
|
case "h":
|
||||||
|
factor = 3600
|
||||||
|
case "m":
|
||||||
|
factor = 60
|
||||||
|
default:
|
||||||
|
suffix = ""
|
||||||
|
}
|
||||||
|
value, err := strconv.ParseInt(strings.TrimSuffix(raw, suffix), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
|
acc.AddError(fmt.Errorf("parsing %q (%v) as duration failed: %w", col.name, raw, err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// seconds in a day
|
fields[col.name] = value * factor
|
||||||
mFields[key] = int64(m) * 60
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := strconv.Atoi(fields[index])
|
acc.AddFields("ntpq", fields, tags)
|
||||||
if err != nil {
|
|
||||||
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
mFields[key] = int64(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get float metrics from output
|
|
||||||
for key, index := range n.floatI {
|
|
||||||
if index == -1 || index >= len(fields) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if fields[index] == "-" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
m, err := strconv.ParseFloat(fields[index], 64)
|
|
||||||
if err != nil {
|
|
||||||
acc.AddError(fmt.Errorf("error ntpq: parsing float: %s", fields[index]))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
mFields[key] = m
|
|
||||||
}
|
|
||||||
|
|
||||||
acc.AddFields("ntpq", mFields, tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
lineCounter++
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NTPQ) runq() ([]byte, error) {
|
func (n *NTPQ) Init() error {
|
||||||
|
if n.runQ == nil {
|
||||||
|
n.runQ = func() ([]byte, error) {
|
||||||
bin, err := exec.LookPath("ntpq")
|
bin, err := exec.LookPath("ntpq")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmd *exec.Cmd
|
args := []string{"-p"}
|
||||||
if n.DNSLookup {
|
if !n.DNSLookup {
|
||||||
cmd = exec.Command(bin, "-p")
|
args = append(args, "-n")
|
||||||
} else {
|
|
||||||
cmd = exec.Command(bin, "-p", "-n")
|
|
||||||
}
|
}
|
||||||
|
cmd := exec.Command(bin, args...)
|
||||||
return cmd.Output()
|
return cmd.Output()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
func newNTPQ() *NTPQ {
|
return nil
|
||||||
// Mapping of the ntpq tag key to the index in the command output
|
|
||||||
tagI := map[string]int{
|
|
||||||
"remote": -1,
|
|
||||||
"refid": -1,
|
|
||||||
"stratum": -1,
|
|
||||||
"type": -1,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mapping of float metrics to their index in the command output
|
func processLine(line string) (string, []string) {
|
||||||
floatI := map[string]int{
|
// if there is an ntpq state prefix, remove it and make it it's own tag
|
||||||
"delay": -1,
|
// see https://github.com/influxdata/telegraf/issues/1161
|
||||||
"offset": -1,
|
var prefix string
|
||||||
"jitter": -1,
|
if strings.ContainsAny(string(line[0]), "*#o+x.-") {
|
||||||
|
prefix = string(line[0])
|
||||||
|
line = strings.TrimLeft(line, "*#o+x.-")
|
||||||
}
|
}
|
||||||
|
line = reBrackets.ReplaceAllString(line, "")
|
||||||
|
|
||||||
// Mapping of int metrics to their index in the command output
|
return prefix, strings.Fields(line)
|
||||||
intI := map[string]int{
|
|
||||||
"when": -1,
|
|
||||||
"poll": -1,
|
|
||||||
"reach": -1,
|
|
||||||
}
|
|
||||||
|
|
||||||
n := &NTPQ{
|
|
||||||
tagI: tagI,
|
|
||||||
floatI: floatI,
|
|
||||||
intI: intI,
|
|
||||||
}
|
|
||||||
n.runQ = n.runq
|
|
||||||
return n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
inputs.Add("ntpq", func() telegraf.Input {
|
inputs.Add("ntpq", func() telegraf.Input {
|
||||||
return newNTPQ()
|
return &NTPQ{}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,557 +1,105 @@
|
||||||
package ntpq
|
package ntpq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
"github.com/influxdata/telegraf/config"
|
||||||
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
|
"github.com/influxdata/telegraf/plugins/parsers/influx"
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSingleNTPQ(t *testing.T) {
|
func TestCases(t *testing.T) {
|
||||||
tt := tester{
|
// Get all directories in testdata
|
||||||
ret: []byte(singleNTPQ),
|
folders, err := os.ReadDir("testcases")
|
||||||
err: nil,
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
// Register the plugin
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
inputs.Add("ntpq", func() telegraf.Input {
|
||||||
|
return &NTPQ{}
|
||||||
|
})
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
// Prepare the influx parser for expectations
|
||||||
"when": int64(101),
|
parser := &influx.Parser{}
|
||||||
"poll": int64(256),
|
require.NoError(t, parser.Init())
|
||||||
"reach": int64(37),
|
|
||||||
"delay": float64(51.016),
|
for _, f := range folders {
|
||||||
"offset": float64(233.010),
|
// Only handle folders
|
||||||
"jitter": float64(17.462),
|
if !f.IsDir() {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
tags := map[string]string{
|
configFilename := filepath.Join("testcases", f.Name(), "telegraf.conf")
|
||||||
"remote": "uschi5-ntp-002.",
|
inputFilename := filepath.Join("testcases", f.Name(), "input.txt")
|
||||||
"state_prefix": "*",
|
inputErrorFilename := filepath.Join("testcases", f.Name(), "input.err")
|
||||||
"refid": "10.177.80.46",
|
expectedFilename := filepath.Join("testcases", f.Name(), "expected.out")
|
||||||
"stratum": "2",
|
expectedErrorFilename := filepath.Join("testcases", f.Name(), "expected.err")
|
||||||
"type": "u",
|
|
||||||
}
|
// Compare options
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
options := []cmp.Option{
|
||||||
|
testutil.IgnoreTime(),
|
||||||
|
testutil.SortMetrics(),
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBadIntNTPQ(t *testing.T) {
|
t.Run(f.Name(), func(t *testing.T) {
|
||||||
tt := tester{
|
// Read the input data
|
||||||
ret: []byte(badIntParseNTPQ),
|
data, err := os.ReadFile(inputFilename)
|
||||||
err: nil,
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
// Read the input error message if any
|
||||||
require.Error(t, acc.GatherError(n.Gather))
|
var inputErr error
|
||||||
|
if _, err := os.Stat(inputErrorFilename); err == nil {
|
||||||
fields := map[string]interface{}{
|
x, err := testutil.ParseLinesFromFile(inputErrorFilename)
|
||||||
"when": int64(101),
|
require.NoError(t, err)
|
||||||
"reach": int64(37),
|
require.Len(t, x, 1)
|
||||||
"delay": float64(51.016),
|
inputErr = errors.New(x[0])
|
||||||
"offset": float64(233.010),
|
|
||||||
"jitter": float64(17.462),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "uschi5-ntp-002.",
|
|
||||||
"state_prefix": "*",
|
|
||||||
"refid": "10.177.80.46",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBadFloatNTPQ(t *testing.T) {
|
// Read the expected output if any
|
||||||
tt := tester{
|
var expected []telegraf.Metric
|
||||||
ret: []byte(badFloatParseNTPQ),
|
if _, err := os.Stat(expectedFilename); err == nil {
|
||||||
err: nil,
|
var err error
|
||||||
}
|
expected, err = testutil.ParseMetricsFromFile(expectedFilename, parser)
|
||||||
n := newNTPQ()
|
require.NoError(t, err)
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.Error(t, acc.GatherError(n.Gather))
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"when": int64(2),
|
|
||||||
"poll": int64(256),
|
|
||||||
"reach": int64(37),
|
|
||||||
"delay": float64(51.016),
|
|
||||||
"jitter": float64(17.462),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "uschi5-ntp-002.",
|
|
||||||
"state_prefix": "*",
|
|
||||||
"refid": "10.177.80.46",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDaysNTPQ(t *testing.T) {
|
// Read the expected output if any
|
||||||
tt := tester{
|
var errorMsg string
|
||||||
ret: []byte(whenDaysNTPQ),
|
if _, err := os.Stat(expectedErrorFilename); err == nil {
|
||||||
err: nil,
|
x, err := testutil.ParseLinesFromFile(expectedErrorFilename)
|
||||||
}
|
require.NoError(t, err)
|
||||||
n := newNTPQ()
|
require.Len(t, x, 1)
|
||||||
n.runQ = tt.runqTest
|
errorMsg = x[0]
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"when": int64(172800),
|
|
||||||
"poll": int64(256),
|
|
||||||
"reach": int64(37),
|
|
||||||
"delay": float64(51.016),
|
|
||||||
"offset": float64(233.010),
|
|
||||||
"jitter": float64(17.462),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "uschi5-ntp-002.",
|
|
||||||
"state_prefix": "*",
|
|
||||||
"refid": "10.177.80.46",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHoursNTPQ(t *testing.T) {
|
// Configure the plugin
|
||||||
tt := tester{
|
cfg := config.NewConfig()
|
||||||
ret: []byte(whenHoursNTPQ),
|
require.NoError(t, cfg.LoadConfig(configFilename))
|
||||||
err: nil,
|
require.Len(t, cfg.Inputs, 1)
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
// Fake the reading
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
plugin := cfg.Inputs[0].Input.(*NTPQ)
|
||||||
|
plugin.runQ = func() ([]byte, error) {
|
||||||
|
return data, inputErr
|
||||||
|
}
|
||||||
|
require.NoError(t, plugin.Init())
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
var acc testutil.Accumulator
|
||||||
"when": int64(7200),
|
if errorMsg != "" {
|
||||||
"poll": int64(256),
|
require.EqualError(t, plugin.Gather(&acc), errorMsg)
|
||||||
"reach": int64(37),
|
return
|
||||||
"delay": float64(51.016),
|
|
||||||
"offset": float64(233.010),
|
|
||||||
"jitter": float64(17.462),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "uschi5-ntp-002.",
|
|
||||||
"state_prefix": "*",
|
|
||||||
"refid": "10.177.80.46",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMinutesNTPQ(t *testing.T) {
|
// No error case
|
||||||
tt := tester{
|
require.NoError(t, plugin.Gather(&acc))
|
||||||
ret: []byte(whenMinutesNTPQ),
|
actual := acc.GetTelegrafMetrics()
|
||||||
err: nil,
|
testutil.RequireMetricsEqual(t, expected, actual, options...)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"when": int64(120),
|
|
||||||
"poll": int64(256),
|
|
||||||
"reach": int64(37),
|
|
||||||
"delay": float64(51.016),
|
|
||||||
"offset": float64(233.010),
|
|
||||||
"jitter": float64(17.462),
|
|
||||||
}
|
}
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "uschi5-ntp-002.",
|
|
||||||
"state_prefix": "*",
|
|
||||||
"refid": "10.177.80.46",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBadWhenNTPQ(t *testing.T) {
|
|
||||||
tt := tester{
|
|
||||||
ret: []byte(whenBadNTPQ),
|
|
||||||
err: nil,
|
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.Error(t, acc.GatherError(n.Gather))
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"poll": int64(256),
|
|
||||||
"reach": int64(37),
|
|
||||||
"delay": float64(51.016),
|
|
||||||
"offset": float64(233.010),
|
|
||||||
"jitter": float64(17.462),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "uschi5-ntp-002.",
|
|
||||||
"state_prefix": "*",
|
|
||||||
"refid": "10.177.80.46",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestParserNTPQ - realated to:
|
|
||||||
// https://github.com/influxdata/telegraf/issues/2386
|
|
||||||
func TestParserNTPQ(t *testing.T) {
|
|
||||||
tt := tester{
|
|
||||||
ret: []byte(multiParserNTPQ),
|
|
||||||
err: nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"poll": int64(64),
|
|
||||||
"when": int64(60),
|
|
||||||
"reach": int64(377),
|
|
||||||
"delay": float64(0.0),
|
|
||||||
"offset": float64(0.045),
|
|
||||||
"jitter": float64(1.012),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "SHM(0)",
|
|
||||||
"state_prefix": "*",
|
|
||||||
"refid": ".PPS.",
|
|
||||||
"stratum": "1",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
|
|
||||||
fields = map[string]interface{}{
|
|
||||||
"poll": int64(128),
|
|
||||||
"when": int64(121),
|
|
||||||
"reach": int64(377),
|
|
||||||
"delay": float64(0.0),
|
|
||||||
"offset": float64(10.105),
|
|
||||||
"jitter": float64(2.012),
|
|
||||||
}
|
|
||||||
tags = map[string]string{
|
|
||||||
"remote": "SHM(1)",
|
|
||||||
"state_prefix": "-",
|
|
||||||
"refid": ".GPS.",
|
|
||||||
"stratum": "1",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
|
|
||||||
fields = map[string]interface{}{
|
|
||||||
"poll": int64(1024),
|
|
||||||
"when": int64(10),
|
|
||||||
"reach": int64(377),
|
|
||||||
"delay": float64(1.748),
|
|
||||||
"offset": float64(0.373),
|
|
||||||
"jitter": float64(0.101),
|
|
||||||
}
|
|
||||||
tags = map[string]string{
|
|
||||||
"remote": "37.58.57.238",
|
|
||||||
"state_prefix": "+",
|
|
||||||
"refid": "192.53.103.103",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMultiNTPQ(t *testing.T) {
|
|
||||||
tt := tester{
|
|
||||||
ret: []byte(multiNTPQ),
|
|
||||||
err: nil,
|
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"delay": float64(54.033),
|
|
||||||
"jitter": float64(449514),
|
|
||||||
"offset": float64(243.426),
|
|
||||||
"poll": int64(1024),
|
|
||||||
"reach": int64(377),
|
|
||||||
"when": int64(740),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"refid": "10.177.80.37",
|
|
||||||
"remote": "83.137.98.96",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
|
|
||||||
fields = map[string]interface{}{
|
|
||||||
"delay": float64(60.785),
|
|
||||||
"jitter": float64(449539),
|
|
||||||
"offset": float64(232.597),
|
|
||||||
"poll": int64(1024),
|
|
||||||
"reach": int64(377),
|
|
||||||
"when": int64(739),
|
|
||||||
}
|
|
||||||
tags = map[string]string{
|
|
||||||
"refid": "10.177.80.37",
|
|
||||||
"remote": "81.7.16.52",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBadHeaderNTPQ(t *testing.T) {
|
|
||||||
tt := tester{
|
|
||||||
ret: []byte(badHeaderNTPQ),
|
|
||||||
err: nil,
|
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"when": int64(101),
|
|
||||||
"poll": int64(256),
|
|
||||||
"reach": int64(37),
|
|
||||||
"delay": float64(51.016),
|
|
||||||
"offset": float64(233.010),
|
|
||||||
"jitter": float64(17.462),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "uschi5-ntp-002.",
|
|
||||||
"state_prefix": "*",
|
|
||||||
"refid": "10.177.80.46",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMissingDelayColumnNTPQ(t *testing.T) {
|
|
||||||
tt := tester{
|
|
||||||
ret: []byte(missingDelayNTPQ),
|
|
||||||
err: nil,
|
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"when": int64(101),
|
|
||||||
"poll": int64(256),
|
|
||||||
"reach": int64(37),
|
|
||||||
"offset": float64(233.010),
|
|
||||||
"jitter": float64(17.462),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "uschi5-ntp-002.",
|
|
||||||
"state_prefix": "*",
|
|
||||||
"refid": "10.177.80.46",
|
|
||||||
"type": "u",
|
|
||||||
}
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLongPoll(t *testing.T) {
|
|
||||||
tt := tester{
|
|
||||||
ret: []byte(longPollTime),
|
|
||||||
err: nil,
|
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
|
||||||
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"when": int64(617),
|
|
||||||
"poll": int64(4080),
|
|
||||||
"reach": int64(377),
|
|
||||||
"offset": float64(2.849),
|
|
||||||
"jitter": float64(1.192),
|
|
||||||
"delay": float64(9.145),
|
|
||||||
}
|
|
||||||
tags := map[string]string{
|
|
||||||
"remote": "uschi5-ntp-002.",
|
|
||||||
"state_prefix": "-",
|
|
||||||
"refid": "10.177.80.46",
|
|
||||||
"type": "u",
|
|
||||||
"stratum": "3",
|
|
||||||
}
|
|
||||||
|
|
||||||
acc.AssertContainsTaggedFields(t, "ntpq", fields, tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFailedNTPQ(t *testing.T) {
|
|
||||||
tt := tester{
|
|
||||||
ret: []byte(singleNTPQ),
|
|
||||||
err: fmt.Errorf("test failure"),
|
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{}
|
|
||||||
require.Error(t, acc.GatherError(n.Gather))
|
|
||||||
}
|
|
||||||
|
|
||||||
// It is possible for the output of ntqp to be missing the refid column. This
|
|
||||||
// is believed to be http://bugs.ntp.org/show_bug.cgi?id=3484 which is fixed
|
|
||||||
// in ntp-4.2.8p12 (included first in Debian Buster).
|
|
||||||
func TestNoRefID(t *testing.T) {
|
|
||||||
now := time.Now()
|
|
||||||
expected := []telegraf.Metric{
|
|
||||||
testutil.MustMetric("ntpq",
|
|
||||||
map[string]string{
|
|
||||||
"refid": "10.177.80.37",
|
|
||||||
"remote": "83.137.98.96",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
},
|
|
||||||
map[string]interface{}{
|
|
||||||
"delay": float64(54.033),
|
|
||||||
"jitter": float64(449514),
|
|
||||||
"offset": float64(243.426),
|
|
||||||
"poll": int64(1024),
|
|
||||||
"reach": int64(377),
|
|
||||||
"when": int64(740),
|
|
||||||
},
|
|
||||||
now),
|
|
||||||
testutil.MustMetric("ntpq",
|
|
||||||
map[string]string{
|
|
||||||
"refid": "10.177.80.37",
|
|
||||||
"remote": "131.188.3.221",
|
|
||||||
"stratum": "2",
|
|
||||||
"type": "u",
|
|
||||||
},
|
|
||||||
map[string]interface{}{
|
|
||||||
"delay": float64(111.820),
|
|
||||||
"jitter": float64(449528),
|
|
||||||
"offset": float64(261.921),
|
|
||||||
"poll": int64(1024),
|
|
||||||
"reach": int64(377),
|
|
||||||
"when": int64(783),
|
|
||||||
},
|
|
||||||
now),
|
|
||||||
}
|
|
||||||
|
|
||||||
tt := tester{
|
|
||||||
ret: []byte(noRefID),
|
|
||||||
err: nil,
|
|
||||||
}
|
|
||||||
n := newNTPQ()
|
|
||||||
n.runQ = tt.runqTest
|
|
||||||
|
|
||||||
acc := testutil.Accumulator{
|
|
||||||
TimeFunc: func() time.Time { return now },
|
|
||||||
}
|
|
||||||
|
|
||||||
require.NoError(t, acc.GatherError(n.Gather))
|
|
||||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics())
|
|
||||||
}
|
|
||||||
|
|
||||||
type tester struct {
|
|
||||||
ret []byte
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tester) runqTest() ([]byte, error) {
|
|
||||||
return t.ret, t.err
|
|
||||||
}
|
|
||||||
|
|
||||||
var singleNTPQ = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010 17.462
|
|
||||||
`
|
|
||||||
|
|
||||||
var badHeaderNTPQ = `remote refid foobar t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010 17.462
|
|
||||||
`
|
|
||||||
|
|
||||||
var missingDelayNTPQ = `remote refid foobar t when poll reach offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 233.010 17.462
|
|
||||||
`
|
|
||||||
|
|
||||||
var whenDaysNTPQ = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*uschi5-ntp-002. 10.177.80.46 2 u 2d 256 37 51.016 233.010 17.462
|
|
||||||
`
|
|
||||||
|
|
||||||
var whenHoursNTPQ = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*uschi5-ntp-002. 10.177.80.46 2 u 2h 256 37 51.016 233.010 17.462
|
|
||||||
`
|
|
||||||
|
|
||||||
var whenMinutesNTPQ = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*uschi5-ntp-002. 10.177.80.46 2 u 2m 256 37 51.016 233.010 17.462
|
|
||||||
`
|
|
||||||
|
|
||||||
var whenBadNTPQ = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*uschi5-ntp-002. 10.177.80.46 2 u 2q 256 37 51.016 233.010 17.462
|
|
||||||
`
|
|
||||||
|
|
||||||
var badFloatParseNTPQ = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*uschi5-ntp-002. 10.177.80.46 2 u 2 256 37 51.016 foobar 17.462
|
|
||||||
`
|
|
||||||
|
|
||||||
var badIntParseNTPQ = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*uschi5-ntp-002. 10.177.80.46 2 u 101 foobar 37 51.016 233.010 17.462
|
|
||||||
`
|
|
||||||
|
|
||||||
var multiNTPQ = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
83.137.98.96 10.177.80.37 2 u 740 1024 377 54.033 243.426 449514.
|
|
||||||
81.7.16.52 10.177.80.37 2 u 739 1024 377 60.785 232.597 449539.
|
|
||||||
131.188.3.221 10.177.80.37 2 u 783 1024 377 111.820 261.921 449528.
|
|
||||||
5.9.29.107 10.177.80.37 2 u 703 1024 377 205.704 160.406 449602.
|
|
||||||
91.189.94.4 10.177.80.37 2 u 673 1024 377 143.047 274.726 449445.
|
|
||||||
`
|
|
||||||
|
|
||||||
var multiParserNTPQ = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
*SHM(0) .PPS. 1 u 60 64 377 0.000 0.045 1.012
|
|
||||||
+37.58.57.238 (d 192.53.103.103 2 u 10 1024 377 1.748 0.373 0.101
|
|
||||||
+37.58.57.238 (domain) 192.53.103.103 2 u 10 1024 377 1.748 0.373 0.101
|
|
||||||
+37.58.57.238 ( 192.53.103.103 2 u 10 1024 377 1.748 0.373 0.101
|
|
||||||
-SHM(1) .GPS. 1 u 121 128 377 0.000 10.105 2.012
|
|
||||||
`
|
|
||||||
|
|
||||||
var noRefID = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
83.137.98.96 10.177.80.37 2 u 740 1024 377 54.033 243.426 449514.
|
|
||||||
91.189.94.4 2 u 673 1024 377 143.047 274.726 449445.
|
|
||||||
131.188.3.221 10.177.80.37 2 u 783 1024 377 111.820 261.921 449528.
|
|
||||||
`
|
|
||||||
|
|
||||||
var longPollTime = ` remote refid st t when poll reach delay offset jitter
|
|
||||||
==============================================================================
|
|
||||||
-uschi5-ntp-002. 10.177.80.46 3 u 617 68m 377 9.145 +2.849 1.192
|
|
||||||
`
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=*,refid=10.177.80.46,stratum=2,type=u when=2i,poll=256i,reach=37i,delay=51.016,jitter=17.462 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 2 256 37 51.016 foobar 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=*,refid=10.177.80.46,type=u when=101i,poll=256i,reach=37i,delay=51.016,offset=233.010,jitter=17.462 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid foobar t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=*,refid=10.177.80.46,stratum=2,type=u when=101i,reach=37i,delay=51.016,offset=233.010,jitter=17.462 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 101 foobar 37 51.016 233.010 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=*,refid=10.177.80.46,stratum=2,type=u poll=256i,reach=37i,delay=51.016,offset=233.010,jitter=17.462 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 2q 256 37 51.016 233.010 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=*,refid=10.177.80.46,stratum=2,type=u when=172800i,poll=256i,reach=37i,delay=51.016,offset=233.010,jitter=17.462 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 2d 256 37 51.016 233.010 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
test failure
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
test failure
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=*,refid=10.177.80.46,stratum=2,type=u when=7200i,poll=256i,reach=37i,delay=51.016,offset=233.010,jitter=17.462 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 2h 256 37 51.016 233.010 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
ntpq,remote=SHM(0),state_prefix=*,refid=.PPS.,stratum=1,type=u when=60i,poll=64i,reach=377i,delay=0.0,offset=0.045,jitter=1.012 0
|
||||||
|
ntpq,remote=SHM(1),state_prefix=-,refid=.GPS.,stratum=1,type=u when=121i,poll=128i,reach=377i,delay=0.0,offset=10.105,jitter=2.012 0
|
||||||
|
ntpq,remote=37.58.57.238,state_prefix=+,refid=192.53.103.103,stratum=2,type=u when=10i,poll=1024i,reach=377i,delay=1.748,offset=0.373,jitter=0.101 0
|
||||||
|
ntpq,remote=37.58.57.238,state_prefix=+,refid=192.53.103.103,stratum=2,type=u when=10i,poll=1024i,reach=377i,delay=1.748,offset=0.373,jitter=0.101 0
|
||||||
|
ntpq,remote=37.58.57.238,state_prefix=+,refid=192.53.103.103,stratum=2,type=u when=10i,poll=1024i,reach=377i,delay=1.748,offset=0.373,jitter=0.101 0
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*SHM(0) .PPS. 1 u 60 64 377 0.000 0.045 1.012
|
||||||
|
+37.58.57.238 (d 192.53.103.103 2 u 10 1024 377 1.748 0.373 0.101
|
||||||
|
+37.58.57.238 (domain) 192.53.103.103 2 u 10 1024 377 1.748 0.373 0.101
|
||||||
|
+37.58.57.238 ( 192.53.103.103 2 u 10 1024 377 1.748 0.373 0.101
|
||||||
|
-SHM(1) .GPS. 1 u 121 128 377 0.000 10.105 2.012
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
# Test related to
|
||||||
|
# https://github.com/influxdata/telegraf/issues/2386
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=-,refid=10.177.80.46,stratum=3,type=u when=617i,poll=4080i,reach=377i,delay=9.145,offset=2.849,jitter=1.192 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
-uschi5-ntp-002. 10.177.80.46 3 u 617 68m 377 9.145 +2.849 1.192
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=*,refid=10.177.80.46,stratum=2,type=u when=120i,poll=256i,reach=37i,delay=51.016,offset=233.010,jitter=17.462 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 2m 256 37 51.016 233.010 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=*,refid=10.177.80.46,type=u when=101i,poll=256i,reach=37i,offset=233.010,jitter=17.462 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid foobar t when poll reach offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 233.010 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
ntpq,refid=10.177.80.37,remote=83.137.98.96,stratum=2,type=u delay=54.033,offset=243.426,jitter=449514,when=740i,poll=1024i,reach=377i 0
|
||||||
|
ntpq,refid=10.177.80.37,remote=81.7.16.52,stratum=2,type=u reach=377i,delay=60.785,offset=232.597,jitter=449539,when=739i,poll=1024i 0
|
||||||
|
ntpq,refid=10.177.80.37,remote=131.188.3.221,stratum=2,type=u when=783i,poll=1024i,reach=377i,delay=111.82,offset=261.921,jitter=449528 0
|
||||||
|
ntpq,refid=10.177.80.37,remote=5.9.29.107,stratum=2,type=u reach=377i,delay=205.704,offset=160.406,jitter=449602,when=703i,poll=1024i 0
|
||||||
|
ntpq,refid=10.177.80.37,remote=91.189.94.4,stratum=2,type=u offset=274.726,jitter=449445,when=673i,poll=1024i,reach=377i,delay=143.047 0
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
83.137.98.96 10.177.80.37 2 u 740 1024 377 54.033 243.426 449514.
|
||||||
|
81.7.16.52 10.177.80.37 2 u 739 1024 377 60.785 232.597 449539.
|
||||||
|
131.188.3.221 10.177.80.37 2 u 783 1024 377 111.820 261.921 449528.
|
||||||
|
5.9.29.107 10.177.80.37 2 u 703 1024 377 205.704 160.406 449602.
|
||||||
|
91.189.94.4 10.177.80.37 2 u 673 1024 377 143.047 274.726 449445.
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
ntpq,remote=83.137.98.96,refid=10.177.80.37,stratum=2,type=u when=740i,poll=1024i,reach=377i,delay=54.033,offset=243.426,jitter=449514 0
|
||||||
|
ntpq,remote=131.188.3.221,refid=10.177.80.37,stratum=2,type=u when=783i,poll=1024i,reach=377i,delay=111.820,offset=261.921,jitter=449528 0
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
83.137.98.96 10.177.80.37 2 u 740 1024 377 54.033 243.426 449514.
|
||||||
|
91.189.94.4 2 u 673 1024 377 143.047 274.726 449445.
|
||||||
|
131.188.3.221 10.177.80.37 2 u 783 1024 377 111.820 261.921 449528.
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
# It is possible for the output of ntqp to be missing the refid column. This
|
||||||
|
# is believed to be http://bugs.ntp.org/show_bug.cgi?id=3484 which is fixed
|
||||||
|
# in ntp-4.2.8p12 (included first in Debian Buster).
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ntpq,remote=uschi5-ntp-002.,state_prefix=*,refid=10.177.80.46,stratum=2,type=u when=101i,poll=256i,reach=37i,delay=51.016,offset=233.010,jitter=17.462 0
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
remote refid st t when poll reach delay offset jitter
|
||||||
|
==============================================================================
|
||||||
|
*uschi5-ntp-002. 10.177.80.46 2 u 101 256 37 51.016 233.010 17.462
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
[[inputs.ntpq]]
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
package testutil
|
package testutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
|
@ -82,3 +84,55 @@ func ParseMetricsFrom(lines []string, header string, parser LineParser) ([]teleg
|
||||||
}
|
}
|
||||||
return metrics, nil
|
return metrics, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ParseMetricsFromFile parses metrics from the given file in line-protocol
|
||||||
|
func ParseMetricsFromFile(filename string, parser telegraf.Parser) ([]telegraf.Metric, error) {
|
||||||
|
var metrics []telegraf.Metric
|
||||||
|
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Bytes()
|
||||||
|
if len(line) == 0 || strings.HasPrefix(string(line), "#") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
nonutc, err := parser.Parse(line)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse metric in %q failed: %v", line, err)
|
||||||
|
}
|
||||||
|
for _, m := range nonutc {
|
||||||
|
// The timezone needs to be UTC to match the timestamp test results
|
||||||
|
m.SetTime(m.Time().UTC())
|
||||||
|
metrics = append(metrics, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return metrics, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//ParseLinesFromFile returns the lines of the file as strings
|
||||||
|
func ParseLinesFromFile(filename string) ([]string, error) {
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
var lines []string
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if len(line) == 0 || strings.HasPrefix(line, "#") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
lines = append(lines, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue