2023-11-04 19:51:35 +00:00
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2024-08-05 13:50:41 +00:00
|
|
|
"errors"
|
2024-08-29 22:26:15 +00:00
|
|
|
"fmt"
|
2023-11-04 19:51:35 +00:00
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alecthomas/kingpin/v2"
|
|
|
|
"github.com/go-kit/log"
|
2023-11-12 12:32:49 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/ad"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/adcs"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/adfs"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/cache"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/container"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/cpu"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/cpu_info"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/cs"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/dfsr"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/dhcp"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/diskdrive"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/dns"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/exchange"
|
2024-08-05 13:50:41 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/fsrmquota"
|
2023-11-12 12:32:49 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/hyperv"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/iis"
|
2024-07-23 11:02:25 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/license"
|
2023-11-12 12:32:49 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/logical_disk"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/logon"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/memory"
|
2024-08-29 20:03:05 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/mscluster"
|
2023-11-12 12:32:49 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/msmq"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/mssql"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/net"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/netframework_clrexceptions"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/netframework_clrinterop"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/netframework_clrjit"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/netframework_clrloading"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/netframework_clrlocksandthreads"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/netframework_clrmemory"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/netframework_clrremoting"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/netframework_clrsecurity"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/nps"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/os"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/physical_disk"
|
2024-05-17 16:47:32 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/printer"
|
2023-11-12 12:32:49 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/process"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/remote_fx"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/scheduled_task"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/service"
|
2023-11-16 13:14:20 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/smb"
|
2024-05-11 16:06:03 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/smbclient"
|
2023-11-12 12:32:49 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/smtp"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/system"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/tcp"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/teradici_pcoip"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/terminal_services"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/textfile"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/thermalzone"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/time"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/vmware"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/collector/vmware_blast"
|
2023-11-04 19:51:35 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/perflib"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/types"
|
2024-08-29 22:26:15 +00:00
|
|
|
"github.com/yusufpapurcu/wmi"
|
2023-11-04 19:51:35 +00:00
|
|
|
)
|
|
|
|
|
2024-08-10 20:05:33 +00:00
|
|
|
// NewWithFlags To be called by the exporter for collector initialization before running kingpin.Parse.
|
2024-08-29 22:26:15 +00:00
|
|
|
func NewWithFlags(app *kingpin.Application) *Collectors {
|
2024-08-05 13:50:41 +00:00
|
|
|
collectors := map[string]Collector{}
|
2023-11-04 19:51:35 +00:00
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
for name, builder := range BuildersWithFlags {
|
2023-11-04 19:51:35 +00:00
|
|
|
collectors[name] = builder(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
return New(collectors)
|
|
|
|
}
|
|
|
|
|
2023-11-12 12:32:49 +00:00
|
|
|
// NewWithConfig To be called by the external libraries for collector initialization without running kingpin.Parse
|
2024-07-24 09:18:08 +00:00
|
|
|
//
|
|
|
|
//goland:noinspection GoUnusedExportedFunction
|
2024-08-29 22:26:15 +00:00
|
|
|
func NewWithConfig(config Config) *Collectors {
|
2024-08-05 13:50:41 +00:00
|
|
|
collectors := map[string]Collector{}
|
2024-08-24 17:14:38 +00:00
|
|
|
collectors[ad.Name] = ad.New(&config.AD)
|
|
|
|
collectors[adcs.Name] = adcs.New(&config.ADCS)
|
|
|
|
collectors[adfs.Name] = adfs.New(&config.ADFS)
|
|
|
|
collectors[cache.Name] = cache.New(&config.Cache)
|
|
|
|
collectors[container.Name] = container.New(&config.Container)
|
|
|
|
collectors[cpu.Name] = cpu.New(&config.CPU)
|
|
|
|
collectors[cpu_info.Name] = cpu_info.New(&config.CPUInfo)
|
|
|
|
collectors[cs.Name] = cs.New(&config.Cs)
|
|
|
|
collectors[dfsr.Name] = dfsr.New(&config.DFSR)
|
|
|
|
collectors[dhcp.Name] = dhcp.New(&config.Dhcp)
|
|
|
|
collectors[diskdrive.Name] = diskdrive.New(&config.DiskDrive)
|
|
|
|
collectors[dns.Name] = dns.New(&config.DNS)
|
|
|
|
collectors[exchange.Name] = exchange.New(&config.Exchange)
|
|
|
|
collectors[fsrmquota.Name] = fsrmquota.New(&config.Fsrmquota)
|
|
|
|
collectors[hyperv.Name] = hyperv.New(&config.Hyperv)
|
|
|
|
collectors[iis.Name] = iis.New(&config.IIS)
|
|
|
|
collectors[license.Name] = license.New(&config.License)
|
|
|
|
collectors[logical_disk.Name] = logical_disk.New(&config.LogicalDisk)
|
|
|
|
collectors[logon.Name] = logon.New(&config.Logon)
|
|
|
|
collectors[memory.Name] = memory.New(&config.Memory)
|
2024-08-29 20:03:05 +00:00
|
|
|
collectors[mscluster.Name] = mscluster.New(&config.Mscluster)
|
2024-08-24 17:14:38 +00:00
|
|
|
collectors[msmq.Name] = msmq.New(&config.Msmq)
|
|
|
|
collectors[mssql.Name] = mssql.New(&config.Mssql)
|
|
|
|
collectors[net.Name] = net.New(&config.Net)
|
|
|
|
collectors[netframework_clrexceptions.Name] = netframework_clrexceptions.New(&config.NetframeworkClrexceptions)
|
|
|
|
collectors[netframework_clrinterop.Name] = netframework_clrinterop.New(&config.NetframeworkClrinterop)
|
|
|
|
collectors[netframework_clrjit.Name] = netframework_clrjit.New(&config.NetframeworkClrjit)
|
|
|
|
collectors[netframework_clrloading.Name] = netframework_clrloading.New(&config.NetframeworkClrloading)
|
|
|
|
collectors[netframework_clrlocksandthreads.Name] = netframework_clrlocksandthreads.New(&config.NetframeworkClrlocksandthreads)
|
|
|
|
collectors[netframework_clrmemory.Name] = netframework_clrmemory.New(&config.NetframeworkClrmemory)
|
|
|
|
collectors[netframework_clrremoting.Name] = netframework_clrremoting.New(&config.NetframeworkClrremoting)
|
|
|
|
collectors[netframework_clrsecurity.Name] = netframework_clrsecurity.New(&config.NetframeworkClrsecurity)
|
|
|
|
collectors[nps.Name] = nps.New(&config.Nps)
|
|
|
|
collectors[os.Name] = os.New(&config.Os)
|
|
|
|
collectors[physical_disk.Name] = physical_disk.New(&config.PhysicalDisk)
|
|
|
|
collectors[printer.Name] = printer.New(&config.Printer)
|
|
|
|
collectors[process.Name] = process.New(&config.Process)
|
|
|
|
collectors[remote_fx.Name] = remote_fx.New(&config.RemoteFx)
|
|
|
|
collectors[scheduled_task.Name] = scheduled_task.New(&config.ScheduledTask)
|
|
|
|
collectors[service.Name] = service.New(&config.Service)
|
|
|
|
collectors[smb.Name] = smb.New(&config.SMB)
|
|
|
|
collectors[smbclient.Name] = smbclient.New(&config.SMBClient)
|
|
|
|
collectors[smtp.Name] = smtp.New(&config.SMTP)
|
|
|
|
collectors[system.Name] = system.New(&config.System)
|
|
|
|
collectors[teradici_pcoip.Name] = teradici_pcoip.New(&config.TeradiciPcoip)
|
|
|
|
collectors[tcp.Name] = tcp.New(&config.TCP)
|
|
|
|
collectors[terminal_services.Name] = terminal_services.New(&config.TerminalServices)
|
|
|
|
collectors[textfile.Name] = textfile.New(&config.Textfile)
|
|
|
|
collectors[thermalzone.Name] = thermalzone.New(&config.Thermalzone)
|
|
|
|
collectors[time.Name] = time.New(&config.Time)
|
|
|
|
collectors[vmware.Name] = vmware.New(&config.Vmware)
|
|
|
|
collectors[vmware_blast.Name] = vmware_blast.New(&config.VmwareBlast)
|
2024-07-24 09:18:08 +00:00
|
|
|
|
2023-11-12 12:32:49 +00:00
|
|
|
return New(collectors)
|
|
|
|
}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
// New To be called by the external libraries for collector initialization.
|
2024-08-29 22:26:15 +00:00
|
|
|
func New(collectors Map) *Collectors {
|
|
|
|
return &Collectors{
|
2023-11-04 19:51:35 +00:00
|
|
|
collectors: collectors,
|
2024-08-29 22:26:15 +00:00
|
|
|
wmiClient: &wmi.Client{
|
|
|
|
AllowMissingFields: true,
|
|
|
|
},
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-24 17:14:38 +00:00
|
|
|
func (c *Collectors) SetPerfCounterQuery(logger log.Logger) error {
|
2023-11-04 19:51:35 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
|
2024-05-12 22:31:11 +00:00
|
|
|
perfCounterNames []string
|
|
|
|
perfIndicies []string
|
2023-11-04 19:51:35 +00:00
|
|
|
)
|
|
|
|
|
2024-05-12 22:31:11 +00:00
|
|
|
perfCounterDependencies := make([]string, 0, len(c.collectors))
|
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
for _, collector := range c.collectors {
|
2024-08-24 17:14:38 +00:00
|
|
|
perfCounterNames, err = collector.GetPerfCounter(logger)
|
2023-11-04 19:51:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
perfIndicies = make([]string, 0, len(perfCounterNames))
|
|
|
|
for _, cn := range perfCounterNames {
|
|
|
|
perfIndicies = append(perfIndicies, perflib.MapCounterToIndex(cn))
|
|
|
|
}
|
|
|
|
|
|
|
|
perfCounterDependencies = append(perfCounterDependencies, strings.Join(perfIndicies, " "))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.perfCounterQuery = strings.Join(perfCounterDependencies, " ")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-10 20:05:33 +00:00
|
|
|
// Enable removes all collectors that not enabledCollectors.
|
2023-11-04 19:51:35 +00:00
|
|
|
func (c *Collectors) Enable(enabledCollectors []string) {
|
|
|
|
for name := range c.collectors {
|
|
|
|
if !slices.Contains(enabledCollectors, name) {
|
|
|
|
delete(c.collectors, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-10 20:05:33 +00:00
|
|
|
// Build To be called by the exporter for collector initialization.
|
2024-08-24 17:14:38 +00:00
|
|
|
func (c *Collectors) Build(logger log.Logger) error {
|
2023-11-04 19:51:35 +00:00
|
|
|
var err error
|
2024-08-05 13:50:41 +00:00
|
|
|
|
2024-08-29 22:26:15 +00:00
|
|
|
c.wmiClient.SWbemServicesClient, err = wmi.InitializeSWbemServices(c.wmiClient)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initialize SWbemServices: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
for _, collector := range c.collectors {
|
2024-08-29 22:26:15 +00:00
|
|
|
if err = collector.Build(logger, c.wmiClient); err != nil {
|
|
|
|
return fmt.Errorf("error build collector %s: %w", collector.GetName(), err)
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-10 20:05:33 +00:00
|
|
|
// PrepareScrapeContext creates a ScrapeContext to be used during a single scrape.
|
2023-11-04 19:51:35 +00:00
|
|
|
func (c *Collectors) PrepareScrapeContext() (*types.ScrapeContext, error) {
|
|
|
|
objs, err := perflib.GetPerflibSnapshot(c.perfCounterQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.ScrapeContext{PerfObjects: objs}, nil
|
|
|
|
}
|
2024-08-05 13:50:41 +00:00
|
|
|
|
2024-08-10 20:05:33 +00:00
|
|
|
// Close To be called by the exporter for collector cleanup.
|
2024-08-05 13:50:41 +00:00
|
|
|
func (c *Collectors) Close() error {
|
|
|
|
errs := make([]error, 0, len(c.collectors))
|
|
|
|
|
|
|
|
for _, collector := range c.collectors {
|
2024-08-24 17:14:38 +00:00
|
|
|
if err := collector.Close(); err != nil {
|
2024-08-05 13:50:41 +00:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-29 22:26:15 +00:00
|
|
|
if c.wmiClient != nil && c.wmiClient.SWbemServicesClient != nil {
|
|
|
|
if err := c.wmiClient.SWbemServicesClient.Close(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
return errors.Join(errs...)
|
|
|
|
}
|