-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ntp_local_defined_server_nowindows.go
48 lines (38 loc) · 1.18 KB
/
ntp_local_defined_server_nowindows.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build !windows
package ntp
import (
"fmt"
"os"
"strings"
)
func getLocalDefinedNTPServers() ([]string, error) {
return getNTPServersFromFiles([]string{"/etc/ntp.conf", "/etc/xntp.conf", "/etc/chrony.conf", "/etc/chrony/chrony.conf", "/etc/ntpd.conf", "/etc/openntpd/ntpd.conf"})
}
func getNTPServersFromFiles(files []string) ([]string, error) {
serversMap := make(map[string]bool)
for _, conf := range files {
content, err := os.ReadFile(conf)
if err == nil {
lines := strings.Split(string(content), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
fields := strings.Fields(line)
if len(fields) >= 2 && fields[0] == "server" {
serversMap[fields[1]] = true
}
}
}
}
if len(serversMap) == 0 {
return nil, fmt.Errorf("Cannot find NTP server in %s", strings.Join(files, ", "))
}
var servers []string
for key := range serversMap {
servers = append(servers, key)
}
return servers, nil
}