refactor parseNTPOffsetFromNTPD
@@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"io"
- "io/ioutil"
"math"
"os"
"os/exec"
@@ -127,34 +126,16 @@ func getNTPOffsetFromNTPD() (offset float64, err error) {
}
func parseNTPOffsetFromNTPD(out io.Reader) (float64, error) {
- output, err := ioutil.ReadAll(out)
- if err != nil {
- return 0.0, err
+ scr := bufio.NewScanner(out)
+ prefix := "offset="
+ for scr.Scan() {
+ line := scr.Text()
+ if strings.HasPrefix(line, prefix) {
+ return strconv.ParseFloat(strings.TrimPrefix(line, prefix), 64)
}
- var line string
- lines := strings.Split(string(output), "\n")
- switch len(lines) {
- case 2:
- line = lines[0]
- case 3:
- /* example on ntp 4.2.2p1-18.el5.centos
- assID=0 status=06f4 leap_none, sync_ntp, 15 events, event_peer/strat_chg,
- offset=0.180
- */
- if strings.Index(lines[0], `assID=0`) == 0 {
- line = lines[1]
- break
- }
- fallthrough
- default:
- return 0.0, fmt.Errorf("couldn't get ntp offset. ntpd process may be down")
}
- o := strings.Split(string(line), "=")
- if len(o) != 2 {
return 0.0, fmt.Errorf("couldn't get ntp offset. ntpd process may be down")
}
- return strconv.ParseFloat(strings.Trim(o[1], "\n"), 64)
-}
func getNTPOffsetFromChrony() (offset float64, err error) {
err = withCmd(exec.Command(cmdChronyc, "tracking"), func(out io.Reader) error {
