Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions pkg/snclient/check_ntp_offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,24 +508,23 @@ func (l *CheckNTPOffset) addW32TM(ctx context.Context, check *CheckData, force b

func (l *CheckNTPOffset) parseW32English(text string) (valid bool, source, offset, stratum, errorStr string) {
for line := range strings.SplitSeq(text, "\n") {
cols := utils.TokenizeBy(line, ":", false, false)
if len(cols) < 2 {
key, val, ok := splitW32Line(line)
if !ok {
continue
}
cols[1] = strings.TrimSpace(cols[1])
switch cols[0] {
switch key {
case "Source":
servers := utils.TokenizeBy(cols[1], ",", false, false)
servers := utils.TokenizeBy(val, ",", false, false)
source = servers[0]
case "Phase Offset":
value, _ := time.ParseDuration(cols[1])
value, _ := time.ParseDuration(val)
offset = fmt.Sprintf("%f", float64(value.Nanoseconds())/1e6)
valid = true
case "Stratum":
stratas := strings.Fields(cols[1])
stratas := strings.Fields(val)
stratum = stratas[0]
case "State Machine":
fields := strings.Fields(cols[1])
fields := strings.Fields(val)
if fields[0] != "2" {
errorStr = fmt.Sprintf("w32tm.exe: %s", line)
}
Expand All @@ -535,6 +534,16 @@ func (l *CheckNTPOffset) parseW32English(text string) (valid bool, source, offse
return valid, source, offset, stratum, errorStr
}

// splitW32Line splits a "key: value" line on the first colon, ignoring quotes.
func splitW32Line(line string) (key, val string, ok bool) {
key, val, ok = strings.Cut(line, ":")
if !ok {
return "", "", false
}

return strings.TrimSpace(key), strings.TrimSpace(val), true
}

func (l *CheckNTPOffset) parseW32AnyLang(text string) (valid bool, source, offset, stratum, errorStr string) {
type attr struct {
key string
Expand All @@ -543,12 +552,11 @@ func (l *CheckNTPOffset) parseW32AnyLang(text string) (valid bool, source, offse
}
attributes := []attr{}
for line := range strings.SplitSeq(text, "\n") {
cols := utils.TokenizeBy(line, ":", false, false)
if len(cols) < 2 {
key, val, ok := splitW32Line(line)
if !ok {
continue
}
cols[1] = strings.TrimSpace(cols[1])
attributes = append(attributes, attr{cols[0], cols[1], line})
attributes = append(attributes, attr{key, val, line})
}

if len(attributes) < 12 {
Expand Down
26 changes: 26 additions & 0 deletions pkg/snclient/check_ntp_offset_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,32 @@ Zeit seit der letzten erfolgr. Synchronisierungszeit: 831.4054861s`,
assert.Equalf(t, "OK - offset -7.842999ms from ntp.company.lan |'offset'=-7.8433ms;-50:50;-100:100 'stratum'=4;;;0",
string(res.BuildPluginOutput()), "output matches")

// french not synchronized
MockSystemUtilities(t, map[string]string{
"w32tm.exe": `Indicateur de dérive : 3(Non synchronisé)
Couche : 0 (Non spécifié)
Précision : -23 (119.209ns par battement)
Délai de racine : 0.0000000s
Dispersion de racine : 0.0000000s
ID de référence : 0x00000000 (Non spécifié)
Heure de la dernière synchronisation réussie : Non spécifié
Source : Local CMOS Clock
Intervalle d'interrogation : 6 (64s)

Décalage de phase : 0.0000000s
Fréquence d'horloge : 0.0156250s
Ordinateur d'état : 0 (Non défini)
Indicateur de source de temps : 0 (Aucun)
Rôle de serveur : 0 (Aucun)
Erreur lors de la dernière synchronisation : 1 (L'ordinateur ne s'est pas resynchronisé car aucune donnée de temps n'était
disponible.)
Durée écoulée depuis l'heure de la dernière synchronisation réussie : 121.1471153s`,
})
res = snc.RunCheck("check_ntp_offset", []string{"source=w32tm"})
assert.Equalf(t, CheckExitCritical, res.State, "state Critical")
assert.Equalf(t, "CRITICAL - w32tm.exe: Ordinateur d'état : 0 (Non défini)",
string(res.BuildPluginOutput()), "output matches")

StopTestAgent(t, snc)
}

Expand Down