From 94ce4fc1e4cf8a070f0cd2b6e2033135132e8a58 Mon Sep 17 00:00:00 2001 From: zwliu Date: Mon, 9 Aug 2021 11:10:37 +0800 Subject: [PATCH 1/2] Improve boot time query on Windows --- platform/os_windows.go | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/platform/os_windows.go b/platform/os_windows.go index 051a32a6c5..8d83d76f42 100644 --- a/platform/os_windows.go +++ b/platform/os_windows.go @@ -6,7 +6,6 @@ package platform import ( "bytes" "fmt" - "golang.org/x/sys/windows" "os" "os/exec" "strings" @@ -14,6 +13,7 @@ import ( "time" "github.com/Azure/azure-container-networking/log" + "golang.org/x/sys/windows" ) const ( @@ -72,40 +72,18 @@ func GetProcessSupport() error { return err } +var tickCount = syscall.NewLazyDLL("kernel32.dll").NewProc("GetTickCount64") + // GetLastRebootTime returns the last time the system rebooted. func GetLastRebootTime() (time.Time, error) { - out, err := exec.Command("cmd", "/c", "wmic os get lastbootuptime").Output() - if err != nil { - log.Printf("Failed to query wmic os get lastbootuptime, err: %v", err) - return time.Time{}.UTC(), err - } - - lastBootupTime := strings.Split(strings.TrimSpace(string(out)), "\n") - if strings.TrimSpace(lastBootupTime[0]) != "LastBootUpTime" || len(lastBootupTime) != 2 { - log.Printf("Failed to retrieve boot time") - return time.Time{}.UTC(), fmt.Errorf("Failed to retrieve boot time with 'wmic os get lastbootuptime'") - } - systemBootupTime := strings.Split(lastBootupTime[1], ".")[0] - - // The systembootuptime is in the format YYYYMMDDHHMMSS - bootYear := systemBootupTime[0:4] - bootMonth := systemBootupTime[4:6] - bootDay := systemBootupTime[6:8] - bootHour := systemBootupTime[8:10] - bootMin := systemBootupTime[10:12] - bootSec := systemBootupTime[12:14] - systemBootTime := bootYear + "-" + bootMonth + "-" + bootDay + " " + bootHour + ":" + bootMin + ":" + bootSec - - log.Printf("Formatted Boot time: %s", systemBootTime) - - // Parse the boot time. - layout := "2006-01-02 15:04:05" - rebootTime, err := time.ParseInLocation(layout, systemBootTime, time.Local) - if err != nil { - log.Printf("Failed to parse boot time, err:%v", err) + currentTime := time.Now() + output, _, err := tickCount.Call() + if errno, ok := err.(syscall.Errno); !ok || errno != 0 { + log.Printf("Failed to call GetTickCount64, err: %v", err) return time.Time{}.UTC(), err } - + rebootTime := currentTime.Add(-time.Duration(output) * time.Millisecond).Truncate(time.Second) + log.Printf("Formatted Boot time: %s", rebootTime.Format("2006-01-02 15:04:05")) return rebootTime.UTC(), nil } From d43ca0be9d048a4e2a1971dbb04151f366ddeccb Mon Sep 17 00:00:00 2001 From: Zhiwei Liu Date: Wed, 11 Aug 2021 09:32:07 +0800 Subject: [PATCH 2/2] Ad tz info of time in log Co-authored-by: tamilmani1989 --- platform/os_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/os_windows.go b/platform/os_windows.go index 8d83d76f42..a5c9ae994d 100644 --- a/platform/os_windows.go +++ b/platform/os_windows.go @@ -83,7 +83,7 @@ func GetLastRebootTime() (time.Time, error) { return time.Time{}.UTC(), err } rebootTime := currentTime.Add(-time.Duration(output) * time.Millisecond).Truncate(time.Second) - log.Printf("Formatted Boot time: %s", rebootTime.Format("2006-01-02 15:04:05")) + log.Printf("Formatted Boot time: %s", rebootTime.Format(time.RFC3339)) return rebootTime.UTC(), nil }