mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-24 13:46:36 +00:00
os: add system installation date to metrics (#2284)
Co-authored-by: EisenbergD <dominik.eisenberg@beiersdorf.com>
This commit is contained in:
committed by
GitHub
parent
0c1336b845
commit
27186f7e78
@@ -44,8 +44,11 @@ var ConfigDefaults = Config{}
|
||||
type Collector struct {
|
||||
config Config
|
||||
|
||||
installTimeTimestamp float64
|
||||
|
||||
hostname *prometheus.Desc
|
||||
osInformation *prometheus.Desc
|
||||
installTime *prometheus.Desc
|
||||
}
|
||||
|
||||
func New(config *Config) *Collector {
|
||||
@@ -78,6 +81,13 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
|
||||
return fmt.Errorf("failed to get Windows version: %w", err)
|
||||
}
|
||||
|
||||
installTimeTimestamp, err := c.getInstallTime()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get install time: %w", err)
|
||||
}
|
||||
|
||||
c.installTimeTimestamp = installTimeTimestamp
|
||||
|
||||
version := osversion.Get()
|
||||
|
||||
// Microsoft has decided to keep the major version as "10" for Windows 11, including the product name.
|
||||
@@ -111,6 +121,13 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
|
||||
nil,
|
||||
)
|
||||
|
||||
c.installTime = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "install_time_timestamp"),
|
||||
"Unix timestamp of OS installation time",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -125,6 +142,12 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
1.0,
|
||||
)
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.installTime,
|
||||
prometheus.GaugeValue,
|
||||
c.installTimeTimestamp,
|
||||
)
|
||||
|
||||
if err := c.collectHostname(ch); err != nil {
|
||||
errs = append(errs, fmt.Errorf("failed to collect hostname metrics: %w", err))
|
||||
}
|
||||
@@ -190,3 +213,23 @@ func (c *Collector) getWindowsVersion() (string, string, string, error) {
|
||||
|
||||
return strings.TrimSpace(productName), strconv.FormatUint(revision, 10), strings.TrimSpace(installationType), nil
|
||||
}
|
||||
|
||||
func (c *Collector) getInstallTime() (float64, error) {
|
||||
ntKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to open registry key: %w", err)
|
||||
}
|
||||
|
||||
defer func(ntKey registry.Key) {
|
||||
_ = ntKey.Close()
|
||||
}(ntKey)
|
||||
|
||||
installDate, _, err := ntKey.GetIntegerValue("InstallDate")
|
||||
if errors.Is(err, registry.ErrNotExist) {
|
||||
return 0, nil
|
||||
} else if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return float64(installDate), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user