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
|
||||
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
|
||||
var tagHeaders = map[string]string{
|
||||
"remote": "remote",
|
||||
|
|
@ -27,13 +47,20 @@ var tagHeaders = map[string]string{
|
|||
"t": "type",
|
||||
}
|
||||
|
||||
type NTPQ struct {
|
||||
runQ func() ([]byte, error)
|
||||
tagI map[string]int
|
||||
floatI map[string]int
|
||||
intI map[string]int
|
||||
// Mapping of fields
|
||||
var fieldElements = map[string]elementType{
|
||||
"delay": FieldFloat,
|
||||
"jitter": FieldFloat,
|
||||
"offset": FieldFloat,
|
||||
"reach": FieldInt,
|
||||
"poll": FieldDuration,
|
||||
"when": FieldDuration,
|
||||
}
|
||||
|
||||
type NTPQ struct {
|
||||
DNSLookup bool `toml:"dns_lookup"`
|
||||
|
||||
runQ func() ([]byte, error)
|
||||
}
|
||||
|
||||
func (*NTPQ) SampleConfig() string {
|
||||
|
|
@ -46,197 +73,141 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
|
|||
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))
|
||||
|
||||
// Look for the header
|
||||
var columns []column
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
tags := make(map[string]string)
|
||||
// if there is an ntpq state prefix, remove it and make it it's own tag
|
||||
// see https://github.com/influxdata/telegraf/issues/1161
|
||||
if strings.ContainsAny(string(line[0]), "*#o+x.-") {
|
||||
tags["state_prefix"] = string(line[0])
|
||||
line = strings.TrimLeft(line, "*#o+x.-")
|
||||
}
|
||||
|
||||
line = reg.ReplaceAllString(line, "")
|
||||
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < 2 {
|
||||
_, elements := processLine(line)
|
||||
if len(elements) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
// If lineCounter == 0, then this is the header line
|
||||
if lineCounter == 0 {
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
mFields := make(map[string]interface{})
|
||||
|
||||
// Get tags from output
|
||||
for key, index := range n.tagI {
|
||||
if index == -1 {
|
||||
continue
|
||||
}
|
||||
tags[key] = fields[index]
|
||||
// Add a field
|
||||
if etype, isField := fieldElements[el]; isField {
|
||||
columns = append(columns, column{
|
||||
name: el,
|
||||
etype: etype,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// Get integer metrics from output
|
||||
for key, index := range n.intI {
|
||||
if index == -1 || index >= len(fields) {
|
||||
continue
|
||||
}
|
||||
if fields[index] == "-" {
|
||||
continue
|
||||
}
|
||||
// Skip the column if not found
|
||||
columns = append(columns, column{etype: None})
|
||||
}
|
||||
break
|
||||
}
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
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 {
|
||||
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
|
||||
continue
|
||||
}
|
||||
// seconds in an hour
|
||||
mFields[key] = int64(m) * 3600
|
||||
continue
|
||||
case strings.HasSuffix(when, "d"):
|
||||
m, err := strconv.Atoi(strings.TrimSuffix(fields[index], "d"))
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
|
||||
continue
|
||||
}
|
||||
// seconds in a day
|
||||
mFields[key] = int64(m) * 86400
|
||||
continue
|
||||
case strings.HasSuffix(when, "m"):
|
||||
m, err := strconv.Atoi(strings.TrimSuffix(fields[index], "m"))
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("error ntpq: parsing %s as int: %s", key, fields[index]))
|
||||
continue
|
||||
}
|
||||
// seconds in a day
|
||||
mFields[key] = int64(m) * 60
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
m, err := strconv.Atoi(fields[index])
|
||||
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)
|
||||
prefix, elements := processLine(line)
|
||||
if len(elements) != len(columns) {
|
||||
continue
|
||||
}
|
||||
|
||||
lineCounter++
|
||||
tags := make(map[string]string)
|
||||
fields := make(map[string]interface{})
|
||||
|
||||
if prefix != "" {
|
||||
tags["state_prefix"] = prefix
|
||||
}
|
||||
|
||||
for i, raw := range elements {
|
||||
col := columns[i]
|
||||
|
||||
switch col.etype {
|
||||
case None:
|
||||
continue
|
||||
case Tag:
|
||||
tags[col.name] = raw
|
||||
case FieldInt:
|
||||
value, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("parsing %q (%v) as int failed: %w", col.name, raw, err))
|
||||
continue
|
||||
}
|
||||
fields[col.name] = value
|
||||
case FieldFloat:
|
||||
value, err := strconv.ParseFloat(raw, 64)
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("parsing %q (%v) as float failed: %w", col.name, raw, err))
|
||||
continue
|
||||
}
|
||||
fields[col.name] = value
|
||||
case FieldDuration:
|
||||
factor := int64(1)
|
||||
suffix := raw[len(raw)-1:]
|
||||
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 {
|
||||
acc.AddError(fmt.Errorf("parsing %q (%v) as duration failed: %w", col.name, raw, err))
|
||||
continue
|
||||
}
|
||||
fields[col.name] = value * factor
|
||||
}
|
||||
}
|
||||
|
||||
acc.AddFields("ntpq", fields, tags)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NTPQ) Init() error {
|
||||
if n.runQ == nil {
|
||||
n.runQ = func() ([]byte, error) {
|
||||
bin, err := exec.LookPath("ntpq")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
args := []string{"-p"}
|
||||
if !n.DNSLookup {
|
||||
args = append(args, "-n")
|
||||
}
|
||||
cmd := exec.Command(bin, args...)
|
||||
return cmd.Output()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NTPQ) runq() ([]byte, error) {
|
||||
bin, err := exec.LookPath("ntpq")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func processLine(line string) (string, []string) {
|
||||
// if there is an ntpq state prefix, remove it and make it it's own tag
|
||||
// see https://github.com/influxdata/telegraf/issues/1161
|
||||
var prefix string
|
||||
if strings.ContainsAny(string(line[0]), "*#o+x.-") {
|
||||
prefix = string(line[0])
|
||||
line = strings.TrimLeft(line, "*#o+x.-")
|
||||
}
|
||||
line = reBrackets.ReplaceAllString(line, "")
|
||||
|
||||
var cmd *exec.Cmd
|
||||
if n.DNSLookup {
|
||||
cmd = exec.Command(bin, "-p")
|
||||
} else {
|
||||
cmd = exec.Command(bin, "-p", "-n")
|
||||
}
|
||||
|
||||
return cmd.Output()
|
||||
}
|
||||
|
||||
func newNTPQ() *NTPQ {
|
||||
// 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
|
||||
floatI := map[string]int{
|
||||
"delay": -1,
|
||||
"offset": -1,
|
||||
"jitter": -1,
|
||||
}
|
||||
|
||||
// Mapping of int metrics to their index in the command output
|
||||
intI := map[string]int{
|
||||
"when": -1,
|
||||
"poll": -1,
|
||||
"reach": -1,
|
||||
}
|
||||
|
||||
n := &NTPQ{
|
||||
tagI: tagI,
|
||||
floatI: floatI,
|
||||
intI: intI,
|
||||
}
|
||||
n.runQ = n.runq
|
||||
return n
|
||||
return prefix, strings.Fields(line)
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("ntpq", func() telegraf.Input {
|
||||
return newNTPQ()
|
||||
return &NTPQ{}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,557 +1,105 @@
|
|||
package ntpq
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestSingleNTPQ(t *testing.T) {
|
||||
tt := tester{
|
||||
ret: []byte(singleNTPQ),
|
||||
err: nil,
|
||||
}
|
||||
n := newNTPQ()
|
||||
n.runQ = tt.runqTest
|
||||
func TestCases(t *testing.T) {
|
||||
// Get all directories in testdata
|
||||
folders, err := os.ReadDir("testcases")
|
||||
require.NoError(t, err)
|
||||
|
||||
acc := testutil.Accumulator{}
|
||||
require.NoError(t, acc.GatherError(n.Gather))
|
||||
// Register the plugin
|
||||
inputs.Add("ntpq", func() telegraf.Input {
|
||||
return &NTPQ{}
|
||||
})
|
||||
|
||||
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),
|
||||
// Prepare the influx parser for expectations
|
||||
parser := &influx.Parser{}
|
||||
require.NoError(t, parser.Init())
|
||||
|
||||
for _, f := range folders {
|
||||
// Only handle folders
|
||||
if !f.IsDir() {
|
||||
continue
|
||||
}
|
||||
configFilename := filepath.Join("testcases", f.Name(), "telegraf.conf")
|
||||
inputFilename := filepath.Join("testcases", f.Name(), "input.txt")
|
||||
inputErrorFilename := filepath.Join("testcases", f.Name(), "input.err")
|
||||
expectedFilename := filepath.Join("testcases", f.Name(), "expected.out")
|
||||
expectedErrorFilename := filepath.Join("testcases", f.Name(), "expected.err")
|
||||
|
||||
// Compare options
|
||||
options := []cmp.Option{
|
||||
testutil.IgnoreTime(),
|
||||
testutil.SortMetrics(),
|
||||
}
|
||||
|
||||
t.Run(f.Name(), func(t *testing.T) {
|
||||
// Read the input data
|
||||
data, err := os.ReadFile(inputFilename)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Read the input error message if any
|
||||
var inputErr error
|
||||
if _, err := os.Stat(inputErrorFilename); err == nil {
|
||||
x, err := testutil.ParseLinesFromFile(inputErrorFilename)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, x, 1)
|
||||
inputErr = errors.New(x[0])
|
||||
}
|
||||
|
||||
// Read the expected output if any
|
||||
var expected []telegraf.Metric
|
||||
if _, err := os.Stat(expectedFilename); err == nil {
|
||||
var err error
|
||||
expected, err = testutil.ParseMetricsFromFile(expectedFilename, parser)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Read the expected output if any
|
||||
var errorMsg string
|
||||
if _, err := os.Stat(expectedErrorFilename); err == nil {
|
||||
x, err := testutil.ParseLinesFromFile(expectedErrorFilename)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, x, 1)
|
||||
errorMsg = x[0]
|
||||
}
|
||||
|
||||
// Configure the plugin
|
||||
cfg := config.NewConfig()
|
||||
require.NoError(t, cfg.LoadConfig(configFilename))
|
||||
require.Len(t, cfg.Inputs, 1)
|
||||
|
||||
// Fake the reading
|
||||
plugin := cfg.Inputs[0].Input.(*NTPQ)
|
||||
plugin.runQ = func() ([]byte, error) {
|
||||
return data, inputErr
|
||||
}
|
||||
require.NoError(t, plugin.Init())
|
||||
|
||||
var acc testutil.Accumulator
|
||||
if errorMsg != "" {
|
||||
require.EqualError(t, plugin.Gather(&acc), errorMsg)
|
||||
return
|
||||
}
|
||||
|
||||
// No error case
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
actual := acc.GetTelegrafMetrics()
|
||||
testutil.RequireMetricsEqual(t, expected, actual, options...)
|
||||
})
|
||||
}
|
||||
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 TestBadIntNTPQ(t *testing.T) {
|
||||
tt := tester{
|
||||
ret: []byte(badIntParseNTPQ),
|
||||
err: nil,
|
||||
}
|
||||
n := newNTPQ()
|
||||
n.runQ = tt.runqTest
|
||||
|
||||
acc := testutil.Accumulator{}
|
||||
require.Error(t, acc.GatherError(n.Gather))
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"when": int64(101),
|
||||
"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 TestBadFloatNTPQ(t *testing.T) {
|
||||
tt := tester{
|
||||
ret: []byte(badFloatParseNTPQ),
|
||||
err: nil,
|
||||
}
|
||||
n := newNTPQ()
|
||||
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) {
|
||||
tt := tester{
|
||||
ret: []byte(whenDaysNTPQ),
|
||||
err: nil,
|
||||
}
|
||||
n := newNTPQ()
|
||||
n.runQ = tt.runqTest
|
||||
|
||||
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) {
|
||||
tt := tester{
|
||||
ret: []byte(whenHoursNTPQ),
|
||||
err: nil,
|
||||
}
|
||||
n := newNTPQ()
|
||||
n.runQ = tt.runqTest
|
||||
|
||||
acc := testutil.Accumulator{}
|
||||
require.NoError(t, acc.GatherError(n.Gather))
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"when": int64(7200),
|
||||
"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 TestMinutesNTPQ(t *testing.T) {
|
||||
tt := tester{
|
||||
ret: []byte(whenMinutesNTPQ),
|
||||
err: nil,
|
||||
}
|
||||
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
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
|
|
@ -82,3 +84,55 @@ func ParseMetricsFrom(lines []string, header string, parser LineParser) ([]teleg
|
|||
}
|
||||
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