aboutsummaryrefslogtreecommitdiff
path: root/vendor
diff options
context:
space:
mode:
authorChris Batey <christopher.batey@sky.uk>2016-06-01 08:56:20 +0100
committerChris Batey <christopher.batey@sky.uk>2016-06-01 08:56:20 +0100
commit387c64424b568293d04ac1f8ae90539cf4141301 (patch)
treed054308d1729af269052e979c5aef68e877437a2 /vendor
parentb93c7c394bf27667c0adb81ed50e56dfc9e5f042 (diff)
downloadprometheus_node_collector-387c64424b568293d04ac1f8ae90539cf4141301.tar.bz2
prometheus_node_collector-387c64424b568293d04ac1f8ae90539cf4141301.tar.xz
prometheus_node_collector-387c64424b568293d04ac1f8ae90539cf4141301.zip
Use the offset calculation that includes round trip time in the ntp collector
Previously the raw time difference was used which includes the network trip time between the node and the ntp server. This makes setting alerts off the value troublesome as it depends on the latency as well as the clock offset.
Diffstat (limited to 'vendor')
-rw-r--r--vendor/github.com/beevik/ntp/CONTRIBUTORS4
-rw-r--r--vendor/github.com/beevik/ntp/README.md3
-rw-r--r--vendor/github.com/beevik/ntp/ntp.go159
-rw-r--r--vendor/vendor.json5
4 files changed, 136 insertions, 35 deletions
diff --git a/vendor/github.com/beevik/ntp/CONTRIBUTORS b/vendor/github.com/beevik/ntp/CONTRIBUTORS
new file mode 100644
index 0000000..7b3b2e2
--- /dev/null
+++ b/vendor/github.com/beevik/ntp/CONTRIBUTORS
@@ -0,0 +1,4 @@
1Brett Vickers (beevik)
2Mikhail Salosin (AlphaB)
3Anton Tolchanov (knyar)
4Christopher Batey (chbatey)
diff --git a/vendor/github.com/beevik/ntp/README.md b/vendor/github.com/beevik/ntp/README.md
index 1745752..ef86b0d 100644
--- a/vendor/github.com/beevik/ntp/README.md
+++ b/vendor/github.com/beevik/ntp/README.md
@@ -1,3 +1,6 @@
1[![Build Status](https://travis-ci.org/beevik/ntp.svg?branch=master)](https://travis-ci.org/beevik/ntp)
2[![GoDoc](https://godoc.org/github.com/beevik/ntp?status.svg)](https://godoc.org/github.com/beevik/ntp)
3
1ntp 4ntp
2=== 5===
3 6
diff --git a/vendor/github.com/beevik/ntp/ntp.go b/vendor/github.com/beevik/ntp/ntp.go
index e6c2efc..d0575d7 100644
--- a/vendor/github.com/beevik/ntp/ntp.go
+++ b/vendor/github.com/beevik/ntp/ntp.go
@@ -2,10 +2,11 @@
2// Use of this source code is governed by a BSD-style 2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file. 3// license that can be found in the LICENSE file.
4 4
5// Package ntp provides a simple mechanism for querying the current time 5// Package ntp provides a simple mechanism for querying the current time from
6// from a remote NTP server. This package only supports NTP client mode 6// a remote NTP server. This package only supports NTP client mode behavior
7// behavior and version 4 of the NTP protocol. See RFC 5905. 7// and version 4 of the NTP protocol. See RFC 5905. Approach inspired by go-
8// Approach inspired by go-nuts post by Michael Hofmann: 8// nuts post by Michael Hofmann:
9//
9// https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/FlcdMU5fkLQ 10// https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/FlcdMU5fkLQ
10package ntp 11package ntp
11 12
@@ -28,16 +29,43 @@ const (
28 reservedPrivate 29 reservedPrivate
29) 30)
30 31
32const (
33 maxStratum = 16
34 nanoPerSec = 1000000000
35)
36
37var (
38 timeout = 5 * time.Second
39 ntpEpoch = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
40)
41
31type ntpTime struct { 42type ntpTime struct {
32 Seconds uint32 43 Seconds uint32
33 Fraction uint32 44 Fraction uint32
34} 45}
35 46
36func (t ntpTime) UTC() time.Time { 47func (t ntpTime) Time() time.Time {
37 nsec := uint64(t.Seconds)*1e9 + (uint64(t.Fraction) * 1e9 >> 32) 48 return ntpEpoch.Add(t.sinceEpoch())
38 return time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(nsec)) 49}
50
51// sinceEpoch converts the ntpTime record t into a duration since the NTP
52// epoch time (Jan 1, 1900).
53func (t ntpTime) sinceEpoch() time.Duration {
54 sec := time.Duration(t.Seconds) * time.Second
55 frac := time.Duration(uint64(t.Fraction) * nanoPerSec >> 32)
56 return sec + frac
57}
58
59// toNtpTime converts the time value t into an ntpTime representation.
60func toNtpTime(t time.Time) ntpTime {
61 nsec := uint64(t.Sub(ntpEpoch))
62 return ntpTime{
63 Seconds: uint32(nsec / nanoPerSec),
64 Fraction: uint32((nsec % nanoPerSec) << 32 / nanoPerSec),
65 }
39} 66}
40 67
68// msg is an internal representation of an NTP packet.
41type msg struct { 69type msg struct {
42 LiVnMode byte // Leap Indicator (2) + Version (3) + Mode (3) 70 LiVnMode byte // Leap Indicator (2) + Version (3) + Mode (3)
43 Stratum byte 71 Stratum byte
@@ -45,69 +73,134 @@ type msg struct {
45 Precision byte 73 Precision byte
46 RootDelay uint32 74 RootDelay uint32
47 RootDispersion uint32 75 RootDispersion uint32
48 ReferenceId uint32 76 ReferenceID uint32
49 ReferenceTime ntpTime 77 ReferenceTime ntpTime
50 OriginTime ntpTime 78 OriginTime ntpTime
51 ReceiveTime ntpTime 79 ReceiveTime ntpTime
52 TransmitTime ntpTime 80 TransmitTime ntpTime
53} 81}
54 82
55// SetVersion sets the NTP protocol version on the message. 83// setVersion sets the NTP protocol version on the message.
56func (m *msg) SetVersion(v byte) { 84func (m *msg) setVersion(v int) {
57 m.LiVnMode = (m.LiVnMode & 0xc7) | v<<3 85 m.LiVnMode = (m.LiVnMode & 0xc7) | uint8(v)<<3
58} 86}
59 87
60// SetMode sets the NTP protocol mode on the message. 88// setMode sets the NTP protocol mode on the message.
61func (m *msg) SetMode(md mode) { 89func (m *msg) setMode(md mode) {
62 m.LiVnMode = (m.LiVnMode & 0xf8) | byte(md) 90 m.LiVnMode = (m.LiVnMode & 0xf8) | byte(md)
63} 91}
64 92
65// Time returns the "receive time" from the remote NTP server 93// A Response contains time data, some of which is returned by the NTP server
66// specifed as host. NTP client mode is used. 94// and some of which is calculated by the client.
67func getTime(host string, version byte) (time.Time, error) { 95type Response struct {
96 Time time.Time // receive time reported by the server
97 RTT time.Duration // round-trip time between client and server
98 ClockOffset time.Duration // local clock offset relative to server
99 Stratum uint8 // stratum level of NTP server's clock
100}
101
102// Query returns information from the remote NTP server specifed as host. NTP
103// client mode is used.
104func Query(host string, version int) (*Response, error) {
105 m, err := getTime(host, version)
106 now := toNtpTime(time.Now())
107 if err != nil {
108 return nil, err
109 }
110
111 r := &Response{
112 Time: m.ReceiveTime.Time(),
113 RTT: rtt(m.OriginTime, m.ReceiveTime, m.TransmitTime, now),
114 ClockOffset: offset(m.OriginTime, m.ReceiveTime, m.TransmitTime, now),
115 Stratum: m.Stratum,
116 }
117
118 // https://tools.ietf.org/html/rfc5905#section-7.3
119 if r.Stratum == 0 {
120 r.Stratum = maxStratum
121 }
122
123 return r, nil
124}
125
126// Time returns the "receive time" from the remote NTP server specifed as
127// host. NTP client mode is used.
128func getTime(host string, version int) (*msg, error) {
68 if version < 2 || version > 4 { 129 if version < 2 || version > 4 {
69 panic("ntp: invalid version number") 130 panic("ntp: invalid version number")
70 } 131 }
71 132
72 raddr, err := net.ResolveUDPAddr("udp", host+":123") 133 raddr, err := net.ResolveUDPAddr("udp", host+":123")
73 if err != nil { 134 if err != nil {
74 return time.Now(), err 135 return nil, err
75 } 136 }
76 137
77 con, err := net.DialUDP("udp", nil, raddr) 138 con, err := net.DialUDP("udp", nil, raddr)
78 if err != nil { 139 if err != nil {
79 return time.Now(), err 140 return nil, err
80 } 141 }
81 defer con.Close() 142 defer con.Close()
82 con.SetDeadline(time.Now().Add(5 * time.Second)) 143 con.SetDeadline(time.Now().Add(timeout))
83 144
84 m := new(msg) 145 m := new(msg)
85 m.SetMode(client) 146 m.setMode(client)
86 m.SetVersion(version) 147 m.setVersion(version)
148 m.TransmitTime = toNtpTime(time.Now())
87 149
88 err = binary.Write(con, binary.BigEndian, m) 150 err = binary.Write(con, binary.BigEndian, m)
89 if err != nil { 151 if err != nil {
90 return time.Now(), err 152 return nil, err
91 } 153 }
92 154
93 err = binary.Read(con, binary.BigEndian, m) 155 err = binary.Read(con, binary.BigEndian, m)
94 if err != nil { 156 if err != nil {
95 return time.Now(), err 157 return nil, err
96 } 158 }
97 159
98 t := m.ReceiveTime.UTC().Local() 160 return m, nil
99 return t, nil
100} 161}
101 162
102// TimeV returns the "receive time" from the remote NTP server 163// TimeV returns the "receive time" from the remote NTP server specifed as
103// specifed as host. Use the NTP client mode with the requested 164// host. Use the NTP client mode with the requested version number (2, 3, or
104// version number (2, 3, or 4). 165// 4).
105func TimeV(host string, version byte) (time.Time, error) { 166func TimeV(host string, version int) (time.Time, error) {
106 return getTime(host, version) 167 m, err := getTime(host, version)
168 if err != nil {
169 return time.Now(), err
170 }
171 return m.ReceiveTime.Time().Local(), nil
107} 172}
108 173
109// Time returns the "receive time" from the remote NTP server 174// Time returns the "receive time" from the remote NTP server specifed as
110// specifed as host. NTP client mode version 4 is used. 175// host. NTP client mode version 4 is used.
111func Time(host string) (time.Time, error) { 176func Time(host string) (time.Time, error) {
112 return getTime(host, 4) 177 return TimeV(host, 4)
178}
179
180func rtt(t1, t2, t3, t4 ntpTime) time.Duration {
181 // round trip delay time (https://tools.ietf.org/html/rfc5905#section-8)
182 // T1 = client send time
183 // T2 = server receive time
184 // T3 = server reply time
185 // T4 = client receive time
186 //
187 // RTT d:
188 // d = (T4-T1) - (T3-T2)
189 a := t4.Time().Sub(t1.Time())
190 b := t3.Time().Sub(t2.Time())
191 return a - b
192}
193
194func offset(t1, t2, t3, t4 ntpTime) time.Duration {
195 // local offset equation (https://tools.ietf.org/html/rfc5905#section-8)
196 // T1 = client send time
197 // T2 = server receive time
198 // T3 = server reply time
199 // T4 = client receive time
200 //
201 // Local clock offset t:
202 // t = ((T2-T1) + (T3-T4)) / 2
203 a := t2.Time().Sub(t1.Time())
204 b := t3.Time().Sub(t4.Time())
205 return (a + b) / time.Duration(2)
113} 206}
diff --git a/vendor/vendor.json b/vendor/vendor.json
index edd27bb..9d394a9 100644
--- a/vendor/vendor.json
+++ b/vendor/vendor.json
@@ -8,9 +8,10 @@
8 "revisionTime": "2016-01-18T19:00:32-05:00" 8 "revisionTime": "2016-01-18T19:00:32-05:00"
9 }, 9 },
10 { 10 {
11 "checksumSHA1": "JhaYHdVIj52Fpdcb7DuDjr/gk0Q=",
11 "path": "github.com/beevik/ntp", 12 "path": "github.com/beevik/ntp",
12 "revision": "283ed9d548825a1dae0994311560e8dbf8efac68", 13 "revision": "f0545e6f2c3cb0d0a2ed115b88c539d8e5247ef3",
13 "revisionTime": "2015-11-09T15:30:19-08:00" 14 "revisionTime": "2016-05-31T23:09:58Z"
14 }, 15 },
15 { 16 {
16 "path": "github.com/beorn7/perks/quantile", 17 "path": "github.com/beorn7/perks/quantile",