Skip to content

Commit

Permalink
perfdata: add generic perfdata collector
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 Sep 1, 2024
1 parent 02ab3b2 commit a8fcf3f
Show file tree
Hide file tree
Showing 31 changed files with 1,932 additions and 112 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ on:
- "tools/e2e-output.txt"
branches:
- master
- next
- main
pull_request:
paths:
- "go.mod"
Expand All @@ -21,6 +23,8 @@ on:
- "tools/e2e-output.txt"
branches:
- master
- next
- main

env:
PROMU_VER: '0.14.0'
Expand Down
46 changes: 11 additions & 35 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
linters:
enable-all: true
disable:
- containedctx
- contextcheck
- cyclop
- depguard
- dogsled
- dupl
- err113
- exhaustive
- exhaustruct
- exportloopref
- fatcontext
- funlen
- gochecknoglobals
- gocognit
- goconst
- gocyclo
- godox
- inamedparam
- ireturn
- lll
- mnd
- nlreturn
- noctx
- testpackage
- varnamelen
- wrapcheck
- wsl
- execinquery
- gomnd
- stylecheck
- maintidx
disable-all: true
enable:
- revive

linters-settings:
gosec:
Expand All @@ -52,7 +22,13 @@ linters-settings:
# Support string case: `camel`, `pascal`, `kebab`, `snake`, `upperSnake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`, `header`
json: camel
yaml: snake

forbidigo:
forbid:
- "^(fmt\\.Print(|f|ln)|print|println)$"
- p: "^syscall\\..*$"
msg: use golang.org/x/sys/windows instead of syscall
- p: "^windows\\.NewLazyDLL$"
msg: use NewLazySystemDLL instead NewLazyDLL
issues:
exclude:
- don't use underscores in Go names
Expand All @@ -65,4 +41,4 @@ issues:
- revive
- text: "don't use ALL_CAPS in Go names; use CamelCase"
linters:
- revive
- revive
114 changes: 114 additions & 0 deletions docs/collector.perfdata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Perfdata collector

The perfdata collector exposes any configured metric.

| | |
|---------------------|-------------------------|
| Metric name prefix | `perfdata` |
| Data source | Performance Data Helper |
| Enabled by default? | No |

## Flags


### `--collector.perfdata.objects`

Objects is a list of objects to collect metrics from. The value takes the form of a JSON array of strings. YAML is also supported.

The collector supports only english named counter. Localized counter-names are not supported.

#### Schema

YAML:
```yaml
- object: "Processor Information"
instances: ["*"]
instance_label: "core"
counters:
"% Processor Time": {}
- object: "Memory"
counters:
"Cache Faults/sec":
type: "counter"
```
JSON:
```json
[
{"object":"Processor Information","instance_label": "core","instances":["*"],"counters": {"% Processor Time": {}}},
{"object":"Memory","counters": {"Cache Faults/sec": {"type": "counter"}}}
]
```

#### name

ObjectName is the Object to query for, like Processor, DirectoryServices, LogicalDisk or similar.

The collector supports only english named counter. Localized counter-names are not supported.

#### instances

The instances key (this is an array) declares the instances of a counter you would like returned, it can be one or more values.

Example: Instances = `["C:","D:","E:"]`

This will return only for the instances C:, D: and E: where relevant. To get all instances of a Counter, use `["*"]` only.

Some Objects like `Memory` do not have instances to select from at all. In this case, the `instances` key can be omitted.

#### counters

The Counters key (this is an object) declares the counters of the ObjectName you would like returned, it can also be one or more values.

Example: Counters = `{"% Idle Time": {}, "% Disk Read Time": {}, "% Disk Write Time": {}}`

This must be specified for every counter you want the results. Wildcards are not supported.

#### counters Sub-Schema

##### type

This key is optional. It indicates the type of the counter. The value can be `counter` or `gauge`.
If not specified, the windows_exporter will try to determine the type based on the counter type.

### Example

```
# HELP windows_perfdata_memory_cache_faults_sec
# TYPE windows_perfdata_memory_cache_faults_sec counter
windows_perfdata_memory_cache_faults_sec 2.369977e+07
# HELP windows_perfdata_processor_information__processor_time
# TYPE windows_perfdata_processor_information__processor_time gauge
windows_perfdata_processor_information__processor_time{instance="0,0"} 1.7259640625e+11
windows_perfdata_processor_information__processor_time{instance="0,1"} 1.7576796875e+11
windows_perfdata_processor_information__processor_time{instance="0,10"} 2.2704234375e+11
windows_perfdata_processor_information__processor_time{instance="0,11"} 2.3069296875e+11
windows_perfdata_processor_information__processor_time{instance="0,12"} 2.3302265625e+11
windows_perfdata_processor_information__processor_time{instance="0,13"} 2.32851875e+11
windows_perfdata_processor_information__processor_time{instance="0,14"} 2.3282421875e+11
windows_perfdata_processor_information__processor_time{instance="0,15"} 2.3271234375e+11
windows_perfdata_processor_information__processor_time{instance="0,16"} 2.329590625e+11
windows_perfdata_processor_information__processor_time{instance="0,17"} 2.32800625e+11
windows_perfdata_processor_information__processor_time{instance="0,18"} 2.3194359375e+11
windows_perfdata_processor_information__processor_time{instance="0,19"} 2.32380625e+11
windows_perfdata_processor_information__processor_time{instance="0,2"} 1.954765625e+11
windows_perfdata_processor_information__processor_time{instance="0,20"} 2.3259765625e+11
windows_perfdata_processor_information__processor_time{instance="0,21"} 2.3268515625e+11
windows_perfdata_processor_information__processor_time{instance="0,22"} 2.3301765625e+11
windows_perfdata_processor_information__processor_time{instance="0,23"} 2.3264328125e+11
windows_perfdata_processor_information__processor_time{instance="0,3"} 1.94745625e+11
windows_perfdata_processor_information__processor_time{instance="0,4"} 2.2011453125e+11
windows_perfdata_processor_information__processor_time{instance="0,5"} 2.27244375e+11
windows_perfdata_processor_information__processor_time{instance="0,6"} 2.25501875e+11
windows_perfdata_processor_information__processor_time{instance="0,7"} 2.2995265625e+11
windows_perfdata_processor_information__processor_time{instance="0,8"} 2.2929890625e+11
windows_perfdata_processor_information__processor_time{instance="0,9"} 2.313540625e+11
windows_perfdata_processor_information__processor_time{instance="0,_Total"} 2.23009459635e+11
```

## Metrics

The perfdata collector returns metrics based on the user configuration.
The metrics are named based on the object name and the counter name.
The instance name is added as a label to the metric.
2 changes: 2 additions & 0 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"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/perfdata"
"github.com/prometheus-community/windows_exporter/pkg/collector/physical_disk"
"github.com/prometheus-community/windows_exporter/pkg/collector/printer"
"github.com/prometheus-community/windows_exporter/pkg/collector/process"
Expand Down Expand Up @@ -117,6 +118,7 @@ func NewWithConfig(config Config) *Collectors {
collectors[netframework_clrsecurity.Name] = netframework_clrsecurity.New(&config.NetframeworkClrsecurity)
collectors[nps.Name] = nps.New(&config.Nps)
collectors[os.Name] = os.New(&config.Os)
collectors[perfdata.Name] = perfdata.New(&config.PerfData)
collectors[physical_disk.Name] = physical_disk.New(&config.PhysicalDisk)
collectors[printer.Name] = printer.New(&config.Printer)
collectors[process.Name] = process.New(&config.Process)
Expand Down
3 changes: 3 additions & 0 deletions pkg/collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"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/perfdata"
"github.com/prometheus-community/windows_exporter/pkg/collector/physical_disk"
"github.com/prometheus-community/windows_exporter/pkg/collector/printer"
"github.com/prometheus-community/windows_exporter/pkg/collector/process"
Expand Down Expand Up @@ -90,6 +91,7 @@ type Config struct {
NetframeworkClrsecurity netframework_clrsecurity.Config `yaml:"netframework_clrsecurity"`
Nps nps.Config `yaml:"nps"`
Os os.Config `yaml:"os"`
PerfData perfdata.Config `yaml:"perf_data"`
PhysicalDisk physical_disk.Config `yaml:"physical_disk"`
Printer printer.Config `yaml:"printer"`
Process process.Config `yaml:"process"`
Expand Down Expand Up @@ -148,6 +150,7 @@ var ConfigDefaults = Config{
NetframeworkClrsecurity: netframework_clrsecurity.ConfigDefaults,
Nps: nps.ConfigDefaults,
Os: os.ConfigDefaults,
PerfData: perfdata.ConfigDefaults,
PhysicalDisk: physical_disk.ConfigDefaults,
Printer: printer.ConfigDefaults,
Process: process.ConfigDefaults,
Expand Down
2 changes: 2 additions & 0 deletions pkg/collector/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"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/perfdata"
"github.com/prometheus-community/windows_exporter/pkg/collector/physical_disk"
"github.com/prometheus-community/windows_exporter/pkg/collector/printer"
"github.com/prometheus-community/windows_exporter/pkg/collector/process"
Expand Down Expand Up @@ -98,6 +99,7 @@ var BuildersWithFlags = map[string]BuilderWithFlags[Collector]{
netframework_clrsecurity.Name: NewBuilderWithFlags(netframework_clrsecurity.NewWithFlags),
nps.Name: NewBuilderWithFlags(nps.NewWithFlags),
os.Name: NewBuilderWithFlags(os.NewWithFlags),
perfdata.Name: NewBuilderWithFlags(perfdata.NewWithFlags),
physical_disk.Name: NewBuilderWithFlags(physical_disk.NewWithFlags),
printer.Name: NewBuilderWithFlags(printer.NewWithFlags),
process.Name: NewBuilderWithFlags(process.NewWithFlags),
Expand Down
4 changes: 2 additions & 2 deletions pkg/collector/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"strconv"
"strings"
"syscall"
"time"

"github.com/alecthomas/kingpin/v2"
Expand All @@ -22,6 +21,7 @@ import (
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/yusufpapurcu/wmi"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)

Expand Down Expand Up @@ -309,7 +309,7 @@ func (c *Collector) collectTime(ch chan<- prometheus.Metric) error {
}

// timeZoneKeyName contains the english name of the timezone.
timezoneName := syscall.UTF16ToString(timeZoneInfo.TimeZoneKeyName[:])
timezoneName := windows.UTF16ToString(timeZoneInfo.TimeZoneKeyName[:])

ch <- prometheus.MustNewConstMetric(
c.time,
Expand Down
Loading

0 comments on commit a8fcf3f

Please sign in to comment.