2024-09-10 22:34:10 +00:00
|
|
|
//go:build windows
|
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
package perfdata
|
2024-09-10 22:34:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-11-14 21:39:59 +00:00
|
|
|
"slices"
|
2024-09-10 22:34:10 +00:00
|
|
|
"strings"
|
2024-11-17 20:51:12 +00:00
|
|
|
"sync"
|
2024-09-10 22:34:10 +00:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
)
|
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
var (
|
|
|
|
InstanceAll = []string{"*"}
|
|
|
|
InstanceTotal = []string{"_Total"}
|
|
|
|
)
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
type Collector struct {
|
2024-11-14 21:39:59 +00:00
|
|
|
object string
|
|
|
|
counters map[string]Counter
|
|
|
|
handle pdhQueryHandle
|
|
|
|
totalCounterRequested bool
|
2024-11-17 20:51:12 +00:00
|
|
|
mu sync.RWMutex
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Counter struct {
|
|
|
|
Name string
|
2024-10-03 12:31:44 +00:00
|
|
|
Desc string
|
2024-09-10 22:34:10 +00:00
|
|
|
Instances map[string]pdhCounterHandle
|
|
|
|
Type uint32
|
2024-11-13 23:06:22 +00:00
|
|
|
Frequency int64
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCollector(object string, instances []string, counters []string) (*Collector, error) {
|
|
|
|
var handle pdhQueryHandle
|
|
|
|
|
|
|
|
if ret := PdhOpenQuery(0, 0, &handle); ret != ErrorSuccess {
|
|
|
|
return nil, NewPdhError(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(instances) == 0 {
|
2024-11-17 20:51:12 +00:00
|
|
|
instances = []string{EmptyInstance}
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
collector := &Collector{
|
2024-11-14 21:39:59 +00:00
|
|
|
object: object,
|
|
|
|
counters: make(map[string]Counter, len(counters)),
|
|
|
|
handle: handle,
|
|
|
|
totalCounterRequested: slices.Contains(instances, "_Total"),
|
2024-11-17 20:51:12 +00:00
|
|
|
mu: sync.RWMutex{},
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, counterName := range counters {
|
|
|
|
if counterName == "*" {
|
|
|
|
return nil, errors.New("wildcard counters are not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
counter := Counter{
|
|
|
|
Name: counterName,
|
|
|
|
Instances: make(map[string]pdhCounterHandle, len(instances)),
|
|
|
|
}
|
|
|
|
|
|
|
|
var counterPath string
|
|
|
|
|
|
|
|
for _, instance := range instances {
|
|
|
|
counterPath = formatCounterPath(object, instance, counterName)
|
|
|
|
|
|
|
|
var counterHandle pdhCounterHandle
|
|
|
|
|
|
|
|
if ret := PdhAddEnglishCounter(handle, counterPath, 0, &counterHandle); ret != ErrorSuccess {
|
|
|
|
return nil, fmt.Errorf("failed to add counter %s: %w", counterPath, NewPdhError(ret))
|
|
|
|
}
|
|
|
|
|
|
|
|
counter.Instances[instance] = counterHandle
|
|
|
|
|
2024-11-13 23:06:22 +00:00
|
|
|
if counter.Type != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2024-11-13 23:06:22 +00:00
|
|
|
// Get the info with the current buffer size
|
|
|
|
bufLen := uint32(0)
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2024-11-14 21:28:28 +00:00
|
|
|
if ret := PdhGetCounterInfo(counterHandle, 0, &bufLen, nil); ret != PdhMoreData {
|
2024-11-13 23:06:22 +00:00
|
|
|
return nil, fmt.Errorf("PdhGetCounterInfo: %w", NewPdhError(ret))
|
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2024-11-13 23:06:22 +00:00
|
|
|
buf := make([]byte, bufLen)
|
2024-11-14 21:28:28 +00:00
|
|
|
if ret := PdhGetCounterInfo(counterHandle, 0, &bufLen, &buf[0]); ret != ErrorSuccess {
|
2024-11-13 23:06:22 +00:00
|
|
|
return nil, fmt.Errorf("PdhGetCounterInfo: %w", NewPdhError(ret))
|
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2024-11-13 23:06:22 +00:00
|
|
|
ci := (*PdhCounterInfo)(unsafe.Pointer(&buf[0]))
|
|
|
|
counter.Type = ci.DwType
|
|
|
|
counter.Desc = windows.UTF16PtrToString(ci.SzExplainText)
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
if counter.Type == PERF_ELAPSED_TIME {
|
2024-11-13 23:06:22 +00:00
|
|
|
if ret := PdhGetCounterTimeBase(counterHandle, &counter.Frequency); ret != ErrorSuccess {
|
2024-09-10 22:34:10 +00:00
|
|
|
return nil, fmt.Errorf("PdhGetCounterTimeBase: %w", NewPdhError(ret))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 12:31:44 +00:00
|
|
|
collector.counters[counterName] = counter
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(collector.counters) == 0 {
|
|
|
|
return nil, errors.New("no counters configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := collector.Collect(); err != nil {
|
2024-11-19 22:36:37 +00:00
|
|
|
return collector, fmt.Errorf("failed to collect initial data: %w", err)
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return collector, nil
|
|
|
|
}
|
|
|
|
|
2024-10-03 12:31:44 +00:00
|
|
|
func (c *Collector) Describe() map[string]string {
|
|
|
|
desc := make(map[string]string, len(c.counters))
|
|
|
|
|
|
|
|
for _, counter := range c.counters {
|
|
|
|
desc[counter.Name] = counter.Desc
|
|
|
|
}
|
|
|
|
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
func (c *Collector) Collect() (map[string]map[string]CounterValues, error) {
|
2024-09-10 22:34:10 +00:00
|
|
|
if len(c.counters) == 0 {
|
2024-11-17 20:51:12 +00:00
|
|
|
return map[string]map[string]CounterValues{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
|
|
|
if c.handle == 0 {
|
|
|
|
return map[string]map[string]CounterValues{}, nil
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ret := PdhCollectQueryData(c.handle); ret != ErrorSuccess {
|
|
|
|
return nil, fmt.Errorf("failed to collect query data: %w", NewPdhError(ret))
|
|
|
|
}
|
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
var data map[string]map[string]CounterValues
|
2024-09-10 22:34:10 +00:00
|
|
|
|
|
|
|
for _, counter := range c.counters {
|
|
|
|
for _, instance := range counter.Instances {
|
|
|
|
// Get the info with the current buffer size
|
|
|
|
var itemCount uint32
|
|
|
|
|
|
|
|
// Get the info with the current buffer size
|
|
|
|
bufLen := uint32(0)
|
|
|
|
|
|
|
|
ret := PdhGetRawCounterArray(instance, &bufLen, &itemCount, nil)
|
|
|
|
if ret != PdhMoreData {
|
|
|
|
return nil, fmt.Errorf("PdhGetRawCounterArray: %w", NewPdhError(ret))
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, bufLen)
|
|
|
|
|
|
|
|
ret = PdhGetRawCounterArray(instance, &bufLen, &itemCount, &buf[0])
|
|
|
|
if ret != ErrorSuccess {
|
|
|
|
if err := NewPdhError(ret); !isKnownCounterDataError(err) {
|
|
|
|
return nil, fmt.Errorf("PdhGetRawCounterArray: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-11-13 23:06:22 +00:00
|
|
|
items := unsafe.Slice((*PdhRawCounterItem)(unsafe.Pointer(&buf[0])), itemCount)
|
2024-09-10 22:34:10 +00:00
|
|
|
|
|
|
|
if data == nil {
|
2024-11-17 20:51:12 +00:00
|
|
|
data = make(map[string]map[string]CounterValues, itemCount)
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var metricType prometheus.ValueType
|
2024-11-17 20:51:12 +00:00
|
|
|
if val, ok := SupportedCounterTypes[counter.Type]; ok {
|
2024-09-10 22:34:10 +00:00
|
|
|
metricType = val
|
|
|
|
} else {
|
|
|
|
metricType = prometheus.GaugeValue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
if item.RawValue.CStatus == PdhCstatusValidData || item.RawValue.CStatus == PdhCstatusNewData {
|
|
|
|
instanceName := windows.UTF16PtrToString(item.SzName)
|
2024-11-14 21:39:59 +00:00
|
|
|
if strings.HasSuffix(instanceName, "_Total") && !c.totalCounterRequested {
|
2024-09-10 22:34:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-05 19:33:40 +00:00
|
|
|
if instanceName == "" || instanceName == "*" {
|
2024-11-17 20:51:12 +00:00
|
|
|
instanceName = EmptyInstance
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := data[instanceName]; !ok {
|
2024-11-17 20:51:12 +00:00
|
|
|
data[instanceName] = make(map[string]CounterValues, len(c.counters))
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
values := CounterValues{
|
2024-09-10 22:34:10 +00:00
|
|
|
Type: metricType,
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a workaround for the issue with the elapsed time counter type.
|
|
|
|
// Source: https://github.com/prometheus-community/windows_exporter/pull/335/files#diff-d5d2528f559ba2648c2866aec34b1eaa5c094dedb52bd0ff22aa5eb83226bd8dR76-R83
|
2024-09-13 20:12:10 +00:00
|
|
|
// Ref: https://learn.microsoft.com/en-us/windows/win32/perfctrs/calculating-counter-values
|
2024-09-10 22:34:10 +00:00
|
|
|
|
|
|
|
switch counter.Type {
|
2024-11-17 20:51:12 +00:00
|
|
|
case PERF_ELAPSED_TIME:
|
|
|
|
values.FirstValue = float64((item.RawValue.FirstValue - WindowsEpoch) / counter.Frequency)
|
|
|
|
case PERF_100NSEC_TIMER, PERF_PRECISION_100NS_TIMER:
|
|
|
|
values.FirstValue = float64(item.RawValue.FirstValue) * TicksToSecondScaleFactor
|
2024-11-19 20:35:32 +00:00
|
|
|
case PERF_AVERAGE_BULK, PERF_RAW_FRACTION:
|
2024-09-10 22:34:10 +00:00
|
|
|
values.FirstValue = float64(item.RawValue.FirstValue)
|
|
|
|
values.SecondValue = float64(item.RawValue.SecondValue)
|
2024-11-13 23:06:22 +00:00
|
|
|
default:
|
|
|
|
values.FirstValue = float64(item.RawValue.FirstValue)
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data[instanceName][counter.Name] = values
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector) Close() {
|
2024-11-17 20:51:12 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
PdhCloseQuery(c.handle)
|
2024-11-17 20:51:12 +00:00
|
|
|
|
|
|
|
c.handle = 0
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func formatCounterPath(object, instance, counterName string) string {
|
|
|
|
var counterPath string
|
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
if instance == EmptyInstance {
|
2024-09-10 22:34:10 +00:00
|
|
|
counterPath = fmt.Sprintf(`\%s\%s`, object, counterName)
|
|
|
|
} else {
|
|
|
|
counterPath = fmt.Sprintf(`\%s(%s)\%s`, object, instance, counterName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return counterPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func isKnownCounterDataError(err error) bool {
|
|
|
|
var pdhErr *Error
|
|
|
|
|
|
|
|
return errors.As(err, &pdhErr) && (pdhErr.ErrorCode == PdhInvalidData ||
|
|
|
|
pdhErr.ErrorCode == PdhCalcNegativeDenominator ||
|
|
|
|
pdhErr.ErrorCode == PdhCalcNegativeValue ||
|
|
|
|
pdhErr.ErrorCode == PdhCstatusInvalidData ||
|
|
|
|
pdhErr.ErrorCode == PdhCstatusNoInstance ||
|
|
|
|
pdhErr.ErrorCode == PdhNoData)
|
|
|
|
}
|