Skip to content

Commit ba605cf

Browse files
authored
system: Metric windows_system_boot_time_timestamp returns a UNIX timestamp again. (#1967)
Signed-off-by: Jan-Otto Kröpke <[email protected]> Signed-off-by: Jan-Otto Kröpke <[email protected]>
1 parent bf56e99 commit ba605cf

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

docs/collector.system.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ None
1616

1717
| Name | Description | Type | Labels |
1818
|----------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|--------|
19-
| `windows_system_boot_time_timestamp_seconds` | Unix timestamp of last system boot | gauge | None |
19+
| `windows_system_boot_time_timestamp` | Unix timestamp of last system boot | gauge | None |
2020
| `windows_system_context_switches_total` | Total number of [context switches](https://en.wikipedia.org/wiki/Context_switch) | counter | None |
2121
| `windows_system_exception_dispatches_total` | Total exceptions dispatched by the system | counter | None |
2222
| `windows_system_processes` | Number of process contexts currently loaded or running on the operating system | gauge | None |
@@ -41,7 +41,7 @@ windows_system_processes{instance="localhost"}
4141
## Useful queries
4242
Find hosts that have rebooted in the last 24 hours
4343
```
44-
time() - windows_system_boot_time_timestamp_seconds < 86400
44+
time() - windows_system_boot_time_timestamp < 86400
4545
```
4646

4747
## Alerting examples

internal/collector/system/system.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ package system
2020
import (
2121
"fmt"
2222
"log/slog"
23+
"time"
2324

2425
"github.com/alecthomas/kingpin/v2"
26+
"github.com/prometheus-community/windows_exporter/internal/headers/kernel32"
2527
"github.com/prometheus-community/windows_exporter/internal/mi"
2628
"github.com/prometheus-community/windows_exporter/internal/pdh"
2729
"github.com/prometheus-community/windows_exporter/internal/types"
@@ -39,6 +41,8 @@ var ConfigDefaults = Config{}
3941
type Collector struct {
4042
config Config
4143

44+
bootTimeTimestamp float64
45+
4246
perfDataCollector *pdh.Collector
4347
perfDataObject []perfDataCounterValues
4448

@@ -48,8 +52,10 @@ type Collector struct {
4852
processes *prometheus.Desc
4953
processesLimit *prometheus.Desc
5054
systemCallsTotal *prometheus.Desc
51-
bootTime *prometheus.Desc
52-
threads *prometheus.Desc
55+
// Deprecated: Use windows_system_boot_time_timestamp instead
56+
bootTimeSeconds *prometheus.Desc
57+
bootTime *prometheus.Desc
58+
threads *prometheus.Desc
5359
}
5460

5561
func New(config *Config) *Collector {
@@ -80,11 +86,17 @@ func (c *Collector) Close() error {
8086

8187
func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
8288
c.bootTime = prometheus.NewDesc(
83-
prometheus.BuildFQName(types.Namespace, Name, "boot_time_timestamp_seconds"),
89+
prometheus.BuildFQName(types.Namespace, Name, "boot_time_timestamp"),
8490
"Unix timestamp of system boot time",
8591
nil,
8692
nil,
8793
)
94+
c.bootTimeSeconds = prometheus.NewDesc(
95+
prometheus.BuildFQName(types.Namespace, Name, "boot_time_timestamp_seconds"),
96+
"Deprecated: Use windows_system_boot_time_timestamp instead",
97+
nil,
98+
nil,
99+
)
88100
c.contextSwitchesTotal = prometheus.NewDesc(
89101
prometheus.BuildFQName(types.Namespace, Name, "context_switches_total"),
90102
"Total number of context switches (WMI source: PerfOS_System.ContextSwitchesPersec)",
@@ -129,6 +141,8 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
129141
nil,
130142
)
131143

144+
c.bootTimeTimestamp = float64(time.Now().Unix() - int64(kernel32.GetTickCount64()/1000))
145+
132146
var err error
133147

134148
c.perfDataCollector, err = pdh.NewCollector[perfDataCounterValues](pdh.CounterTypeRaw, "System", nil)
@@ -173,14 +187,21 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
173187
c.perfDataObject[0].SystemCallsPerSec,
174188
)
175189
ch <- prometheus.MustNewConstMetric(
176-
c.bootTime,
190+
c.threads,
177191
prometheus.GaugeValue,
178-
c.perfDataObject[0].SystemUpTime,
192+
c.perfDataObject[0].Threads,
179193
)
194+
180195
ch <- prometheus.MustNewConstMetric(
181-
c.threads,
196+
c.bootTimeSeconds,
182197
prometheus.GaugeValue,
183-
c.perfDataObject[0].Threads,
198+
c.bootTimeTimestamp,
199+
)
200+
201+
ch <- prometheus.MustNewConstMetric(
202+
c.bootTime,
203+
prometheus.GaugeValue,
204+
c.bootTimeTimestamp,
184205
)
185206

186207
// Windows has no defined limit, and is based off available resources. This currently isn't calculated by WMI and is set to default value.

internal/collector/system/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ type perfDataCounterValues struct {
2222
ExceptionDispatchesPerSec float64 `perfdata:"Exception Dispatches/sec"`
2323
ProcessorQueueLength float64 `perfdata:"Processor Queue Length"`
2424
SystemCallsPerSec float64 `perfdata:"System Calls/sec"`
25-
SystemUpTime float64 `perfdata:"System Up Time"`
2625
Processes float64 `perfdata:"Processes"`
2726
Threads float64 `perfdata:"Threads"`
2827
}

internal/headers/kernel32/kernel32.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import (
2525

2626
//nolint:gochecknoglobals
2727
var (
28-
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
28+
modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
2929

30-
procGetDynamicTimeZoneInformationSys = kernel32.NewProc("GetDynamicTimeZoneInformation")
31-
kernelLocalFileTimeToFileTime = kernel32.NewProc("LocalFileTimeToFileTime")
30+
procGetDynamicTimeZoneInformationSys = modkernel32.NewProc("GetDynamicTimeZoneInformation")
31+
procKernelLocalFileTimeToFileTime = modkernel32.NewProc("LocalFileTimeToFileTime")
32+
procGetTickCount = modkernel32.NewProc("GetTickCount64")
3233
)
3334

3435
// SYSTEMTIME contains a date and time.
@@ -72,9 +73,15 @@ func GetDynamicTimeZoneInformation() (DynamicTimezoneInformation, error) {
7273
}
7374

7475
func LocalFileTimeToFileTime(localFileTime, utcFileTime *windows.Filetime) uint32 {
75-
ret, _, _ := kernelLocalFileTimeToFileTime.Call(
76+
ret, _, _ := procKernelLocalFileTimeToFileTime.Call(
7677
uintptr(unsafe.Pointer(localFileTime)),
7778
uintptr(unsafe.Pointer(utcFileTime)))
7879

7980
return uint32(ret)
8081
}
82+
83+
func GetTickCount64() uint64 {
84+
ret, _, _ := procGetTickCount.Call()
85+
86+
return uint64(ret)
87+
}

tools/e2e-output.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ windows_service_state{name="Themes",state="running"} 1
393393
windows_service_state{name="Themes",state="start pending"} 0
394394
windows_service_state{name="Themes",state="stop pending"} 0
395395
windows_service_state{name="Themes",state="stopped"} 0
396-
# HELP windows_system_boot_time_timestamp_seconds Unix timestamp of system boot time
396+
# HELP windows_system_boot_time_timestamp Unix timestamp of system boot time
397+
# TYPE windows_system_boot_time_timestamp gauge
398+
# HELP windows_system_boot_time_timestamp_seconds Deprecated: Use windows_system_boot_time_timestamp instead
397399
# TYPE windows_system_boot_time_timestamp_seconds gauge
398400
# HELP windows_system_context_switches_total Total number of context switches (WMI source: PerfOS_System.ContextSwitchesPersec)
399401
# TYPE windows_system_context_switches_total counter

0 commit comments

Comments
 (0)