-
Notifications
You must be signed in to change notification settings - Fork 0
/
clockoffset.go
81 lines (73 loc) · 1.42 KB
/
clockoffset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/beevik/ntp"
)
func main() {
ntpserver := flag.String(
"ntpserver",
"",
"ntp server hostname",
)
format := flag.String(
"format",
"ms",
"output formats:\n"+
" s seconds\n"+
" ms miliseconds\n"+
" us microseconds\n"+
" h human readable\n",
)
quiet := flag.Bool(
"quiet",
false,
"suppress output to standard output",
)
limit := flag.Int64(
"limit",
0,
"exit(2) if offset diff is greather than <n> ms",
)
sleep := flag.Int(
"sleep",
0,
"sleep for <n> ms if the limit was exceeded",
)
flag.Parse()
if *format != "s" && *format != "ms" && *format != "us" && *format != "h" {
log.Fatal("format must be one of: {s,ms,us,h}")
}
if *ntpserver == "" {
log.Fatal("ntpserver is required")
}
response, err := ntp.Query(*ntpserver)
if err != nil {
log.Fatal(err)
}
if *quiet == false {
switch {
case *format == "s":
fmt.Println(response.ClockOffset.Seconds())
case *format == "ms":
fmt.Println(response.ClockOffset.Milliseconds())
case *format == "us":
fmt.Println(response.ClockOffset.Microseconds())
case *format == "h":
fmt.Println("Clock offset is:", response.ClockOffset.String())
}
}
ms := response.ClockOffset.Milliseconds()
if ms < 0 {
ms = -ms
}
if *limit > 0 && ms > *limit {
if *sleep > 0 {
time.Sleep(time.Duration(*sleep) * time.Millisecond)
}
os.Exit(2)
}
}