From bc0df0e429d18c178b05482aa07a8a81b0f26f87 Mon Sep 17 00:00:00 2001 From: k9fr4n Date: Mon, 9 Nov 2026 09:44:31 +0000 Subject: [PATCH] fix: check_ntp_offset fails on French Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit w32tm output parsing used quote-aware tokenizing, so French key names with apostrophes (Ordinateur d'etat, Fréquence d'horloge, ...) were dropped and positional parsing read the wrong lines. Split w32tm lines on the first colon instead and add a real French Windows Server 2025 output as test case. Fixes https://github.com/ConSol-Monitoring/snclient/issues/224 --- pkg/snclient/check_ntp_offset.go | 32 +++++++++++++-------- pkg/snclient/check_ntp_offset_linux_test.go | 26 +++++++++++++++++ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/pkg/snclient/check_ntp_offset.go b/pkg/snclient/check_ntp_offset.go index f0bcfcbe..d3b7a4ef 100644 --- a/pkg/snclient/check_ntp_offset.go +++ b/pkg/snclient/check_ntp_offset.go @@ -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) } @@ -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 @@ -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 { diff --git a/pkg/snclient/check_ntp_offset_linux_test.go b/pkg/snclient/check_ntp_offset_linux_test.go index 615f5b8c..9845bd45 100644 --- a/pkg/snclient/check_ntp_offset_linux_test.go +++ b/pkg/snclient/check_ntp_offset_linux_test.go @@ -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) }