system: read the boot time from the kernel on every scrape (#2690)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2026-10-11 20:47:11 +02:00
committed by GitHub
parent 9869fb12d1
commit be5036dfa8
5 changed files with 139 additions and 30 deletions
-7
View File
@@ -29,7 +29,6 @@ var (
procGetDynamicTimeZoneInformationSys = modkernel32.NewProc("GetDynamicTimeZoneInformation")
procKernelLocalFileTimeToFileTime = modkernel32.NewProc("LocalFileTimeToFileTime")
procGetTickCount = modkernel32.NewProc("GetTickCount64")
procOpenJobObject = modkernel32.NewProc("OpenJobObjectW")
procIsProcessInJob = modkernel32.NewProc("IsProcessInJob")
)
@@ -81,9 +80,3 @@ func LocalFileTimeToFileTime(localFileTime, utcFileTime *windows.Filetime) uint3
return uint32(ret)
}
func GetTickCount64() uint64 {
ret, _, _ := procGetTickCount.Call()
return uint64(ret)
}
+63
View File
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
package ntdll
import (
"fmt"
"time"
"unsafe"
"golang.org/x/sys/windows"
)
// SystemTimeOfDayInformation is the SYSTEM_TIMEOFDAY_INFORMATION structure.
// The times are in 100-nanosecond intervals since January 1, 1601 (UTC).
type SystemTimeOfDayInformation struct {
BootTime int64
CurrentTime int64
TimeZoneBias int64
TimeZoneID uint32
Reserved uint32
BootTimeBias uint64
SleepTimeBias uint64
}
// BootTime returns the time the system booted.
//
// The kernel moves the boot time along when the system time is set, so it
// stays correct after a clock correction.
func BootTime() (time.Time, error) {
var info SystemTimeOfDayInformation
err := windows.NtQuerySystemInformation(
windows.SystemTimeOfDayInformation,
unsafe.Pointer(&info),
uint32(unsafe.Sizeof(info)),
nil,
)
if err != nil {
return time.Time{}, fmt.Errorf("NtQuerySystemInformation(SystemTimeOfDayInformation): %w", err)
}
ft := windows.Filetime{
LowDateTime: uint32(info.BootTime),
HighDateTime: uint32(info.BootTime >> 32),
}
return time.Unix(0, ft.Nanoseconds()), nil
}
+42
View File
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
package ntdll_test
import (
"testing"
"time"
"github.com/prometheus-community/windows_exporter/internal/headers/ntdll"
"github.com/stretchr/testify/require"
)
func TestBootTime(t *testing.T) {
t.Parallel()
bootTime, err := ntdll.BootTime()
require.NoError(t, err)
require.True(t, bootTime.Before(time.Now()), "boot time %s is in the future", bootTime)
require.True(t, bootTime.After(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)), "boot time %s is too old", bootTime)
// The boot time must not depend on when it is read. Allow for a clock
// correction between the two calls.
again, err := ntdll.BootTime()
require.NoError(t, err)
require.WithinDuration(t, bootTime, again, time.Second)
}