mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-23 21:26:36 +00:00
mssql: expose correct patch level without restart (#2187)
This commit is contained in:
@@ -18,34 +18,63 @@
|
|||||||
package mssql
|
package mssql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"golang.org/x/sys/windows/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
type collectorInstance struct {
|
type collectorInstance struct {
|
||||||
instances *prometheus.GaugeVec
|
instances *prometheus.Desc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collector) buildInstance() error {
|
func (c *Collector) buildInstance() error {
|
||||||
c.instances = prometheus.NewGaugeVec(
|
c.instances = prometheus.NewDesc(
|
||||||
prometheus.GaugeOpts{
|
prometheus.BuildFQName(types.Namespace, Name, "instance_info"),
|
||||||
Namespace: types.Namespace,
|
"A metric with a constant '1' value labeled with mssql instance information",
|
||||||
Subsystem: Name,
|
|
||||||
Name: "instance_info",
|
|
||||||
Help: "A metric with a constant '1' value labeled with mssql instance information",
|
|
||||||
},
|
|
||||||
[]string{"edition", "mssql_instance", "patch", "version"},
|
[]string{"edition", "mssql_instance", "patch", "version"},
|
||||||
|
nil,
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, instance := range c.mssqlInstances {
|
|
||||||
c.instances.WithLabelValues(instance.edition, instance.name, instance.patchVersion, instance.majorVersion.String()).Set(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collector) collectInstance(ch chan<- prometheus.Metric) error {
|
func (c *Collector) collectInstance(ch chan<- prometheus.Metric) error {
|
||||||
c.instances.Collect(ch)
|
for _, instance := range c.mssqlInstances {
|
||||||
|
regKeyName := fmt.Sprintf(`Software\Microsoft\Microsoft SQL Server\%s\Setup`, instance.instanceName)
|
||||||
|
|
||||||
|
regKey, err := registry.OpenKey(registry.LOCAL_MACHINE, regKeyName, registry.QUERY_VALUE)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Debug(fmt.Sprintf("couldn't open registry %s:", regKeyName),
|
||||||
|
slog.Any("err", err),
|
||||||
|
)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
patchVersion, _, err := regKey.GetStringValue("PatchLevel")
|
||||||
|
_ = regKey.Close()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Debug("couldn't get version from registry",
|
||||||
|
slog.Any("err", err),
|
||||||
|
)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
c.instances,
|
||||||
|
prometheus.GaugeValue,
|
||||||
|
1,
|
||||||
|
instance.edition,
|
||||||
|
instance.name,
|
||||||
|
patchVersion,
|
||||||
|
instance.majorVersion.String(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ import (
|
|||||||
|
|
||||||
type mssqlInstance struct {
|
type mssqlInstance struct {
|
||||||
name string
|
name string
|
||||||
|
instanceName string
|
||||||
majorVersion mssqlServerMajorVersion
|
majorVersion mssqlServerMajorVersion
|
||||||
patchVersion string
|
|
||||||
edition string
|
edition string
|
||||||
isFirstInstance bool
|
isFirstInstance bool
|
||||||
}
|
}
|
||||||
@@ -54,14 +54,15 @@ func newMssqlInstance(key, name string) (mssqlInstance, error) {
|
|||||||
return mssqlInstance{}, fmt.Errorf("couldn't get version from registry: %w", err)
|
return mssqlInstance{}, fmt.Errorf("couldn't get version from registry: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
instanceName := name
|
||||||
_, name, _ = strings.Cut(name, ".")
|
_, name, _ = strings.Cut(name, ".")
|
||||||
|
|
||||||
return mssqlInstance{
|
return mssqlInstance{
|
||||||
edition: edition,
|
edition: edition,
|
||||||
name: name,
|
name: name,
|
||||||
majorVersion: newMajorVersion(patchVersion),
|
majorVersion: newMajorVersion(patchVersion),
|
||||||
patchVersion: patchVersion,
|
|
||||||
isFirstInstance: key == "MSSQLSERVER",
|
isFirstInstance: key == "MSSQLSERVER",
|
||||||
|
instanceName: instanceName,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user