-
Notifications
You must be signed in to change notification settings - Fork 71
/
oscillatord.go
107 lines (89 loc) · 3.48 KB
/
oscillatord.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright (c) Facebook, Inc. and its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"net"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/facebook/time/oscillatord"
)
var (
oscillatordPortFlag int
oscillatordAddressFlag string
oscillatorJSONFlag bool
oscillatorJSONPrefixFlag string
)
func init() {
RootCmd.AddCommand(oscillatordCmd)
oscillatordCmd.Flags().StringVarP(&oscillatordAddressFlag, "address", "a", "127.0.0.1", "address to connect to")
oscillatordCmd.Flags().IntVarP(&oscillatordPortFlag, "port", "p", oscillatord.MonitoringPort, "port to connect to")
oscillatordCmd.Flags().BoolVarP(&oscillatorJSONFlag, "json", "j", false, "JSON output")
oscillatordCmd.Flags().StringVarP(&oscillatorJSONPrefixFlag, "prefix", "r", "ptp.timecard", "JSON prefix")
}
func printOscillatord(status *oscillatord.Status) {
fmt.Println("Oscillator:")
fmt.Printf("\tmodel: %s\n", status.Oscillator.Model)
fmt.Printf("\tfine_ctrl: %d\n", status.Oscillator.FineCtrl)
fmt.Printf("\tcoarse_ctrl: %d\n", status.Oscillator.CoarseCtrl)
fmt.Printf("\tlock: %v\n", status.Oscillator.Lock)
fmt.Printf("\ttemperature: %.2fC\n", status.Oscillator.Temperature)
fmt.Println("GNSS:")
fmt.Printf("\tfix: %s (%d)\n", status.GNSS.Fix, status.GNSS.Fix)
fmt.Printf("\tfixOk: %v\n", status.GNSS.FixOK)
fmt.Printf("\tantenna_power: %s (%d)\n", status.GNSS.AntennaPower, status.GNSS.AntennaPower)
fmt.Printf("\tantenna_status: %s (%d)\n", status.GNSS.AntennaStatus, status.GNSS.AntennaStatus)
fmt.Printf("\tleap_second_change: %s (%d)\n", status.GNSS.LSChange, status.GNSS.LSChange)
fmt.Printf("\tleap_seconds: %d\n", status.GNSS.LeapSeconds)
fmt.Printf("\tsatellites_count: %d\n", status.GNSS.SatellitesCount)
fmt.Printf("\tsurvey_in_position_error: %d\n", status.GNSS.SurveyInPositionError)
fmt.Printf("\ttime_accuracy: %d\n", status.GNSS.TimeAccuracy)
fmt.Println("Clock:")
fmt.Printf("\tclass: %s (%d)\n", status.Clock.Class, status.Clock.Class)
fmt.Printf("\toffset: %d\n", status.Clock.Offset)
}
func oscillatordRun(address string, jsonOut bool) error {
timeout := 1 * time.Second
conn, err := net.Dial("tcp", address)
if err != nil {
return fmt.Errorf("connecting to oscillatord: %w", err)
}
defer conn.Close()
deadline := time.Now().Add(timeout)
if err = conn.SetDeadline(deadline); err != nil {
return fmt.Errorf("setting connection deadline: %w", err)
}
status, err := oscillatord.ReadStatus(conn)
if err != nil {
return err
}
if jsonOut {
toPrint, err := status.MonitoringJSON(oscillatorJSONPrefixFlag)
fmt.Println(string(toPrint))
return err
}
printOscillatord(status)
return nil
}
var oscillatordCmd = &cobra.Command{
Use: "oscillatord",
Short: "Print Time Card stats reported by oscillatord",
Run: func(_ *cobra.Command, _ []string) {
ConfigureVerbosity()
address := net.JoinHostPort(oscillatordAddressFlag, fmt.Sprint(oscillatordPortFlag))
if err := oscillatordRun(address, oscillatorJSONFlag); err != nil {
log.Fatal(err)
}
},
}