Skip to content

Commit

Permalink
*: avoid using default wmi client.
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
  • Loading branch information
jkroepke committed Aug 29, 2024
1 parent 3ce25ff commit ac43ab1
Show file tree
Hide file tree
Showing 65 changed files with 533 additions and 585 deletions.
6 changes: 0 additions & 6 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/log/flag"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus-community/windows_exporter/pkg/utils"
"github.com/prometheus-community/windows_exporter/pkg/wmi"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
Expand Down Expand Up @@ -196,11 +195,6 @@ func main() {
}
}

if err = wmi.InitWbem(logger); err != nil {
_ = level.Error(logger).Log("err", err)
os.Exit(1)
}

enabledCollectorList := utils.ExpandEnabledCollectors(*enabledCollectors)
collectors.Enable(enabledCollectorList)

Expand Down
20 changes: 13 additions & 7 deletions pkg/collector/ad/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus-community/windows_exporter/pkg/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const Name = "ad"
Expand All @@ -21,7 +21,8 @@ var ConfigDefaults = Config{}

// A Collector is a Prometheus Collector for WMI Win32_PerfRawData_DirectoryServices_DirectoryServices metrics.
type Collector struct {
config Config
config Config
wmiClient *wmi.Client

addressBookClientSessions *prometheus.Desc
addressBookOperationsTotal *prometheus.Desc
Expand Down Expand Up @@ -115,7 +116,13 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(_ log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
if wmiClient == nil || wmiClient.SWbemServicesClient == nil {
return errors.New("wmiClient or SWbemServicesClient is nil")
}

c.wmiClient = wmiClient

c.addressBookOperationsTotal = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "address_book_operations_total"),
"",
Expand Down Expand Up @@ -497,7 +504,7 @@ func (c *Collector) Build(_ log.Logger) error {
// to the provided prometheus Metric channel.
func (c *Collector) Collect(_ *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
logger = log.With(logger, "collector", Name)
if err := c.collect(logger, ch); err != nil {
if err := c.collect(ch); err != nil {
_ = level.Error(logger).Log("msg", "failed collecting ad metrics", "err", err)
return err
}
Expand Down Expand Up @@ -656,10 +663,9 @@ type Win32_PerfRawData_DirectoryServices_DirectoryServices struct {
TransitivesuboperationsPersec uint32
}

func (c *Collector) collect(logger log.Logger, ch chan<- prometheus.Metric) error {
func (c *Collector) collect(ch chan<- prometheus.Metric) error {
var dst []Win32_PerfRawData_DirectoryServices_DirectoryServices
q := wmi.QueryAll(&dst, logger)
if err := wmi.Query(q, &dst); err != nil {
if err := c.wmiClient.Query("SELECT * FROM Win32_PerfRawData_DirectoryServices_DirectoryServices", &dst); err != nil {
return err
}
if len(dst) == 0 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/adcs/adcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus-community/windows_exporter/pkg/utils"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const Name = "adcs"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(_ log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
c.requestsPerSecond = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "requests_total"),
"Total certificate requests processed",
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/adfs/adfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/perflib"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const Name = "adfs"
Expand Down Expand Up @@ -94,7 +95,7 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(_ log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
c.adLoginConnectionFailures = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "ad_login_connection_failures_total"),
"Total number of connection failures to an Active Directory domain controller",
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/perflib"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const Name = "cache"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(_ log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
c.asyncCopyReadsTotal = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "async_copy_reads_total"),
"(AsyncCopyReadsTotal)",
Expand Down
34 changes: 22 additions & 12 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package collector

import (
"errors"
"fmt"
"slices"
"strings"

Expand Down Expand Up @@ -63,10 +64,11 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/collector/vmware_blast"
"github.com/prometheus-community/windows_exporter/pkg/perflib"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/yusufpapurcu/wmi"
)

// NewWithFlags To be called by the exporter for collector initialization before running kingpin.Parse.
func NewWithFlags(app *kingpin.Application) Collectors {
func NewWithFlags(app *kingpin.Application) *Collectors {
collectors := map[string]Collector{}

for name, builder := range BuildersWithFlags {
Expand All @@ -76,16 +78,10 @@ func NewWithFlags(app *kingpin.Application) Collectors {
return New(collectors)
}

func NewBuilderWithFlags[C Collector](fn BuilderWithFlags[C]) BuilderWithFlags[Collector] {
return func(app *kingpin.Application) Collector {
return fn(app)
}
}

// NewWithConfig To be called by the external libraries for collector initialization without running kingpin.Parse
//
//goland:noinspection GoUnusedExportedFunction
func NewWithConfig(config Config) Collectors {
func NewWithConfig(config Config) *Collectors {
collectors := map[string]Collector{}
collectors[ad.Name] = ad.New(&config.AD)
collectors[adcs.Name] = adcs.New(&config.ADCS)
Expand Down Expand Up @@ -144,9 +140,12 @@ func NewWithConfig(config Config) Collectors {
}

// New To be called by the external libraries for collector initialization.
func New(collectors Map) Collectors {
return Collectors{
func New(collectors Map) *Collectors {
return &Collectors{
collectors: collectors,
wmiClient: &wmi.Client{
AllowMissingFields: true,
},
}
}

Expand Down Expand Up @@ -192,9 +191,14 @@ func (c *Collectors) Enable(enabledCollectors []string) {
func (c *Collectors) Build(logger log.Logger) error {
var err error

c.wmiClient.SWbemServicesClient, err = wmi.InitializeSWbemServices(c.wmiClient)
if err != nil {
return fmt.Errorf("initialize SWbemServices: %w", err)
}

for _, collector := range c.collectors {
if err = collector.Build(logger); err != nil {
return err
if err = collector.Build(logger, c.wmiClient); err != nil {
return fmt.Errorf("error build collector %s: %w", collector.GetName(), err)
}
}

Expand All @@ -221,5 +225,11 @@ func (c *Collectors) Close() error {
}
}

if c.wmiClient != nil && c.wmiClient.SWbemServicesClient != nil {
if err := c.wmiClient.SWbemServicesClient.Close(); err != nil {
errs = append(errs, err)
}
}

return errors.Join(errs...)
}
3 changes: 2 additions & 1 deletion pkg/collector/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/perflib"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const Name = "container"
Expand Down Expand Up @@ -84,7 +85,7 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(_ log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
c.containerAvailable = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "available"),
"Available",
Expand Down
9 changes: 5 additions & 4 deletions pkg/collector/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus-community/windows_exporter/pkg/winversion"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const Name = "cpu"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (c *Collector) GetName() string {
}

func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) {
if winversion.WindowsVersionFloat > 6.05 {
if winversion.WindowsVersionFloat() > 6.05 {
return []string{"Processor Information"}, nil
}
return []string{"Processor"}, nil
Expand All @@ -68,7 +69,7 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(_ log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
c.cStateSecondsTotal = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "cstate_seconds_total"),
"Time spent in low-power idle state",
Expand Down Expand Up @@ -100,7 +101,7 @@ func (c *Collector) Build(_ log.Logger) error {
// are added in later versions, so we aren't guaranteed to get all of
// them).
// Value 6.05 was selected to split between Windows versions.
if winversion.WindowsVersionFloat < 6.05 {
if winversion.WindowsVersionFloat() < 6.05 {
return nil
}

Expand Down Expand Up @@ -188,7 +189,7 @@ func (c *Collector) Build(_ log.Logger) error {

func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
logger = log.With(logger, "collector", Name)
if winversion.WindowsVersionFloat > 6.05 {
if winversion.WindowsVersionFloat() > 6.05 {
return c.CollectFull(ctx, logger, ch)
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/collector/cpu_info/cpu_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus-community/windows_exporter/pkg/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const (
Expand All @@ -29,7 +29,8 @@ var ConfigDefaults = Config{}
type Collector struct {
config Config

cpuInfo *prometheus.Desc
wmiClient *wmi.Client
cpuInfo *prometheus.Desc
}

func New(config *Config) *Collector {
Expand Down Expand Up @@ -60,7 +61,12 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(_ log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
if wmiClient == nil || wmiClient.SWbemServicesClient == nil {
return errors.New("wmiClient or SWbemServicesClient is nil")
}

c.wmiClient = wmiClient
c.cpuInfo = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, "", Name),
"Labelled CPU information as provided by Win32_Processor",
Expand Down Expand Up @@ -105,7 +111,7 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error {
// We use a static query here because the provided methods in wmi.go all issue a SELECT *;
// This results in the time-consuming LoadPercentage field being read which seems to measure each CPU
// serially over a 1 second interval, so the scrape time is at least 1s * num_sockets
if err := wmi.Query(win32ProcessorQuery, &dst); err != nil {
if err := c.wmiClient.Query(win32ProcessorQuery, &dst); err != nil {
return err
}
if len(dst) == 0 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/cs/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/headers/sysinfoapi"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const Name = "cs"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(_ log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
c.logicalProcessors = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "logical_processors"),
"ComputerSystem.NumberOfLogicalProcessors",
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/dfsr/dfsr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/perflib"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const Name = "dfsr"
Expand Down Expand Up @@ -152,7 +153,7 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(logger log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
logger = log.With(logger, "collector", Name)

_ = level.Info(logger).Log("msg", "dfsr collector is in an experimental state! Metrics for this collector have not been tested.")
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/dhcp/dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/perflib"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
)

const Name = "dhcp"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *Collector) Close() error {
return nil
}

func (c *Collector) Build(_ log.Logger) error {
func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error {
c.packetsReceivedTotal = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "packets_received_total"),
"Total number of packets received by the DHCP server (PacketsReceivedTotal)",
Expand Down
Loading

0 comments on commit ac43ab1

Please sign in to comment.