2019-04-05 13:59:40 +00:00
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2020-05-24 15:23:13 +00:00
|
|
|
"fmt"
|
2020-12-03 10:20:28 +00:00
|
|
|
"sort"
|
2019-08-03 11:15:45 +00:00
|
|
|
"strconv"
|
2020-02-09 20:09:26 +00:00
|
|
|
"strings"
|
2019-08-03 11:15:45 +00:00
|
|
|
|
2023-07-10 00:17:24 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/perflib"
|
|
|
|
|
2023-04-16 09:45:07 +00:00
|
|
|
"github.com/alecthomas/kingpin/v2"
|
2023-04-22 10:17:51 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2019-04-05 13:59:40 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-08-03 11:15:45 +00:00
|
|
|
"golang.org/x/sys/windows/registry"
|
2019-04-05 13:59:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ...
|
|
|
|
const (
|
|
|
|
// TODO: Make package-local
|
2020-05-24 18:36:25 +00:00
|
|
|
Namespace = "windows"
|
2019-04-05 13:59:40 +00:00
|
|
|
|
|
|
|
// Conversion factors
|
|
|
|
ticksToSecondsScaleFactor = 1 / 1e7
|
|
|
|
windowsEpoch = 116444736000000000
|
|
|
|
)
|
|
|
|
|
2019-08-03 11:15:45 +00:00
|
|
|
// getWindowsVersion reads the version number of the OS from the Registry
|
|
|
|
// See https://docs.microsoft.com/en-us/windows/desktop/sysinfo/operating-system-version
|
2023-04-22 10:17:51 +00:00
|
|
|
func getWindowsVersion(logger log.Logger) float64 {
|
2019-08-03 11:15:45 +00:00
|
|
|
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
|
|
|
|
if err != nil {
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Warn(logger).Log("msg", "Couldn't open registry", "err", err)
|
2019-08-03 11:15:45 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = k.Close()
|
|
|
|
if err != nil {
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Warn(logger).Log("msg", "Failed to close registry key", "err", err)
|
2019-08-03 11:15:45 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
currentv, _, err := k.GetStringValue("CurrentVersion")
|
|
|
|
if err != nil {
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Warn(logger).Log("msg", "Couldn't open registry to determine current Windows version", "err", err)
|
2019-08-03 11:15:45 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
currentv_flt, err := strconv.ParseFloat(currentv, 64)
|
|
|
|
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Debug(logger).Log("msg", fmt.Sprintf("Detected Windows version %f\n", currentv_flt))
|
2019-08-03 11:15:45 +00:00
|
|
|
|
|
|
|
return currentv_flt
|
|
|
|
}
|
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
type collectorBuilder func(log.Logger) (Collector, error)
|
2023-04-16 09:45:07 +00:00
|
|
|
type flagsBuilder func(*kingpin.Application)
|
2023-04-22 10:17:51 +00:00
|
|
|
type perfCounterNamesBuilder func(log.Logger) []string
|
2020-02-09 20:09:26 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
builders = make(map[string]collectorBuilder)
|
|
|
|
perfCounterDependencies = make(map[string]string)
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerCollector(name string, builder collectorBuilder, perfCounterNames ...string) {
|
|
|
|
builders[name] = builder
|
2020-09-01 04:45:46 +00:00
|
|
|
addPerfCounterDependencies(name, perfCounterNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPerfCounterDependencies(name string, perfCounterNames []string) {
|
2020-02-09 20:09:26 +00:00
|
|
|
perfIndicies := make([]string, 0, len(perfCounterNames))
|
|
|
|
for _, cn := range perfCounterNames {
|
|
|
|
perfIndicies = append(perfIndicies, MapCounterToIndex(cn))
|
|
|
|
}
|
|
|
|
perfCounterDependencies[name] = strings.Join(perfIndicies, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Available() []string {
|
|
|
|
cs := make([]string, 0, len(builders))
|
|
|
|
for c := range builders {
|
|
|
|
cs = append(cs, c)
|
|
|
|
}
|
|
|
|
return cs
|
|
|
|
}
|
2023-04-22 10:17:51 +00:00
|
|
|
func Build(collector string, logger log.Logger) (Collector, error) {
|
2020-05-24 15:23:13 +00:00
|
|
|
builder, exists := builders[collector]
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("Unknown collector %q", collector)
|
|
|
|
}
|
2023-04-22 10:17:51 +00:00
|
|
|
return builder(logger)
|
2020-02-09 20:09:26 +00:00
|
|
|
}
|
|
|
|
func getPerfQuery(collectors []string) string {
|
|
|
|
parts := make([]string, 0, len(collectors))
|
|
|
|
for _, c := range collectors {
|
|
|
|
if p := perfCounterDependencies[c]; p != "" {
|
|
|
|
parts = append(parts, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(parts, " ")
|
|
|
|
}
|
2019-04-05 13:59:40 +00:00
|
|
|
|
|
|
|
// Collector is the interface a collector has to implement.
|
|
|
|
type Collector interface {
|
|
|
|
// Get new metrics and expose them via prometheus registry.
|
|
|
|
Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) (err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ScrapeContext struct {
|
|
|
|
perfObjects map[string]*perflib.PerfObject
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareScrapeContext creates a ScrapeContext to be used during a single scrape
|
2020-02-09 20:09:26 +00:00
|
|
|
func PrepareScrapeContext(collectors []string) (*ScrapeContext, error) {
|
|
|
|
q := getPerfQuery(collectors) // TODO: Memoize
|
|
|
|
objs, err := getPerflibSnapshot(q)
|
2019-04-05 13:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ScrapeContext{objs}, nil
|
|
|
|
}
|
2020-06-07 15:40:55 +00:00
|
|
|
func boolToFloat(b bool) float64 {
|
|
|
|
if b {
|
|
|
|
return 1.0
|
|
|
|
}
|
|
|
|
return 0.0
|
|
|
|
}
|
2020-11-01 17:26:14 +00:00
|
|
|
|
|
|
|
func find(slice []string, val string) bool {
|
|
|
|
for _, item := range slice {
|
|
|
|
if item == val {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-12-03 10:20:28 +00:00
|
|
|
|
|
|
|
// Used by more complex collectors where user input specifies enabled child collectors.
|
|
|
|
// Splits provided child collectors and deduplicate.
|
|
|
|
func expandEnabledChildCollectors(enabled string) []string {
|
|
|
|
separated := strings.Split(enabled, ",")
|
|
|
|
unique := map[string]bool{}
|
|
|
|
for _, s := range separated {
|
|
|
|
if s != "" {
|
|
|
|
unique[s] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result := make([]string, 0, len(unique))
|
|
|
|
for s := range unique {
|
|
|
|
result = append(result, s)
|
|
|
|
}
|
|
|
|
// Ensure result is ordered, to prevent test failure
|
|
|
|
sort.Strings(result)
|
|
|
|
return result
|
|
|
|
}
|
2022-01-02 06:24:11 +00:00
|
|
|
|
|
|
|
func milliSecToSec(t float64) float64 {
|
|
|
|
return t / 1000
|
|
|
|
}
|