@@ -6,14 +6,14 @@ package platform
66import (
77 "bytes"
88 "fmt"
9- "golang.org/x/sys/windows"
109 "os"
1110 "os/exec"
1211 "strings"
1312 "syscall"
1413 "time"
1514
1615 "github.com/Azure/azure-container-networking/log"
16+ "golang.org/x/sys/windows"
1717)
1818
1919const (
@@ -72,40 +72,18 @@ func GetProcessSupport() error {
7272 return err
7373}
7474
75+ var tickCount = syscall .NewLazyDLL ("kernel32.dll" ).NewProc ("GetTickCount64" )
76+
7577// GetLastRebootTime returns the last time the system rebooted.
7678func GetLastRebootTime () (time.Time , error ) {
77- out , err := exec .Command ("cmd" , "/c" , "wmic os get lastbootuptime" ).Output ()
78- if err != nil {
79- log .Printf ("Failed to query wmic os get lastbootuptime, err: %v" , err )
80- return time.Time {}.UTC (), err
81- }
82-
83- lastBootupTime := strings .Split (strings .TrimSpace (string (out )), "\n " )
84- if strings .TrimSpace (lastBootupTime [0 ]) != "LastBootUpTime" || len (lastBootupTime ) != 2 {
85- log .Printf ("Failed to retrieve boot time" )
86- return time.Time {}.UTC (), fmt .Errorf ("Failed to retrieve boot time with 'wmic os get lastbootuptime'" )
87- }
88- systemBootupTime := strings .Split (lastBootupTime [1 ], "." )[0 ]
89-
90- // The systembootuptime is in the format YYYYMMDDHHMMSS
91- bootYear := systemBootupTime [0 :4 ]
92- bootMonth := systemBootupTime [4 :6 ]
93- bootDay := systemBootupTime [6 :8 ]
94- bootHour := systemBootupTime [8 :10 ]
95- bootMin := systemBootupTime [10 :12 ]
96- bootSec := systemBootupTime [12 :14 ]
97- systemBootTime := bootYear + "-" + bootMonth + "-" + bootDay + " " + bootHour + ":" + bootMin + ":" + bootSec
98-
99- log .Printf ("Formatted Boot time: %s" , systemBootTime )
100-
101- // Parse the boot time.
102- layout := "2006-01-02 15:04:05"
103- rebootTime , err := time .ParseInLocation (layout , systemBootTime , time .Local )
104- if err != nil {
105- log .Printf ("Failed to parse boot time, err:%v" , err )
79+ currentTime := time .Now ()
80+ output , _ , err := tickCount .Call ()
81+ if errno , ok := err .(syscall.Errno ); ! ok || errno != 0 {
82+ log .Printf ("Failed to call GetTickCount64, err: %v" , err )
10683 return time.Time {}.UTC (), err
10784 }
108-
85+ rebootTime := currentTime .Add (- time .Duration (output ) * time .Millisecond ).Truncate (time .Second )
86+ log .Printf ("Formatted Boot time: %s" , rebootTime .Format (time .RFC3339 ))
10987 return rebootTime .UTC (), nil
11088}
11189
0 commit comments