From b2dbb4e833fb5e32b512a5b8dba0767e52bbbce9 Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Wed, 20 Apr 2022 23:45:02 +0800 Subject: [PATCH] add ntp plugin --- README.md | 10 +++--- agent/agent.go | 1 + conf/input.ntp/ntp.toml | 5 +++ inputs/ntp/ntp.go | 70 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 conf/input.ntp/ntp.toml create mode 100644 inputs/ntp/ntp.go diff --git a/README.md b/README.md index 6428e74..72cb745 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,14 @@ tar zcvf categraf.tar.gz categraf conf ## todo -- []ntp +- [x]ntp - []procstat -- []promscrape -- []mysql -- []redis - []nginx vts - []tomcat - []... +- []promscrape +- []mysql +- []redis +- []statsd +- []... - []io.util \ No newline at end of file diff --git a/agent/agent.go b/agent/agent.go index 0ee6978..373245e 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -25,6 +25,7 @@ import ( _ "flashcat.cloud/categraf/inputs/net" _ "flashcat.cloud/categraf/inputs/netresponse" _ "flashcat.cloud/categraf/inputs/netstat" + _ "flashcat.cloud/categraf/inputs/ntp" _ "flashcat.cloud/categraf/inputs/oracle" _ "flashcat.cloud/categraf/inputs/ping" _ "flashcat.cloud/categraf/inputs/processes" diff --git a/conf/input.ntp/ntp.toml b/conf/input.ntp/ntp.toml new file mode 100644 index 0000000..6fc3665 --- /dev/null +++ b/conf/input.ntp/ntp.toml @@ -0,0 +1,5 @@ +# # collect interval +# interval = 15 + +# # ntp servers +# ntp_servers = ["ntp1.aliyun.com"] \ No newline at end of file diff --git a/inputs/ntp/ntp.go b/inputs/ntp/ntp.go new file mode 100644 index 0000000..158e9e5 --- /dev/null +++ b/inputs/ntp/ntp.go @@ -0,0 +1,70 @@ +package ntp + +import ( + "errors" + "log" + "time" + + "flashcat.cloud/categraf/config" + "flashcat.cloud/categraf/inputs" + "flashcat.cloud/categraf/types" + "github.com/toolkits/pkg/nux" +) + +const inputName = "ntp" + +type NTPStat struct { + Interval config.Duration `toml:"interval"` + NTPServers []string `toml:"ntp_servers"` + server string +} + +func init() { + inputs.Add(inputName, func() inputs.Input { + return &NTPStat{} + }) +} + +func (n *NTPStat) GetInputName() string { + return inputName +} + +func (n *NTPStat) GetInterval() config.Duration { + return n.Interval +} + +func (n *NTPStat) Drop() {} + +func (n *NTPStat) Init() error { + if len(n.NTPServers) == 0 { + return errors.New("ntp servers empty") + } + return nil +} + +func (n *NTPStat) Gather() (samples []*types.Sample) { + for _, server := range n.NTPServers { + if n.server == "" { + n.server = server + } + + orgTime := time.Now() + serverReciveTime, serverTransmitTime, err := nux.NtpTwoTime(n.server) + if err != nil { + log.Println("E! failed to connect ntp server:", n.server, "error:", err) + n.server = "" + continue + } + + dstTime := time.Now() + + // https://en.wikipedia.org/wiki/Network_Time_Protocol + duration := ((serverReciveTime.UnixNano() - orgTime.UnixNano()) + (serverTransmitTime.UnixNano() - dstTime.UnixNano())) / 2 + + delta := duration / 1e6 // convert to ms + samples = append(samples, inputs.NewSample("offset_ms", delta)) + break + } + + return +}