From f712c07c3825ef51398aec94f7253d732384ba1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Wed, 11 Sep 2024 00:34:10 +0200 Subject: [PATCH] chore: release 0.29.0.rc0 (#1600) --- .editorconfig | 14 +- .github/workflows/lint.yml | 6 +- .golangci.yaml | 51 +- .promu.yml | 33 +- README.md | 4 +- config.yaml | 2 +- docs/collector.cpu.md | 36 +- docs/collector.cs.md | 4 + docs/collector.memory.md | 105 +-- docs/collector.os.md | 62 +- docs/collector.perfdata.md | 114 ++++ docs/collector.service.md | 154 ++--- docs/collector.system.md | 28 +- docs/collector.time.md | 18 +- exporter.go | 280 ++++---- go.mod | 45 +- go.sum | 62 +- pkg/collector/ad/ad.go | 20 +- pkg/collector/adcs/adcs.go | 24 +- pkg/collector/adfs/adfs.go | 15 +- pkg/collector/cache/cache.go | 25 +- pkg/collector/collector.go | 58 +- pkg/collector/config.go | 3 + pkg/collector/container/container.go | 58 +- pkg/collector/cpu/cpu.go | 157 +---- pkg/collector/cpu_info/cpu_info.go | 20 +- pkg/collector/cs/cs.go | 44 +- pkg/collector/dfsr/dfsr.go | 44 +- pkg/collector/dhcp/dhcp.go | 16 +- pkg/collector/diskdrive/diskdrive.go | 20 +- pkg/collector/dns/dns.go | 21 +- pkg/collector/exchange/exchange.go | 92 ++- pkg/collector/fsrmquota/fsrmquota.go | 22 +- pkg/collector/handler.go | 85 --- pkg/collector/hyperv/hyperv.go | 99 ++- pkg/collector/iis/iis.go | 87 ++- pkg/collector/license/license.go | 20 +- pkg/collector/logical_disk/logical_disk.go | 35 +- pkg/collector/logon/logon.go | 22 +- pkg/collector/map.go | 8 +- pkg/collector/memory/memory.go | 96 ++- pkg/collector/mscluster/mscluster.go | 10 +- pkg/collector/msmq/msmq.go | 24 +- pkg/collector/mssql/mssql.go | 122 ++-- pkg/collector/net/net.go | 24 +- .../netframework_clrexceptions.go | 20 +- .../netframework_clrinterop.go | 20 +- .../netframework_clrjit.go | 21 +- .../netframework_clrloading.go | 20 +- .../netframework_clrlocksandthreads.go | 20 +- .../netframework_clrmemory.go | 20 +- .../netframework_clrremoting.go | 20 +- .../netframework_clrsecurity.go | 20 +- pkg/collector/nps/nps.go | 22 +- pkg/collector/os/os.go | 413 ++++++++---- pkg/collector/perfdata/perfdata.go | 181 +++++ .../perfdata/perfdata_collector_test.go | 87 +++ pkg/collector/perfdata/perfdata_test.go | 18 + pkg/collector/perfdata/types.go | 18 + pkg/collector/physical_disk/physical_disk.go | 25 +- pkg/collector/printer/printer.go | 25 +- pkg/collector/process/process.go | 46 +- pkg/collector/prometheus.go | 243 ++++--- pkg/collector/remote_fx/remote_fx.go | 37 +- .../scheduled_task/scheduled_task.go | 29 +- pkg/collector/service/service.go | 629 ++++++++--------- pkg/collector/smb/smb.go | 26 +- pkg/collector/smbclient/smbclient.go | 26 +- pkg/collector/smtp/smtp.go | 30 +- pkg/collector/system/system.go | 63 +- pkg/collector/tcp/const.go | 16 + pkg/collector/tcp/tcp.go | 117 ++-- .../teradici_pcoip/teradici_pcoip.go | 54 +- .../terminal_services/terminal_services.go | 59 +- pkg/collector/textfile/textfile.go | 96 ++- pkg/collector/textfile/textfile_test.go | 3 + pkg/collector/textfile/textfile_test_test.go | 19 +- pkg/collector/thermalzone/thermalzone.go | 20 +- pkg/collector/time/time.go | 94 ++- pkg/collector/types.go | 19 +- pkg/collector/vmware/vmware.go | 28 +- pkg/collector/vmware_blast/vmware_blast.go | 91 ++- pkg/config/config.go | 38 +- pkg/config/flatten.go | 6 + pkg/config/flatten_test.go | 2 + pkg/headers/kernel32/kernel32.go | 14 +- pkg/headers/netapi32/netapi32.go | 4 + pkg/headers/sysinfoapi/sysinfoapi.go | 6 + pkg/headers/wtsapi32/wtsapi32.go | 24 +- pkg/httphandler/health.go | 19 + pkg/httphandler/httphandler.go | 192 ++++++ pkg/httphandler/version.go | 45 ++ pkg/initiate/initiate.go | 4 + pkg/log/eventlog/eventlog.go | 118 +--- pkg/log/flag/flag.go | 16 +- pkg/log/logger.go | 62 +- pkg/perfdata/collector.go | 236 +++++++ pkg/perfdata/collector_bench_test.go | 49 ++ pkg/perfdata/collector_test.go | 86 +++ pkg/perfdata/const.go | 78 +++ pkg/perfdata/error.go | 18 + pkg/perfdata/pdh.go | 638 ++++++++++++++++++ pkg/perfdata/pdh_amd64.go | 143 ++++ pkg/perfdata/pdh_arm64.go | 143 ++++ pkg/perflib/nametable.go | 4 + pkg/perflib/perflib.go | 18 +- pkg/perflib/raw_types.go | 5 +- pkg/perflib/unmarshal.go | 19 +- pkg/perflib/utf16.go | 8 +- pkg/perflib/utils.go | 1 + pkg/perflib/utils_test.go | 9 +- pkg/testutils/testutils.go | 6 +- pkg/types/const.go | 2 +- pkg/utils/collector.go | 3 + pkg/utils/collector_test.go | 3 + pkg/utils/utils.go | 5 +- tools/e2e-output.txt | 211 ++++-- tools/end-to-end-test.ps1 | 32 +- tools/promtool.ps1 | 3 +- 119 files changed, 5056 insertions(+), 2198 deletions(-) create mode 100644 docs/collector.perfdata.md delete mode 100644 pkg/collector/handler.go create mode 100644 pkg/collector/perfdata/perfdata.go create mode 100644 pkg/collector/perfdata/perfdata_collector_test.go create mode 100644 pkg/collector/perfdata/perfdata_test.go create mode 100644 pkg/collector/perfdata/types.go create mode 100644 pkg/collector/tcp/const.go create mode 100644 pkg/httphandler/health.go create mode 100644 pkg/httphandler/httphandler.go create mode 100644 pkg/httphandler/version.go create mode 100644 pkg/perfdata/collector.go create mode 100644 pkg/perfdata/collector_bench_test.go create mode 100644 pkg/perfdata/collector_test.go create mode 100644 pkg/perfdata/const.go create mode 100644 pkg/perfdata/error.go create mode 100644 pkg/perfdata/pdh.go create mode 100644 pkg/perfdata/pdh_amd64.go create mode 100644 pkg/perfdata/pdh_arm64.go diff --git a/.editorconfig b/.editorconfig index dd4715ef9..688f63734 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,16 @@ root = true -[*.wxs] +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +tab_width = 4 +[*.{json,wxs,xml}] indent_style = space -indent_size = 4 \ No newline at end of file +indent_size = 4 + +[*.{yml,yaml}] +indent_style = space +indent_size = 2 \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 627e2b12a..d0d752a76 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -12,6 +12,8 @@ on: - "tools/e2e-output.txt" branches: - master + - next + - main pull_request: paths: - "go.mod" @@ -21,6 +23,8 @@ on: - "tools/e2e-output.txt" branches: - master + - next + - main env: VERSION_PROMU: '0.14.0' @@ -101,4 +105,4 @@ jobs: uses: golangci/golangci-lint-action@v6 with: version: v1.60 - args: "--timeout=5m --max-same-issues=0" \ No newline at end of file + args: "--max-same-issues=0" \ No newline at end of file diff --git a/.golangci.yaml b/.golangci.yaml index 9aaffccfc..65a996417 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,13 +1,11 @@ linters: enable-all: true disable: - - containedctx - - contextcheck - cyclop - depguard - - dogsled - dupl - err113 + - execinquery - exhaustive - exhaustruct - exportloopref @@ -17,21 +15,16 @@ linters: - gocognit - goconst - gocyclo - - godox - - inamedparam - - ireturn + - gomnd - lll + - maintidx - mnd - - nlreturn - - noctx - testpackage - varnamelen - wrapcheck - - wsl - - execinquery - - gomnd - - stylecheck - - maintidx + +run: + timeout: 5m linters-settings: gosec: @@ -52,7 +45,33 @@ linters-settings: # Support string case: `camel`, `pascal`, `kebab`, `snake`, `upperSnake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`, `header` json: camel yaml: snake - + gomoddirectives: + replace-allow-list: + - github.com/prometheus/common # https://github.com/prometheus/common/pull/694 + 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 + sloglint: + no-mixed-args: true + kv-only: false + attr-only: true + no-global: "all" + context: "scope" + static-msg: false + no-raw-keys: false + key-naming-case: snake + forbidden-keys: + - time + - level + - msg + - source + args-on-sep-lines: true + stylecheck: + checks: ["all", "-ST1003"] issues: exclude: - don't use underscores in Go names @@ -66,3 +85,7 @@ issues: - text: "don't use ALL_CAPS in Go names; use CamelCase" linters: - revive + - path: pkg/perflib/ + linters: + - godox + - stylecheck diff --git a/.promu.yml b/.promu.yml index 67a904239..80678c915 100644 --- a/.promu.yml +++ b/.promu.yml @@ -1,19 +1,24 @@ go: - version: 1.20 + # Whenever the Go version is updated here, + # .github/workflows should also be updated. + version: 1.23 repository: - path: github.com/prometheus-community/windows_exporter + path: github.com/prometheus-community/windows_exporter build: - binaries: - - name: windows_exporter - ldflags: | - -X github.com/prometheus/common/version.Version={{.Version}} - -X github.com/prometheus/common/version.Revision={{.Revision}} - -X github.com/prometheus/common/version.Branch={{.Branch}} - -X github.com/prometheus/common/version.BuildUser={{user}}@{{host}} - -X github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}} + binaries: + - name: windows_exporter + tags: + all: + - trimpath + ldflags: | + -X github.com/prometheus/common/version.Version={{.Version}} + -X github.com/prometheus/common/version.Revision={{.Revision}} + -X github.com/prometheus/common/version.Branch={{.Branch}} + -X github.com/prometheus/common/version.BuildUser={{user}}@{{host}} + -X github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}} tarball: - files: - - LICENSE + files: + - LICENSE crossbuild: - platforms: - - windows + platforms: + - windows diff --git a/README.md b/README.md index 81df33156..6b23bb04f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Name | Description | Enabled by default [license](docs/collector.license.md) | Windows license status | [logical_disk](docs/collector.logical_disk.md) | Logical disks, disk I/O | ✓ [logon](docs/collector.logon.md) | User logon sessions | -[memory](docs/collector.memory.md) | Memory usage metrics | +[memory](docs/collector.memory.md) | Memory usage metrics | ✓ [mscluster](docs/collector.mscluster.md) | MSCluster metrics | [msmq](docs/collector.msmq.md) | MSMQ queues | [mssql](docs/collector.mssql.md) | [SQL Server Performance Objects](https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/use-sql-server-objects#SQLServerPOs) metrics | @@ -218,7 +218,7 @@ If you need to skip TLS verification, you can use the `--config.file.insecure-sk ```yaml collectors: - enabled: cpu,cs,net,service + enabled: cpu,net,service collector: service: services-where: "Name='windows_exporter'" diff --git a/config.yaml b/config.yaml index bbb26da76..f837deff8 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,5 @@ collectors: - enabled: cpu,cpu_info,cs,exchange,iis,logical_disk,logon,memory,net,os,process,remote_fx,service,system,tcp,time,terminal_services,textfile + enabled: cpu,cpu_info,exchange,iis,logical_disk,logon,memory,net,os,process,remote_fx,service,system,tcp,time,terminal_services,textfile collector: service: services-where: "Name='windows_exporter'" diff --git a/docs/collector.cpu.md b/docs/collector.cpu.md index f5a5d19fd..f031579ba 100644 --- a/docs/collector.cpu.md +++ b/docs/collector.cpu.md @@ -16,26 +16,22 @@ None ## Metrics These metrics are available on all versions of Windows: -Name | Description | Type | Labels ------|-------------|------|------- -`windows_cpu_cstate_seconds_total` | Time spent in low-power idle states | counter | `core`, `state` -`windows_cpu_time_total` | Time that processor spent in different modes (dpc, idle, interrupt, privileged, user) | counter | `core`, `mode` -`windows_cpu_interrupts_total` | Total number of received and serviced hardware interrupts | counter | `core` -`windows_cpu_dpcs_total` | Total number of received and serviced deferred procedure calls (DPCs) | counter | `core` - -These metrics are only exposed on Windows Server 2008R2 and later: - -Name | Description | Type | Labels ------|-------------|------|------- -`windows_cpu_clock_interrupts_total` | Total number of received and serviced clock tick interrupts | counter | `core` -`windows_cpu_idle_break_events_total` | Total number of time processor was woken from idle | counter | `core` -`windows_cpu_parking_status` | Parking Status represents whether a processor is parked or not | gauge | `core` -`windows_cpu_core_frequency_mhz` | Core frequency in megahertz | gauge | `core` -`windows_cpu_processor_performance_total` | Processor Performance is the number of CPU cycles executing instructions by each core; it is believed to be similar to the value that the APERF MSR would show, were it exposed | counter | `core` -`windows_cpu_processor_mperf_total` | Processor MPerf Total is proportioanl to the number of TSC ticks each core has accumulated while executing instructions. Due to the manner in which it is presented, it should be scaled by 1e2 to properly line up with Processor Performance Total. As above, it is believed to be closely related to the MPERF MSR. | counter | `core` -`windows_cpu_processor_rtc_total` | RTC total is assumed to represent the 64Hz tick rate in Windows. It is not by itself useful, but can be used with `windows_cpu_processor_utility_total` to more accurately measure CPU utilisation than with `windows_cpu_time_total` | counter | `core` -`windows_cpu_processor_utility_total` | Processor Utility Total is a newer, more accurate measure of CPU utilization, in particular handling modern CPUs with variant CPU frequencies. The rate of this counter divided by the rate of `windows_cpu_processor_rtc_total` should provide an accurate view of CPU utilisation on modern systems, as observed in Task Manager. | counter | `core` -`windows_cpu_processor_privileged_utility_total` | Processor Privileged Utility Total, when used in a similar fashion to `windows_cpu_processor_utility_total` will show the portion of CPU utilization which is happening in privileged mode. | counter | `core` +| Name | Description | Type | Labels | +|--------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|-----------------| +| `windows_cpu_logical_processor` | Number of installed logical processors | counter | `core`, `state` | +| `windows_cpu_cstate_seconds_total` | Time spent in low-power idle states | counter | `core`, `state` | +| `windows_cpu_time_total` | Time that processor spent in different modes (dpc, idle, interrupt, privileged, user) | counter | `core`, `mode` | +| `windows_cpu_interrupts_total` | Total number of received and serviced hardware interrupts | counter | `core` | +| `windows_cpu_dpcs_total` | Total number of received and serviced deferred procedure calls (DPCs) | counter | `core` | +| `windows_cpu_clock_interrupts_total` | Total number of received and serviced clock tick interrupts | counter | `core` | +| `windows_cpu_idle_break_events_total` | Total number of time processor was woken from idle | counter | `core` | +| `windows_cpu_parking_status` | Parking Status represents whether a processor is parked or not | gauge | `core` | +| `windows_cpu_core_frequency_mhz` | Core frequency in megahertz | gauge | `core` | +| `windows_cpu_processor_performance_total` | Processor Performance is the number of CPU cycles executing instructions by each core; it is believed to be similar to the value that the APERF MSR would show, were it exposed | counter | `core` | +| `windows_cpu_processor_mperf_total` | Processor MPerf Total is proportioanl to the number of TSC ticks each core has accumulated while executing instructions. Due to the manner in which it is presented, it should be scaled by 1e2 to properly line up with Processor Performance Total. As above, it is believed to be closely related to the MPERF MSR. | counter | `core` | +| `windows_cpu_processor_rtc_total` | RTC total is assumed to represent the 64Hz tick rate in Windows. It is not by itself useful, but can be used with `windows_cpu_processor_utility_total` to more accurately measure CPU utilisation than with `windows_cpu_time_total` | counter | `core` | +| `windows_cpu_processor_utility_total` | Processor Utility Total is a newer, more accurate measure of CPU utilization, in particular handling modern CPUs with variant CPU frequencies. The rate of this counter divided by the rate of `windows_cpu_processor_rtc_total` should provide an accurate view of CPU utilisation on modern systems, as observed in Task Manager. | counter | `core` | +| `windows_cpu_processor_privileged_utility_total` | Processor Privileged Utility Total, when used in a similar fashion to `windows_cpu_processor_utility_total` will show the portion of CPU utilization which is happening in privileged mode. | counter | `core` | ### Example metric Show frequency of host CPU cores diff --git a/docs/collector.cs.md b/docs/collector.cs.md index ffec191b4..33465407f 100644 --- a/docs/collector.cs.md +++ b/docs/collector.cs.md @@ -1,5 +1,9 @@ # cs collector +> [!CAUTION] +> This collector is deprecated and will be removed in a future release. +> See https://github.com/prometheus-community/windows_exporter/pull/1596 for more information. + The cs collector exposes metrics detailing the hardware of the computer system ||| diff --git a/docs/collector.memory.md b/docs/collector.memory.md index 18a7b2686..f77ad7e3a 100644 --- a/docs/collector.memory.md +++ b/docs/collector.memory.md @@ -5,9 +5,9 @@ The memory collector exposes metrics about system memory usage ||| -|- Metric name prefix | `memory` -Data source | Perflib -Classes | `Win32_PerfRawData_PerfOS_Memory` -Enabled by default? | No +Data source | Performance Counters +Classes | - +Enabled by default? | Yes ## Flags @@ -15,46 +15,73 @@ None ## Metrics -Name | Description | Type | Labels ------|-------------|------|------- -`windows_memory_available_bytes` | The amount of physical memory immediately available for allocation to a process or for system use. It is equal to the sum of memory assigned to the standby (cached), free and zero page lists | gauge | None -`windows_memory_cache_bytes` | Number of bytes currently being used by the file system cache | gauge | None -`windows_memory_cache_bytes_peak` | Maximum number of CacheBytes after the system was last restarted | gauge | None -`windows_memory_cache_faults_total` | Number of faults which occur when a page sought in the file system cache is not found there and must be retrieved from elsewhere in memory (soft fault) or from disk (hard fault) | counter | None -`windows_memory_commit_limit` | Amount of virtual memory, in bytes, that can be committed without having to extend the paging file(s) | gauge | None -`windows_memory_committed_bytes` | Amount of committed virtual memory, in bytes | gauge | None -`windows_memory_demand_zero_faults_total` | The number of zeroed pages required to satisfy faults. Zeroed pages, pages emptied of previously stored data and filled with zeros, are a security feature of Windows that prevent processes from seeing data stored by earlier processes that used the memory space | counter | None -`windows_memory_free_and_zero_page_list_bytes` | The amount of physical memory, in bytes, that is assigned to the free and zero page lists. This memory does not contain cached data. It is immediately available for allocation to a process or for system use | gauge | None -`windows_memory_free_system_page_table_entries` | Number of page table entries not being used by the system | gauge | None -`windows_memory_modified_page_list_bytes` | The amount of physical memory, in bytes, that is assigned to the modified page list. This memory contains cached data and code that is not actively in use by processes, the system and the system cache. This memory needs to be written out before it will be available for allocation to a process or for system use | gauge | None -`windows_memory_page_faults_total` | Overall rate at which faulted pages are handled by the processor | counter | None -`windows_memory_swap_page_reads_total` | Number of disk page reads (a single read operation reading several pages is still only counted once) | counter | None -`windows_memory_swap_pages_read_total` | Number of pages read across all page reads (ie counting all pages read even if they are read in a single operation) | counter | None -`windows_memory_swap_pages_written_total` | Number of pages written across all page writes (ie counting all pages written even if they are written in a single operation) | counter | None -`windows_memory_swap_page_operations_total` | Total number of swap page read and writes (PagesPersec) | counter | None -`windows_memory_swap_page_writes_total` | Number of disk page writes (a single write operation writing several pages is still only counted once) | counter | None -`windows_memory_pool_nonpaged_allocs_total` | The number of calls to allocate space in the nonpaged pool. The nonpaged pool is an area of system memory area for objects that cannot be written to disk, and must remain in physical memory as long as they are allocated | counter | None -`windows_memory_pool_nonpaged_bytes` | Number of bytes in the non-paged pool, an area of the system virtual memory that is used for objects that cannot be written to disk, but must remain in physical memory as long as they are allocated | gauge | None -`windows_memory_pool_paged_allocs_total` | Number of calls to allocate space in the paged pool, regardless of the amount of space allocated in each call | counter | None -`windows_memory_pool_paged_bytes` | Number of bytes in the paged pool | gauge | None -`windows_memory_pool_paged_resident_bytes` | The size, in bytes, of the portion of the paged pool that is currently resident and active in physical memory. The paged pool is an area of the system virtual memory that is used for objects that can be written to disk when they are not being used | gauge | None -`windows_memory_standby_cache_core_bytes` | The amount of physical memory, in bytes, that is assigned to the core standby cache page lists. This memory contains cached data and code that is not actively in use by processes, the system and the system cache. It is immediately available for allocation to a process or for system use. If the system runs out of available free and zero memory, memory on lower priority standby cache page lists will be repurposed before memory on higher priority standby cache page lists | gauge | None -`windows_memory_standby_cache_normal_priority_bytes` | The amount of physical memory, in bytes, that is assigned to the normal priority standby cache page lists. This memory contains cached data and code that is not actively in use by processes, the system and the system cache. It is immediately available for allocation to a process or for system use. If the system runs out of available free and zero memory, memory on lower priority standby cache page lists will be repurposed before memory on higher priority standby cache page lists | gauge | None -`windows_memory_standby_cache_reserve_bytes` | The amount of physical memory, in bytes, that is assigned to the reserve standby cache page lists. This memory contains cached data and code that is not actively in use by processes, the system and the system cache. It is immediately available for allocation to a process or for system use. If the system runs out of available free and zero memory, memory on lower priority standby cache page lists will be repurposed before memory on higher priority standby cache page lists | gauge | None -`windows_memory_system_cache_resident_bytes` | The size, in bytes, of the portion of the system file cache which is currently resident and active in physical memory | gauge | None -`windows_memory_system_code_resident_bytes` | The size, in bytes, of the pageable operating system code that is currently resident and active in physical memory. This value is a component of Memory\\System Code Total Bytes. Memory\\System Code Resident Bytes (and Memory\\System Code Total Bytes) does not include code that must remain in physical memory and cannot be written to disk | gauge | None -`windows_memory_system_code_total_bytes` | The size, in bytes, of the pageable operating system code currently mapped into the system virtual address space. This value is calculated by summing the bytes in Ntoskrnl.exe, Hal.dll, the boot drivers, and file systems loaded by Ntldr/osloader. This counter does not include code that must remain in physical memory and cannot be written to disk | gauge | None -`windows_memory_system_driver_resident_bytes` | The size, in bytes, of the pageable physical memory being used by device drivers. It is the working set (physical memory area) of the drivers. This value is a component of Memory\\System Driver Total Bytes, which also includes driver memory that has been written to disk. Neither Memory\\System Driver Resident Bytes nor Memory\\System Driver Total Bytes includes memory that cannot be written to disk | gauge | None -`windows_memory_system_driver_total_bytes` | The size, in bytes, of the pageable virtual memory currently being used by device drivers. Pageable memory can be written to disk when it is not being used. It includes both physical memory (Memory\\System Driver Resident Bytes) and code and data paged to disk. It is a component of Memory\\System Code Total Bytes | gauge | None -`windows_memory_transition_faults_total` | Number of faults rate at which page faults are resolved by recovering pages that were being used by another process sharing the page, or were on the modified page list or the standby list, or were being written to disk at the time of the page fault. The pages were recovered without additional disk activity. Transition faults are counted in numbers of faults; because only one page is faulted in each operation, it is also equal to the number of pages faulted | counter | None -`windows_memory_transition_pages_repurposed_total` | Transition Pages RePurposed is the rate at which the number of transition cache pages were reused for a different purpose. These pages would have otherwise remained in the page cache to provide a (fast) soft fault (instead of retrieving it from backing store) in the event the page was accessed in the future | counter | None -`windows_memory_write_copies_total` | The number of page faults caused by attempting to write that were satisfied by copying the page from elsewhere in physical memory | counter | None +| Name | Description | Type | Labels | +|------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|--------| +| `windows_memory_available_bytes` | The amount of physical memory immediately available for allocation to a process or for system use. It is equal to the sum of memory assigned to the standby (cached), free and zero page lists | gauge | None | +| `windows_memory_cache_bytes` | Number of bytes currently being used by the file system cache | gauge | None | +| `windows_memory_cache_bytes_peak` | Maximum number of CacheBytes after the system was last restarted | gauge | None | +| `windows_memory_cache_faults_total` | Number of faults which occur when a page sought in the file system cache is not found there and must be retrieved from elsewhere in memory (soft fault) or from disk (hard fault) | counter | None | +| `windows_memory_commit_limit` | Amount of virtual memory, in bytes, that can be committed without having to extend the paging file(s) | gauge | None | +| `windows_memory_committed_bytes` | Amount of committed virtual memory, in bytes | gauge | None | +| `windows_memory_demand_zero_faults_total` | The number of zeroed pages required to satisfy faults. Zeroed pages, pages emptied of previously stored data and filled with zeros, are a security feature of Windows that prevent processes from seeing data stored by earlier processes that used the memory space | counter | None | +| `windows_memory_free_and_zero_page_list_bytes` | The amount of physical memory, in bytes, that is assigned to the free and zero page lists. This memory does not contain cached data. It is immediately available for allocation to a process or for system use | gauge | None | +| `windows_memory_free_system_page_table_entries` | Number of page table entries not being used by the system | gauge | None | +| `windows_memory_modified_page_list_bytes` | The amount of physical memory, in bytes, that is assigned to the modified page list. This memory contains cached data and code that is not actively in use by processes, the system and the system cache. This memory needs to be written out before it will be available for allocation to a process or for system use | gauge | None | +| `windows_memory_page_faults_total` | Overall rate at which faulted pages are handled by the processor | counter | None | +| `windows_memory_swap_page_reads_total` | Number of disk page reads (a single read operation reading several pages is still only counted once) | counter | None | +| `windows_memory_swap_pages_read_total` | Number of pages read across all page reads (ie counting all pages read even if they are read in a single operation) | counter | None | +| `windows_memory_swap_pages_written_total` | Number of pages written across all page writes (ie counting all pages written even if they are written in a single operation) | counter | None | +| `windows_memory_swap_page_operations_total` | Total number of swap page read and writes (PagesPersec) | counter | None | +| `windows_memory_swap_page_writes_total` | Number of disk page writes (a single write operation writing several pages is still only counted once) | counter | None | +| `windows_memory_physical_free_bytes` | Bytes of physical memory currently unused and available | gauge | None | +| `windows_memory_physical_total_bytes` | Total bytes of physical memory available to the operating system. This value does not necessarily indicate the true amount of physical memory, but what is reported to the operating system as available to it | gauge | None | +| `windows_memory_pool_nonpaged_allocs_total` | The number of calls to allocate space in the nonpaged pool. The nonpaged pool is an area of system memory area for objects that cannot be written to disk, and must remain in physical memory as long as they are allocated | counter | None | +| `windows_memory_pool_nonpaged_bytes` | Number of bytes in the non-paged pool, an area of the system virtual memory that is used for objects that cannot be written to disk, but must remain in physical memory as long as they are allocated | gauge | None | +| `windows_memory_pool_paged_allocs_total` | Number of calls to allocate space in the paged pool, regardless of the amount of space allocated in each call | counter | None | +| `windows_memory_pool_paged_bytes` | Number of bytes in the paged pool | gauge | None | +| `windows_memory_pool_paged_resident_bytes` | The size, in bytes, of the portion of the paged pool that is currently resident and active in physical memory. The paged pool is an area of the system virtual memory that is used for objects that can be written to disk when they are not being used | gauge | None | +| `windows_memory_process_memory_limit_bytes` | Maximum number of bytes of memory that can be allocated to a process | gauge | None | +| `windows_memory_standby_cache_core_bytes` | The amount of physical memory, in bytes, that is assigned to the core standby cache page lists. This memory contains cached data and code that is not actively in use by processes, the system and the system cache. It is immediately available for allocation to a process or for system use. If the system runs out of available free and zero memory, memory on lower priority standby cache page lists will be repurposed before memory on higher priority standby cache page lists | gauge | None | +| `windows_memory_standby_cache_normal_priority_bytes` | The amount of physical memory, in bytes, that is assigned to the normal priority standby cache page lists. This memory contains cached data and code that is not actively in use by processes, the system and the system cache. It is immediately available for allocation to a process or for system use. If the system runs out of available free and zero memory, memory on lower priority standby cache page lists will be repurposed before memory on higher priority standby cache page lists | gauge | None | +| `windows_memory_standby_cache_reserve_bytes` | The amount of physical memory, in bytes, that is assigned to the reserve standby cache page lists. This memory contains cached data and code that is not actively in use by processes, the system and the system cache. It is immediately available for allocation to a process or for system use. If the system runs out of available free and zero memory, memory on lower priority standby cache page lists will be repurposed before memory on higher priority standby cache page lists | gauge | None | +| `windows_memory_system_cache_resident_bytes` | The size, in bytes, of the portion of the system file cache which is currently resident and active in physical memory | gauge | None | +| `windows_memory_system_code_resident_bytes` | The size, in bytes, of the pageable operating system code that is currently resident and active in physical memory. This value is a component of Memory\\System Code Total Bytes. Memory\\System Code Resident Bytes (and Memory\\System Code Total Bytes) does not include code that must remain in physical memory and cannot be written to disk | gauge | None | +| `windows_memory_system_code_total_bytes` | The size, in bytes, of the pageable operating system code currently mapped into the system virtual address space. This value is calculated by summing the bytes in Ntoskrnl.exe, Hal.dll, the boot drivers, and file systems loaded by Ntldr/osloader. This counter does not include code that must remain in physical memory and cannot be written to disk | gauge | None | +| `windows_memory_system_driver_resident_bytes` | The size, in bytes, of the pageable physical memory being used by device drivers. It is the working set (physical memory area) of the drivers. This value is a component of Memory\\System Driver Total Bytes, which also includes driver memory that has been written to disk. Neither Memory\\System Driver Resident Bytes nor Memory\\System Driver Total Bytes includes memory that cannot be written to disk | gauge | None | +| `windows_memory_system_driver_total_bytes` | The size, in bytes, of the pageable virtual memory currently being used by device drivers. Pageable memory can be written to disk when it is not being used. It includes both physical memory (Memory\\System Driver Resident Bytes) and code and data paged to disk. It is a component of Memory\\System Code Total Bytes | gauge | None | +| `windows_memory_transition_faults_total` | Number of faults rate at which page faults are resolved by recovering pages that were being used by another process sharing the page, or were on the modified page list or the standby list, or were being written to disk at the time of the page fault. The pages were recovered without additional disk activity. Transition faults are counted in numbers of faults; because only one page is faulted in each operation, it is also equal to the number of pages faulted | counter | None | +| `windows_memory_transition_pages_repurposed_total` | Transition Pages RePurposed is the rate at which the number of transition cache pages were reused for a different purpose. These pages would have otherwise remained in the page cache to provide a (fast) soft fault (instead of retrieving it from backing store) in the event the page was accessed in the future | counter | None | +| `windows_memory_write_copies_total` | The number of page faults caused by attempting to write that were satisfied by copying the page from elsewhere in physical memory | counter | None | ### Example metric _This collector does not yet have explained examples, we would appreciate your help adding them!_ ## Useful queries -_This collector does not yet have any useful queries added, we would appreciate your help adding them!_ +Show memory usage for instance (%) +``` +100 - 100 * windows_memory_physical_free_bytes{instance="localhost"} / windows_memory_physical_total_bytes +``` ## Alerting examples -_This collector does not yet have alerting examples, we would appreciate your help adding them!_ + +**prometheus.rules** +```yaml +# Alert on hosts that have exhausted all available physical memory +- alert: MemoryExhausted + expr: windows_os_physical_memory_free_bytes == 0 + for: 10m + labels: + severity: high + annotations: + summary: "Host {{ $labels.instance }} is out of memory" + description: "{{ $labels.instance }} has exhausted all available physical memory" + +# Alert on hosts with greater than 90% memory usage +- alert: MemoryLow + expr: 100 - 100 * windows_memory_physical_free_bytes{instance="localhost"} / windows_memory_physical_total_bytes > 90 + for: 10m + labels: + severity: warning + annotations: + summary: "Memory usage for host {{ $labels.instance }} is greater than 90%" +``` diff --git a/docs/collector.os.md b/docs/collector.os.md index a262cad60..34b3ab0d5 100644 --- a/docs/collector.os.md +++ b/docs/collector.os.md @@ -14,58 +14,26 @@ None ## Metrics -Name | Description | Type | Labels ------|-------------|------|------- -`windows_os_info` | Contains full product name & version in labels. Note that the `major_version` for Windows 11 is "10"; a build number greater than 22000 represents Windows 11. | gauge | `product`, `version`, `major_version`, `minor_version`, `build_number` -`windows_os_paging_limit_bytes` | Total number of bytes that can be stored in the operating system paging files. 0 (zero) indicates that there are no paging files | gauge | None -`windows_os_paging_free_bytes` | Number of bytes that can be mapped into the operating system paging files without causing any other pages to be swapped out | gauge | None -`windows_os_physical_memory_free_bytes` | Bytes of physical memory currently unused and available | gauge | None -`windows_os_time` | Current time as reported by the operating system, in [Unix time](https://en.wikipedia.org/wiki/Unix_time). See [time.Unix()](https://golang.org/pkg/time/#Unix) for details | gauge | None -`windows_os_timezone` | Current timezone as reported by the operating system. See [time.Zone()](https://golang.org/pkg/time/#Time.Zone) for details | gauge | `timezone` -`windows_os_processes` | Number of process contexts currently loaded or running on the operating system | gauge | None -`windows_os_processes_limit` | Maximum number of process contexts the operating system can support. The default value set by the provider is 4294967295 (0xFFFFFFFF) | gauge | None -`windows_os_process_memory_limit_bytes` | Maximum number of bytes of memory that can be allocated to a process | gauge | None -`windows_os_users` | Number of user sessions for which the operating system is storing state information currently. For a list of current active logon sessions, see [`logon`](collector.logon.md) | gauge | None -`windows_os_virtual_memory_bytes` | Bytes of virtual memory | gauge | None -`windows_os_visible_memory_bytes` | Total bytes of physical memory available to the operating system. This value does not necessarily indicate the true amount of physical memory, but what is reported to the operating system as available to it | gauge | None -`windows_os_virtual_memory_free_bytes` | Bytes of virtual memory currently unused and available | gauge | None +| Name | Description | Type | Labels | +|---------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------|------------------------------------------------------------------------| +| `windows_os_info` | Contains full product name & version in labels. Note that the `major_version` for Windows 11 is "10"; a build number greater than 22000 represents Windows 11. | gauge | `product`, `version`, `major_version`, `minor_version`, `build_number` | +| `windows_os_paging_limit_bytes` | Total number of bytes that can be stored in the operating system paging files. 0 (zero) indicates that there are no paging files | gauge | None | +| `windows_os_paging_free_bytes` | Number of bytes that can be mapped into the operating system paging files without causing any other pages to be swapped out | gauge | None | + ### Example metric -Show current number of processes -``` -windows_os_processes{instance="localhost"} -``` -## Useful queries -Find all devices not set to UTC timezone ``` -windows_os_timezone{timezone != "UTC"} +# HELP windows_os_hostname Labelled system hostname information as provided by ComputerSystem.DNSHostName and ComputerSystem.Domain +# TYPE windows_os_hostname gauge +windows_os_hostname{domain="",fqdn="PC",hostname="PC"} 1 +# HELP windows_os_info Contains full product name & version in labels. Note that the "major_version" for Windows 11 is \\"10\\"; a build number greater than 22000 represents Windows 11. +# TYPE windows_os_info gauge +windows_os_info{build_number="19045",major_version="10",minor_version="0",product="Windows 10 Pro",revision="4842",version="10.0.19045"} 1 ``` -Show memory usage for instance (%) -``` -100 - 100 * windows_os_physical_memory_free_bytes{instance="localhost"} / windows_cs_physical_memory_bytes{instance="localhost"} -``` +## Useful queries +_This collector does not yet have useful queries, we would appreciate your help adding them!_ ## Alerting examples -**prometheus.rules** -```yaml -# Alert on hosts that have exhausted all available physical memory -- alert: MemoryExhausted - expr: windows_os_physical_memory_free_bytes == 0 - for: 10m - labels: - severity: high - annotations: - summary: "Host {{ $labels.instance }} is out of memory" - description: "{{ $labels.instance }} has exhausted all available physical memory" - -# Alert on hosts with greater than 90% memory usage -- alert: MemoryLow - expr: 100 - 100 * windows_os_physical_memory_free_bytes / windows_cs_physical_memory_bytes > 90 - for: 10m - labels: - severity: warning - annotations: - summary: "Memory usage for host {{ $labels.instance }} is greater than 90%" -``` +_This collector does not yet have alerting examples, we would appreciate your help adding them!_ \ No newline at end of file diff --git a/docs/collector.perfdata.md b/docs/collector.perfdata.md new file mode 100644 index 000000000..ef1271cc1 --- /dev/null +++ b/docs/collector.perfdata.md @@ -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. diff --git a/docs/collector.service.md b/docs/collector.service.md index 232523c29..c8d97222b 100644 --- a/docs/collector.service.md +++ b/docs/collector.service.md @@ -2,47 +2,24 @@ The service collector exposes metrics about Windows Services -The collector exists in 2 different version. Version 1 is using WMI to query all services and is able to provide additional -information. Version 2 is a more efficient solution by directly connecting to the service manager, but is not able to -provide additional information like `run_as` or start configuration - -## Flags - -### `--collector.service.services-where` - -A WMI filter on which services to include. Recommended to keep down number of returned metrics. - -Example: `--collector.service.services-where="Name='windows_exporter'"` - -Example config win_exporter.yml for multiple services: `services-where: Name='SQLServer' OR Name='Couchbase' OR Name='Spooler' OR Name='ActiveMQ'` - -### `--collector.service.use-api` - -Uses API calls instead of WMI for performance optimization. **Note** the previous flag (`--collector.service.services-where`) won't have any effect on this mode. - -### `--collector.service.v2` - -Version 2 of the service collector. Is using API calls for performance optimization. **Note** the previous flag (`--collector.service.services-where`) won't have any effect on this mode. -For additional performance reasons, it doesn't provide any additional information like `run_as` or start configuration. - -# collector V1 - ||| -|- Metric name prefix | `service` -Classes | [`Win32_Service`](https://msdn.microsoft.com/en-us/library/aa394418(v=vs.85).aspx) +Classes | none Enabled by default? | Yes -## Metrics +## Flags + +None -Name | Description | Type | Labels ------|-------------|------|------- -`windows_service_info` | Contains service information in labels, constant 1 | gauge | name, display_name, process_id, run_as -`windows_service_state` | The state of the service, 1 if the current state, 0 otherwise | gauge | name, state -`windows_service_start_mode` | The start mode of the service, 1 if the current start mode, 0 otherwise | gauge | name, start_mode -`windows_service_status` | The status of the service, 1 if the current status, 0 otherwise | gauge | name, status +## Metrics -For the values of the `state`, `start_mode`, `status` and `run_as` labels, see below. +| Name | Description | Type | Labels | +|------------------------------|-----------------------------------------------------------------------------------------------|-------|---------------------------------------| +| `windows_service_info` | Contains service information run as user in labels, constant 1 | gauge | name, display_name, path_name, run_as | +| `windows_service_start_mode` | The start mode of the service, 1 if the current start mode, 0 otherwise | gauge | name, start_mode | +| `windows_service_state` | The state of the service, 1 if the current state, 0 otherwise | gauge | name, state | +| `windows_service_process` | Process of started service. The value is the creation time of the process as a unix timestamp | gauge | name, process_id | ### States @@ -65,81 +42,50 @@ A service can have the following start modes: - `manual` - `disabled` -### Status (not available in API mode) - -A service can have any of the following statuses: -- `ok` -- `error` -- `degraded` -- `unknown` -- `pred fail` -- `starting` -- `stopping` -- `service` -- `stressed` -- `nonrecover` -- `no contact` -- `lost comm` - Note that there is some overlap with service state. ### Run As Account name under which a service runs. Depending on the service type, the account name may be in the form of "DomainName\Username" or UPN format ("Username@DomainName"). -It corresponds to the `StartName` attribute of the `Win32_Service` class. -`StartName` attribute can be NULL and in such case the label is reported as an empty string. Notice that if the attribute is NULL the service is logged on as the `LocalSystem` account or, for kernel or system-level drive, it runs with a default object name created by the I/O system based on the service name, for example, DWDOM\Admin. - -### Example metric -Lists the services that have a 'disabled' start mode. -``` -windows_service_start_mode{exported_name=~"(mssqlserver|sqlserveragent)",start_mode="disabled"} -``` - -## Useful queries -Counts the number of Microsoft SQL Server/Agent Processes -``` -count(windows_service_state{exported_name=~"(sqlserveragent|mssqlserver)",state="running"}) -``` - -# collector V2 - - -||| --|- -Metric name prefix | `service` -Classes | none -Enabled by default? | No - - -## Metrics - -Name | Description | Type | Labels ------|-------------|------|------- -`windows_service_state` | The state of the service, 1 if the current state, 0 otherwise | gauge | name, display_name, state - -### States - -A service can be in the following states: -- `stopped` -- `start pending` -- `stop pending` -- `running` -- `continue pending` -- `pause pending` -- `paused` -- `unknown` - ### Example metric ``` -windows_service_state{display_name="Declared Configuration(DC) service",name="dcsvc",status="continue pending"} 0 -windows_service_state{display_name="Declared Configuration(DC) service",name="dcsvc",status="pause pending"} 0 -windows_service_state{display_name="Declared Configuration(DC) service",name="dcsvc",status="paused"} 0 -windows_service_state{display_name="Declared Configuration(DC) service",name="dcsvc",status="running"} 0 -windows_service_state{display_name="Declared Configuration(DC) service",name="dcsvc",status="start pending"} 0 -windows_service_state{display_name="Declared Configuration(DC) service",name="dcsvc",status="stop pending"} 0 -windows_service_state{display_name="Declared Configuration(DC) service",name="dcsvc",status="stopped"} 1 +# HELP windows_service_info A metric with a constant '1' value labeled with service information +# TYPE windows_service_info gauge +windows_service_info{display_name="Declared Configuration(DC) service",name="dcsvc",path_name="C:\\WINDOWS\\system32\\svchost.exe -k netsvcs -p",run_as="LocalSystem"} 1 +windows_service_info{display_name="Designs",name="Themes",path_name="C:\\WINDOWS\\System32\\svchost.exe -k netsvcs -p",run_as="LocalSystem"} 1 +# HELP windows_service_process Process of started service. The value is the creation time of the process as a unix timestamp. +# TYPE windows_service_process gauge +windows_service_process{name="Themes",process_id="2856"} 1.7244891e+09 +# HELP windows_service_start_mode The start mode of the service (StartMode) +# TYPE windows_service_start_mode gauge +windows_service_start_mode{name="Themes",start_mode="auto"} 1 +windows_service_start_mode{name="Themes",start_mode="boot"} 0 +windows_service_start_mode{name="Themes",start_mode="disabled"} 0 +windows_service_start_mode{name="Themes",start_mode="manual"} 0 +windows_service_start_mode{name="Themes",start_mode="system"} 0 +windows_service_start_mode{name="dcsvc",start_mode="auto"} 0 +windows_service_start_mode{name="dcsvc",start_mode="boot"} 0 +windows_service_start_mode{name="dcsvc",start_mode="disabled"} 0 +windows_service_start_mode{name="dcsvc",start_mode="manual"} 1 +windows_service_start_mode{name="dcsvc",start_mode="system"} 0 +# HELP windows_service_state The state of the service (State) +# TYPE windows_service_state gauge +windows_service_state{name="Themes",status="continue pending"} 0 +windows_service_state{name="Themes",status="pause pending"} 0 +windows_service_state{name="Themes",status="paused"} 0 +windows_service_state{name="Themes",status="running"} 1 +windows_service_state{name="Themes",status="start pending"} 0 +windows_service_state{name="Themes",status="stop pending"} 0 +windows_service_state{name="Themes",status="stopped"} 0 +windows_service_state{name="dcsvc",status="continue pending"} 0 +windows_service_state{name="dcsvc",status="pause pending"} 0 +windows_service_state{name="dcsvc",status="paused"} 0 +windows_service_state{name="dcsvc",status="running"} 0 +windows_service_state{name="dcsvc",status="start pending"} 0 +windows_service_state{name="dcsvc",status="stop pending"} 0 +windows_service_state{name="dcsvc",status="stopped"} 1 ``` ## Useful queries @@ -163,8 +109,8 @@ groups: labels: severity: high annotations: - summary: "Service {{ $labels.exported_name }} down" - description: "Service {{ $labels.exported_name }} on instance {{ $labels.instance }} has been down for more than 3 minutes." + summary: "Service {{ $labels.name }} down" + description: "Service {{ $labels.name }} on instance {{ $labels.instance }} has been down for more than 3 minutes." # Sends an alert when the 'mssqlserver' service is not in the running state for 3 minutes. - alert: SQL Server DOWN @@ -173,7 +119,7 @@ groups: labels: severity: high annotations: - summary: "Service {{ $labels.exported_name }} down" - description: "Service {{ $labels.exported_name }} on instance {{ $labels.instance }} has been down for more than 3 minutes." + summary: "Service {{ $labels.name }} down" + description: "Service {{ $labels.name }} on instance {{ $labels.instance }} has been down for more than 3 minutes." ``` In this example, `instance` is the target label of the host. So each alert will be processed per host, which is then used in the alert description. diff --git a/docs/collector.system.md b/docs/collector.system.md index 61d9b88bc..1a68f1e1a 100644 --- a/docs/collector.system.md +++ b/docs/collector.system.md @@ -5,8 +5,7 @@ The system collector exposes metrics about ... ||| -|- Metric name prefix | `system` -Data source | Perflib -Classes | [`Win32_PerfRawData_PerfOS_System`](https://web.archive.org/web/20050830140516/http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_perfrawdata_perfos_system.asp) +Data source | Performance Counters Enabled by default? | Yes ## Flags @@ -15,14 +14,18 @@ None ## Metrics -Name | Description | Type | Labels ------|-------------|------|------- -`windows_system_context_switches_total` | Total number of [context switches](https://en.wikipedia.org/wiki/Context_switch) | counter | None -`windows_system_exception_dispatches_total` | Total exceptions dispatched by the system | counter | None -`windows_system_processor_queue_length` | Number of threads in the processor queue. There is a single queue for processor time even on computers with multiple processors. | gauge | None -`windows_system_system_calls_total` | Total combined calls to Windows NT system service routines by all processes running on the computer | counter | None -`windows_system_system_up_time` | Time of last boot of system | gauge | None -`windows_system_threads` | Number of Windows system [threads](https://en.wikipedia.org/wiki/Thread_(computing)) | gauge | None +| Name | Description | Type | Labels | +|---------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|--------| +| `windows_system_context_switches_total` | Total number of [context switches](https://en.wikipedia.org/wiki/Context_switch) | counter | None | +| `windows_system_exception_dispatches_total` | Total exceptions dispatched by the system | counter | None | +| `windows_system_processes` | Number of process contexts currently loaded or running on the operating system | gauge | None | +| `windows_system_process_limit` | The size of the user-mode portion of the virtual address space of the calling process, in bytes. This value depends on the type of process, the type of processor, and the configuration of the operating system. | gauge | None | +| `windows_system_processor_queue_length` | Number of threads in the processor queue. There is a single queue for processor time even on computers with multiple processors. | gauge | None | +| `windows_system_system_calls_total` | Total combined calls to Windows NT system service routines by all processes running on the computer | counter | None | +| `windows_system_system_up_time` | Time of last boot of system | gauge | None | +| `windows_system_threads` | Number of Windows system [threads](https://en.wikipedia.org/wiki/Thread_(computing)) | gauge | None | + + ### Example metric Show current number of system threads @@ -30,6 +33,11 @@ Show current number of system threads windows_system_threads{instance="localhost"} ``` +Show current number of processes +``` +windows_system_processes{instance="localhost"} +``` + ## Useful queries Find hosts that have rebooted in the last 24 hours ``` diff --git a/docs/collector.time.md b/docs/collector.time.md index 662745a2a..214d4fa17 100644 --- a/docs/collector.time.md +++ b/docs/collector.time.md @@ -17,14 +17,16 @@ None ## Metrics -| Name | Description | Type | Labels | -|-----------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|--------| -| `windows_time_clock_frequency_adjustment_ppb_total` | Total adjustment made to the local system clock frequency by W32Time in parts per billion (PPB) units. 1 PPB adjustment implies the system clock was adjusted at a rate of 1 nanosecond per second (1 ns/s). The smallest possible adjustment can vary and is expected to be in the order of 100's of PPB. | counter | None | -| `windows_time_computed_time_offset_seconds` | The absolute time offset between the system clock and the chosen time source, as computed by the W32Time service in microseconds. When a new valid sample is available, the computed time is updated with the time offset indicated by the sample. This time is the actual time offset of the local clock. W32Time initiates clock correction by using this offset and updates the computed time in between samples with the remaining time offset that needs to be applied to the local clock. Clock accuracy can be tracked by using this performance counter with a low polling interval (for example, 256 seconds or less) and looking for the counter value to be smaller than the desired clock accuracy limit. | gauge | None | -| `windows_time_ntp_client_time_sources` | Active number of NTP Time sources being used by the client. This is a count of active, distinct IP addresses of time servers that are responding to this client's requests. | gauge | None | -| `windows_time_ntp_round_trip_delay_seconds` | Total roundtrip delay experienced by the NTP client in receiving a response from the server for the most recent request, in seconds. This is the time elapsed on the NTP client between transmitting a request to the NTP server and receiving a valid response from the server. | gauge | None | -| `windows_time_ntp_server_outgoing_responses_total` | Total number of requests responded to by the NTP server. | counter | None | -| `windows_time_ntp_server_incoming_requests_total` | Total number of requests received by the NTP server. | counter | None | +| Name | Description | Type | Labels | +|-----------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|------------| +| `windows_time_clock_frequency_adjustment_ppb_total` | Total adjustment made to the local system clock frequency by W32Time in parts per billion (PPB) units. 1 PPB adjustment implies the system clock was adjusted at a rate of 1 nanosecond per second (1 ns/s). The smallest possible adjustment can vary and is expected to be in the order of 100's of PPB. | counter | None | +| `windows_time_computed_time_offset_seconds` | The absolute time offset between the system clock and the chosen time source, as computed by the W32Time service in microseconds. When a new valid sample is available, the computed time is updated with the time offset indicated by the sample. This time is the actual time offset of the local clock. W32Time initiates clock correction by using this offset and updates the computed time in between samples with the remaining time offset that needs to be applied to the local clock. Clock accuracy can be tracked by using this performance counter with a low polling interval (for example, 256 seconds or less) and looking for the counter value to be smaller than the desired clock accuracy limit. | gauge | None | +| `windows_time_ntp_client_time_sources` | Active number of NTP Time sources being used by the client. This is a count of active, distinct IP addresses of time servers that are responding to this client's requests. | gauge | None | +| `windows_time_ntp_round_trip_delay_seconds` | Total roundtrip delay experienced by the NTP client in receiving a response from the server for the most recent request, in seconds. This is the time elapsed on the NTP client between transmitting a request to the NTP server and receiving a valid response from the server. | gauge | None | +| `windows_time_ntp_server_outgoing_responses_total` | Total number of requests responded to by the NTP server. | counter | None | +| `windows_time_ntp_server_incoming_requests_total` | Total number of requests received by the NTP server. | counter | None | +| `windows_time_current_timestamp_seconds` | Current time as reported by the operating system, in [Unix time](https://en.wikipedia.org/wiki/Unix_time). See [time.Unix()](https://golang.org/pkg/time/#Unix) for details | gauge | None | +| `windows_time_timezone` | Current timezone as reported by the operating system. | gauge | `timezone` | ### Example metric _This collector does not yet have explained examples, we would appreciate your help adding them!_ diff --git a/exporter.go b/exporter.go index 493ad40c5..7d0c393e5 100644 --- a/exporter.go +++ b/exporter.go @@ -11,9 +11,9 @@ import ( "github.com/prometheus-community/windows_exporter/pkg/initiate" "context" - "encoding/json" "errors" "fmt" + "log/slog" "net/http" "net/http/pprof" "os" @@ -25,9 +25,9 @@ import ( "time" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/collector" "github.com/prometheus-community/windows_exporter/pkg/config" + "github.com/prometheus-community/windows_exporter/pkg/httphandler" winlog "github.com/prometheus-community/windows_exporter/pkg/log" "github.com/prometheus-community/windows_exporter/pkg/log/flag" "github.com/prometheus-community/windows_exporter/pkg/types" @@ -38,17 +38,6 @@ import ( "golang.org/x/sys/windows" ) -// Same struct prometheus uses for their /version endpoint. -// Separate copy to avoid pulling all of prometheus as a dependency. -type prometheusVersion struct { - Version string `json:"version"` - Revision string `json:"revision"` - Branch string `json:"branch"` - BuildUser string `json:"buildUser"` - BuildDate string `json:"buildDate"` - GoVersion string `json:"goVersion"` -} - // Mapping of priority names to uin32 values required by windows.SetPriorityClass. var priorityStringToInt = map[string]uint32{ "realtime": windows.REALTIME_PRIORITY_CLASS, @@ -59,29 +48,13 @@ var priorityStringToInt = map[string]uint32{ "low": windows.IDLE_PRIORITY_CLASS, } -func setPriorityWindows(pid int, priority uint32) error { - // https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights - handle, err := windows.OpenProcess( - windows.STANDARD_RIGHTS_REQUIRED|windows.SYNCHRONIZE|windows.SPECIFIC_RIGHTS_ALL, - false, uint32(pid), - ) - if err != nil { - return err - } - - if err = windows.SetPriorityClass(handle, priority); err != nil { - return err - } - - if err = windows.CloseHandle(handle); err != nil { - return fmt.Errorf("failed to close handle: %w", err) - } - - return nil +func main() { + os.Exit(run()) } -func main() { +func run() int { app := kingpin.New("windows_exporter", "A metrics collector for Windows.") + var ( configFile = app.Flag( "config.file", @@ -137,25 +110,41 @@ func main() { // Load values from configuration file(s). Executable flags must first be parsed, in order // to load the specified file(s). - kingpin.MustParse(app.Parse(os.Args[1:])) + if _, err := app.Parse(os.Args[1:]); err != nil { + //nolint:sloglint // we do not have an logger yet + slog.Error("Failed to parse CLI args", + slog.Any("err", err), + ) + + return 1 + } + logger, err := winlog.New(winlogConfig) if err != nil { - _ = level.Error(logger).Log("err", err) - os.Exit(1) - } + //nolint:sloglint // we do not have an logger yet + slog.Error("failed to create logger", + slog.Any("err", err), + ) - _ = level.Debug(logger).Log("msg", "Logging has Started") + return 1 + } if *configFile != "" { resolver, err := config.NewResolver(*configFile, logger, *insecureSkipVerify) if err != nil { - _ = level.Error(logger).Log("msg", "could not load config file", "err", err) - os.Exit(1) + logger.Error("could not load config file", + slog.Any("err", err), + ) + + return 1 } - err = resolver.Bind(app, os.Args[1:]) - if err != nil { - _ = level.Error(logger).Log("err", err) - os.Exit(1) + + if err = resolver.Bind(app, os.Args[1:]); err != nil { + logger.Error("Failed to bind configuration", + slog.Any("err", err), + ) + + return 1 } // NOTE: This is temporary fix for issue #1092, calling kingpin.Parse @@ -164,34 +153,43 @@ func main() { *webConfig.WebListenAddresses = (*webConfig.WebListenAddresses)[1:] // Parse flags once more to include those discovered in configuration file(s). - kingpin.MustParse(app.Parse(os.Args[1:])) + if _, err = app.Parse(os.Args[1:]); err != nil { + logger.Error("Failed to parse CLI args from YAML file", + slog.Any("err", err), + ) + + return 1 + } logger, err = winlog.New(winlogConfig) if err != nil { - _ = level.Error(logger).Log("err", err) - os.Exit(1) + //nolint:sloglint // we do not have an logger yet + slog.Error("failed to create logger", + slog.Any("err", err), + ) + + return 1 } } - if *printCollectors { - collectorNames := collector.Available() - sort.Strings(collectorNames) + logger.Debug("Logging has Started") - fmt.Printf("Available collectors:\n") //nolint:forbidigo - for _, n := range collectorNames { - fmt.Printf(" - %s\n", n) //nolint:forbidigo - } + if *printCollectors { + printCollectorsToStdout() - return + return 0 } // Only set process priority if a non-default and valid value has been set - if *processPriority != "normal" && priorityStringToInt[*processPriority] != 0 { - _ = level.Debug(logger).Log("msg", "setting process priority to "+*processPriority) - err = setPriorityWindows(os.Getpid(), priorityStringToInt[*processPriority]) - if err != nil { - _ = level.Error(logger).Log("msg", "failed to set process priority", "err", err) - os.Exit(1) + if priority, ok := priorityStringToInt[*processPriority]; ok && priority != windows.NORMAL_PRIORITY_CLASS { + logger.Debug("setting process priority to " + *processPriority) + + if err = setPriorityWindows(os.Getpid(), priority); err != nil { + logger.Error("failed to set process priority", + slog.Any("err", err), + ) + + return 1 } } @@ -199,79 +197,68 @@ func main() { collectors.Enable(enabledCollectorList) // Initialize collectors before loading - err = collectors.Build(logger) - if err != nil { - _ = level.Error(logger).Log("msg", "Couldn't load collectors", "err", err) - os.Exit(1) - } - err = collectors.SetPerfCounterQuery(logger) - if err != nil { - _ = level.Error(logger).Log("msg", "Couldn't set performance counter query", "err", err) - os.Exit(1) + if err = collectors.Build(logger); err != nil { + logger.Error("Couldn't load collectors", + slog.Any("err", err), + ) + + return 1 } - if u, err := user.Current(); err != nil { - _ = level.Warn(logger).Log("msg", "Unable to determine which user is running this exporter. More info: https://github.com/golang/go/issues/37348") - } else { - _ = level.Info(logger).Log("msg", fmt.Sprintf("Running as %v", u.Username)) + if err = collectors.SetPerfCounterQuery(logger); err != nil { + logger.Error("Couldn't set performance counter query", + slog.Any("err", err), + ) - if strings.Contains(u.Username, "ContainerAdministrator") || strings.Contains(u.Username, "ContainerUser") { - _ = level.Warn(logger).Log("msg", "Running as a preconfigured Windows Container user. This may mean you do not have Windows HostProcess containers configured correctly and some functionality will not work as expected.") - } + return 1 } - _ = level.Info(logger).Log("msg", fmt.Sprintf("Enabled collectors: %v", strings.Join(enabledCollectorList, ", "))) + logCurrentUser(logger) + + logger.Info("Enabled collectors: " + strings.Join(enabledCollectorList, ", ")) mux := http.NewServeMux() - mux.HandleFunc(*metricsPath, withConcurrencyLimit(*maxRequests, collectors.BuildServeHTTP(logger, *disableExporterMetrics, *timeoutMargin))) - mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) { - w.Header().Set("Content-Type", "application/json") - _, err := fmt.Fprintln(w, `{"status":"ok"}`) - if err != nil { - _ = level.Debug(logger).Log("msg", "Failed to write to stream", "err", err) - } - }) - mux.HandleFunc("/version", func(w http.ResponseWriter, _ *http.Request) { - // we can't use "version" directly as it is a package, and not an object that - // can be serialized. - err := json.NewEncoder(w).Encode(prometheusVersion{ - Version: version.Version, - Revision: version.Revision, - Branch: version.Branch, - BuildUser: version.BuildUser, - BuildDate: version.BuildDate, - GoVersion: version.GoVersion, - }) - if err != nil { - http.Error(w, fmt.Sprintf("error encoding JSON: %s", err), http.StatusInternalServerError) - } - }) + mux.Handle("GET /health", httphandler.NewHealthHandler()) + mux.Handle("GET /version", httphandler.NewVersionHandler()) + mux.Handle("GET "+*metricsPath, httphandler.New(logger, collectors, &httphandler.Options{ + DisableExporterMetrics: *disableExporterMetrics, + TimeoutMargin: *timeoutMargin, + MaxRequests: *maxRequests, + })) if *debugEnabled { - mux.HandleFunc("/debug/pprof/", pprof.Index) - mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) - mux.HandleFunc("/debug/pprof/profile", pprof.Profile) - mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) - mux.HandleFunc("/debug/pprof/trace", pprof.Trace) + mux.HandleFunc("GET /debug/pprof/", pprof.Index) + mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline) + mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile) + mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol) + mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace) } - _ = level.Info(logger).Log("msg", "Starting windows_exporter", "version", version.Info()) - _ = level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext()) - _ = level.Debug(logger).Log("msg", "Go MAXPROCS", "procs", runtime.GOMAXPROCS(0)) + logger.Info("Starting windows_exporter", + slog.String("version", version.Version), + slog.String("branch", version.Branch), + slog.String("revision", version.GetRevision()), + slog.String("goversion", version.GoVersion), + slog.String("builddate", version.BuildDate), + slog.Int("maxprocs", runtime.GOMAXPROCS(0)), + ) server := &http.Server{ ReadHeaderTimeout: 5 * time.Second, IdleTimeout: 60 * time.Second, ReadTimeout: 5 * time.Second, - WriteTimeout: 10 * time.Minute, + WriteTimeout: 5 * time.Minute, Handler: mux, } + errCh := make(chan error, 1) + go func() { if err := web.ListenAndServe(server, webConfig, logger); err != nil && !errors.Is(err, http.ErrServerClosed) { - _ = level.Error(logger).Log("msg", "cannot start windows_exporter", "err", err) - os.Exit(1) + errCh <- err } + + errCh <- nil }() ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) @@ -279,9 +266,17 @@ func main() { select { case <-ctx.Done(): - _ = level.Info(logger).Log("msg", "Shutting down windows_exporter via kill signal") + logger.Info("Shutting down windows_exporter via kill signal") case <-initiate.StopCh: - _ = level.Info(logger).Log("msg", "Shutting down windows_exporter via service control") + logger.Info("Shutting down windows_exporter via service control") + case err := <-errCh: + if err != nil { + logger.Error("Failed to start windows_exporter", + slog.Any("err", err), + ) + + return 1 + } } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) @@ -289,24 +284,53 @@ func main() { _ = server.Shutdown(ctx) - _ = level.Info(logger).Log("msg", "windows_exporter has shut down") + logger.Info("windows_exporter has shut down") + + return 0 } -func withConcurrencyLimit(n int, next http.HandlerFunc) http.HandlerFunc { - if n <= 0 { - return next +func printCollectorsToStdout() { + collectorNames := collector.Available() + sort.Strings(collectorNames) + + fmt.Println("Available collectors:") //nolint:forbidigo + + for _, n := range collectorNames { + fmt.Printf(" - %s\n", n) //nolint:forbidigo } +} + +func logCurrentUser(logger *slog.Logger) { + if u, err := user.Current(); err == nil { + logger.Info("Running as " + u.Username) - sem := make(chan struct{}, n) - return func(w http.ResponseWriter, r *http.Request) { - select { - case sem <- struct{}{}: - defer func() { <-sem }() - default: - w.WriteHeader(http.StatusServiceUnavailable) - _, _ = w.Write([]byte("Too many concurrent requests")) - return + if strings.Contains(u.Username, "ContainerAdministrator") || strings.Contains(u.Username, "ContainerUser") { + logger.Warn("Running as a preconfigured Windows Container user. This may mean you do not have Windows HostProcess containers configured correctly and some functionality will not work as expected.") } - next(w, r) + + return } + + logger.Warn("Unable to determine which user is running this exporter. More info: https://github.com/golang/go/issues/37348") +} + +func setPriorityWindows(pid int, priority uint32) error { + // https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights + handle, err := windows.OpenProcess( + windows.STANDARD_RIGHTS_REQUIRED|windows.SYNCHRONIZE|windows.SPECIFIC_RIGHTS_ALL, + false, uint32(pid), + ) + if err != nil { + return fmt.Errorf("failed to open own process: %w", err) + } + + if err = windows.SetPriorityClass(handle, priority); err != nil { + return fmt.Errorf("failed to set priority class: %w", err) + } + + if err = windows.CloseHandle(handle); err != nil { + return fmt.Errorf("failed to close handle: %w", err) + } + + return nil } diff --git a/go.mod b/go.mod index 5a3c4e283..b00027939 100644 --- a/go.mod +++ b/go.mod @@ -1,56 +1,55 @@ module github.com/prometheus-community/windows_exporter -go 1.22 +go 1.23 require ( github.com/Microsoft/hcsshim v0.12.6 github.com/alecthomas/kingpin/v2 v2.4.0 github.com/dimchansky/utfbom v1.1.1 - github.com/go-kit/log v0.2.1 github.com/go-ole/go-ole v1.3.0 - github.com/prometheus/client_golang v1.20.2 + github.com/google/uuid v1.6.0 + github.com/pkg/errors v0.9.1 + github.com/prometheus/client_golang v1.20.3 github.com/prometheus/client_model v0.6.1 - github.com/prometheus/common v0.57.0 - github.com/prometheus/exporter-toolkit v0.11.0 - github.com/sirupsen/logrus v1.9.3 // indirect + github.com/prometheus/common v0.59.1 + github.com/prometheus/exporter-toolkit v0.13.0 github.com/stretchr/testify v1.9.0 github.com/yusufpapurcu/wmi v1.2.4 - go.opencensus.io v0.24.0 // indirect - golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa - golang.org/x/sys v0.24.0 + golang.org/x/sys v0.25.0 gopkg.in/yaml.v3 v3.0.1 ) -require ( - github.com/google/uuid v1.6.0 - github.com/pkg/errors v0.9.1 -) - require ( github.com/Microsoft/go-winio v0.6.2 // indirect - github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect + github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/containerd/cgroups/v3 v3.0.3 // indirect github.com/containerd/errdefs v0.1.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/klauspost/compress v1.17.9 // indirect + github.com/mdlayher/socket v0.5.1 // indirect + github.com/mdlayher/vsock v1.2.1 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/xhit/go-str2duration/v2 v2.1.0 // indirect - golang.org/x/crypto v0.25.0 // indirect - golang.org/x/net v0.27.0 // indirect - golang.org/x/oauth2 v0.21.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/text v0.16.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect - google.golang.org/grpc v1.65.0 // indirect + go.opencensus.io v0.24.0 // indirect + golang.org/x/crypto v0.27.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/oauth2 v0.23.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/text v0.18.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect + google.golang.org/grpc v1.66.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) + +// https://github.com/prometheus/common/pull/694 +replace github.com/prometheus/common v0.59.1 => github.com/jkroepke/prometheus-common v0.0.0-20240907211841-5f9af24b97ad diff --git a/go.sum b/go.sum index f02fc444f..7b77742b7 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/Microsoft/hcsshim v0.12.6 h1:qEnZjoHXv+4/s0LmKZWE0/AiZmMWEIkFfWBSf1a0 github.com/Microsoft/hcsshim v0.12.6/go.mod h1:ZABCLVcvLMjIkzr9rUGcQ1QA0p0P3Ps+d3N1g2DsFfk= github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY= github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE= -github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc= -github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= +github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 h1:t3eaIm0rUkzbrIewtiFmMK5RXHej2XnoXNhxVsAYUfg= +github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30/go.mod h1:fvzegU4vN3H1qMT+8wDmzjAcDONcgo2/SZ/TyfdUOFs= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -30,10 +30,6 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= -github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= -github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= -github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= @@ -63,6 +59,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jkroepke/prometheus-common v0.0.0-20240907211841-5f9af24b97ad h1:sFDfDs4nDXjES8PdrFPiXeYt8dtaxn10M/Ebxe4IuiI= +github.com/jkroepke/prometheus-common v0.0.0-20240907211841-5f9af24b97ad/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= @@ -73,6 +71,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/mdlayher/socket v0.5.1 h1:VZaqt6RkGkt2OE9l3GcC6nZkqD3xKeQLyfleW/uBcos= +github.com/mdlayher/socket v0.5.1/go.mod h1:TjPLHI1UgwEv5J1B5q0zTZq12A/6H7nKmtTanQE37IQ= +github.com/mdlayher/vsock v1.2.1 h1:pC1mTJTvjo1r9n9fbm7S1j04rCgCzhCOS5DY0zqHlnQ= +github.com/mdlayher/vsock v1.2.1/go.mod h1:NRfCibel++DgeMD8z/hP+PPTjlNJsdPOmxcnENvE+SE= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= @@ -81,15 +83,13 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.20.2 h1:5ctymQzZlyOON1666svgwn3s6IKWgfbjsejTMiXIyjg= -github.com/prometheus/client_golang v1.20.2/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= +github.com/prometheus/client_golang v1.20.3 h1:oPksm4K8B+Vt35tUhw6GbSNSgVlVSBH0qELP/7u83l4= +github.com/prometheus/client_golang v1.20.3/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= -github.com/prometheus/common v0.57.0 h1:Ro/rKjwdq9mZn1K5QPctzh+MA4Lp0BuYk5ZZEVhoNcY= -github.com/prometheus/common v0.57.0/go.mod h1:7uRPFSUTbfZWsJ7MHY56sqt7hLQu3bxXHDnNhl8E9qI= -github.com/prometheus/exporter-toolkit v0.11.0 h1:yNTsuZ0aNCNFQ3aFTD2uhPOvr4iD7fdBvKPAEGkNf+g= -github.com/prometheus/exporter-toolkit v0.11.0/go.mod h1:BVnENhnNecpwoTLiABx7mrPB/OLRIgN74qlQbV+FK1Q= +github.com/prometheus/exporter-toolkit v0.13.0 h1:lmA0Q+8IaXgmFRKw09RldZmZdnvu9wwcDLIXGmTPw1c= +github.com/prometheus/exporter-toolkit v0.13.0/go.mod h1:2uop99EZl80KdXhv/MxVI2181fMcwlsumFOqBecGkG0= github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= @@ -99,11 +99,12 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc= @@ -114,11 +115,9 @@ go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ= -golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -128,16 +127,16 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= -golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= -golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= +golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -145,12 +144,12 @@ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= -golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -162,15 +161,15 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 h1:pPJltXNxVzT4pK9yD8vR9X75DaWYYmLGMsEvBfFQZzQ= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= -google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc= -google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= +google.golang.org/grpc v1.66.0 h1:DibZuoBznOxbDQxRINckZcUvnCEvrW9pcWIE2yF9r1c= +google.golang.org/grpc v1.66.0/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -185,7 +184,6 @@ google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWn gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/collector/ad/ad.go b/pkg/collector/ad/ad.go index b0f43db41..4d3dfd5a4 100644 --- a/pkg/collector/ad/ad.go +++ b/pkg/collector/ad/ad.go @@ -4,10 +4,9 @@ package ad import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -108,15 +107,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -502,12 +501,16 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting ad metrics", "err", err) + logger.Error("failed collecting ad metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -668,6 +671,7 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM Win32_PerfRawData_DirectoryServices_DirectoryServices", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } diff --git a/pkg/collector/adcs/adcs.go b/pkg/collector/adcs/adcs.go index 2adb710bb..d89d0611b 100644 --- a/pkg/collector/adcs/adcs.go +++ b/pkg/collector/adcs/adcs.go @@ -4,11 +4,10 @@ package adcs import ( "errors" + "log/slog" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus-community/windows_exporter/pkg/utils" @@ -60,15 +59,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"Certification Authority"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.requestsPerSecond = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "requests_total"), "Total certificate requests processed", @@ -151,12 +150,16 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { return nil } -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectADCSCounters(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting ADCS metrics", "err", err) + logger.Error("failed collecting ADCS metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -177,15 +180,18 @@ type perflibADCS struct { SignedCertificateTimestampListProcessingTime float64 `perflib:"Signed Certificate Timestamp List processing time (ms)"` } -func (c *Collector) collectADCSCounters(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { +func (c *Collector) collectADCSCounters(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { dst := make([]perflibADCS, 0) + if _, ok := ctx.PerfObjects["Certification Authority"]; !ok { return errors.New("perflib did not contain an entry for Certification Authority") } + err := perflib.UnmarshalObject(ctx.PerfObjects["Certification Authority"], &dst, logger) if err != nil { return err } + if len(dst) == 0 { return errors.New("perflib query for Certification Authority (ADCS) returned empty result set") } diff --git a/pkg/collector/adfs/adfs.go b/pkg/collector/adfs/adfs.go index 9ff0c20bb..722e8d536 100644 --- a/pkg/collector/adfs/adfs.go +++ b/pkg/collector/adfs/adfs.go @@ -3,10 +3,10 @@ package adfs import ( + "log/slog" "math" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -87,15 +87,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"AD FS"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *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", @@ -404,9 +404,11 @@ type perflibADFS struct { FederationMetadataRequests float64 `perflib:"Federation Metadata Requests"` } -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var adfsData []perflibADFS + err := perflib.UnmarshalObject(ctx.PerfObjects["AD FS"], &adfsData, logger) if err != nil { return err @@ -669,5 +671,6 @@ func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan prometheus.CounterValue, adfsData[0].FederationMetadataRequests, ) + return nil } diff --git a/pkg/collector/cache/cache.go b/pkg/collector/cache/cache.go index 239af787a..6e33351cb 100644 --- a/pkg/collector/cache/cache.go +++ b/pkg/collector/cache/cache.go @@ -3,9 +3,9 @@ package cache import ( + "log/slog" + "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/pkg/errors" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" @@ -74,15 +74,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"Cache"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.asyncCopyReadsTotal = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "async_copy_reads_total"), "(AsyncCopyReadsTotal)", @@ -257,14 +257,17 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { nil, nil, ) + return nil } // Collect implements the Collector interface. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting cache metrics", "err", err) + logger.Error("failed collecting cache metrics", + slog.Any("err", err), + ) return err } @@ -306,9 +309,11 @@ type perflibCache struct { SyncPinReadsTotal float64 `perflib:"Sync Pin Reads/sec"` } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []perflibCache // Single-instance class, array is required but will have single entry. + if err := perflib.UnmarshalObject(ctx.PerfObjects["Cache"], &dst, logger); err != nil { return err } diff --git a/pkg/collector/collector.go b/pkg/collector/collector.go index d4b86593f..88cb73560 100644 --- a/pkg/collector/collector.go +++ b/pkg/collector/collector.go @@ -5,11 +5,11 @@ package collector import ( "errors" "fmt" + "log/slog" "slices" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" "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" @@ -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" @@ -68,7 +69,7 @@ import ( ) // 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) *MetricCollectors { collectors := map[string]Collector{} for name, builder := range BuildersWithFlags { @@ -81,8 +82,8 @@ func NewWithFlags(app *kingpin.Application) *Collectors { // NewWithConfig To be called by the external libraries for collector initialization without running kingpin.Parse // //goland:noinspection GoUnusedExportedFunction -func NewWithConfig(config Config) *Collectors { - collectors := map[string]Collector{} +func NewWithConfig(config Config) *MetricCollectors { + collectors := Map{} collectors[ad.Name] = ad.New(&config.AD) collectors[adcs.Name] = adcs.New(&config.ADCS) collectors[adfs.Name] = adfs.New(&config.ADFS) @@ -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) @@ -140,16 +142,16 @@ func NewWithConfig(config Config) *Collectors { } // New To be called by the external libraries for collector initialization. -func New(collectors Map) *Collectors { - return &Collectors{ - collectors: collectors, - wmiClient: &wmi.Client{ +func New(collectors Map) *MetricCollectors { + return &MetricCollectors{ + Collectors: collectors, + WMIClient: &wmi.Client{ AllowMissingFields: true, }, } } -func (c *Collectors) SetPerfCounterQuery(logger log.Logger) error { +func (c *MetricCollectors) SetPerfCounterQuery(logger *slog.Logger) error { var ( err error @@ -157,9 +159,9 @@ func (c *Collectors) SetPerfCounterQuery(logger log.Logger) error { perfIndicies []string ) - perfCounterDependencies := make([]string, 0, len(c.collectors)) + perfCounterDependencies := make([]string, 0, len(c.Collectors)) - for _, collector := range c.collectors { + for _, collector := range c.Collectors { perfCounterNames, err = collector.GetPerfCounter(logger) if err != nil { return err @@ -173,31 +175,31 @@ func (c *Collectors) SetPerfCounterQuery(logger log.Logger) error { perfCounterDependencies = append(perfCounterDependencies, strings.Join(perfIndicies, " ")) } - c.perfCounterQuery = strings.Join(perfCounterDependencies, " ") + c.PerfCounterQuery = strings.Join(perfCounterDependencies, " ") return nil } // Enable removes all collectors that not enabledCollectors. -func (c *Collectors) Enable(enabledCollectors []string) { - for name := range c.collectors { +func (c *MetricCollectors) Enable(enabledCollectors []string) { + for name := range c.Collectors { if !slices.Contains(enabledCollectors, name) { - delete(c.collectors, name) + delete(c.Collectors, name) } } } // Build To be called by the exporter for collector initialization. -func (c *Collectors) Build(logger log.Logger) error { +func (c *MetricCollectors) Build(logger *slog.Logger) error { var err error - c.wmiClient.SWbemServicesClient, err = wmi.InitializeSWbemServices(c.wmiClient) + 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, c.wmiClient); err != nil { + for _, collector := range c.Collectors { + if err = collector.Build(logger, c.WMIClient); err != nil { return fmt.Errorf("error build collector %s: %w", collector.GetName(), err) } } @@ -206,12 +208,12 @@ func (c *Collectors) Build(logger log.Logger) error { } // PrepareScrapeContext creates a ScrapeContext to be used during a single scrape. -func (c *Collectors) PrepareScrapeContext() (*types.ScrapeContext, error) { - if c.perfCounterQuery == "" { // if perfCounterQuery is empty, no perf counters are needed. +func (c *MetricCollectors) PrepareScrapeContext() (*types.ScrapeContext, error) { + if c.PerfCounterQuery == "" { // if perfCounterQuery is empty, no perf counters are needed. return &types.ScrapeContext{}, nil } - objs, err := perflib.GetPerflibSnapshot(c.perfCounterQuery) + objs, err := perflib.GetPerflibSnapshot(c.PerfCounterQuery) if err != nil { return nil, err } @@ -220,17 +222,17 @@ func (c *Collectors) PrepareScrapeContext() (*types.ScrapeContext, error) { } // Close To be called by the exporter for collector cleanup. -func (c *Collectors) Close() error { - errs := make([]error, 0, len(c.collectors)) +func (c *MetricCollectors) Close(logger *slog.Logger) error { + errs := make([]error, 0, len(c.Collectors)) - for _, collector := range c.collectors { - if err := collector.Close(); err != nil { + for _, collector := range c.Collectors { + if err := collector.Close(logger); err != nil { errs = append(errs, err) } } - if c.wmiClient != nil && c.wmiClient.SWbemServicesClient != nil { - if err := c.wmiClient.SWbemServicesClient.Close(); err != nil { + if c.WMIClient != nil && c.WMIClient.SWbemServicesClient != nil { + if err := c.WMIClient.SWbemServicesClient.Close(); err != nil { errs = append(errs, err) } } diff --git a/pkg/collector/config.go b/pkg/collector/config.go index ea34bd78a..239f178d5 100644 --- a/pkg/collector/config.go +++ b/pkg/collector/config.go @@ -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" @@ -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"` @@ -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, diff --git a/pkg/collector/container/container.go b/pkg/collector/container/container.go index 3d8e64995..023e52015 100644 --- a/pkg/collector/container/container.go +++ b/pkg/collector/container/container.go @@ -5,12 +5,11 @@ package container import ( "errors" "fmt" + "log/slog" "strings" "github.com/Microsoft/hcsshim" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -79,15 +78,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.containerAvailable = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "available"), "Available", @@ -202,21 +201,27 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting collector metrics", "err", err) + logger.Error("failed collecting collector metrics", + slog.Any("err", err), + ) + return err } return nil } -func (c *Collector) collect(logger log.Logger, ch chan<- prometheus.Metric) error { +func (c *Collector) collect(logger *slog.Logger, ch chan<- prometheus.Metric) error { // Types Container is passed to get the containers compute systems only containers, err := hcsshim.GetContainers(hcsshim.ComputeSystemQuery{Types: []string{"Container"}}) if err != nil { - _ = level.Error(logger).Log("msg", "Err in Getting containers", "err", err) + logger.Error("Err in Getting containers", + slog.Any("err", err), + ) + return err } @@ -240,9 +245,16 @@ func (c *Collector) collect(logger log.Logger, ch chan<- prometheus.Metric) erro if err = c.collectContainer(logger, ch, containerDetails, containerIdWithPrefix); err != nil { if hcsshim.IsNotExist(err) { - _ = level.Debug(logger).Log("msg", "err in fetching container statistics", "containerId", containerDetails.ID, "err", err) + logger.Debug("err in fetching container statistics", + slog.String("container_id", containerDetails.ID), + slog.Any("err", err), + ) } else { - _ = level.Error(logger).Log("msg", "err in fetching container statistics", "containerId", containerDetails.ID, "err", err) + logger.Error("err in fetching container statistics", + slog.String("container_id", containerDetails.ID), + slog.Any("err", err), + ) + collectErrors = append(collectErrors, err) } @@ -263,7 +275,7 @@ func (c *Collector) collect(logger log.Logger, ch chan<- prometheus.Metric) erro return nil } -func (c *Collector) collectContainer(logger log.Logger, ch chan<- prometheus.Metric, containerDetails hcsshim.ContainerProperties, containerIdWithPrefix string) error { +func (c *Collector) collectContainer(logger *slog.Logger, ch chan<- prometheus.Metric, containerDetails hcsshim.ContainerProperties, containerIdWithPrefix string) error { container, err := hcsshim.OpenContainer(containerDetails.ID) if err != nil { return fmt.Errorf("error in opening container: %w", err) @@ -275,7 +287,9 @@ func (c *Collector) collectContainer(logger log.Logger, ch chan<- prometheus.Met } if err := container.Close(); err != nil { - _ = level.Error(logger).Log("err", fmt.Errorf("error in closing container: %w", err)) + logger.Error("error in closing container", + slog.Any("err", err), + ) } }() @@ -358,22 +372,27 @@ func (c *Collector) collectContainer(logger log.Logger, ch chan<- prometheus.Met // With HNSv2, the network stats must be collected from hcsshim.HNSListEndpointRequest. // Network statistics from the container.Statistics() are providing data only, if HNSv1 is used. // Ref: https://github.com/prometheus-community/windows_exporter/pull/1218 -func (c *Collector) collectNetworkMetrics(logger log.Logger, ch chan<- prometheus.Metric, containerPrefixes map[string]string) error { +func (c *Collector) collectNetworkMetrics(logger *slog.Logger, ch chan<- prometheus.Metric, containerPrefixes map[string]string) error { hnsEndpoints, err := hcsshim.HNSListEndpointRequest() if err != nil { - _ = level.Warn(logger).Log("msg", "Failed to collect network stats for containers") + logger.Warn("Failed to collect network stats for containers") + return err } if len(hnsEndpoints) == 0 { - _ = level.Info(logger).Log("msg", "No network stats for containers to collect") + logger.Info("No network stats for containers to collect") + return nil } for _, endpoint := range hnsEndpoints { endpointStats, err := hcsshim.GetHNSEndpointStats(endpoint.Id) if err != nil { - _ = level.Warn(logger).Log("msg", "Failed to collect network stats for interface "+endpoint.Id, "err", err) + logger.Warn("Failed to collect network stats for interface "+endpoint.Id, + slog.Any("err", err), + ) + continue } @@ -381,7 +400,8 @@ func (c *Collector) collectNetworkMetrics(logger log.Logger, ch chan<- prometheu containerIdWithPrefix, ok := containerPrefixes[containerId] if !ok { - _ = level.Debug(logger).Log("msg", "Failed to collect network stats for container "+containerId) + logger.Debug("Failed to collect network stats for container " + containerId) + continue } diff --git a/pkg/collector/cpu/cpu.go b/pkg/collector/cpu/cpu.go index 111589028..f88d507c5 100644 --- a/pkg/collector/cpu/cpu.go +++ b/pkg/collector/cpu/cpu.go @@ -3,13 +3,12 @@ package cpu import ( + "log/slog" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" "github.com/prometheus-community/windows_exporter/pkg/perflib" "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" ) @@ -23,6 +22,7 @@ var ConfigDefaults = Config{} type Collector struct { config Config + logicalProcessors *prometheus.Desc cStateSecondsTotal *prometheus.Desc timeTotal *prometheus.Desc interruptsTotal *prometheus.Desc @@ -58,18 +58,22 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { - if winversion.WindowsVersionFloat() > 6.05 { - return []string{"Processor Information"}, nil - } - return []string{"Processor"}, nil +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { + return []string{"Processor Information"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { + c.logicalProcessors = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "logical_processor"), + "Total number of logical processors", + nil, + nil, + ) + c.cStateSecondsTotal = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "cstate_seconds_total"), "Time spent in low-power idle state", @@ -95,16 +99,6 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { nil, ) - // For Windows 2008 (version 6.0) or earlier we only have the "Processor" - // class. As of Windows 2008 R2 (version 6.1) the more detailed - // "Processor Information" set is available (although some of the counters - // 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 { - return nil - } - c.cStateSecondsTotal = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "cstate_seconds_total"), "Time spent in low-power idle state", @@ -187,113 +181,10 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { return nil } -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 { - return c.CollectFull(ctx, logger, ch) - } +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) - return c.CollectBasic(ctx, logger, ch) -} - -type perflibProcessor struct { - Name string - C1Transitions float64 `perflib:"C1 Transitions/sec"` - C2Transitions float64 `perflib:"C2 Transitions/sec"` - C3Transitions float64 `perflib:"C3 Transitions/sec"` - DPCRate float64 `perflib:"DPC Rate"` - DPCsQueued float64 `perflib:"DPCs Queued/sec"` - Interrupts float64 `perflib:"Interrupts/sec"` - PercentC1Time float64 `perflib:"% C1 Time"` - PercentC2Time float64 `perflib:"% C2 Time"` - PercentC3Time float64 `perflib:"% C3 Time"` - PercentDPCTime float64 `perflib:"% DPC Time"` - PercentIdleTime float64 `perflib:"% Idle Time"` - PercentInterruptTime float64 `perflib:"% Interrupt Time"` - PercentPrivilegedTime float64 `perflib:"% Privileged Time"` - PercentProcessorTime float64 `perflib:"% Processor Time"` - PercentUserTime float64 `perflib:"% User Time"` -} - -func (c *Collector) CollectBasic(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) - data := make([]perflibProcessor, 0) - err := perflib.UnmarshalObject(ctx.PerfObjects["Processor"], &data, logger) - if err != nil { - return err - } - - for _, cpu := range data { - if strings.Contains(strings.ToLower(cpu.Name), "_total") { - continue - } - core := cpu.Name - - ch <- prometheus.MustNewConstMetric( - c.cStateSecondsTotal, - prometheus.CounterValue, - cpu.PercentC1Time, - core, "c1", - ) - ch <- prometheus.MustNewConstMetric( - c.cStateSecondsTotal, - prometheus.CounterValue, - cpu.PercentC2Time, - core, "c2", - ) - ch <- prometheus.MustNewConstMetric( - c.cStateSecondsTotal, - prometheus.CounterValue, - cpu.PercentC3Time, - core, "c3", - ) - - ch <- prometheus.MustNewConstMetric( - c.timeTotal, - prometheus.CounterValue, - cpu.PercentIdleTime, - core, "idle", - ) - ch <- prometheus.MustNewConstMetric( - c.timeTotal, - prometheus.CounterValue, - cpu.PercentInterruptTime, - core, "interrupt", - ) - ch <- prometheus.MustNewConstMetric( - c.timeTotal, - prometheus.CounterValue, - cpu.PercentDPCTime, - core, "dpc", - ) - ch <- prometheus.MustNewConstMetric( - c.timeTotal, - prometheus.CounterValue, - cpu.PercentPrivilegedTime, - core, "privileged", - ) - ch <- prometheus.MustNewConstMetric( - c.timeTotal, - prometheus.CounterValue, - cpu.PercentUserTime, - core, "user", - ) - - ch <- prometheus.MustNewConstMetric( - c.interruptsTotal, - prometheus.CounterValue, - cpu.Interrupts, - core, - ) - ch <- prometheus.MustNewConstMetric( - c.dpcsTotal, - prometheus.CounterValue, - cpu.DPCsQueued, - core, - ) - } - - return nil + return c.CollectFull(ctx, logger, ch) } type perflibProcessorInformation struct { @@ -325,20 +216,26 @@ type perflibProcessorInformation struct { UserTimeSeconds float64 `perflib:"% User Time"` } -func (c *Collector) CollectFull(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) CollectFull(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) data := make([]perflibProcessorInformation, 0) + err := perflib.UnmarshalObject(ctx.PerfObjects["Processor Information"], &data, logger) if err != nil { return err } + var coreCount float64 + for _, cpu := range data { if strings.Contains(strings.ToLower(cpu.Name), "_total") { continue } + core := cpu.Name + coreCount++ + ch <- prometheus.MustNewConstMetric( c.cStateSecondsTotal, prometheus.CounterValue, @@ -459,5 +356,11 @@ func (c *Collector) CollectFull(ctx *types.ScrapeContext, logger log.Logger, ch ) } + ch <- prometheus.MustNewConstMetric( + c.logicalProcessors, + prometheus.GaugeValue, + coreCount, + ) + return nil } diff --git a/pkg/collector/cpu_info/cpu_info.go b/pkg/collector/cpu_info/cpu_info.go index fb5f43a2f..3d123c9d8 100644 --- a/pkg/collector/cpu_info/cpu_info.go +++ b/pkg/collector/cpu_info/cpu_info.go @@ -4,12 +4,11 @@ package cpu_info import ( "errors" + "log/slog" "strconv" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -58,15 +57,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -152,12 +151,16 @@ type win32Processor struct { // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting cpu_info metrics", "err", err) + logger.Error("failed collecting cpu_info metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -169,6 +172,7 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT Architecture, DeviceId, Description, Family, L2CacheSize, L3CacheSize, Name, ThreadCount, NumberOfCores, NumberOfEnabledCore, NumberOfLogicalProcessors FROM Win32_Processor", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } diff --git a/pkg/collector/cs/cs.go b/pkg/collector/cs/cs.go index 8e14cce80..21d6fe550 100644 --- a/pkg/collector/cs/cs.go +++ b/pkg/collector/cs/cs.go @@ -3,9 +3,9 @@ package cs import ( + "log/slog" + "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/headers/sysinfoapi" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -22,9 +22,15 @@ var ConfigDefaults = Config{} type Collector struct { config Config + // physicalMemoryBytes + // Deprecated: Use windows_cpu_logical_processor instead physicalMemoryBytes *prometheus.Desc - logicalProcessors *prometheus.Desc - hostname *prometheus.Desc + // logicalProcessors + // Deprecated: Use windows_physical_memory_total_bytes instead + logicalProcessors *prometheus.Desc + // hostname + // Deprecated: Use windows_os_hostname instead + hostname *prometheus.Desc } func New(config *Config) *Collector { @@ -47,30 +53,35 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(logger *slog.Logger, _ *wmi.Client) error { + logger.Warn("The cs collector is deprecated and will be removed in a future release. " + + "Logical processors has been moved to cpu_info collector. " + + "Physical memory has been moved to memory collector. " + + "Hostname has been moved to os collector.") + c.logicalProcessors = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "logical_processors"), - "ComputerSystem.NumberOfLogicalProcessors", + "Deprecated: Use windows_cpu_logical_processor instead", nil, nil, ) c.physicalMemoryBytes = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "physical_memory_bytes"), - "ComputerSystem.TotalPhysicalMemory", + "Deprecated: Use windows_physical_memory_total_bytes instead", nil, nil, ) c.hostname = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "hostname"), - "Labelled system hostname information as provided by ComputerSystem.DNSHostName and ComputerSystem.Domain", + "Deprecated: Use windows_os_hostname instead", []string{ "hostname", "domain", @@ -78,17 +89,22 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { }, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting cs metrics", "err", err) + logger.Error("failed collecting cs metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -118,10 +134,12 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error { if err != nil { return err } + domain, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSDomain) if err != nil { return err } + fqdn, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSFullyQualified) if err != nil { return err diff --git a/pkg/collector/dfsr/dfsr.go b/pkg/collector/dfsr/dfsr.go index 384d49246..9e2e09bd0 100644 --- a/pkg/collector/dfsr/dfsr.go +++ b/pkg/collector/dfsr/dfsr.go @@ -3,12 +3,11 @@ package dfsr import ( + "log/slog" "slices" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -80,13 +79,14 @@ type Collector struct { dfsrChildCollectors []dfsrCollectorFunc } -type dfsrCollectorFunc func(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error +type dfsrCollectorFunc func(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error // Map Perflib sources to DFSR Collector names // e.g, volume -> DFS Replication Service Volumes. func dfsrGetPerfObjectName(collector string) string { prefix := "DFS " suffix := "" + switch collector { case "connection": suffix = "Replication Connections" @@ -95,6 +95,7 @@ func dfsrGetPerfObjectName(collector string) string { case "volume": suffix = "Replication Service Volumes" } + return prefix + suffix } @@ -137,7 +138,7 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { // Perflib sources are dynamic, depending on the enabled child collectors expandedChildCollectors := slices.Compact(c.config.CollectorsEnabled) perflibDependencies := make([]string, 0, len(expandedChildCollectors)) @@ -149,14 +150,14 @@ func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { return perflibDependencies, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(logger log.Logger, _ *wmi.Client) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Build(logger *slog.Logger, _ *wmi.Client) error { + logger = logger.With(slog.String("collector", Name)) - _ = level.Info(logger).Log("msg", "dfsr collector is in an experimental state! Metrics for this collector have not been tested.") + logger.Info("dfsr collector is in an experimental state! Metrics for this collector have not been tested.") // connection c.connectionBandwidthSavingsUsingDFSReplicationTotal = prometheus.NewDesc( @@ -459,6 +460,7 @@ func (c *Collector) Build(logger log.Logger, _ *wmi.Client) error { // for use in Collector.Collect(). func (c *Collector) getDFSRChildCollectors(enabledCollectors []string) []dfsrCollectorFunc { var dfsrCollectors []dfsrCollectorFunc + for _, collector := range enabledCollectors { switch collector { case "connection": @@ -475,14 +477,15 @@ func (c *Collector) getDFSRChildCollectors(enabledCollectors []string) []dfsrCol // Collect implements the Collector interface. // Sends metric values for each metric to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) for _, fn := range c.dfsrChildCollectors { err := fn(ctx, logger, ch) if err != nil { return err } } + return nil } @@ -501,9 +504,11 @@ type PerflibDFSRConnection struct { SizeOfFilesReceivedTotal float64 `perflib:"Size of Files Received"` } -func (c *Collector) collectConnection(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectConnection(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []PerflibDFSRConnection + if err := perflib.UnmarshalObject(ctx.PerfObjects["DFS Replication Connections"], &dst, logger); err != nil { return err } @@ -572,6 +577,7 @@ func (c *Collector) collectConnection(ctx *types.ScrapeContext, logger log.Logge connection.Name, ) } + return nil } @@ -608,9 +614,11 @@ type perflibDFSRFolder struct { UpdatesDroppedTotal float64 `perflib:"Updates Dropped"` } -func (c *Collector) collectFolder(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectFolder(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []perflibDFSRFolder + if err := perflib.UnmarshalObject(ctx.PerfObjects["DFS Replicated Folders"], &dst, logger); err != nil { return err } @@ -805,6 +813,7 @@ func (c *Collector) collectFolder(ctx *types.ScrapeContext, logger log.Logger, c folder.Name, ) } + return nil } @@ -819,9 +828,11 @@ type perflibDFSRVolume struct { USNJournalUnreadPercentage float64 `perflib:"USN Journal Records Unread Percentage"` } -func (c *Collector) collectVolume(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectVolume(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []perflibDFSRVolume + if err := perflib.UnmarshalObject(ctx.PerfObjects["DFS Replication Service Volumes"], &dst, logger); err != nil { return err } @@ -862,5 +873,6 @@ func (c *Collector) collectVolume(ctx *types.ScrapeContext, logger log.Logger, c volume.Name, ) } + return nil } diff --git a/pkg/collector/dhcp/dhcp.go b/pkg/collector/dhcp/dhcp.go index 49789ca89..a715f54b5 100644 --- a/pkg/collector/dhcp/dhcp.go +++ b/pkg/collector/dhcp/dhcp.go @@ -3,8 +3,9 @@ package dhcp import ( + "log/slog" + "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -68,15 +69,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"DHCP Server"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *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)", @@ -227,6 +228,7 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { nil, nil, ) + return nil } @@ -261,9 +263,11 @@ type dhcpPerf struct { FailoverBndupdDropped float64 `perflib:"Failover: BndUpd Dropped."` } -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dhcpPerfs []dhcpPerf + if err := perflib.UnmarshalObject(ctx.PerfObjects["DHCP Server"], &dhcpPerfs, logger); err != nil { return err } diff --git a/pkg/collector/diskdrive/diskdrive.go b/pkg/collector/diskdrive/diskdrive.go index b89c543b0..276072c77 100644 --- a/pkg/collector/diskdrive/diskdrive.go +++ b/pkg/collector/diskdrive/diskdrive.go @@ -4,11 +4,10 @@ package diskdrive import ( "errors" + "log/slog" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -55,15 +54,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -161,12 +160,16 @@ var ( ) // Collect sends the metric values for each metric 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting disk_drive_info metrics", "err", err) + logger.Error("failed collecting disk_drive_info metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -176,6 +179,7 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query(win32DiskQuery, &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } diff --git a/pkg/collector/dns/dns.go b/pkg/collector/dns/dns.go index 851c65efc..d6e65cf63 100644 --- a/pkg/collector/dns/dns.go +++ b/pkg/collector/dns/dns.go @@ -4,10 +4,9 @@ package dns import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -68,15 +67,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -215,17 +214,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { nil, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting dns metrics", "err", err) + logger.Error("failed collecting dns metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -280,6 +284,7 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM Win32_PerfRawData_DNS_DNS", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } diff --git a/pkg/collector/exchange/exchange.go b/pkg/collector/exchange/exchange.go index 225856070..263be7343 100644 --- a/pkg/collector/exchange/exchange.go +++ b/pkg/collector/exchange/exchange.go @@ -4,12 +4,11 @@ package exchange import ( "fmt" + "log/slog" "os" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -103,6 +102,7 @@ func NewWithFlags(app *kingpin.Application) *Collector { c.config.CollectorsEnabled = make([]string, 0) var listAllCollectors bool + var collectorsEnabled string app.Flag( @@ -158,7 +158,7 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{ "MSExchange ADAccess Processes", "MSExchangeTransport Queues", @@ -173,11 +173,11 @@ func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { }, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { // desc creates a new prometheus description desc := func(metricName string, description string, labels ...string) *prometheus.Desc { return prometheus.NewDesc( @@ -231,9 +231,9 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { } // Collect collects exchange metrics and sends them to prometheus. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) - collectorFuncs := map[string]func(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error{ +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + collectorFuncs := map[string]func(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error{ "ADAccessProcesses": c.collectADAccessProcesses, "TransportQueues": c.collectTransportQueues, "HttpProxy": c.collectHTTPProxy, @@ -248,10 +248,14 @@ func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan for _, collectorName := range c.config.CollectorsEnabled { if err := collectorFuncs[collectorName](ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "Error in "+collectorName, "err", err) + logger.Error("Error in "+collectorName, + slog.Any("err", err), + ) + return err } } + return nil } @@ -266,14 +270,17 @@ type perflibADAccessProcesses struct { LongRunningLDAPOperationsPerMin float64 `perflib:"Long Running LDAP Operations/min"` } -func (c *Collector) collectADAccessProcesses(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectADAccessProcesses(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibADAccessProcesses + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchange ADAccess Processes"], &data, logger); err != nil { return err } labelUseCount := make(map[string]int) + for _, proc := range data { labelName := c.toLabelName(proc.Name) if strings.HasSuffix(labelName, "_total") { @@ -317,6 +324,7 @@ func (c *Collector) collectADAccessProcesses(ctx *types.ScrapeContext, logger lo labelName, ) } + return nil } @@ -325,9 +333,11 @@ type perflibAvailabilityService struct { RequestsSec float64 `perflib:"Availability Requests (sec)"` } -func (c *Collector) collectAvailabilityService(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectAvailabilityService(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibAvailabilityService + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchange Availability Service"], &data, logger); err != nil { return err } @@ -339,6 +349,7 @@ func (c *Collector) collectAvailabilityService(ctx *types.ScrapeContext, logger availservice.RequestsSec, ) } + return nil } @@ -354,9 +365,11 @@ type perflibHTTPProxy struct { ProxyRequestsPerSec float64 `perflib:"Proxy Requests/Sec"` } -func (c *Collector) collectHTTPProxy(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectHTTPProxy(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibHTTPProxy + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchange HttpProxy"], &data, logger); err != nil { return err } @@ -400,6 +413,7 @@ func (c *Collector) collectHTTPProxy(ctx *types.ScrapeContext, logger log.Logger labelName, ) } + return nil } @@ -409,9 +423,11 @@ type perflibOWA struct { RequestsPerSec float64 `perflib:"Requests/sec"` } -func (c *Collector) collectOWA(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectOWA(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibOWA + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchange OWA"], &data, logger); err != nil { return err } @@ -428,6 +444,7 @@ func (c *Collector) collectOWA(ctx *types.ScrapeContext, logger log.Logger, ch c owa.RequestsPerSec, ) } + return nil } @@ -438,9 +455,11 @@ type perflibActiveSync struct { SyncCommandsPerSec float64 `perflib:"Sync Commands/sec"` } -func (c *Collector) collectActiveSync(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectActiveSync(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibActiveSync + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchange ActiveSync"], &data, logger); err != nil { return err } @@ -462,6 +481,7 @@ func (c *Collector) collectActiveSync(ctx *types.ScrapeContext, logger log.Logge instance.SyncCommandsPerSec, ) } + return nil } @@ -475,9 +495,11 @@ type perflibRPCClientAccess struct { UserCount float64 `perflib:"User Count"` } -func (c *Collector) collectRPC(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectRPC(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibRPCClientAccess + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchange RpcClientAccess"], &data, logger); err != nil { return err } @@ -532,9 +554,11 @@ type perflibTransportQueues struct { PoisonQueueLength float64 `perflib:"Poison Queue Length"` } -func (c *Collector) collectTransportQueues(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectTransportQueues(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibTransportQueues + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchangeTransport Queues"], &data, logger); err != nil { return err } @@ -593,6 +617,7 @@ func (c *Collector) collectTransportQueues(ctx *types.ScrapeContext, logger log. labelName, ) } + return nil } @@ -607,9 +632,11 @@ type perflibWorkloadManagementWorkloads struct { IsActive float64 `perflib:"Active"` } -func (c *Collector) collectWorkloadManagementWorkloads(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectWorkloadManagementWorkloads(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibWorkloadManagementWorkloads + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchange WorkloadManagement Workloads"], &data, logger); err != nil { return err } @@ -659,12 +686,15 @@ type perflibAutodiscover struct { RequestsPerSec float64 `perflib:"Requests/sec"` } -func (c *Collector) collectAutoDiscover(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectAutoDiscover(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibAutodiscover + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchangeAutodiscover"], &data, logger); err != nil { return err } + for _, autodisc := range data { ch <- prometheus.MustNewConstMetric( c.autoDiscoverRequestsPerSec, @@ -672,6 +702,7 @@ func (c *Collector) collectAutoDiscover(ctx *types.ScrapeContext, logger log.Log autodisc.RequestsPerSec, ) } + return nil } @@ -680,9 +711,11 @@ type perflibMapiHttpEmsmdb struct { ActiveUserCount float64 `perflib:"Active User Count"` } -func (c *Collector) collectMapiHttpEmsmdb(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectMapiHttpEmsmdb(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibMapiHttpEmsmdb + if err := perflib.UnmarshalObject(ctx.PerfObjects["MSExchange MapiHttp Emsmdb"], &data, logger); err != nil { return err } @@ -702,6 +735,7 @@ func (c *Collector) collectMapiHttpEmsmdb(ctx *types.ScrapeContext, logger log.L func (c *Collector) toLabelName(name string) string { s := strings.ReplaceAll(strings.Join(strings.Fields(strings.ToLower(name)), "_"), ".", "_") s = strings.ReplaceAll(s, "__", "_") + return s } diff --git a/pkg/collector/fsrmquota/fsrmquota.go b/pkg/collector/fsrmquota/fsrmquota.go index 6eddd0d0b..10f15f8d9 100644 --- a/pkg/collector/fsrmquota/fsrmquota.go +++ b/pkg/collector/fsrmquota/fsrmquota.go @@ -4,10 +4,9 @@ package fsrmquota import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "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/utils" "github.com/prometheus/client_golang/prometheus" @@ -56,15 +55,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -125,17 +124,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"path", "template"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting fsrmquota metrics", "err", err) + logger.Error("failed collecting fsrmquota metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -158,6 +162,7 @@ type MSFT_FSRMQuota struct { func (c *Collector) collect(ch chan<- prometheus.Metric) error { var dst []MSFT_FSRMQuota + var count int if err := c.wmiClient.Query("SELECT * FROM MSFT_FSRMQuota", &dst, nil, "root/microsoft/windows/fsrm"); err != nil { @@ -225,5 +230,6 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error { prometheus.GaugeValue, float64(count), ) + return nil } diff --git a/pkg/collector/handler.go b/pkg/collector/handler.go deleted file mode 100644 index 6f1e6cd07..000000000 --- a/pkg/collector/handler.go +++ /dev/null @@ -1,85 +0,0 @@ -//go:build windows - -package collector - -import ( - "fmt" - stdlog "log" - "net/http" - "strconv" - "time" - - "github.com/go-kit/log" - "github.com/go-kit/log/level" - "github.com/google/uuid" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/collectors" - "github.com/prometheus/client_golang/prometheus/collectors/version" - "github.com/prometheus/client_golang/prometheus/promhttp" -) - -func (c *Collectors) BuildServeHTTP(logger log.Logger, disableExporterMetrics bool, timeoutMargin float64) http.HandlerFunc { - collectorFactory := func(timeout time.Duration, requestedCollectors []string) (error, *Prometheus) { - filteredCollectors := make(map[string]Collector) - // scrape all enabled collectors if no collector is requested - if len(requestedCollectors) == 0 { - filteredCollectors = c.collectors - } - for _, name := range requestedCollectors { - col, exists := c.collectors[name] - if !exists { - return fmt.Errorf("unavailable collector: %s", name), nil - } - filteredCollectors[name] = col - } - - filtered := Collectors{ - collectors: filteredCollectors, - perfCounterQuery: c.perfCounterQuery, - } - - return nil, NewPrometheus(timeout, &filtered, logger) - } - - return func(w http.ResponseWriter, r *http.Request) { - logger := log.With(logger, "remote", r.RemoteAddr, "correlation_id", uuid.New().String()) - - const defaultTimeout = 10.0 - - var timeoutSeconds float64 - if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" { - var err error - timeoutSeconds, err = strconv.ParseFloat(v, 64) - if err != nil { - _ = level.Warn(logger).Log("msg", fmt.Sprintf("Couldn't parse X-Prometheus-Scrape-Timeout-Seconds: %q. Defaulting timeout to %f", v, defaultTimeout)) - } - } - if timeoutSeconds == 0 { - timeoutSeconds = defaultTimeout - } - timeoutSeconds -= timeoutMargin - - reg := prometheus.NewRegistry() - err, wc := collectorFactory(time.Duration(timeoutSeconds*float64(time.Second)), r.URL.Query()["collect[]"]) - if err != nil { - _ = level.Warn(logger).Log("msg", "Couldn't create filtered metrics handler", "err", err) - w.WriteHeader(http.StatusBadRequest) - _, _ = w.Write([]byte(fmt.Sprintf("Couldn't create filtered metrics handler: %s", err))) - return - } - - reg.MustRegister(wc) - if !disableExporterMetrics { - reg.MustRegister( - collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), - collectors.NewGoCollector(), - version.NewCollector("windows_exporter"), - ) - } - - h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{ - ErrorLog: stdlog.New(log.NewStdlibAdapter(level.Error(logger)), "", stdlog.Lshortfile), - }) - h.ServeHTTP(w, r) - } -} diff --git a/pkg/collector/hyperv/hyperv.go b/pkg/collector/hyperv/hyperv.go index fe76bc091..1a474e68d 100644 --- a/pkg/collector/hyperv/hyperv.go +++ b/pkg/collector/hyperv/hyperv.go @@ -5,11 +5,10 @@ package hyperv import ( "errors" "fmt" + "log/slog" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -161,15 +160,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -750,70 +749,107 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"vm"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectVmHealth(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV health status metrics", "err", err) + logger.Error("failed collecting hyperV health status metrics", + slog.Any("err", err), + ) + return err } if err := c.collectVmVid(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV pages metrics", "err", err) + logger.Error("failed collecting hyperV pages metrics", + slog.Any("err", err), + ) + return err } if err := c.collectVmHv(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV hv status metrics", "err", err) + logger.Error("failed collecting hyperV hv status metrics", + slog.Any("err", err), + ) + return err } if err := c.collectVmProcessor(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV processor metrics", "err", err) + logger.Error("failed collecting hyperV processor metrics", + slog.Any("err", err), + ) + return err } if err := c.collectHostLPUsage(logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV host logical processors metrics", "err", err) + logger.Error("failed collecting hyperV host logical processors metrics", + slog.Any("err", err), + ) + return err } if err := c.collectHostCpuUsage(logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV host CPU metrics", "err", err) + logger.Error("failed collecting hyperV host CPU metrics", + slog.Any("err", err), + ) + return err } if err := c.collectVmCpuUsage(logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV VM CPU metrics", "err", err) + logger.Error("failed collecting hyperV VM CPU metrics", + slog.Any("err", err), + ) + return err } if err := c.collectVmSwitch(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV switch metrics", "err", err) + logger.Error("failed collecting hyperV switch metrics", + slog.Any("err", err), + ) + return err } if err := c.collectVmEthernet(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV ethernet metrics", "err", err) + logger.Error("failed collecting hyperV ethernet metrics", + slog.Any("err", err), + ) + return err } if err := c.collectVmStorage(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV virtual storage metrics", "err", err) + logger.Error("failed collecting hyperV virtual storage metrics", + slog.Any("err", err), + ) + return err } if err := c.collectVmNetwork(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV virtual network metrics", "err", err) + logger.Error("failed collecting hyperV virtual network metrics", + slog.Any("err", err), + ) + return err } if err := c.collectVmMemory(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting hyperV virtual memory metrics", "err", err) + logger.Error("failed collecting hyperV virtual memory metrics", + slog.Any("err", err), + ) + return err } @@ -1086,7 +1122,7 @@ type Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor struct { PercentTotalRunTime uint } -func (c *Collector) collectHostLPUsage(logger log.Logger, ch chan<- prometheus.Metric) error { +func (c *Collector) collectHostLPUsage(logger *slog.Logger, ch chan<- prometheus.Metric) error { var dst []Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor if err := c.wmiClient.Query("SELECT * FROM Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor", &dst); err != nil { return err @@ -1096,12 +1132,15 @@ func (c *Collector) collectHostLPUsage(logger log.Logger, ch chan<- prometheus.M if strings.Contains(obj.Name, "_Total") { continue } + // The name format is Hv LP parts := strings.Split(obj.Name, " ") if len(parts) != 3 { - _ = level.Warn(logger).Log("msg", fmt.Sprintf("Unexpected format of Name in collectHostLPUsage: %q", obj.Name)) + logger.Warn(fmt.Sprintf("Unexpected format of Name in collectHostLPUsage: %q", obj.Name)) + continue } + coreId := parts[2] ch <- prometheus.MustNewConstMetric( @@ -1139,7 +1178,7 @@ type Win32_PerfRawData_HvStats_HyperVHypervisorRootVirtualProcessor struct { CPUWaitTimePerDispatch uint64 } -func (c *Collector) collectHostCpuUsage(logger log.Logger, ch chan<- prometheus.Metric) error { +func (c *Collector) collectHostCpuUsage(logger *slog.Logger, ch chan<- prometheus.Metric) error { var dst []Win32_PerfRawData_HvStats_HyperVHypervisorRootVirtualProcessor if err := c.wmiClient.Query("SELECT * FROM Win32_PerfRawData_HvStats_HyperVHypervisorRootVirtualProcessor", &dst); err != nil { return err @@ -1149,12 +1188,15 @@ func (c *Collector) collectHostCpuUsage(logger log.Logger, ch chan<- prometheus. if strings.Contains(obj.Name, "_Total") { continue } + // The name format is Root VP parts := strings.Split(obj.Name, " ") if len(parts) != 3 { - _ = level.Warn(logger).Log("msg", "Unexpected format of Name in collectHostCpuUsage: "+obj.Name) + logger.Warn("Unexpected format of Name in collectHostCpuUsage: " + obj.Name) + continue } + coreId := parts[2] ch <- prometheus.MustNewConstMetric( @@ -1206,7 +1248,7 @@ type Win32_PerfRawData_HvStats_HyperVHypervisorVirtualProcessor struct { CPUWaitTimePerDispatch uint64 } -func (c *Collector) collectVmCpuUsage(logger log.Logger, ch chan<- prometheus.Metric) error { +func (c *Collector) collectVmCpuUsage(logger *slog.Logger, ch chan<- prometheus.Metric) error { var dst []Win32_PerfRawData_HvStats_HyperVHypervisorVirtualProcessor if err := c.wmiClient.Query("SELECT * FROM Win32_PerfRawData_HvStats_HyperVHypervisorVirtualProcessor", &dst); err != nil { return err @@ -1216,17 +1258,22 @@ func (c *Collector) collectVmCpuUsage(logger log.Logger, ch chan<- prometheus.Me if strings.Contains(obj.Name, "_Total") { continue } + // The name format is :Hv VP parts := strings.Split(obj.Name, ":") if len(parts) != 2 { - _ = level.Warn(logger).Log("msg", fmt.Sprintf("Unexpected format of Name in collectVmCpuUsage: %q, expected %q. Skipping.", obj.Name, ":Hv VP ")) + logger.Warn(fmt.Sprintf("Unexpected format of Name in collectVmCpuUsage: %q, expected %q. Skipping.", obj.Name, ":Hv VP ")) + continue } + coreParts := strings.Split(parts[1], " ") if len(coreParts) != 3 { - _ = level.Warn(logger).Log("msg", fmt.Sprintf("Unexpected format of core identifier in collectVmCpuUsage: %q, expected %q. Skipping.", parts[1], "Hv VP ")) + logger.Warn(fmt.Sprintf("Unexpected format of core identifier in collectVmCpuUsage: %q, expected %q. Skipping.", parts[1], "Hv VP ")) + continue } + vmName := parts[0] coreId := coreParts[2] diff --git a/pkg/collector/iis/iis.go b/pkg/collector/iis/iis.go index cc7d43de5..378ec6e80 100644 --- a/pkg/collector/iis/iis.go +++ b/pkg/collector/iis/iis.go @@ -4,13 +4,12 @@ package iis import ( "fmt" + "log/slog" "regexp" "sort" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -250,7 +249,7 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{ "Web Service", "APP_POOL_WAS", @@ -259,12 +258,12 @@ func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { }, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(logger log.Logger, _ *wmi.Client) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Build(logger *slog.Logger, _ *wmi.Client) error { + logger = logger.With(slog.String("collector", Name)) c.iisVersion = getIISVersion(logger) @@ -894,31 +893,44 @@ type simpleVersion struct { minor uint64 } -func getIISVersion(logger log.Logger) simpleVersion { +func getIISVersion(logger *slog.Logger) simpleVersion { k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\InetStp\`, registry.QUERY_VALUE) if err != nil { - _ = level.Warn(logger).Log("msg", "Couldn't open registry to determine IIS version", "err", err) + logger.Warn("Couldn't open registry to determine IIS version", + slog.Any("err", err), + ) + return simpleVersion{} } + defer func() { err = k.Close() if err != nil { - _ = level.Warn(logger).Log("msg", "Failed to close registry key", "err", err) + logger.Warn("Failed to close registry key", + slog.Any("err", err), + ) } }() major, _, err := k.GetIntegerValue("MajorVersion") if err != nil { - _ = level.Warn(logger).Log("msg", "Couldn't open registry to determine IIS version", "err", err) + logger.Warn("Couldn't open registry to determine IIS version", + slog.Any("err", err), + ) + return simpleVersion{} } + minor, _, err := k.GetIntegerValue("MinorVersion") if err != nil { - _ = level.Warn(logger).Log("msg", "Couldn't open registry to determine IIS version", "err", err) + logger.Warn("Couldn't open registry to determine IIS version", + slog.Any("err", err), + ) + return simpleVersion{} } - _ = level.Debug(logger).Log("msg", fmt.Sprintf("Detected IIS %d.%d\n", major, minor)) + logger.Debug(fmt.Sprintf("Detected IIS %d.%d\n", major, minor)) return simpleVersion{ major: major, @@ -928,25 +940,37 @@ func getIISVersion(logger log.Logger) simpleVersion { // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectWebService(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting iis metrics", "err", err) + logger.Error("failed collecting iis metrics", + slog.Any("err", err), + ) + return err } if err := c.collectAPP_POOL_WAS(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting iis metrics", "err", err) + logger.Error("failed collecting iis metrics", + slog.Any("err", err), + ) + return err } if err := c.collectW3SVC_W3WP(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting iis metrics", "err", err) + logger.Error("failed collecting iis metrics", + slog.Any("err", err), + ) + return err } if err := c.collectWebServiceCache(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting iis metrics", "err", err) + logger.Error("failed collecting iis metrics", + slog.Any("err", err), + ) + return err } @@ -1040,12 +1064,15 @@ func dedupIISNames[V hasGetIISName](services []V) map[string]V { name := strings.Split(entry.getIISName(), "#")[0] webServiceDeDuplicated[name] = entry } + return webServiceDeDuplicated } -func (c *Collector) collectWebService(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectWebService(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var webService []perflibWebService + if err := perflib.UnmarshalObject(ctx.PerfObjects["Web Service"], &webService, logger); err != nil { return err } @@ -1336,9 +1363,11 @@ var applicationStates = map[uint32]string{ 7: "Delete Pending", } -func (c *Collector) collectAPP_POOL_WAS(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectAPP_POOL_WAS(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var APP_POOL_WAS []perflibAPP_POOL_WAS + if err := perflib.UnmarshalObject(ctx.PerfObjects["APP_POOL_WAS"], &APP_POOL_WAS, logger); err != nil { return err } @@ -1514,9 +1543,11 @@ type perflibW3SVC_W3WP_IIS8 struct { WebSocketConnectionsRejected float64 `perflib:"WebSocket Connections Rejected / Sec"` } -func (c *Collector) collectW3SVC_W3WP(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectW3SVC_W3WP(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var W3SVC_W3WP []perflibW3SVC_W3WP + if err := perflib.UnmarshalObject(ctx.PerfObjects["W3SVC_W3WP"], &W3SVC_W3WP, logger); err != nil { return err } @@ -1526,6 +1557,7 @@ func (c *Collector) collectW3SVC_W3WP(ctx *types.ScrapeContext, logger log.Logge for w3Name, app := range w3svcW3WPDeduplicated { // Extract the apppool name from the format _ pid := workerProcessNameExtractor.ReplaceAllString(w3Name, "$1") + name := workerProcessNameExtractor.ReplaceAllString(w3Name, "$2") if name == "" || name == "_Total" || c.config.AppExclude.MatchString(name) || @@ -1784,6 +1816,7 @@ func (c *Collector) collectW3SVC_W3WP(ctx *types.ScrapeContext, logger log.Logge for w3Name, app := range w3svcW3WPIIS8Deduplicated { // Extract the apppool name from the format _ pid := workerProcessNameExtractor.ReplaceAllString(w3Name, "$1") + name := workerProcessNameExtractor.ReplaceAllString(w3Name, "$2") if name == "" || name == "_Total" || c.config.AppExclude.MatchString(name) || @@ -1912,9 +1945,11 @@ type perflibWebServiceCache struct { ServiceCache_OutputCacheQueriesTotal float64 } -func (c *Collector) collectWebServiceCache(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectWebServiceCache(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var WebServiceCache []perflibWebServiceCache + if err := perflib.UnmarshalObject(ctx.PerfObjects["Web Service Cache"], &WebServiceCache, logger); err != nil { return err } diff --git a/pkg/collector/license/license.go b/pkg/collector/license/license.go index 74abb3e4a..2d87c2882 100644 --- a/pkg/collector/license/license.go +++ b/pkg/collector/license/license.go @@ -3,9 +3,9 @@ package license import ( + "log/slog" + "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/headers/slc" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -53,15 +53,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.licenseStatus = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "status"), "Status of windows license", @@ -74,12 +74,16 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting license metrics", "err", err) + logger.Error("failed collecting license metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/logical_disk/logical_disk.go b/pkg/collector/logical_disk/logical_disk.go index b12a96589..eb2cd8bb6 100644 --- a/pkg/collector/logical_disk/logical_disk.go +++ b/pkg/collector/logical_disk/logical_disk.go @@ -5,14 +5,13 @@ package logical_disk import ( "encoding/binary" "fmt" + "log/slog" "regexp" "slices" "strconv" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -124,15 +123,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"LogicalDisk"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.information = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "info"), "A metric with a constant '1' value labeled with logical disk information", @@ -262,12 +261,16 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting logical_disk metrics", "err", err) + logger.Error("failed collecting logical_disk metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -294,8 +297,9 @@ type logicalDisk struct { AvgDiskSecPerTransfer float64 `perflib:"Avg. Disk sec/Transfer"` } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var ( err error diskID string @@ -316,12 +320,16 @@ func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan diskID, err = getDiskIDByVolume(volume.Name) if err != nil { - _ = level.Warn(logger).Log("msg", "failed to get disk ID for "+volume.Name, "err", err) + logger.Warn("failed to get disk ID for "+volume.Name, + slog.Any("err", err), + ) } info, err = getVolumeInfo(volume.Name) if err != nil { - _ = level.Warn(logger).Log("msg", "failed to get volume information for %s"+volume.Name, "err", err) + logger.Warn("failed to get volume information for %s"+volume.Name, + slog.Any("err", err), + ) } ch <- prometheus.MustNewConstMetric( @@ -480,11 +488,13 @@ const diskExtentSize = 24 func getDiskIDByVolume(rootDrive string) (string, error) { // Open a volume handle to the Disk Root. var err error + var f windows.Handle // mode has to include FILE_SHARE permission to allow concurrent access to the disk. // use 0 as access mode to avoid admin permission. mode := uint32(windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_DELETE) + f, err = windows.CreateFile( windows.StringToUTF16Ptr(`\\.\`+rootDrive), 0, mode, nil, windows.OPEN_EXISTING, uint32(windows.FILE_ATTRIBUTE_READONLY), 0) @@ -498,6 +508,7 @@ func getDiskIDByVolume(rootDrive string) (string, error) { volumeDiskExtents := make([]byte, 16*1024) var bytesReturned uint32 + err = windows.DeviceIoControl(f, controlCode, nil, 0, &volumeDiskExtents[0], uint32(len(volumeDiskExtents)), &bytesReturned, nil) if err != nil { return "", fmt.Errorf("could not identify physical drive for %s: %w", rootDrive, err) diff --git a/pkg/collector/logon/logon.go b/pkg/collector/logon/logon.go index 4bed72915..beebdd0cc 100644 --- a/pkg/collector/logon/logon.go +++ b/pkg/collector/logon/logon.go @@ -4,10 +4,9 @@ package logon import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -47,15 +46,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -67,17 +66,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"status"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting user metrics", "err", err) + logger.Error("failed collecting user metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -92,6 +96,7 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM Win32_LogonSession", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } @@ -232,5 +237,6 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error { float64(cachedunlock), "cached_unlock", ) + return nil } diff --git a/pkg/collector/map.go b/pkg/collector/map.go index 715a579d3..8635ffa37 100644 --- a/pkg/collector/map.go +++ b/pkg/collector/map.go @@ -1,6 +1,9 @@ package collector import ( + "maps" + "slices" + "github.com/alecthomas/kingpin/v2" "github.com/prometheus-community/windows_exporter/pkg/collector/ad" "github.com/prometheus-community/windows_exporter/pkg/collector/adcs" @@ -36,6 +39,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" @@ -54,7 +58,6 @@ import ( "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" - "golang.org/x/exp/maps" ) func NewBuilderWithFlags[C Collector](fn BuilderWithFlags[C]) BuilderWithFlags[Collector] { @@ -98,6 +101,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), @@ -119,5 +123,5 @@ var BuildersWithFlags = map[string]BuilderWithFlags[Collector]{ } func Available() []string { - return maps.Keys(BuildersWithFlags) + return slices.Sorted(maps.Keys(BuildersWithFlags)) } diff --git a/pkg/collector/memory/memory.go b/pkg/collector/memory/memory.go index 96157f536..b563f53fa 100644 --- a/pkg/collector/memory/memory.go +++ b/pkg/collector/memory/memory.go @@ -6,9 +6,12 @@ package memory import ( + "errors" + "fmt" + "log/slog" + "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" + "github.com/prometheus-community/windows_exporter/pkg/headers/sysinfoapi" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -25,6 +28,7 @@ var ConfigDefaults = Config{} type Collector struct { config Config + // Performance metrics availableBytes *prometheus.Desc cacheBytes *prometheus.Desc cacheBytesPeak *prometheus.Desc @@ -57,6 +61,11 @@ type Collector struct { transitionFaultsTotal *prometheus.Desc transitionPagesRepurposedTotal *prometheus.Desc writeCopiesTotal *prometheus.Desc + + // Global memory status + processMemoryLimitBytes *prometheus.Desc + physicalMemoryTotalBytes *prometheus.Desc + physicalMemoryFreeBytes *prometheus.Desc } func New(config *Config) *Collector { @@ -79,15 +88,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"Memory"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.availableBytes = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "available_bytes"), "The amount of physical memory immediately available for allocation to a process or for system use. It is equal to the sum of memory assigned to"+ @@ -292,17 +301,78 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { nil, nil, ) + c.processMemoryLimitBytes = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "process_memory_limit_bytes"), + "The size of the user-mode portion of the virtual address space of the calling process, in bytes. This value depends on the type of process, the type of processor, and the configuration of the operating system.", + nil, + nil, + ) + c.physicalMemoryTotalBytes = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "physical_total_bytes"), + "The amount of actual physical memory, in bytes.", + nil, + nil, + ) + c.physicalMemoryFreeBytes = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "physical_free_bytes"), + "The amount of physical memory currently available, in bytes. This is the amount of physical memory that can be immediately reused without having to write its contents to disk first. It is the sum of the size of the standby, free, and zero lists.", + nil, + nil, + ) + return nil } // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) - if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting memory metrics", "err", err) - return err +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + + errs := make([]error, 0, 2) + + if err := c.collectPerformanceData(ctx, logger, ch); err != nil { + logger.Error("failed collecting memory metrics", + slog.Any("err", err), + ) + + errs = append(errs, err) + } + + if err := c.collectGlobalMemoryStatus(ch); err != nil { + logger.Error("failed collecting memory metrics", + slog.Any("err", err), + ) + + errs = append(errs, err) } + + return errors.Join(errs...) +} + +func (c *Collector) collectGlobalMemoryStatus(ch chan<- prometheus.Metric) error { + memoryStatusEx, err := sysinfoapi.GlobalMemoryStatusEx() + if err != nil { + return fmt.Errorf("failed to get memory status: %w", err) + } + + ch <- prometheus.MustNewConstMetric( + c.processMemoryLimitBytes, + prometheus.GaugeValue, + float64(memoryStatusEx.TotalVirtual), + ) + + ch <- prometheus.MustNewConstMetric( + c.physicalMemoryTotalBytes, + prometheus.GaugeValue, + float64(memoryStatusEx.TotalPhys), + ) + + ch <- prometheus.MustNewConstMetric( + c.physicalMemoryFreeBytes, + prometheus.GaugeValue, + float64(memoryStatusEx.AvailPhys), + ) + return nil } @@ -343,9 +413,11 @@ type memory struct { WriteCopiesPersec float64 `perflib:"Write Copies/sec"` } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectPerformanceData(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []memory + if err := perflib.UnmarshalObject(ctx.PerfObjects["Memory"], &dst, logger); err != nil { return err } diff --git a/pkg/collector/mscluster/mscluster.go b/pkg/collector/mscluster/mscluster.go index 16b99930f..69eedab76 100644 --- a/pkg/collector/mscluster/mscluster.go +++ b/pkg/collector/mscluster/mscluster.go @@ -3,11 +3,11 @@ package mscluster import ( "errors" "fmt" + "log/slog" "slices" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -213,15 +213,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"Memory"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if len(c.config.CollectorsEnabled) == 0 { return nil } @@ -257,7 +257,7 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(_ *types.ScrapeContext, _ log.Logger, ch chan<- prometheus.Metric) error { +func (c *Collector) Collect(_ *types.ScrapeContext, _ *slog.Logger, ch chan<- prometheus.Metric) error { if len(c.config.CollectorsEnabled) == 0 { return nil } diff --git a/pkg/collector/msmq/msmq.go b/pkg/collector/msmq/msmq.go index b2043da7c..91f1a1a3c 100644 --- a/pkg/collector/msmq/msmq.go +++ b/pkg/collector/msmq/msmq.go @@ -4,11 +4,10 @@ package msmq import ( "errors" + "log/slog" "strings" "github.com/alecthomas/kingpin/v2" - "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/utils" "github.com/prometheus/client_golang/prometheus" @@ -68,16 +67,16 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Build(logger *slog.Logger, wmiClient *wmi.Client) error { + logger = logger.With(slog.String("collector", Name)) if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") @@ -86,7 +85,7 @@ func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { c.wmiClient = wmiClient if *c.config.QueryWhereClause == "" { - _ = level.Warn(logger).Log("msg", "No where-clause specified for msmq collector. This will generate a very large number of metrics!") + logger.Warn("No where-clause specified for msmq collector. This will generate a very large number of metrics!") } c.bytesInJournalQueue = prometheus.NewDesc( @@ -113,17 +112,22 @@ func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { []string{"name"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting msmq metrics", "err", err) + logger.Error("failed collecting msmq metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/mssql/mssql.go b/pkg/collector/mssql/mssql.go index 4bb97585b..a36331437 100644 --- a/pkg/collector/mssql/mssql.go +++ b/pkg/collector/mssql/mssql.go @@ -5,6 +5,7 @@ package mssql import ( "errors" "fmt" + "log/slog" "os" "sort" "strings" @@ -12,8 +13,6 @@ import ( "time" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -46,7 +45,7 @@ var ConfigDefaults = Config{ type mssqlInstancesType map[string]string -func getMSSQLInstances(logger log.Logger) mssqlInstancesType { +func getMSSQLInstances(logger *slog.Logger) mssqlInstancesType { sqlInstances := make(mssqlInstancesType) // in case querying the registry fails, return the default instance @@ -54,21 +53,31 @@ func getMSSQLInstances(logger log.Logger) mssqlInstancesType { sqlDefaultInstance["MSSQLSERVER"] = "" regKey := `Software\Microsoft\Microsoft SQL Server\Instance Names\SQL` + k, err := registry.OpenKey(registry.LOCAL_MACHINE, regKey, registry.QUERY_VALUE) if err != nil { - _ = level.Warn(logger).Log("msg", "Couldn't open registry to determine SQL instances", "err", err) + logger.Warn("Couldn't open registry to determine SQL instances", + slog.Any("err", err), + ) + return sqlDefaultInstance } + defer func() { err = k.Close() if err != nil { - _ = level.Warn(logger).Log("msg", "Failed to close registry key", "err", err) + logger.Warn("Failed to close registry key", + slog.Any("err", err), + ) } }() instanceNames, err := k.ReadValueNames(0) if err != nil { - _ = level.Warn(logger).Log("msg", "Can't ReadSubKeyNames", "err", err) + logger.Warn("Can't ReadSubKeyNames", + slog.Any("err", err), + ) + return sqlDefaultInstance } @@ -78,7 +87,7 @@ func getMSSQLInstances(logger log.Logger) mssqlInstancesType { } } - _ = level.Debug(logger).Log("msg", fmt.Sprintf("Detected MSSQL Instances: %#v\n", sqlInstances)) + logger.Debug(fmt.Sprintf("Detected MSSQL Instances: %#v\n", sqlInstances)) return sqlInstances } @@ -110,7 +119,9 @@ func mssqlGetPerfObjectName(sqlInstance string, collector string) string { if sqlInstance != "MSSQLSERVER" { prefix = "MSSQL$" + sqlInstance + ":" } + suffix := "" + switch collector { case "accessmethods": suffix = "Access Methods" @@ -137,6 +148,7 @@ func mssqlGetPerfObjectName(sqlInstance string, collector string) string { case "waitstats": suffix = "Wait Statistics" } + return prefix + suffix } @@ -436,6 +448,7 @@ func NewWithFlags(app *kingpin.Application) *Collector { } var listAllCollectors bool + var collectorsEnabled string app.Flag( @@ -478,7 +491,7 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(logger log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(logger *slog.Logger) ([]string, error) { c.mssqlInstances = getMSSQLInstances(logger) perfCounters := make([]string, 0, len(c.mssqlInstances)*len(c.config.CollectorsEnabled)) @@ -491,11 +504,11 @@ func (c *Collector) GetPerfCounter(logger log.Logger) ([]string, error) { return perfCounters, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { // Result must order, to prevent test failures. sort.Strings(c.config.CollectorsEnabled) @@ -1971,24 +1984,30 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { return nil } -type mssqlCollectorFunc func(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error +type mssqlCollectorFunc func(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error -func (c *Collector) execute(ctx *types.ScrapeContext, logger log.Logger, name string, fn mssqlCollectorFunc, ch chan<- prometheus.Metric, sqlInstance string, wg *sync.WaitGroup) { +func (c *Collector) execute(ctx *types.ScrapeContext, logger *slog.Logger, name string, fn mssqlCollectorFunc, ch chan<- prometheus.Metric, sqlInstance string, wg *sync.WaitGroup) { // Reset failure counter on each scrape c.mssqlChildCollectorFailure = 0 + defer wg.Done() begin := time.Now() err := fn(ctx, logger, ch, sqlInstance) duration := time.Since(begin) + var success float64 if err != nil { - _ = level.Error(logger).Log("msg", fmt.Sprintf("mssql class collector %s failed after %fs", name, duration.Seconds()), "err", err) + logger.Error(fmt.Sprintf("mssql class collector %s failed after %fs", name, duration.Seconds()), + slog.Any("err", err), + ) + success = 0 c.mssqlChildCollectorFailure++ } else { - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql class collector %s succeeded after %fs.", name, duration.Seconds())) + logger.Debug(fmt.Sprintf("mssql class collector %s succeeded after %fs.", name, duration.Seconds())) + success = 1 } ch <- prometheus.MustNewConstMetric( @@ -2007,8 +2026,8 @@ func (c *Collector) execute(ctx *types.ScrapeContext, logger log.Logger, name st // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) wg := sync.WaitGroup{} for sqlInstance := range c.mssqlInstances { @@ -2016,6 +2035,7 @@ func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan function := c.mssqlCollectors[name] wg.Add(1) + go c.execute(ctx, logger, name, function, ch, sqlInstance, &wg) } } @@ -2079,9 +2099,10 @@ type mssqlAccessMethods struct { WorktablesFromCacheRatioBase float64 `perflib:"Worktables From Cache Base_Base"` } -func (c *Collector) collectAccessMethods(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectAccessMethods(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlAccessMethods - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_accessmethods collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_accessmethods collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "accessmethods")], &dst, logger); err != nil { return err @@ -2396,6 +2417,7 @@ func (c *Collector) collectAccessMethods(ctx *types.ScrapeContext, logger log.Lo sqlInstance, ) } + return nil } @@ -2414,9 +2436,10 @@ type mssqlAvailabilityReplica struct { SendstoTransportPerSec float64 `perflib:"Sends to Transport/sec"` } -func (c *Collector) collectAvailabilityReplica(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectAvailabilityReplica(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlAvailabilityReplica - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_availreplica collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_availreplica collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "availreplica")], &dst, logger); err != nil { return err @@ -2426,6 +2449,7 @@ func (c *Collector) collectAvailabilityReplica(ctx *types.ScrapeContext, logger if strings.ToLower(v.Name) == "_total" { continue } + replicaName := v.Name ch <- prometheus.MustNewConstMetric( @@ -2491,6 +2515,7 @@ func (c *Collector) collectAvailabilityReplica(ctx *types.ScrapeContext, logger sqlInstance, replicaName, ) } + return nil } @@ -2522,9 +2547,10 @@ type mssqlBufferManager struct { TargetPages float64 `perflib:"Target pages"` } -func (c *Collector) collectBufferManager(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectBufferManager(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlBufferManager - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_bufman collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_bufman collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "bufman")], &dst, logger); err != nil { return err @@ -2726,9 +2752,10 @@ type mssqlDatabaseReplica struct { TransactionDelay float64 `perflib:"Transaction Delay"` } -func (c *Collector) collectDatabaseReplica(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectDatabaseReplica(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlDatabaseReplica - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_dbreplica collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_dbreplica collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "dbreplica")], &dst, logger); err != nil { return err @@ -2738,6 +2765,7 @@ func (c *Collector) collectDatabaseReplica(ctx *types.ScrapeContext, logger log. if strings.ToLower(v.Name) == "_total" { continue } + replicaName := v.Name ch <- prometheus.MustNewConstMetric( @@ -2908,6 +2936,7 @@ func (c *Collector) collectDatabaseReplica(ctx *types.ScrapeContext, logger log. sqlInstance, replicaName, ) } + return nil } @@ -2965,9 +2994,10 @@ type mssqlDatabases struct { XTPMemoryUsedKB float64 `perflib:"XTP Memory Used (KB)"` } -func (c *Collector) collectDatabases(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectDatabases(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlDatabases - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_databases collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_databases collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "databases")], &dst, logger); err != nil { return err @@ -2977,6 +3007,7 @@ func (c *Collector) collectDatabases(ctx *types.ScrapeContext, logger log.Logger if strings.ToLower(v.Name) == "_total" { continue } + dbName := v.Name ch <- prometheus.MustNewConstMetric( @@ -3315,6 +3346,7 @@ func (c *Collector) collectDatabases(ctx *types.ScrapeContext, logger log.Logger sqlInstance, dbName, ) } + return nil } @@ -3347,9 +3379,10 @@ type mssqlGeneralStatistics struct { UserConnections float64 `perflib:"User Connections"` } -func (c *Collector) collectGeneralStatistics(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectGeneralStatistics(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlGeneralStatistics - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_genstats collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_genstats collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "genstats")], &dst, logger); err != nil { return err @@ -3542,9 +3575,10 @@ type mssqlLocks struct { NumberOfDeadlocksPerSec float64 `perflib:"Number of Deadlocks/sec"` } -func (c *Collector) collectLocks(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectLocks(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlLocks - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_locks collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_locks collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "locks")], &dst, logger); err != nil { return err @@ -3554,6 +3588,7 @@ func (c *Collector) collectLocks(ctx *types.ScrapeContext, logger log.Logger, ch if strings.ToLower(v.Name) == "_total" { continue } + lockResourceName := v.Name ch <- prometheus.MustNewConstMetric( @@ -3612,6 +3647,7 @@ func (c *Collector) collectLocks(ctx *types.ScrapeContext, logger log.Logger, ch sqlInstance, lockResourceName, ) } + return nil } @@ -3640,9 +3676,10 @@ type mssqlMemoryManager struct { TotalServerMemoryKB float64 `perflib:"Total Server Memory (KB)"` } -func (c *Collector) collectMemoryManager(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectMemoryManager(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlMemoryManager - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_memmgr collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_memmgr collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "memmgr")], &dst, logger); err != nil { return err @@ -3809,9 +3846,10 @@ type mssqlSQLStatistics struct { UnsafeAutoParamsPerSec float64 `perflib:"Unsafe Auto-Params/sec"` } -func (c *Collector) collectSQLStats(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectSQLStats(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlSQLStatistics - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_sqlstats collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_sqlstats collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "sqlstats")], &dst, logger); err != nil { return err @@ -3917,9 +3955,10 @@ type mssqlWaitStatistics struct { WaitStatsTransactionOwnershipWaits float64 `perflib:"Transaction ownership waits"` } -func (c *Collector) collectWaitStats(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectWaitStats(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlWaitStatistics - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_waitstats collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_waitstats collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "waitstats")], &dst, logger); err != nil { return err @@ -4023,9 +4062,10 @@ type mssqlSQLErrors struct { // Win32_PerfRawData_MSSQLSERVER_SQLServerErrors docs: // - https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-sql-errors-object -func (c *Collector) collectSQLErrors(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectSQLErrors(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlSQLErrors - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_sqlerrors collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_sqlerrors collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "sqlerrors")], &dst, logger); err != nil { return err @@ -4035,6 +4075,7 @@ func (c *Collector) collectSQLErrors(ctx *types.ScrapeContext, logger log.Logger if strings.ToLower(v.Name) == "_total" { continue } + resource := v.Name ch <- prometheus.MustNewConstMetric( @@ -4066,9 +4107,10 @@ type mssqlTransactions struct { // Win32_PerfRawData_MSSQLSERVER_Transactions docs: // - https://docs.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-transactions-object -func (c *Collector) collectTransactions(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { +func (c *Collector) collectTransactions(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric, sqlInstance string) error { var dst []mssqlTransactions - _ = level.Debug(logger).Log("msg", fmt.Sprintf("mssql_transactions collector iterating sql instance %s.", sqlInstance)) + + logger.Debug(fmt.Sprintf("mssql_transactions collector iterating sql instance %s.", sqlInstance)) if err := perflib.UnmarshalObject(ctx.PerfObjects[mssqlGetPerfObjectName(sqlInstance, "transactions")], &dst, logger); err != nil { return err diff --git a/pkg/collector/net/net.go b/pkg/collector/net/net.go index 983134cbb..4bf449336 100644 --- a/pkg/collector/net/net.go +++ b/pkg/collector/net/net.go @@ -4,11 +4,10 @@ package net import ( "fmt" + "log/slog" "regexp" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -108,15 +107,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"Network Interface"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.bytesReceivedTotal = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "bytes_received_total"), "(Network.BytesReceivedPerSec)", @@ -201,12 +200,16 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting net metrics", "err", err) + logger.Error("failed collecting net metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -235,8 +238,9 @@ type networkInterface struct { CurrentBandwidth float64 `perflib:"Current Bandwidth"` } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []networkInterface if err := perflib.UnmarshalObject(ctx.PerfObjects["Network Interface"], &dst, logger); err != nil { diff --git a/pkg/collector/netframework_clrexceptions/netframework_clrexceptions.go b/pkg/collector/netframework_clrexceptions/netframework_clrexceptions.go index 8543a8800..51575320e 100644 --- a/pkg/collector/netframework_clrexceptions/netframework_clrexceptions.go +++ b/pkg/collector/netframework_clrexceptions/netframework_clrexceptions.go @@ -4,10 +4,9 @@ package netframework_clrexceptions import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -50,15 +49,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -88,17 +87,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"process"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting win32_perfrawdata_netframework_netclrexceptions metrics", "err", err) + logger.Error("failed collecting win32_perfrawdata_netframework_netclrexceptions metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/netframework_clrinterop/netframework_clrinterop.go b/pkg/collector/netframework_clrinterop/netframework_clrinterop.go index e1ad3a476..0e049f24b 100644 --- a/pkg/collector/netframework_clrinterop/netframework_clrinterop.go +++ b/pkg/collector/netframework_clrinterop/netframework_clrinterop.go @@ -4,10 +4,9 @@ package netframework_clrinterop import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -49,15 +48,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -81,17 +80,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"process"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting win32_perfrawdata_netframework_netclrinterop metrics", "err", err) + logger.Error("failed collecting win32_perfrawdata_netframework_netclrinterop metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/netframework_clrjit/netframework_clrjit.go b/pkg/collector/netframework_clrjit/netframework_clrjit.go index 7037f22fd..6d92670e4 100644 --- a/pkg/collector/netframework_clrjit/netframework_clrjit.go +++ b/pkg/collector/netframework_clrjit/netframework_clrjit.go @@ -3,9 +3,9 @@ package netframework_clrjit import ( + "log/slog" + "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -48,15 +48,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.numberOfMethodsJitted = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "jit_methods_total"), "Displays the total number of methods JIT-compiled since the application started. This counter does not include pre-JIT-compiled methods.", @@ -81,17 +81,22 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { []string{"process"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting win32_perfrawdata_netframework_netclrjit metrics", "err", err) + logger.Error("failed collecting win32_perfrawdata_netframework_netclrjit metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/netframework_clrloading/netframework_clrloading.go b/pkg/collector/netframework_clrloading/netframework_clrloading.go index 923c046e3..2ee2a9b61 100644 --- a/pkg/collector/netframework_clrloading/netframework_clrloading.go +++ b/pkg/collector/netframework_clrloading/netframework_clrloading.go @@ -4,10 +4,9 @@ package netframework_clrloading import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -55,15 +54,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -124,17 +123,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"process"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting win32_perfrawdata_netframework_netclrloading metrics", "err", err) + logger.Error("failed collecting win32_perfrawdata_netframework_netclrloading metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/netframework_clrlocksandthreads/netframework_clrlocksandthreads.go b/pkg/collector/netframework_clrlocksandthreads/netframework_clrlocksandthreads.go index a0073b78b..c12ed09f3 100644 --- a/pkg/collector/netframework_clrlocksandthreads/netframework_clrlocksandthreads.go +++ b/pkg/collector/netframework_clrlocksandthreads/netframework_clrlocksandthreads.go @@ -4,10 +4,9 @@ package netframework_clrlocksandthreads import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -53,15 +52,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -110,17 +109,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"process"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting win32_perfrawdata_netframework_netclrlocksandthreads metrics", "err", err) + logger.Error("failed collecting win32_perfrawdata_netframework_netclrlocksandthreads metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/netframework_clrmemory/netframework_clrmemory.go b/pkg/collector/netframework_clrmemory/netframework_clrmemory.go index dc6b3b2f7..d5ddf8c17 100644 --- a/pkg/collector/netframework_clrmemory/netframework_clrmemory.go +++ b/pkg/collector/netframework_clrmemory/netframework_clrmemory.go @@ -4,10 +4,9 @@ package netframework_clrmemory import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -58,15 +57,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -145,17 +144,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"process"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting win32_perfrawdata_netframework_netclrmemory metrics", "err", err) + logger.Error("failed collecting win32_perfrawdata_netframework_netclrmemory metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/netframework_clrremoting/netframework_clrremoting.go b/pkg/collector/netframework_clrremoting/netframework_clrremoting.go index 81d36cb97..13eb00894 100644 --- a/pkg/collector/netframework_clrremoting/netframework_clrremoting.go +++ b/pkg/collector/netframework_clrremoting/netframework_clrremoting.go @@ -4,10 +4,9 @@ package netframework_clrremoting import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -52,15 +51,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -103,17 +102,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"process"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting win32_perfrawdata_netframework_netclrremoting metrics", "err", err) + logger.Error("failed collecting win32_perfrawdata_netframework_netclrremoting metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/netframework_clrsecurity/netframework_clrsecurity.go b/pkg/collector/netframework_clrsecurity/netframework_clrsecurity.go index 76cf50505..24f1ab23e 100644 --- a/pkg/collector/netframework_clrsecurity/netframework_clrsecurity.go +++ b/pkg/collector/netframework_clrsecurity/netframework_clrsecurity.go @@ -4,10 +4,9 @@ package netframework_clrsecurity import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -50,15 +49,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -88,17 +87,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { []string{"process"}, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting win32_perfrawdata_netframework_netclrsecurity metrics", "err", err) + logger.Error("failed collecting win32_perfrawdata_netframework_netclrsecurity metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/nps/nps.go b/pkg/collector/nps/nps.go index 78f6b2318..607455c76 100644 --- a/pkg/collector/nps/nps.go +++ b/pkg/collector/nps/nps.go @@ -3,10 +3,9 @@ package nps import ( "errors" "fmt" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -71,15 +70,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -236,21 +235,26 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { nil, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.CollectAccept(ch); err != nil { - _ = level.Error(logger).Log("msg", fmt.Sprintf("failed collecting NPS accept data: %s", err)) + logger.Error(fmt.Sprintf("failed collecting NPS accept data: %s", err)) + return err } + if err := c.CollectAccounting(ch); err != nil { - _ = level.Error(logger).Log("msg", fmt.Sprintf("failed collecting NPS accounting data: %s", err)) + logger.Error(fmt.Sprintf("failed collecting NPS accounting data: %s", err)) + return err } + return nil } diff --git a/pkg/collector/os/os.go b/pkg/collector/os/os.go index 531010c7d..feb483021 100644 --- a/pkg/collector/os/os.go +++ b/pkg/collector/os/os.go @@ -5,15 +5,13 @@ package os import ( "errors" "fmt" + "log/slog" "os" "strconv" "strings" - "syscall" "time" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/headers/kernel32" "github.com/prometheus-community/windows_exporter/pkg/headers/netapi32" "github.com/prometheus-community/windows_exporter/pkg/headers/psapi" @@ -22,6 +20,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" ) @@ -35,19 +34,45 @@ var ConfigDefaults = Config{} type Collector struct { config Config - osInformation *prometheus.Desc - pagingFreeBytes *prometheus.Desc - pagingLimitBytes *prometheus.Desc + hostname *prometheus.Desc + osInformation *prometheus.Desc + pagingFreeBytes *prometheus.Desc + pagingLimitBytes *prometheus.Desc + + // users + // Deprecated: Use windows_system_processes instead. + processes *prometheus.Desc + // users + // Deprecated: Use windows_system_process_limit instead. + processesLimit *prometheus.Desc + + // users + // Deprecated: Use count(windows_logon_logon_type) instead. + users *prometheus.Desc + + // physicalMemoryFreeBytes + // Deprecated: Use windows_memory_physical_free_bytes instead. physicalMemoryFreeBytes *prometheus.Desc + + // processMemoryLimitBytes + // Deprecated: Use windows_memory_process_memory_limit_bytes instead. processMemoryLimitBytes *prometheus.Desc - processes *prometheus.Desc - processesLimit *prometheus.Desc - time *prometheus.Desc - timezone *prometheus.Desc - users *prometheus.Desc - virtualMemoryBytes *prometheus.Desc - virtualMemoryFreeBytes *prometheus.Desc - visibleMemoryBytes *prometheus.Desc + + // time + // Deprecated: Use windows_time_current_timestamp_seconds instead. + time *prometheus.Desc + // timezone + // Deprecated: Use windows_time_timezone instead. + timezone *prometheus.Desc + // virtualMemoryBytes + // Deprecated: Use windows_memory_commit_limit instead. + virtualMemoryBytes *prometheus.Desc + // virtualMemoryFreeBytes + // Deprecated: Use windows_memory_commit_limit instead. + virtualMemoryFreeBytes *prometheus.Desc + // visibleMemoryBytes + // Deprecated: Use windows_memory_physical_total_bytes instead. + visibleMemoryBytes *prometheus.Desc } type pagingFileCounter struct { @@ -76,19 +101,50 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"Paging File"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(logger *slog.Logger, _ *wmi.Client) error { + logger.Warn("The os collect holds a number of deprecated metrics and will be removed mid 2025. " + + "See https://github.com/prometheus-community/windows_exporter/pull/1596 for more information.") + + workstationInfo, err := netapi32.GetWorkstationInfo() + if err != nil { + return fmt.Errorf("failed to get workstation info: %w", err) + } + + productName, buildNumber, revision, err := c.getWindowsVersion() + if err != nil { + return fmt.Errorf("failed to get Windows version: %w", err) + } + c.osInformation = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "info"), - "OperatingSystem.Caption, OperatingSystem.Version", - []string{"product", "version", "major_version", "minor_version", "build_number", "revision"}, + `Contains full product name & version in labels. Note that the "major_version" for Windows 11 is \"10\"; a build number greater than 22000 represents Windows 11.`, + nil, + prometheus.Labels{ + "product": productName, + "version": fmt.Sprintf("%d.%d.%s", workstationInfo.VersionMajor, workstationInfo.VersionMinor, buildNumber), + "major_version": strconv.FormatUint(uint64(workstationInfo.VersionMajor), 10), + "minor_version": strconv.FormatUint(uint64(workstationInfo.VersionMinor), 10), + "build_number": buildNumber, + "revision": revision, + }, + ) + + c.hostname = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "hostname"), + "Labelled system hostname information as provided by ComputerSystem.DNSHostName and ComputerSystem.Domain", + []string{ + "hostname", + "domain", + "fqdn", + }, nil, ) c.pagingLimitBytes = prometheus.NewDesc( @@ -105,118 +161,228 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { ) c.physicalMemoryFreeBytes = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "physical_memory_free_bytes"), - "OperatingSystem.FreePhysicalMemory", + "Deprecated: Use `windows_memory_physical_free_bytes` instead.", nil, nil, ) c.time = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "time"), - "OperatingSystem.LocalDateTime", + "Deprecated: Use windows_time_current_timestamp_seconds instead.", nil, nil, ) c.timezone = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "timezone"), - "OperatingSystem.LocalDateTime", + "Deprecated: Use windows_time_timezone instead.", []string{"timezone"}, nil, ) c.processes = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "processes"), - "OperatingSystem.NumberOfProcesses", + "Deprecated: Use `windows_system_processes` instead.", nil, nil, ) c.processesLimit = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "processes_limit"), - "OperatingSystem.MaxNumberOfProcesses", + "Deprecated: Use `windows_system_process_limit` instead.", nil, nil, ) c.processMemoryLimitBytes = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "process_memory_limit_bytes"), - "OperatingSystem.MaxProcessMemorySize", + "Deprecated: Use `windows_memory_process_memory_limit_bytes` instead.", nil, nil, ) c.users = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "users"), - "OperatingSystem.NumberOfUsers", + "Deprecated: Use `count(windows_logon_logon_type)` instead.", nil, nil, ) c.virtualMemoryBytes = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "virtual_memory_bytes"), - "OperatingSystem.TotalVirtualMemorySize", + "Deprecated: Use `windows_memory_commit_limit` instead.", nil, nil, ) c.visibleMemoryBytes = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "visible_memory_bytes"), - "OperatingSystem.TotalVisibleMemorySize", + "Deprecated: Use `windows_memory_physical_total_bytes` instead.", nil, nil, ) c.virtualMemoryFreeBytes = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "virtual_memory_free_bytes"), - "OperatingSystem.FreeVirtualMemory", + "Deprecated: Use `windows_memory_commit_limit - windows_memory_committed_bytes` instead.", nil, nil, ) + return nil } // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) - if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting os metrics", "err", err) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + + errs := make([]error, 0, 5) + + c.collect(ch) + + if err := c.collectHostname(ch); err != nil { + logger.Error("failed collecting os metrics", + slog.Any("err", err), + ) + + errs = append(errs, err) + } + + if err := c.collectLoggedInUserCount(ch); err != nil { + logger.Error("failed collecting os user count metrics", + slog.Any("err", err), + ) + + errs = append(errs, err) + } + + if err := c.collectMemory(ch); err != nil { + logger.Error("failed collecting os memory metrics", + slog.Any("err", err), + ) + + errs = append(errs, err) + } + + if err := c.collectTime(ch); err != nil { + logger.Error("failed collecting os time metrics", + slog.Any("err", err), + ) + + errs = append(errs, err) + } + + if err := c.collectPaging(ctx, logger, ch); err != nil { + logger.Error("failed collecting os paging metrics", + slog.Any("err", err), + ) + + errs = append(errs, err) + } + + return errors.Join(errs...) +} + +func (c *Collector) collectLoggedInUserCount(ch chan<- prometheus.Metric) error { + workstationInfo, err := netapi32.GetWorkstationInfo() + if err != nil { return err } + + ch <- prometheus.MustNewConstMetric( + c.users, + prometheus.GaugeValue, + float64(workstationInfo.LoggedOnUsers), + ) + return nil } -// Win32_OperatingSystem docs: -// - https://msdn.microsoft.com/en-us/library/aa394239 - Win32_OperatingSystem class. -type Win32_OperatingSystem struct { - Caption string - FreePhysicalMemory uint64 - FreeSpaceInPagingFiles uint64 - FreeVirtualMemory uint64 - LocalDateTime time.Time - MaxNumberOfProcesses uint32 - MaxProcessMemorySize uint64 - NumberOfProcesses uint32 - NumberOfUsers uint32 - SizeStoredInPagingFiles uint64 - TotalVirtualMemorySize uint64 - TotalVisibleMemorySize uint64 - Version string -} +func (c *Collector) collectHostname(ch chan<- prometheus.Metric) error { + hostname, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSHostname) + if err != nil { + return err + } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) - nwgi, err := netapi32.GetWorkstationInfo() + domain, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSDomain) if err != nil { return err } - gmse, err := sysinfoapi.GlobalMemoryStatusEx() + fqdn, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSFullyQualified) if err != nil { return err } - currentTime := time.Now() + ch <- prometheus.MustNewConstMetric( + c.hostname, + prometheus.GaugeValue, + 1.0, + hostname, + domain, + fqdn, + ) + + return nil +} +func (c *Collector) collectTime(ch chan<- prometheus.Metric) error { timeZoneInfo, err := kernel32.GetDynamicTimeZoneInformation() if err != nil { return err } // timeZoneKeyName contains the english name of the timezone. - timezoneName := syscall.UTF16ToString(timeZoneInfo.TimeZoneKeyName[:]) + timezoneName := windows.UTF16ToString(timeZoneInfo.TimeZoneKeyName[:]) + + ch <- prometheus.MustNewConstMetric( + c.time, + prometheus.GaugeValue, + float64(time.Now().Unix()), + ) + + ch <- prometheus.MustNewConstMetric( + c.timezone, + prometheus.GaugeValue, + 1.0, + timezoneName, + ) + + return nil +} + +func (c *Collector) collectMemory(ch chan<- prometheus.Metric) error { + memoryStatusEx, err := sysinfoapi.GlobalMemoryStatusEx() + if err != nil { + return err + } + ch <- prometheus.MustNewConstMetric( + c.physicalMemoryFreeBytes, + prometheus.GaugeValue, + float64(memoryStatusEx.AvailPhys), + ) + + ch <- prometheus.MustNewConstMetric( + c.virtualMemoryFreeBytes, + prometheus.GaugeValue, + float64(memoryStatusEx.AvailPageFile), + ) + + ch <- prometheus.MustNewConstMetric( + c.virtualMemoryBytes, + prometheus.GaugeValue, + float64(memoryStatusEx.TotalPageFile), + ) + + ch <- prometheus.MustNewConstMetric( + c.visibleMemoryBytes, + prometheus.GaugeValue, + float64(memoryStatusEx.TotalPhys), + ) + + ch <- prometheus.MustNewConstMetric( + c.processMemoryLimitBytes, + prometheus.GaugeValue, + float64(memoryStatusEx.TotalVirtual), + ) + + return nil +} + +func (c *Collector) collectPaging(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { // Get total allocation of paging files across all disks. memManKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management`, registry.QUERY_VALUE) if err != nil { @@ -228,96 +394,43 @@ func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan pagingFiles, _, pagingErr := memManKey.GetStringsValue("ExistingPageFiles") var fsipf float64 + for _, pagingFile := range pagingFiles { fileString := strings.ReplaceAll(pagingFile, `\??\`, "") file, err := os.Stat(fileString) // For unknown reasons, Windows doesn't always create a page file. Continue collection rather than aborting. if err != nil { - _ = level.Debug(logger).Log("msg", fmt.Sprintf("Failed to read page file (reason: %s): %s\n", err, fileString)) + logger.Debug(fmt.Sprintf("Failed to read page file (reason: %s): %s\n", err, fileString)) } else { fsipf += float64(file.Size()) } } - // Get build number and product name from registry - ntKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) - if err != nil { - return err - } - - defer ntKey.Close() - - pn, _, err := ntKey.GetStringValue("ProductName") - if err != nil { - return err - } - - bn, _, err := ntKey.GetStringValue("CurrentBuildNumber") - if err != nil { - return err - } - - revision, _, err := ntKey.GetIntegerValue("UBR") - if errors.Is(err, registry.ErrNotExist) { - revision = 0 - } else if err != nil { - return err - } - gpi, err := psapi.GetPerformanceInfo() if err != nil { return err } pfc := make([]pagingFileCounter, 0) - if err := perflib.UnmarshalObject(ctx.PerfObjects["Paging File"], &pfc, logger); err != nil { + if err = perflib.UnmarshalObject(ctx.PerfObjects["Paging File"], &pfc, logger); err != nil { return err } // Get current page file usage. var pfbRaw float64 + for _, pageFile := range pfc { if strings.Contains(strings.ToLower(pageFile.Name), "_total") { continue } + pfbRaw += pageFile.Usage } - // Subtract from total page file allocation on disk. - pfb := fsipf - (pfbRaw * float64(gpi.PageSize)) - - ch <- prometheus.MustNewConstMetric( - c.osInformation, - prometheus.GaugeValue, - 1.0, - "Microsoft "+pn, // Caption - fmt.Sprintf("%d.%d.%s", nwgi.VersionMajor, nwgi.VersionMinor, bn), // Version - strconv.FormatUint(uint64(nwgi.VersionMajor), 10), // Major Version - strconv.FormatUint(uint64(nwgi.VersionMinor), 10), // Minor Version - bn, // Build number - strconv.FormatUint(revision, 10), // Revision - ) - - ch <- prometheus.MustNewConstMetric( - c.physicalMemoryFreeBytes, - prometheus.GaugeValue, - float64(gmse.AvailPhys), - ) - - ch <- prometheus.MustNewConstMetric( - c.time, - prometheus.GaugeValue, - float64(currentTime.Unix()), - ) - - ch <- prometheus.MustNewConstMetric( - c.timezone, - prometheus.GaugeValue, - 1.0, - timezoneName, - ) - if pagingErr == nil { + // Subtract from total page file allocation on disk. + pfb := fsipf - (pfbRaw * float64(gpi.PageSize)) + ch <- prometheus.MustNewConstMetric( c.pagingFreeBytes, prometheus.GaugeValue, @@ -330,12 +443,23 @@ func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan fsipf, ) } else { - _ = level.Debug(logger).Log("msg", "Could not find HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management key. windows_os_paging_free_bytes and windows_os_paging_limit_bytes will be omitted.") + logger.Debug("Could not find HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management key. windows_os_paging_free_bytes and windows_os_paging_limit_bytes will be omitted.") } + ch <- prometheus.MustNewConstMetric( - c.virtualMemoryFreeBytes, + c.processes, + prometheus.GaugeValue, + float64(gpi.ProcessCount), + ) + + return nil +} + +func (c *Collector) collect(ch chan<- prometheus.Metric) { + ch <- prometheus.MustNewConstMetric( + c.osInformation, prometheus.GaugeValue, - float64(gmse.AvailPageFile), + 1.0, ) // Windows has no defined limit, and is based off available resources. This currently isn't calculated by WMI and is set to default value. @@ -346,36 +470,33 @@ func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan prometheus.GaugeValue, float64(4294967295), ) +} - ch <- prometheus.MustNewConstMetric( - c.processMemoryLimitBytes, - prometheus.GaugeValue, - float64(gmse.TotalVirtual), - ) +func (c *Collector) getWindowsVersion() (string, string, string, error) { + // Get build number and product name from registry + ntKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) + if err != nil { + return "", "", "", fmt.Errorf("failed to open registry key: %w", err) + } - ch <- prometheus.MustNewConstMetric( - c.processes, - prometheus.GaugeValue, - float64(gpi.ProcessCount), - ) + defer ntKey.Close() - ch <- prometheus.MustNewConstMetric( - c.users, - prometheus.GaugeValue, - float64(nwgi.LoggedOnUsers), - ) + productName, _, err := ntKey.GetStringValue("ProductName") + if err != nil { + return "", "", "", err + } - ch <- prometheus.MustNewConstMetric( - c.virtualMemoryBytes, - prometheus.GaugeValue, - float64(gmse.TotalPageFile), - ) + buildNumber, _, err := ntKey.GetStringValue("CurrentBuildNumber") + if err != nil { + return "", "", "", err + } - ch <- prometheus.MustNewConstMetric( - c.visibleMemoryBytes, - prometheus.GaugeValue, - float64(gmse.TotalPhys), - ) + revision, _, err := ntKey.GetIntegerValue("UBR") + if errors.Is(err, registry.ErrNotExist) { + revision = 0 + } else if err != nil { + return "", "", "", err + } - return nil + return productName, buildNumber, strconv.FormatUint(revision, 10), nil } diff --git a/pkg/collector/perfdata/perfdata.go b/pkg/collector/perfdata/perfdata.go new file mode 100644 index 000000000..f37e9c057 --- /dev/null +++ b/pkg/collector/perfdata/perfdata.go @@ -0,0 +1,181 @@ +//go:build windows + +package perfdata + +import ( + "encoding/json" + "fmt" + "log/slog" + "maps" + "slices" + "strings" + + "github.com/alecthomas/kingpin/v2" + "github.com/prometheus-community/windows_exporter/pkg/perfdata" + "github.com/prometheus-community/windows_exporter/pkg/types" + "github.com/prometheus/client_golang/prometheus" + "github.com/yusufpapurcu/wmi" +) + +const ( + Name = "perfdata" +) + +type Config struct { + Objects []Object `yaml:"objects"` +} + +var ConfigDefaults = Config{ + Objects: make([]Object, 0), +} + +// A Collector is a Prometheus collector for perfdata metrics. +type Collector struct { + config Config +} + +func New(config *Config) *Collector { + if config == nil { + config = &ConfigDefaults + } + + if config.Objects == nil { + config.Objects = ConfigDefaults.Objects + } + + c := &Collector{ + config: *config, + } + + return c +} + +func NewWithFlags(app *kingpin.Application) *Collector { + c := &Collector{ + config: ConfigDefaults, + } + + var objects string + + app.Flag( + "collector.perfdata.objects", + "Objects of performance data to observe. See docs for more information on how to use this flag. By default, no objects are observed.", + ).Default("").StringVar(&objects) + + app.Action(func(*kingpin.ParseContext) error { + if objects == "" { + return nil + } + + if err := json.Unmarshal([]byte(objects), &c.config.Objects); err != nil { + return fmt.Errorf("failed to parse objects: %w", err) + } + + return nil + }) + + return c +} + +func (c *Collector) GetName() string { + return Name +} + +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { + return []string{}, nil +} + +func (c *Collector) Close(_ *slog.Logger) error { + for _, object := range c.config.Objects { + object.collector.Close() + } + + return nil +} + +func (c *Collector) Build(logger *slog.Logger, _ *wmi.Client) error { + logger.Warn("The perfdata collector is in an experimental state! The configuration may change in future. Please report any issues.") + + for i, object := range c.config.Objects { + collector, err := perfdata.NewCollector(object.Object, object.Instances, slices.Sorted(maps.Keys(object.Counters))) + if err != nil { + return fmt.Errorf("failed to create pdh collector: %w", err) + } + + if object.InstanceLabel == "" { + c.config.Objects[i].InstanceLabel = "instance" + } + + c.config.Objects[i].collector = collector + } + + return nil +} + +// Collect sends the metric values for each metric +// to the provided prometheus Metric channel. +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + if err := c.collect(ch); err != nil { + logger.Error("failed collecting performance data metrics", + slog.Any("err", err), + ) + + return err + } + + return nil +} + +func (c *Collector) collect(ch chan<- prometheus.Metric) error { + for _, object := range c.config.Objects { + data, err := object.collector.Collect() + if err != nil { + return fmt.Errorf("failed to collect data: %w", err) + } + + for instance, counters := range data { + for counter, value := range counters { + var labels prometheus.Labels + if instance != perfdata.EmptyInstance { + labels = prometheus.Labels{object.InstanceLabel: instance} + } + + metricType := value.Type + + if val, ok := object.Counters[counter]; ok { + switch val.Type { + case "counter": + metricType = prometheus.CounterValue + case "gauge": + metricType = prometheus.GaugeValue + } + } + + ch <- prometheus.MustNewConstMetric( + prometheus.NewDesc( + sanitizeMetricName(fmt.Sprintf("%s_perfdata_%s_%s", types.Namespace, object.Object, counter)), + fmt.Sprintf("Performance data for \\%s\\%s", object.Object, counter), + nil, + labels, + ), + metricType, + value.FirstValue, + ) + } + } + } + + return nil +} + +func sanitizeMetricName(name string) string { + replacer := strings.NewReplacer( + ".", "", + "%", "", + "/", "_", + " ", "_", + "-", "_", + ) + + return strings.Trim(replacer.Replace(strings.ToLower(name)), "_") +} diff --git a/pkg/collector/perfdata/perfdata_collector_test.go b/pkg/collector/perfdata/perfdata_collector_test.go new file mode 100644 index 000000000..eddbd2b10 --- /dev/null +++ b/pkg/collector/perfdata/perfdata_collector_test.go @@ -0,0 +1,87 @@ +//go:build windows + +package perfdata_test + +import ( + "fmt" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "regexp" + "testing" + + "github.com/prometheus-community/windows_exporter/pkg/collector/perfdata" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type collectorAdapter struct { + perfdata.Collector +} + +// Describe implements the prometheus.Collector interface. +func (a collectorAdapter) Describe(_ chan<- *prometheus.Desc) {} + +// Collect implements the prometheus.Collector interface. +func (a collectorAdapter) Collect(ch chan<- prometheus.Metric) { + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + + if err := a.Collector.Collect(nil, logger, ch); err != nil { + panic(fmt.Sprintf("failed to update collector: %v", err)) + } +} + +func TestCollector(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + object string + instances []string + counters map[string]perfdata.Counter + expectedMetrics *regexp.Regexp + }{ + { + object: "Memory", + instances: nil, + counters: map[string]perfdata.Counter{"Available Bytes": {Type: "gauge"}}, + expectedMetrics: regexp.MustCompile(`^# HELP windows_perfdata_memory_available_bytes Performance data for \\\\Memory\\\\Available Bytes\s*# TYPE windows_perfdata_memory_available_bytes gauge\s*windows_perfdata_memory_available_bytes \d`), + }, + { + object: "Process", + instances: []string{"*"}, + counters: map[string]perfdata.Counter{"Thread Count": {Type: "counter"}}, + expectedMetrics: regexp.MustCompile(`^# HELP windows_perfdata_process_thread_count Performance data for \\\\Process\\\\Thread Count\s*# TYPE windows_perfdata_process_thread_count counter\s*windows_perfdata_process_thread_count\{instance=".+"} \d`), + }, + } { + t.Run(tc.object, func(t *testing.T) { + t.Parallel() + + perfDataCollector := perfdata.New(&perfdata.Config{ + Objects: []perfdata.Object{ + { + Object: tc.object, + Instances: tc.instances, + Counters: tc.counters, + }, + }, + }) + + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + err := perfDataCollector.Build(logger, nil) + require.NoError(t, err) + + registry := prometheus.NewRegistry() + registry.MustRegister(collectorAdapter{*perfDataCollector}) + + rw := httptest.NewRecorder() + promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(rw, &http.Request{}) + got := rw.Body.String() + + assert.NotEmpty(t, got) + assert.Regexp(t, tc.expectedMetrics, got) + }) + } +} diff --git a/pkg/collector/perfdata/perfdata_test.go b/pkg/collector/perfdata/perfdata_test.go new file mode 100644 index 000000000..17222815f --- /dev/null +++ b/pkg/collector/perfdata/perfdata_test.go @@ -0,0 +1,18 @@ +//go:build windows + +package perfdata_test + +import ( + "testing" + + "github.com/alecthomas/kingpin/v2" + "github.com/prometheus-community/windows_exporter/pkg/collector/perfdata" + "github.com/prometheus-community/windows_exporter/pkg/testutils" +) + +func BenchmarkCollector(b *testing.B) { + perfDataObjects := `[{"object":"Processor Information","instances":["*"],"counters":{"*": {}}}]` + kingpin.CommandLine.GetArg("collector.perfdata.objects").StringVar(&perfDataObjects) + + testutils.FuncBenchmarkCollector(b, perfdata.Name, perfdata.NewWithFlags) +} diff --git a/pkg/collector/perfdata/types.go b/pkg/collector/perfdata/types.go new file mode 100644 index 000000000..6b77d3dcf --- /dev/null +++ b/pkg/collector/perfdata/types.go @@ -0,0 +1,18 @@ +package perfdata + +import ( + "github.com/prometheus-community/windows_exporter/pkg/perfdata" +) + +type Object struct { + Object string `json:"object" yaml:"object"` + Instances []string `json:"instances" yaml:"instances"` + Counters map[string]Counter `json:"counters" yaml:"counters"` + InstanceLabel string `json:"instance_label" yaml:"instance_label"` //nolint:tagliatelle + + collector *perfdata.Collector +} + +type Counter struct { + Type string `json:"type" yaml:"type"` +} diff --git a/pkg/collector/physical_disk/physical_disk.go b/pkg/collector/physical_disk/physical_disk.go index d762fcd56..2a5816bda 100644 --- a/pkg/collector/physical_disk/physical_disk.go +++ b/pkg/collector/physical_disk/physical_disk.go @@ -4,12 +4,11 @@ package physical_disk import ( "fmt" + "log/slog" "regexp" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -106,15 +105,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"PhysicalDisk"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.requestsQueued = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "requests_queued"), "The number of requests queued to the disk (PhysicalDisk.CurrentDiskQueueLength)", @@ -204,12 +203,16 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting physical_disk metrics", "err", err) + logger.Error("failed collecting physical_disk metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -232,9 +235,11 @@ type PhysicalDisk struct { AvgDiskSecPerTransfer float64 `perflib:"Avg. Disk sec/Transfer"` } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []PhysicalDisk + if err := perflib.UnmarshalObject(ctx.PerfObjects["PhysicalDisk"], &dst, logger); err != nil { return err } diff --git a/pkg/collector/printer/printer.go b/pkg/collector/printer/printer.go index b3b1d61b9..a22dd2bac 100644 --- a/pkg/collector/printer/printer.go +++ b/pkg/collector/printer/printer.go @@ -5,12 +5,11 @@ package printer import ( "errors" "fmt" + "log/slog" "regexp" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -104,11 +103,11 @@ func NewWithFlags(app *kingpin.Application) *Collector { return c } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -139,7 +138,9 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { return []string{"Printer"}, nil } +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { + return []string{"Printer"}, nil +} type wmiPrinter struct { Name string @@ -153,15 +154,21 @@ type wmiPrintJob struct { Status string } -func (c *Collector) Collect(_ *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectPrinterStatus(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed to collect printer status metrics", "err", err) + logger.Error("failed to collect printer status metrics", + slog.Any("err", err), + ) + return err } if err := c.collectPrinterJobStatus(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed to collect printer job status metrics", "err", err) + logger.Error("failed to collect printer job status metrics", + slog.Any("err", err), + ) + return err } diff --git a/pkg/collector/process/process.go b/pkg/collector/process/process.go index 31d58492a..7e422b793 100644 --- a/pkg/collector/process/process.go +++ b/pkg/collector/process/process.go @@ -5,15 +5,13 @@ package process import ( "errors" "fmt" + "log/slog" "regexp" "strconv" "strings" - "syscall" "unsafe" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -124,16 +122,16 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"Process"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Build(logger *slog.Logger, wmiClient *wmi.Client) error { + logger = logger.With(slog.String("collector", Name)) if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") @@ -142,7 +140,7 @@ func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { c.wmiClient = wmiClient if c.config.ProcessInclude.String() == "^(?:.*)$" && c.config.ProcessExclude.String() == "^(?:)$" { - _ = level.Warn(logger).Log("msg", "No filters specified for process collector. This will generate a very large number of metrics!") + logger.Warn("No filters specified for process collector. This will generate a very large number of metrics!") } c.info = prometheus.NewDesc( @@ -285,9 +283,10 @@ type WorkerProcess struct { ProcessId uint64 } -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) data := make([]perflibProcess, 0) + err := perflib.UnmarshalObject(ctx.PerfObjects["Process"], &data, logger) if err != nil { return err @@ -296,7 +295,9 @@ func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan var workerProcesses []WorkerProcess if c.config.EnableWorkerProcess { if err := c.wmiClient.Query("SELECT * FROM WorkerProcess", &workerProcesses, nil, "root\\WebAdministration"); err != nil { - _ = level.Debug(logger).Log("msg", "Could not query WebAdministration namespace for IIS worker processes", "err", err) + logger.Debug("Could not query WebAdministration namespace for IIS worker processes", + slog.Any("err", err), + ) } } @@ -316,6 +317,7 @@ func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan for _, wp := range workerProcesses { if wp.ProcessId == uint64(process.IDProcess) { processName = strings.Join([]string{processName, wp.AppPoolName}, "_") + break } } @@ -323,7 +325,10 @@ func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan cmdLine, processOwner, processGroupID, err := c.getProcessInformation(logger, uint32(process.IDProcess)) if err != nil { - _ = level.Debug(logger).Log("msg", "Failed to get process information", "pid", pid, "err", err) + logger.Debug("Failed to get process information", + slog.String("pid", pid), + slog.Any("err", err), + ) } ch <- prometheus.MustNewConstMetric( @@ -485,7 +490,7 @@ func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan } // ref: https://github.com/microsoft/hcsshim/blob/8beabacfc2d21767a07c20f8dd5f9f3932dbf305/internal/uvm/stats.go#L25 -func (c *Collector) getProcessInformation(logger log.Logger, pid uint32) (string, string, uint32, error) { +func (c *Collector) getProcessInformation(logger *slog.Logger, pid uint32) (string, string, uint32, error) { if pid == 0 { return "", "", 0, nil } @@ -501,7 +506,9 @@ func (c *Collector) getProcessInformation(logger log.Logger, pid uint32) (string defer func(hProcess windows.Handle) { if err := windows.CloseHandle(hProcess); err != nil { - _ = level.Warn(logger).Log("msg", "CloseHandle failed", "err", err) + logger.Warn("CloseHandle failed", + slog.Any("err", err), + ) } }(hProcess) @@ -528,12 +535,14 @@ func (c *Collector) getProcessInformation(logger log.Logger, pid uint32) (string func (c *Collector) getExtendedProcessInformation(hProcess windows.Handle) (string, uint32, error) { // Get the process environment block (PEB) address var pbi windows.PROCESS_BASIC_INFORMATION + retLen := uint32(unsafe.Sizeof(pbi)) if err := windows.NtQueryInformationProcess(hProcess, windows.ProcessBasicInformation, unsafe.Pointer(&pbi), retLen, &retLen); err != nil { return "", 0, fmt.Errorf("failed to query process basic information: %w", err) } peb := windows.PEB{} + err := windows.ReadProcessMemory(hProcess, uintptr(unsafe.Pointer(pbi.PebBaseAddress)), (*byte)(unsafe.Pointer(&peb)), @@ -545,6 +554,7 @@ func (c *Collector) getExtendedProcessInformation(hProcess windows.Handle) (stri } processParameters := windows.RTL_USER_PROCESS_PARAMETERS{} + err = windows.ReadProcessMemory(hProcess, uintptr(unsafe.Pointer(peb.ProcessParameters)), (*byte)(unsafe.Pointer(&processParameters)), @@ -570,7 +580,7 @@ func (c *Collector) getExtendedProcessInformation(hProcess windows.Handle) (stri return strings.TrimSpace(windows.UTF16ToString(cmdLineUTF16)), processParameters.ProcessGroupId, nil } -func (c *Collector) getProcessOwner(logger log.Logger, hProcess windows.Handle) (string, error) { +func (c *Collector) getProcessOwner(logger *slog.Logger, hProcess windows.Handle) (string, error) { var tok windows.Token if err := windows.OpenProcessToken(hProcess, windows.TOKEN_QUERY, &tok); err != nil { @@ -583,7 +593,9 @@ func (c *Collector) getProcessOwner(logger log.Logger, hProcess windows.Handle) defer func(tok windows.Token) { if err := tok.Close(); err != nil { - _ = level.Warn(logger).Log("msg", "Token close failed", "err", err) + logger.Warn("Token close failed", + slog.Any("err", err), + ) } }(tok) @@ -620,7 +632,7 @@ func (c *Collector) openProcess(pid uint32) (windows.Handle, bool, error) { return 0, false, fmt.Errorf("failed to open process: %w", err) } - if errors.Is(err, syscall.Errno(0x57)) { // invalid parameter, for PIDs that don't exist + if errors.Is(err, windows.Errno(0x57)) { // invalid parameter, for PIDs that don't exist return 0, false, errors.New("process not found") } diff --git a/pkg/collector/prometheus.go b/pkg/collector/prometheus.go index 843f7bba4..9c6ef06e9 100644 --- a/pkg/collector/prometheus.go +++ b/pkg/collector/prometheus.go @@ -4,48 +4,71 @@ package collector import ( "fmt" + "log/slog" "sync" + "sync/atomic" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" ) -// Prometheus implements prometheus.Collector for a set of Windows Collectors. +// Interface guard. +var _ prometheus.Collector = (*Prometheus)(nil) + +// Prometheus implements prometheus.Collector for a set of Windows MetricCollectors. type Prometheus struct { maxScrapeDuration time.Duration - collectors *Collectors - logger log.Logger + logger *slog.Logger + metricCollectors *MetricCollectors // Base metrics returned by Prometheus - scrapeDurationDesc *prometheus.Desc - scrapeSuccessDesc *prometheus.Desc - scrapeTimeoutDesc *prometheus.Desc - snapshotDuration *prometheus.Desc + scrapeDurationDesc *prometheus.Desc + collectorScrapeDurationDesc *prometheus.Desc + collectorScrapeSuccessDesc *prometheus.Desc + collectorScrapeTimeoutDesc *prometheus.Desc + snapshotDuration *prometheus.Desc +} + +type collectorStatus struct { + name string + statusCode collectorStatusCode } -// NewPrometheus returns a new Prometheus where the set of Collectors must +type collectorStatusCode int + +const ( + pending collectorStatusCode = iota + success + failed +) + +// NewPrometheusCollector returns a new Prometheus where the set of MetricCollectors must // return metrics within the given timeout. -func NewPrometheus(timeout time.Duration, cs *Collectors, logger log.Logger) *Prometheus { +func (c *MetricCollectors) NewPrometheusCollector(timeout time.Duration, logger *slog.Logger) *Prometheus { return &Prometheus{ maxScrapeDuration: timeout, - collectors: cs, + metricCollectors: c, logger: logger, scrapeDurationDesc: prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, "exporter", "scrape_duration_seconds"), + "windows_exporter: Total scrape duration.", + nil, + nil, + ), + collectorScrapeDurationDesc: prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, "exporter", "collector_duration_seconds"), "windows_exporter: Duration of a collection.", []string{"collector"}, nil, ), - scrapeSuccessDesc: prometheus.NewDesc( + collectorScrapeSuccessDesc: prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, "exporter", "collector_success"), "windows_exporter: Whether the collector was successful.", []string{"collector"}, nil, ), - scrapeTimeoutDesc: prometheus.NewDesc( + collectorScrapeTimeoutDesc: prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, "exporter", "collector_timeout"), "windows_exporter: Whether the collector timed out.", []string{"collector"}, @@ -60,133 +83,159 @@ func NewPrometheus(timeout time.Duration, cs *Collectors, logger log.Logger) *Pr } } -// Describe sends all the descriptors of the Collectors included to -// the provided channel. -func (coll *Prometheus) Describe(ch chan<- *prometheus.Desc) { - ch <- coll.scrapeDurationDesc - ch <- coll.scrapeSuccessDesc -} - -type collectorOutcome int - -const ( - pending collectorOutcome = iota - success - failed -) +func (p *Prometheus) Describe(_ chan<- *prometheus.Desc) {} -// Collect sends the collected metrics from each of the Collectors to +// Collect sends the collected metrics from each of the MetricCollectors to // prometheus. -func (coll *Prometheus) Collect(ch chan<- prometheus.Metric) { +func (p *Prometheus) Collect(ch chan<- prometheus.Metric) { t := time.Now() - scrapeContext, err := coll.collectors.PrepareScrapeContext() + // Scrape Performance Counters for all collectors + scrapeContext, err := p.metricCollectors.PrepareScrapeContext() + ch <- prometheus.MustNewConstMetric( - coll.snapshotDuration, + p.snapshotDuration, prometheus.GaugeValue, time.Since(t).Seconds(), ) + if err != nil { - ch <- prometheus.NewInvalidMetric(coll.scrapeSuccessDesc, fmt.Errorf("failed to prepare scrape: %w", err)) + ch <- prometheus.NewInvalidMetric(p.collectorScrapeSuccessDesc, fmt.Errorf("failed to prepare scrape: %w", err)) + return } + // WaitGroup to wait for all collectors to finish wg := sync.WaitGroup{} - wg.Add(len(coll.collectors.collectors)) - collectorOutcomes := make(map[string]collectorOutcome) - for name := range coll.collectors.collectors { - collectorOutcomes[name] = pending - } + wg.Add(len(p.metricCollectors.Collectors)) - metricsBuffer := make(chan prometheus.Metric) - l := sync.Mutex{} - finished := false - go func() { - for m := range metricsBuffer { - l.Lock() - if !finished { - ch <- m - } - l.Unlock() - } - }() + // Using a channel to collect the status of each collector + // A channel is safe to use concurrently while a map is not + collectorStatusCh := make(chan collectorStatus, len(p.metricCollectors.Collectors)) - for name, c := range coll.collectors.collectors { - go func(name string, c Collector) { + // Execute all collectors concurrently + // timeout handling is done in the execute function + for name, metricsCollector := range p.metricCollectors.Collectors { + go func(name string, metricsCollector Collector) { defer wg.Done() - outcome := coll.execute(coll.logger, name, c, scrapeContext, metricsBuffer) - l.Lock() - if !finished { - collectorOutcomes[name] = outcome + + collectorStatusCh <- collectorStatus{ + name: name, + statusCode: p.execute(name, metricsCollector, scrapeContext, ch), } - l.Unlock() - }(name, c) + }(name, metricsCollector) } - allDone := make(chan struct{}) - go func() { - wg.Wait() - close(allDone) - close(metricsBuffer) - }() + // Wait for all collectors to finish + wg.Wait() - // Wait until either all Collectors finish, or timeout expires - select { - case <-allDone: - case <-time.After(coll.maxScrapeDuration): - } + // Close the channel since we are done writing to it + close(collectorStatusCh) - l.Lock() - finished = true - - remainingCollectorNames := make([]string, 0) - for name, outcome := range collectorOutcomes { + for status := range collectorStatusCh { var successValue, timeoutValue float64 - if outcome == pending { + if status.statusCode == pending { timeoutValue = 1.0 - remainingCollectorNames = append(remainingCollectorNames, name) } - if outcome == success { + + if status.statusCode == success { successValue = 1.0 } ch <- prometheus.MustNewConstMetric( - coll.scrapeSuccessDesc, + p.collectorScrapeSuccessDesc, prometheus.GaugeValue, successValue, - name, + status.name, ) + ch <- prometheus.MustNewConstMetric( - coll.scrapeTimeoutDesc, + p.collectorScrapeTimeoutDesc, prometheus.GaugeValue, timeoutValue, - name, + status.name, ) } - if len(remainingCollectorNames) > 0 { - _ = level.Warn(coll.logger).Log("msg", fmt.Sprintf("Collection timed out, still waiting for %v", remainingCollectorNames)) - } - - l.Unlock() -} - -func (coll *Prometheus) execute(logger log.Logger, name string, c Collector, ctx *types.ScrapeContext, ch chan<- prometheus.Metric) collectorOutcome { - t := time.Now() - err := c.Collect(ctx, logger, ch) - duration := time.Since(t).Seconds() ch <- prometheus.MustNewConstMetric( - coll.scrapeDurationDesc, + p.scrapeDurationDesc, prometheus.GaugeValue, - duration, - name, + time.Since(t).Seconds(), ) +} + +func (p *Prometheus) execute(name string, c Collector, ctx *types.ScrapeContext, ch chan<- prometheus.Metric) collectorStatusCode { + var ( + err error + duration time.Duration + timeout atomic.Bool + ) + + // bufCh is a buffer channel to store the metrics + // This is needed because once timeout is reached, the prometheus registry channel is closed. + bufCh := make(chan prometheus.Metric, 10) + errCh := make(chan error, 1) + + // Execute the collector + go func() { + errCh <- c.Collect(ctx, p.logger, bufCh) + + close(bufCh) + }() + + go func() { + defer func() { + // This prevents a panic from race-condition when closing the ch channel too early. + _ = recover() + }() + + // Pass metrics to the prometheus registry + // If timeout is reached, the channel is closed. + // This will cause a panic if we try to write to it. + for m := range bufCh { + if !timeout.Load() { + ch <- m + } + } + }() + + t := time.Now() + + // Wait for the collector to finish or timeout + select { + case err = <-errCh: + duration = time.Since(t) + ch <- prometheus.MustNewConstMetric( + p.collectorScrapeDurationDesc, + prometheus.GaugeValue, + duration.Seconds(), + name, + ) + case <-time.After(p.maxScrapeDuration): + timeout.Store(true) + + duration = time.Since(t) + ch <- prometheus.MustNewConstMetric( + p.collectorScrapeDurationDesc, + prometheus.GaugeValue, + duration.Seconds(), + name, + ) + + p.logger.Warn(fmt.Sprintf("collector %s timeouted after %s", name, p.maxScrapeDuration)) + + return pending + } if err != nil { - _ = level.Error(coll.logger).Log("msg", fmt.Sprintf("collector %s failed after %fs", name, duration), "err", err) + p.logger.Error(fmt.Sprintf("collector %s failed after %s", name, p.maxScrapeDuration), + slog.Any("err", err), + ) + return failed } - _ = level.Debug(coll.logger).Log("msg", fmt.Sprintf("collector %s succeeded after %fs.", name, duration)) + p.logger.Error(fmt.Sprintf("collector %s succeeded after %s", name, p.maxScrapeDuration)) + return success } diff --git a/pkg/collector/remote_fx/remote_fx.go b/pkg/collector/remote_fx/remote_fx.go index 146a129c4..c6ad73129 100644 --- a/pkg/collector/remote_fx/remote_fx.go +++ b/pkg/collector/remote_fx/remote_fx.go @@ -3,11 +3,10 @@ package remote_fx import ( + "log/slog" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus-community/windows_exporter/pkg/utils" @@ -74,15 +73,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"RemoteFX Network", "RemoteFX Graphics"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(log.Logger, *wmi.Client) error { +func (c *Collector) Build(*slog.Logger, *wmi.Client) error { // net c.baseTCPRTT = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "net_base_tcp_rtt_seconds"), @@ -206,21 +205,30 @@ func (c *Collector) Build(log.Logger, *wmi.Client) error { []string{"session_name"}, nil, ) + return nil } // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectRemoteFXNetworkCount(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting terminal services session count metrics", "err", err) + logger.Error("failed collecting terminal services session count metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectRemoteFXGraphicsCounters(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting terminal services session count metrics", "err", err) + logger.Error("failed collecting terminal services session count metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -241,9 +249,10 @@ type perflibRemoteFxNetwork struct { RetransmissionRate float64 `perflib:"Percentage of packets that have been retransmitted"` } -func (c *Collector) collectRemoteFXNetworkCount(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectRemoteFXNetworkCount(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) dst := make([]perflibRemoteFxNetwork, 0) + err := perflib.UnmarshalObject(ctx.PerfObjects["RemoteFX Network"], &dst, logger) if err != nil { return err @@ -336,6 +345,7 @@ func (c *Collector) collectRemoteFXNetworkCount(ctx *types.ScrapeContext, logger normalizeSessionName(d.Name), ) } + return nil } @@ -352,9 +362,10 @@ type perflibRemoteFxGraphics struct { SourceFramesPerSecond float64 `perflib:"Source Frames/Second"` } -func (c *Collector) collectRemoteFXGraphicsCounters(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectRemoteFXGraphicsCounters(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) dst := make([]perflibRemoteFxGraphics, 0) + err := perflib.UnmarshalObject(ctx.PerfObjects["RemoteFX Graphics"], &dst, logger) if err != nil { return err diff --git a/pkg/collector/scheduled_task/scheduled_task.go b/pkg/collector/scheduled_task/scheduled_task.go index c53598b5f..861bafe45 100644 --- a/pkg/collector/scheduled_task/scheduled_task.go +++ b/pkg/collector/scheduled_task/scheduled_task.go @@ -5,13 +5,12 @@ package scheduled_task import ( "errors" "fmt" + "log/slog" "regexp" "runtime" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/go-ole/go-ole" "github.com/go-ole/go-ole/oleutil" "github.com/prometheus-community/windows_exporter/pkg/types" @@ -129,15 +128,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.lastResult = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "last_result"), "The result that was returned the last time the registered task was run", @@ -162,10 +161,13 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { return nil } -func (c *Collector) Collect(_ *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting user metrics", "err", err) + logger.Error("failed collecting user metrics", + slog.Any("err", err), + ) + return err } @@ -264,10 +266,12 @@ func getScheduledTasks() (ScheduledTasks, error) { defer taskSchedulerObj.Release() taskServiceObj := taskSchedulerObj.MustQueryInterface(ole.IID_IDispatch) + _, err = oleutil.CallMethod(taskServiceObj, "Connect") if err != nil { return scheduledTasks, err } + defer taskServiceObj.Release() res, err := oleutil.CallMethod(taskServiceObj, "GetFolder", `\`) @@ -325,6 +329,7 @@ func fetchTasksRecursively(folder *ole.IDispatch, scheduledTasks *ScheduledTasks err = oleutil.ForEach(subFolders, func(v *ole.VARIANT) error { subFolder := v.ToIDispatch() defer subFolder.Release() + return fetchTasksRecursively(subFolder, scheduledTasks) }) @@ -338,6 +343,7 @@ func parseTask(task *ole.IDispatch) (ScheduledTask, error) { if err != nil { return scheduledTask, err } + defer func() { if tempErr := taskNameVar.Clear(); tempErr != nil { err = tempErr @@ -348,6 +354,7 @@ func parseTask(task *ole.IDispatch) (ScheduledTask, error) { if err != nil { return scheduledTask, err } + defer func() { if tempErr := taskPathVar.Clear(); tempErr != nil { err = tempErr @@ -358,6 +365,7 @@ func parseTask(task *ole.IDispatch) (ScheduledTask, error) { if err != nil { return scheduledTask, err } + defer func() { if tempErr := taskEnabledVar.Clear(); tempErr != nil { err = tempErr @@ -368,6 +376,7 @@ func parseTask(task *ole.IDispatch) (ScheduledTask, error) { if err != nil { return scheduledTask, err } + defer func() { if tempErr := taskStateVar.Clear(); tempErr != nil { err = tempErr @@ -378,6 +387,7 @@ func parseTask(task *ole.IDispatch) (ScheduledTask, error) { if err != nil { return scheduledTask, err } + defer func() { if tempErr := taskNumberOfMissedRunsVar.Clear(); tempErr != nil { err = tempErr @@ -388,6 +398,7 @@ func parseTask(task *ole.IDispatch) (ScheduledTask, error) { if err != nil { return scheduledTask, err } + defer func() { if tempErr := taskLastTaskResultVar.Clear(); tempErr != nil { err = tempErr @@ -396,9 +407,11 @@ func parseTask(task *ole.IDispatch) (ScheduledTask, error) { scheduledTask.Name = taskNameVar.ToString() scheduledTask.Path = strings.ReplaceAll(taskPathVar.ToString(), "\\", "/") + if val, ok := taskEnabledVar.Value().(bool); ok { scheduledTask.Enabled = val } + scheduledTask.State = TaskState(taskStateVar.Val) scheduledTask.MissedRunsCount = float64(taskNumberOfMissedRunsVar.Val) scheduledTask.LastTaskResult = TaskResult(taskLastTaskResultVar.Val) diff --git a/pkg/collector/service/service.go b/pkg/collector/service/service.go index 8442569ab..9b1f8237a 100644 --- a/pkg/collector/service/service.go +++ b/pkg/collector/service/service.go @@ -5,16 +5,13 @@ package service import ( "errors" "fmt" + "log/slog" + "regexp" "strconv" - "strings" - "syscall" "unsafe" "github.com/alecthomas/kingpin/v2" - "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/utils" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" "golang.org/x/sys/windows" @@ -24,30 +21,25 @@ import ( const Name = "service" type Config struct { - ServiceWhereClause string `yaml:"service_where_clause"` - UseAPI bool `yaml:"use_api"` - V2 bool `yaml:"v2"` + ServiceInclude *regexp.Regexp `yaml:"service_include"` + ServiceExclude *regexp.Regexp `yaml:"service_exclude"` } var ConfigDefaults = Config{ - ServiceWhereClause: "", - UseAPI: false, - V2: false, + ServiceInclude: types.RegExpAny, + ServiceExclude: types.RegExpEmpty, } -// A Collector is a Prometheus Collector for WMI Win32_Service metrics. +// A Collector is a Prometheus Collector for service metrics. type Collector struct { - serviceWhereClause *string - useAPI *bool - v2 *bool + config Config - wmiClient *wmi.Client + state *prometheus.Desc + processID *prometheus.Desc + info *prometheus.Desc + startMode *prometheus.Desc - Information *prometheus.Desc - State *prometheus.Desc - StartMode *prometheus.Desc - Status *prometheus.Desc - StateV2 *prometheus.Desc + serviceManagerHandle *mgr.Mgr } func New(config *Config) *Collector { @@ -55,372 +47,287 @@ func New(config *Config) *Collector { config = &ConfigDefaults } + if config.ServiceExclude == nil { + config.ServiceExclude = ConfigDefaults.ServiceExclude + } + + if config.ServiceInclude == nil { + config.ServiceInclude = ConfigDefaults.ServiceInclude + } + c := &Collector{ - serviceWhereClause: &config.ServiceWhereClause, - useAPI: &config.UseAPI, + config: *config, } return c } func NewWithFlags(app *kingpin.Application) *Collector { - return &Collector{ - serviceWhereClause: app.Flag( - "collector.service.services-where", - "WQL 'where' clause to use in WMI metrics query. Limits the response to the services you specify and reduces the size of the response.", - ).Default(ConfigDefaults.ServiceWhereClause).String(), - useAPI: app.Flag( - "collector.service.use-api", - "Use API calls to collect service data instead of WMI. Flag 'collector.service.services-where' won't be effective.", - ).Default(strconv.FormatBool(ConfigDefaults.UseAPI)).Bool(), - v2: app.Flag( - "collector.service.v2", - "Enable V2 service collector. This collector can services state much more efficiently, can't provide general service information.", - ).Default(strconv.FormatBool(ConfigDefaults.V2)).Bool(), + c := &Collector{ + config: ConfigDefaults, } + + var serviceExclude, serviceInclude string + + app.Flag( + "collector.service.exclude", + "Regexp of service to exclude. Service name (not the display name!) must both match include and not match exclude to be included.", + ).Default(c.config.ServiceExclude.String()).StringVar(&serviceExclude) + + app.Flag( + "collector.service.include", + "Regexp of service to include. Process name (not the display name!) must both match include and not match exclude to be included.", + ).Default(c.config.ServiceInclude.String()).StringVar(&serviceInclude) + + app.Action(func(*kingpin.ParseContext) error { + var err error + + c.config.ServiceExclude, err = regexp.Compile(fmt.Sprintf("^(?:%s)$", serviceExclude)) + if err != nil { + return fmt.Errorf("collector.process.exclude: %w", err) + } + + c.config.ServiceInclude, err = regexp.Compile(fmt.Sprintf("^(?:%s)$", serviceInclude)) + if err != nil { + return fmt.Errorf("collector.process.include: %w", err) + } + + return nil + }) + + return c } func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { - return nil -} - -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 +func (c *Collector) Build(logger *slog.Logger, _ *wmi.Client) error { + logger = logger.With(slog.String("collector", Name)) - logger = log.With(logger, "collector", Name) - - if utils.IsEmpty(c.serviceWhereClause) { - _ = level.Warn(logger).Log("msg", "No where-clause specified for service collector. This will generate a very large number of metrics!") - } - if *c.useAPI { - _ = level.Warn(logger).Log("msg", "API collection is enabled.") + if c.config.ServiceInclude.String() == "^(?:.*)$" && c.config.ServiceExclude.String() == "^(?:)$" { + logger.Warn("No filters specified for service collector. This will generate a very large number of metrics!") } - c.Information = prometheus.NewDesc( + c.info = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "info"), "A metric with a constant '1' value labeled with service information", - []string{"name", "display_name", "process_id", "run_as"}, + []string{"name", "display_name", "run_as", "path_name"}, nil, ) - c.State = prometheus.NewDesc( + c.state = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "state"), "The state of the service (State)", - []string{"name", "state"}, + []string{"name", "status"}, nil, ) - c.StartMode = prometheus.NewDesc( + c.startMode = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "start_mode"), "The start mode of the service (StartMode)", []string{"name", "start_mode"}, nil, ) - c.Status = prometheus.NewDesc( - prometheus.BuildFQName(types.Namespace, Name, "status"), - "The status of the service (Status)", - []string{"name", "status"}, - nil, - ) - c.StateV2 = prometheus.NewDesc( - prometheus.BuildFQName(types.Namespace, Name, "state"), - "The state of the service (State)", - []string{"name", "display_name", "status"}, + c.processID = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "process"), + "Process of started service. The value is the creation time of the process as a unix timestamp.", + []string{"name", "process_id"}, nil, ) + // EnumServiceStatusEx requires only SC_MANAGER_ENUM_SERVICE. + handle, err := windows.OpenSCManager(nil, nil, windows.SC_MANAGER_ENUMERATE_SERVICE) + if err != nil { + return fmt.Errorf("failed to open scm: %w", err) + } + + c.serviceManagerHandle = &mgr.Mgr{Handle: handle} + return nil } -// Collect sends the metric values for each metric -// 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) - var err error - - switch { - case *c.useAPI: - if err = c.collectAPI(logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting API service metrics:", "err", err) - } - case *c.v2: - if err = c.collectAPIV2(logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting API service metrics:", "err", err) - } - default: - if err = c.collectWMI(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting WMI service metrics:", "err", err) - } +func (c *Collector) Close(logger *slog.Logger) error { + if err := c.serviceManagerHandle.Disconnect(); err != nil { + logger.Warn("Failed to disconnect from scm", + slog.Any("err", err), + ) } - return err + return nil } -// Win32_Service docs: -// - https://msdn.microsoft.com/en-us/library/aa394418(v=vs.85).aspx -type Win32_Service struct { - DisplayName string - Name string - ProcessId uint32 - State string - Status string - StartMode string - StartName *string -} +// Collect sends the metric values for each metric +// to the provided prometheus Metric channel. +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) -var ( - allStates = []string{ - "stopped", - "start pending", - "stop pending", - "running", - "continue pending", - "pause pending", - "paused", - "unknown", - } - apiStateValues = map[uint32]string{ - windows.SERVICE_CONTINUE_PENDING: "continue pending", - windows.SERVICE_PAUSE_PENDING: "pause pending", - windows.SERVICE_PAUSED: "paused", - windows.SERVICE_RUNNING: "running", - windows.SERVICE_START_PENDING: "start pending", - windows.SERVICE_STOP_PENDING: "stop pending", - windows.SERVICE_STOPPED: "stopped", - } - allStartModes = []string{ - "boot", - "system", - "auto", - "manual", - "disabled", - } - apiStartModeValues = map[uint32]string{ - windows.SERVICE_AUTO_START: "auto", - windows.SERVICE_BOOT_START: "boot", - windows.SERVICE_DEMAND_START: "manual", - windows.SERVICE_DISABLED: "disabled", - windows.SERVICE_SYSTEM_START: "system", - } - allStatuses = []string{ - "ok", - "error", - "degraded", - "unknown", - "pred fail", - "starting", - "stopping", - "service", - "stressed", - "nonrecover", - "no contact", - "lost comm", + if err := c.collect(logger, ch); err != nil { + logger.Error("failed collecting API service metrics:", + slog.Any("err", err), + ) + + return fmt.Errorf("failed collecting API service metrics: %w", err) } -) -func (c *Collector) collectWMI(ch chan<- prometheus.Metric) error { - var dst []Win32_Service - query := "SELECT * FROM Win32_Service" + return nil +} - if *c.serviceWhereClause != "" { - query += " WHERE " + *c.serviceWhereClause - } +func (c *Collector) collect(logger *slog.Logger, ch chan<- prometheus.Metric) error { + services, err := c.queryAllServices() + if err != nil { + logger.Warn("Failed to query services", + slog.Any("err", err), + ) - if err := c.wmiClient.Query(query, &dst); err != nil { return err } - for _, service := range dst { - pid := strconv.FormatUint(uint64(service.ProcessId), 10) - runAs := "" - if service.StartName != nil { - runAs = *service.StartName - } - ch <- prometheus.MustNewConstMetric( - c.Information, - prometheus.GaugeValue, - 1.0, - strings.ToLower(service.Name), - service.DisplayName, - pid, - runAs, - ) + if services == nil { + logger.Warn("No services queried") - for _, state := range allStates { - isCurrentState := 0.0 - if state == strings.ToLower(service.State) { - isCurrentState = 1.0 - } - ch <- prometheus.MustNewConstMetric( - c.State, - prometheus.GaugeValue, - isCurrentState, - strings.ToLower(service.Name), - state, - ) - } + return nil + } - for _, startMode := range allStartModes { - isCurrentStartMode := 0.0 - if startMode == strings.ToLower(service.StartMode) { - isCurrentStartMode = 1.0 - } - ch <- prometheus.MustNewConstMetric( - c.StartMode, - prometheus.GaugeValue, - isCurrentStartMode, - strings.ToLower(service.Name), - startMode, - ) + // Iterate through the Services List. + for _, service := range services { + serviceName := windows.UTF16PtrToString(service.ServiceName) + if c.config.ServiceExclude.MatchString(serviceName) || + !c.config.ServiceInclude.MatchString(serviceName) { + continue } - for _, status := range allStatuses { - isCurrentStatus := 0.0 - if status == strings.ToLower(service.Status) { - isCurrentStatus = 1.0 - } - ch <- prometheus.MustNewConstMetric( - c.Status, - prometheus.GaugeValue, - isCurrentStatus, - strings.ToLower(service.Name), - status, + if err := c.collectService(ch, logger, service); err != nil { + logger.Warn("failed collecting service info", + slog.Any("err", err), + slog.String("service", windows.UTF16PtrToString(service.ServiceName)), ) } } + return nil } -func (c *Collector) collectAPI(logger log.Logger, ch chan<- prometheus.Metric) error { - svcmgrConnection, err := mgr.Connect() - if err != nil { - return err - } - defer svcmgrConnection.Disconnect() //nolint:errcheck +var apiStateValues = map[uint32]string{ + windows.SERVICE_CONTINUE_PENDING: "continue pending", + windows.SERVICE_PAUSE_PENDING: "pause pending", + windows.SERVICE_PAUSED: "paused", + windows.SERVICE_RUNNING: "running", + windows.SERVICE_START_PENDING: "start pending", + windows.SERVICE_STOP_PENDING: "stop pending", + windows.SERVICE_STOPPED: "stopped", +} + +var apiStartModeValues = map[uint32]string{ + windows.SERVICE_AUTO_START: "auto", + windows.SERVICE_BOOT_START: "boot", + windows.SERVICE_DEMAND_START: "manual", + windows.SERVICE_DISABLED: "disabled", + windows.SERVICE_SYSTEM_START: "system", +} - // List All Services from the Services Manager. - serviceList, err := svcmgrConnection.ListServices() +func (c *Collector) collectService(ch chan<- prometheus.Metric, logger *slog.Logger, service windows.ENUM_SERVICE_STATUS_PROCESS) error { + // Open connection for service handler. + serviceHandle, err := windows.OpenService(c.serviceManagerHandle.Handle, service.ServiceName, windows.SERVICE_QUERY_CONFIG) if err != nil { - return err + return fmt.Errorf("failed to open service: %w", err) } - // Iterate through the Services List. - for _, service := range serviceList { - (func() { - // Get UTF16 service name. - serviceName, err := syscall.UTF16PtrFromString(service) - if err != nil { - _ = level.Warn(logger).Log("msg", fmt.Sprintf("Service %s get name error: %#v", service, err)) - return - } - - // Open connection for service handler. - serviceHandle, err := windows.OpenService(svcmgrConnection.Handle, serviceName, windows.GENERIC_READ) - if err != nil { - _ = level.Warn(logger).Log("msg", fmt.Sprintf("Open service %s error: %#v", service, err)) - return - } + serviceNameString := windows.UTF16PtrToString(service.ServiceName) - // Create handle for each service. - serviceManager := &mgr.Service{Name: service, Handle: serviceHandle} - defer serviceManager.Close() - - // Get Service Configuration. - serviceConfig, err := serviceManager.Config() - if err != nil { - _ = level.Warn(logger).Log("msg", fmt.Sprintf("Get service %s config error: %#v", service, err)) - return - } + // Create handle for each service. + serviceManager := &mgr.Service{Name: serviceNameString, Handle: serviceHandle} + defer func(serviceManager *mgr.Service) { + if err := serviceManager.Close(); err != nil { + logger.Warn("failed to close service handle", + slog.Any("err", err), + slog.String("service", serviceNameString), + ) + } + }(serviceManager) - // Get Service Current Status. - serviceStatus, err := serviceManager.Query() - if err != nil { - _ = level.Warn(logger).Log("msg", fmt.Sprintf("Get service %s status error: %#v", service, err)) - return - } + // Get Service Configuration. + serviceConfig, err := serviceManager.Config() + if err != nil { + if !errors.Is(err, windows.ERROR_FILE_NOT_FOUND) && !errors.Is(err, windows.ERROR_MUI_FILE_NOT_FOUND) { + return fmt.Errorf("failed to get service configuration: %w", err) + } - pid := strconv.FormatUint(uint64(serviceStatus.ProcessId), 10) + logger.Debug("failed collecting service", + slog.Any("err", err), + slog.String("service", serviceNameString), + ) + } - ch <- prometheus.MustNewConstMetric( - c.Information, - prometheus.GaugeValue, - 1.0, - strings.ToLower(service), - serviceConfig.DisplayName, - pid, - serviceConfig.ServiceStartName, - ) + ch <- prometheus.MustNewConstMetric( + c.info, + prometheus.GaugeValue, + 1.0, + serviceNameString, + serviceConfig.DisplayName, + serviceConfig.ServiceStartName, + serviceConfig.BinaryPathName, + ) - for _, state := range apiStateValues { - isCurrentState := 0.0 - if state == apiStateValues[uint32(serviceStatus.State)] { - isCurrentState = 1.0 - } - ch <- prometheus.MustNewConstMetric( - c.State, - prometheus.GaugeValue, - isCurrentState, - strings.ToLower(service), - state, - ) - } + var ( + isCurrentStartMode float64 + isCurrentState float64 + ) - for _, startMode := range apiStartModeValues { - isCurrentStartMode := 0.0 - if startMode == apiStartModeValues[serviceConfig.StartType] { - isCurrentStartMode = 1.0 - } - ch <- prometheus.MustNewConstMetric( - c.StartMode, - prometheus.GaugeValue, - isCurrentStartMode, - strings.ToLower(service), - startMode, - ) - } - })() + for _, startMode := range apiStartModeValues { + isCurrentStartMode = 0.0 + if startMode == apiStartModeValues[serviceConfig.StartType] { + isCurrentStartMode = 1.0 + } + ch <- prometheus.MustNewConstMetric( + c.startMode, + prometheus.GaugeValue, + isCurrentStartMode, + serviceNameString, + startMode, + ) } - return nil -} -func (c *Collector) collectAPIV2(logger log.Logger, ch chan<- prometheus.Metric) error { - services, err := c.queryAllServiceStates(logger) - if err != nil { - _ = level.Warn(logger).Log("msg", "Failed to query services", "err", err) - return err - } + for state, stateValue := range apiStateValues { + isCurrentState = 0.0 + if state == service.ServiceStatusProcess.CurrentState { + isCurrentState = 1.0 + } - if services == nil { - _ = level.Warn(logger).Log("msg", "No services queried") - return nil + ch <- prometheus.MustNewConstMetric( + c.state, + prometheus.GaugeValue, + isCurrentState, + serviceNameString, + stateValue, + ) } - var isCurrentState float64 + processID := strconv.FormatUint(uint64(service.ServiceStatusProcess.ProcessId), 10) - for _, svc := range services { - for state, stateValue := range apiStateValues { - isCurrentState = 0.0 - if state == svc.ServiceStatusProcess.CurrentState { - isCurrentState = 1.0 + if processID != "0" { //nolint: nestif + processStartTime, err := getProcessStartTime(logger, service.ServiceStatusProcess.ProcessId) + if err != nil { + if errors.Is(err, windows.ERROR_ACCESS_DENIED) { + logger.Debug("failed to get process start time", + slog.String("service", serviceNameString), + slog.Any("err", err), + ) + } else { + logger.Warn("failed to get process start time", + slog.String("service", serviceNameString), + slog.Any("err", err), + ) } - + } else { ch <- prometheus.MustNewConstMetric( - c.StateV2, + c.processID, prometheus.GaugeValue, - isCurrentState, - windows.UTF16PtrToString(svc.ServiceName), - windows.UTF16PtrToString(svc.DisplayName), - stateValue, + float64(processStartTime/1_000_000_000), + serviceNameString, + processID, ) } } @@ -428,56 +335,82 @@ func (c *Collector) collectAPIV2(logger log.Logger, ch chan<- prometheus.Metric) return nil } -// queryAllServiceStates returns all service states of the current Windows system +// queryAllServices returns all service states of the current Windows system // This is realized by ask Service Manager directly. -// -// Unless explicitly stated otherwise all files in this repository are licensed -// under the Apache License Version 2.0. -// This product includes software developed at Datadog (https://www.datadoghq.com/). -// Copyright 2016-present Datadog, Inc. -// -// Source: https://github.com/DataDog/datadog-agent/blob/afbd8b6c87939c92610c654cb07fdfd439e4fb27/pkg/util/winutil/scmmonitor.go#L61-L96 -func (c *Collector) queryAllServiceStates(logger log.Logger) ([]windows.ENUM_SERVICE_STATUS_PROCESS, error) { - // EnumServiceStatusEx requires only SC_MANAGER_ENUM_SERVICE. - h, err := windows.OpenSCManager(nil, nil, windows.SC_MANAGER_ENUMERATE_SERVICE) - if err != nil { - return nil, fmt.Errorf("failed to open scm: %w", err) +func (c *Collector) queryAllServices() ([]windows.ENUM_SERVICE_STATUS_PROCESS, error) { + var ( + bytesNeeded uint32 + servicesReturned uint32 + resumeHandle uint32 + ) + + if err := windows.EnumServicesStatusEx( + c.serviceManagerHandle.Handle, + windows.SC_STATUS_PROCESS_INFO, + windows.SERVICE_WIN32, + windows.SERVICE_STATE_ALL, + nil, + 0, + &bytesNeeded, + &servicesReturned, + &resumeHandle, + nil, + ); !errors.Is(err, windows.ERROR_MORE_DATA) { + return nil, fmt.Errorf("could not fetch buffer size for EnumServicesStatusEx: %w", err) } - m := &mgr.Mgr{Handle: h} - defer func() { - if err := m.Disconnect(); err != nil { - _ = level.Warn(logger).Log("msg", "Failed to disconnect from scm", "err", err) - } - }() - - var bytesNeeded, servicesReturned uint32 - var buf []byte - for { - var p *byte - if len(buf) > 0 { - p = &buf[0] - } - err = windows.EnumServicesStatusEx(m.Handle, windows.SC_ENUM_PROCESS_INFO, - windows.SERVICE_WIN32, windows.SERVICE_STATE_ALL, - p, uint32(len(buf)), &bytesNeeded, &servicesReturned, nil, nil) - if err == nil { - break - } - if !errors.Is(err, windows.ERROR_MORE_DATA) { - return nil, fmt.Errorf("failed to enum services %w", err) - } - if bytesNeeded <= uint32(len(buf)) { - return nil, err - } - buf = make([]byte, bytesNeeded) + buf := make([]byte, bytesNeeded) + if err := windows.EnumServicesStatusEx( + c.serviceManagerHandle.Handle, + windows.SC_STATUS_PROCESS_INFO, + windows.SERVICE_WIN32, + windows.SERVICE_STATE_ALL, + &buf[0], + bytesNeeded, + &bytesNeeded, + &servicesReturned, + &resumeHandle, + nil, + ); err != nil { + return nil, fmt.Errorf("could not query windows service list: %w", err) } if servicesReturned == 0 { return nil, nil } - services := unsafe.Slice((*windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0])), servicesReturned) + services := unsafe.Slice((*windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0])), int(servicesReturned)) return services, nil } + +func getProcessStartTime(logger *slog.Logger, pid uint32) (uint64, error) { + handle, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid) + if err != nil { + return 0, fmt.Errorf("failed to open process %w", err) + } + + defer func(handle windows.Handle) { + err := windows.CloseHandle(handle) + if err != nil { + logger.Warn("failed to close process handle", + slog.Any("err", err), + ) + } + }(handle) + + var creation windows.Filetime + + var exit windows.Filetime + + var krn windows.Filetime + + var user windows.Filetime + + err = windows.GetProcessTimes(handle, &creation, &exit, &krn, &user) + if err != nil { + return 0, fmt.Errorf("failed to get process times %w", err) + } + + return uint64(creation.Nanoseconds()), nil +} diff --git a/pkg/collector/smb/smb.go b/pkg/collector/smb/smb.go index e2437110a..8c6d9f37e 100644 --- a/pkg/collector/smb/smb.go +++ b/pkg/collector/smb/smb.go @@ -3,11 +3,10 @@ package smb import ( + "log/slog" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -47,17 +46,17 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{ "SMB Server Shares", }, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { // desc creates a new prometheus description desc := func(metricName string, description string, labels ...string) *prometheus.Desc { return prometheus.NewDesc( @@ -75,10 +74,12 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { } // Collect collects smb metrics and sends them to prometheus. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectServerShares(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed to collect server share metrics", "err", err) + logger.Error("failed to collect server share metrics", + slog.Any("err", err), + ) return err } @@ -94,12 +95,15 @@ type perflibServerShares struct { TreeConnectCount float64 `perflib:"Tree Connect Count"` } -func (c *Collector) collectServerShares(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectServerShares(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibServerShares + if err := perflib.UnmarshalObject(ctx.PerfObjects["SMB Server Shares"], &data, logger); err != nil { return err } + for _, instance := range data { labelName := c.toLabelName(instance.Name) if !strings.HasSuffix(labelName, "_total") { @@ -118,6 +122,7 @@ func (c *Collector) collectServerShares(ctx *types.ScrapeContext, logger log.Log instance.TreeConnectCount, ) } + return nil } @@ -125,5 +130,6 @@ func (c *Collector) collectServerShares(ctx *types.ScrapeContext, logger log.Log func (c *Collector) toLabelName(name string) string { s := strings.ReplaceAll(strings.Join(strings.Fields(strings.ToLower(name)), "_"), ".", "_") s = strings.ReplaceAll(s, "__", "_") + return s } diff --git a/pkg/collector/smbclient/smbclient.go b/pkg/collector/smbclient/smbclient.go index 4a8e4f613..498f31754 100644 --- a/pkg/collector/smbclient/smbclient.go +++ b/pkg/collector/smbclient/smbclient.go @@ -3,11 +3,10 @@ package smbclient import ( + "log/slog" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -69,17 +68,17 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{ "SMB Client Shares", }, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { // desc creates a new prometheus description desc := func(metricName string, description string, labels []string) *prometheus.Desc { return prometheus.NewDesc( @@ -179,10 +178,13 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { } // Collect collects smb client metrics and sends them to prometheus. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectClientShares(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "Error in ClientShares", "err", err) + logger.Error("Error in ClientShares", + slog.Any("err", err), + ) + return err } @@ -216,12 +218,15 @@ type perflibClientShares struct { WriteRequestsPerSec float64 `perflib:"Write Requests/sec"` } -func (c *Collector) collectClientShares(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectClientShares(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var data []perflibClientShares + if err := perflib.UnmarshalObject(ctx.PerfObjects["SMB Client Shares"], &data, logger); err != nil { return err } + for _, instance := range data { if instance.Name == "_Total" { continue @@ -380,5 +385,6 @@ func (c *Collector) collectClientShares(ctx *types.ScrapeContext, logger log.Log serverValue, shareValue, ) } + return nil } diff --git a/pkg/collector/smtp/smtp.go b/pkg/collector/smtp/smtp.go index 1134ea267..aba30dee8 100644 --- a/pkg/collector/smtp/smtp.go +++ b/pkg/collector/smtp/smtp.go @@ -4,11 +4,10 @@ package smtp import ( "fmt" + "log/slog" "regexp" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -134,18 +133,18 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"SMTP Server"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(logger log.Logger, _ *wmi.Client) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Build(logger *slog.Logger, _ *wmi.Client) error { + logger = logger.With(slog.String("collector", Name)) - _ = level.Info(logger).Log("msg", "smtp collector is in an experimental state! Metrics for this collector have not been tested.") + logger.Info("smtp collector is in an experimental state! Metrics for this collector have not been tested.") c.badMailedMessagesBadPickupFileTotal = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "badmailed_messages_bad_pickup_file_total"), @@ -405,12 +404,16 @@ func (c *Collector) Build(logger log.Logger, _ *wmi.Client) error { // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting smtp metrics", "err", err) + logger.Error("failed collecting smtp metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -462,9 +465,11 @@ type PerflibSMTPServer struct { RoutingTableLookupsTotal float64 `perflib:"Routing Table Lookups Total"` } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []PerflibSMTPServer + if err := perflib.UnmarshalObject(ctx.PerfObjects["SMTP Server"], &dst, logger); err != nil { return err } @@ -763,5 +768,6 @@ func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan server.Name, ) } + return nil } diff --git a/pkg/collector/system/system.go b/pkg/collector/system/system.go index 15620d53a..fc02e0c51 100644 --- a/pkg/collector/system/system.go +++ b/pkg/collector/system/system.go @@ -3,9 +3,10 @@ package system import ( + "errors" + "log/slog" + "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -25,6 +26,8 @@ type Collector struct { contextSwitchesTotal *prometheus.Desc exceptionDispatchesTotal *prometheus.Desc processorQueueLength *prometheus.Desc + processes *prometheus.Desc + processesLimit *prometheus.Desc systemCallsTotal *prometheus.Desc systemUpTime *prometheus.Desc threads *prometheus.Desc @@ -50,15 +53,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"System"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { c.contextSwitchesTotal = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "context_switches_total"), "Total number of context switches (WMI source: PerfOS_System.ContextSwitchesPersec)", @@ -71,6 +74,19 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { nil, nil, ) + c.processes = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "processes"), + "Current number of processes (WMI source: PerfOS_System.Processes)", + nil, + nil, + ) + c.processesLimit = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "processes_limit"), + "Maximum number of processes.", + nil, + nil, + ) + c.processorQueueLength = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "processor_queue_length"), "Length of processor queue (WMI source: PerfOS_System.ProcessorQueueLength)", @@ -95,17 +111,22 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { nil, nil, ) + return nil } // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting system metrics", "err", err) + logger.Error("failed collecting system metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -117,16 +138,23 @@ type system struct { ProcessorQueueLength float64 `perflib:"Processor Queue Length"` SystemCallsPersec float64 `perflib:"System Calls/sec"` SystemUpTime float64 `perflib:"System Up Time"` + Processes float64 `perflib:"Processes"` Threads float64 `perflib:"Threads"` } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []system + if err := perflib.UnmarshalObject(ctx.PerfObjects["System"], &dst, logger); err != nil { return err } + if len(dst) == 0 { + return errors.New("no data returned from Performance Counter") + } + ch <- prometheus.MustNewConstMetric( c.contextSwitchesTotal, prometheus.CounterValue, @@ -142,6 +170,11 @@ func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan prometheus.GaugeValue, dst[0].ProcessorQueueLength, ) + ch <- prometheus.MustNewConstMetric( + c.processes, + prometheus.GaugeValue, + dst[0].Processes, + ) ch <- prometheus.MustNewConstMetric( c.systemCallsTotal, prometheus.CounterValue, @@ -157,5 +190,15 @@ func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan prometheus.GaugeValue, dst[0].Threads, ) + + // Windows has no defined limit, and is based off available resources. This currently isn't calculated by WMI and is set to default value. + // https://techcommunity.microsoft.com/t5/windows-blog-archive/pushing-the-limits-of-windows-processes-and-threads/ba-p/723824 + // https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem + ch <- prometheus.MustNewConstMetric( + c.processesLimit, + prometheus.GaugeValue, + float64(4294967295), + ) + return nil } diff --git a/pkg/collector/tcp/const.go b/pkg/collector/tcp/const.go new file mode 100644 index 000000000..7de6573bd --- /dev/null +++ b/pkg/collector/tcp/const.go @@ -0,0 +1,16 @@ +package tcp + +// Win32_PerfRawData_Tcpip_TCPv4 docs +// - https://msdn.microsoft.com/en-us/library/aa394341(v=vs.85).aspx +// The TCPv6 performance object uses the same fields. +const ( + ConnectionFailures = "Connection Failures" + ConnectionsActive = "Connections Active" + ConnectionsEstablished = "Connections Established" + ConnectionsPassive = "Connections Passive" + ConnectionsReset = "Connections Reset" + SegmentsPersec = "Segments/sec" + SegmentsReceivedPersec = "Segments Received/sec" + SegmentsRetransmittedPersec = "Segments Retransmitted/sec" + SegmentsSentPersec = "Segments Sent/sec" +) diff --git a/pkg/collector/tcp/tcp.go b/pkg/collector/tcp/tcp.go index e2110089f..08fc765aa 100644 --- a/pkg/collector/tcp/tcp.go +++ b/pkg/collector/tcp/tcp.go @@ -3,10 +3,11 @@ package tcp import ( + "fmt" + "log/slog" + "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" - "github.com/prometheus-community/windows_exporter/pkg/perflib" + "github.com/prometheus-community/windows_exporter/pkg/perfdata" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -22,6 +23,9 @@ var ConfigDefaults = Config{} type Collector struct { config Config + perfDataCollector4 *perfdata.Collector + perfDataCollector6 *perfdata.Collector + connectionFailures *prometheus.Desc connectionsActive *prometheus.Desc connectionsEstablished *prometheus.Desc @@ -53,15 +57,39 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { - return []string{"TCPv4"}, nil +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { + return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { + counters := []string{ + ConnectionFailures, + ConnectionsActive, + ConnectionsEstablished, + ConnectionsPassive, + ConnectionsReset, + SegmentsPersec, + SegmentsReceivedPersec, + SegmentsRetransmittedPersec, + SegmentsSentPersec, + } + + var err error + + c.perfDataCollector4, err = perfdata.NewCollector("TCPv4", nil, counters) + if err != nil { + return fmt.Errorf("failed to create TCPv4 collector: %w", err) + } + + c.perfDataCollector6, err = perfdata.NewCollector("TCPv6", nil, counters) + if err != nil { + return fmt.Errorf("failed to create TCPv6 collector: %w", err) + } + c.connectionFailures = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "connection_failures_total"), "(TCP.ConnectionFailures)", @@ -116,111 +144,96 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { []string{"af"}, nil, ) + return nil } // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) - if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting tcp metrics", "err", err) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + if err := c.collect(ch); err != nil { + logger.Error("failed collecting tcp metrics", + slog.Any("err", err), + ) + return err } - return nil -} -// Win32_PerfRawData_Tcpip_TCPv4 docs -// - https://msdn.microsoft.com/en-us/library/aa394341(v=vs.85).aspx -// The TCPv6 performance object uses the same fields. -type tcp struct { - ConnectionFailures float64 `perflib:"Connection Failures"` - ConnectionsActive float64 `perflib:"Connections Active"` - ConnectionsEstablished float64 `perflib:"Connections Established"` - ConnectionsPassive float64 `perflib:"Connections Passive"` - ConnectionsReset float64 `perflib:"Connections Reset"` - SegmentsPersec float64 `perflib:"Segments/sec"` - SegmentsReceivedPersec float64 `perflib:"Segments Received/sec"` - SegmentsRetransmittedPersec float64 `perflib:"Segments Retransmitted/sec"` - SegmentsSentPersec float64 `perflib:"Segments Sent/sec"` + return nil } -func writeTCPCounters(metrics tcp, labels []string, c *Collector, ch chan<- prometheus.Metric) { +func writeTCPCounters(metrics map[string]perfdata.CounterValues, labels []string, c *Collector, ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric( c.connectionFailures, prometheus.CounterValue, - metrics.ConnectionFailures, + metrics[ConnectionFailures].FirstValue, labels..., ) ch <- prometheus.MustNewConstMetric( c.connectionsActive, prometheus.CounterValue, - metrics.ConnectionsActive, + metrics[ConnectionsActive].FirstValue, labels..., ) ch <- prometheus.MustNewConstMetric( c.connectionsEstablished, prometheus.GaugeValue, - metrics.ConnectionsEstablished, + metrics[ConnectionsEstablished].FirstValue, labels..., ) ch <- prometheus.MustNewConstMetric( c.connectionsPassive, prometheus.CounterValue, - metrics.ConnectionsPassive, + metrics[ConnectionsPassive].FirstValue, labels..., ) ch <- prometheus.MustNewConstMetric( c.connectionsReset, prometheus.CounterValue, - metrics.ConnectionsReset, + metrics[ConnectionsReset].FirstValue, labels..., ) ch <- prometheus.MustNewConstMetric( c.segmentsTotal, prometheus.CounterValue, - metrics.SegmentsPersec, + metrics[SegmentsPersec].FirstValue, labels..., ) ch <- prometheus.MustNewConstMetric( c.segmentsReceivedTotal, prometheus.CounterValue, - metrics.SegmentsReceivedPersec, + metrics[SegmentsReceivedPersec].FirstValue, labels..., ) ch <- prometheus.MustNewConstMetric( c.segmentsRetransmittedTotal, prometheus.CounterValue, - metrics.SegmentsRetransmittedPersec, + metrics[SegmentsRetransmittedPersec].FirstValue, labels..., ) ch <- prometheus.MustNewConstMetric( c.segmentsSentTotal, prometheus.CounterValue, - metrics.SegmentsSentPersec, + metrics[SegmentsSentPersec].FirstValue, labels..., ) } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) - var dst []tcp - - // TCPv4 counters - if err := perflib.UnmarshalObject(ctx.PerfObjects["TCPv4"], &dst, logger); err != nil { - return err - } - if len(dst) != 0 { - writeTCPCounters(dst[0], []string{"ipv4"}, c, ch) +func (c *Collector) collect(ch chan<- prometheus.Metric) error { + data, err := c.perfDataCollector4.Collect() + if err != nil { + return fmt.Errorf("failed to collect TCPv4 metrics: %w", err) } - // TCPv6 counters - if err := perflib.UnmarshalObject(ctx.PerfObjects["TCPv6"], &dst, logger); err != nil { - return err - } - if len(dst) != 0 { - writeTCPCounters(dst[0], []string{"ipv6"}, c, ch) + writeTCPCounters(data[perfdata.EmptyInstance], []string{"ipv4"}, c, ch) + + data, err = c.perfDataCollector6.Collect() + if err != nil { + return fmt.Errorf("failed to collect TCPv6 metrics: %w", err) } + writeTCPCounters(data[perfdata.EmptyInstance], []string{"ipv6"}, c, ch) + return nil } diff --git a/pkg/collector/teradici_pcoip/teradici_pcoip.go b/pkg/collector/teradici_pcoip/teradici_pcoip.go index a2ab5ce62..6484a9f93 100644 --- a/pkg/collector/teradici_pcoip/teradici_pcoip.go +++ b/pkg/collector/teradici_pcoip/teradici_pcoip.go @@ -4,10 +4,9 @@ package teradici_pcoip import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -92,17 +91,18 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { - _ = level.Warn(logger). - Log("msg", "teradici_pcoip collector is deprecated and will be removed in the future.", "collector", Name) +func (c *Collector) Build(logger *slog.Logger, wmiClient *wmi.Client) error { + logger.Warn("teradici_pcoip collector is deprecated and will be removed in the future.", + slog.String("collector", Name), + ) if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") @@ -336,33 +336,54 @@ func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { nil, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectAudio(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting teradici session audio metrics", "err", err) + logger.Error("failed collecting teradici session audio metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectGeneral(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting teradici session general metrics", "err", err) + logger.Error("failed collecting teradici session general metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectImaging(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting teradici session imaging metrics", "err", err) + logger.Error("failed collecting teradici session imaging metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectNetwork(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting teradici session network metrics", "err", err) + logger.Error("failed collecting teradici session network metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectUsb(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting teradici session USB metrics", "err", err) + logger.Error("failed collecting teradici session USB metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -423,6 +444,7 @@ func (c *Collector) collectAudio(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM win32_PerfRawData_TeradiciPerf_PCoIPSessionAudioStatistics", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } @@ -465,6 +487,7 @@ func (c *Collector) collectGeneral(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM win32_PerfRawData_TeradiciPerf_PCoIPSessionGeneralStatistics", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } @@ -519,6 +542,7 @@ func (c *Collector) collectImaging(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM win32_PerfRawData_TeradiciPerf_PCoIPSessionImagingStatistics", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } @@ -597,6 +621,7 @@ func (c *Collector) collectNetwork(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM win32_PerfRawData_TeradiciPerf_PCoIPSessionNetworkStatistics", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } @@ -669,6 +694,7 @@ func (c *Collector) collectUsb(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM win32_PerfRawData_TeradiciPerf_PCoIPSessionUsbStatistics", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } diff --git a/pkg/collector/terminal_services/terminal_services.go b/pkg/collector/terminal_services/terminal_services.go index 3d591a72e..ac243621f 100644 --- a/pkg/collector/terminal_services/terminal_services.go +++ b/pkg/collector/terminal_services/terminal_services.go @@ -5,18 +5,17 @@ package terminal_services import ( "errors" "fmt" + "log/slog" "strconv" "strings" - "syscall" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/headers/wtsapi32" "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" + "golang.org/x/sys/windows" ) const ( @@ -32,17 +31,20 @@ type Win32_ServerFeature struct { ID uint32 } -func isConnectionBrokerServer(logger log.Logger, wmiClient *wmi.Client) bool { +func isConnectionBrokerServer(logger *slog.Logger, wmiClient *wmi.Client) bool { var dst []Win32_ServerFeature if err := wmiClient.Query("SELECT * FROM Win32_ServerFeature", &dst); err != nil { return false } + for _, d := range dst { if d.ID == ConnectionBrokerFeatureID { return true } } - _ = level.Debug(logger).Log("msg", "host is not a connection broker skipping Connection Broker performance metrics.") + + logger.Debug("host is not a connection broker skipping Connection Broker performance metrics.") + return false } @@ -55,7 +57,7 @@ type Collector struct { connectionBrokerEnabled bool - hServer syscall.Handle + hServer windows.Handle sessionInfo *prometheus.Desc connectionBrokerPerformance *prometheus.Desc @@ -94,14 +96,14 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{ "Terminal Services Session", "Remote Desktop Connection Broker Counterset", }, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { err := wtsapi32.WTSCloseServer(c.hServer) if err != nil { return fmt.Errorf("failed to close WTS server: %w", err) @@ -110,8 +112,8 @@ func (c *Collector) Close() error { return nil } -func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Build(logger *slog.Logger, wmiClient *wmi.Client) error { + logger = logger.With(slog.String("collector", Name)) c.connectionBrokerEnabled = isConnectionBrokerServer(logger, wmiClient) @@ -218,24 +220,35 @@ func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectWTSSessions(logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting terminal services session infos", "err", err) + logger.Error("failed collecting terminal services session infos", + slog.Any("err", err), + ) + return err } + if err := c.collectTSSessionCounters(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting terminal services session count metrics", "err", err) + logger.Error("failed collecting terminal services session count metrics", + slog.Any("err", err), + ) + return err } // only collect CollectionBrokerPerformance if host is a Connection Broker if c.connectionBrokerEnabled { if err := c.collectCollectionBrokerPerformanceCounter(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting Connection Broker performance metrics", "err", err) + logger.Error("failed collecting Connection Broker performance metrics", + slog.Any("err", err), + ) + return err } } + return nil } @@ -258,13 +271,15 @@ type perflibTerminalServicesSession struct { WorkingSetPeak float64 `perflib:"Working Set Peak"` } -func (c *Collector) collectTSSessionCounters(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectTSSessionCounters(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) dst := make([]perflibTerminalServicesSession, 0) + err := perflib.UnmarshalObject(ctx.PerfObjects["Terminal Services Session"], &dst, logger) if err != nil { return err } + names := make(map[string]bool) for _, d := range dst { @@ -277,6 +292,7 @@ func (c *Collector) collectTSSessionCounters(ctx *types.ScrapeContext, logger lo if _, ok := names[n]; ok { continue } + names[n] = true ch <- prometheus.MustNewConstMetric( @@ -373,6 +389,7 @@ func (c *Collector) collectTSSessionCounters(ctx *types.ScrapeContext, logger lo d.Name, ) } + return nil } @@ -382,13 +399,15 @@ type perflibRemoteDesktopConnectionBrokerCounterset struct { FailedConnections float64 `perflib:"Failed Connections"` } -func (c *Collector) collectCollectionBrokerPerformanceCounter(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectCollectionBrokerPerformanceCounter(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) dst := make([]perflibRemoteDesktopConnectionBrokerCounterset, 0) + err := perflib.UnmarshalObject(ctx.PerfObjects["Remote Desktop Connection Broker Counterset"], &dst, logger) if err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } @@ -417,7 +436,7 @@ func (c *Collector) collectCollectionBrokerPerformanceCounter(ctx *types.ScrapeC return nil } -func (c *Collector) collectWTSSessions(logger log.Logger, ch chan<- prometheus.Metric) error { +func (c *Collector) collectWTSSessions(logger *slog.Logger, ch chan<- prometheus.Metric) error { sessions, err := wtsapi32.WTSEnumerateSessionsEx(c.hServer, logger) if err != nil { return fmt.Errorf("failed to enumerate WTS sessions: %w", err) diff --git a/pkg/collector/textfile/textfile.go b/pkg/collector/textfile/textfile.go index c424550aa..498ea16ca 100644 --- a/pkg/collector/textfile/textfile.go +++ b/pkg/collector/textfile/textfile.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" "io" + "log/slog" "os" "path/filepath" "reflect" @@ -28,8 +29,6 @@ import ( "github.com/alecthomas/kingpin/v2" "github.com/dimchansky/utfbom" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" @@ -97,17 +96,18 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(logger log.Logger, _ *wmi.Client) error { - _ = level.Info(logger). - Log("msg", "textfile Collector directories: "+strings.Join(c.config.TextFileDirectories, ","), "collector", Name) +func (c *Collector) Build(logger *slog.Logger, _ *wmi.Client) error { + logger.Info("textfile Collector directories: "+strings.Join(c.config.TextFileDirectories, ","), + slog.String("collector", Name), + ) c.mTimeDesc = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, "textfile", "mtime_seconds"), @@ -123,11 +123,14 @@ func (c *Collector) Build(logger log.Logger, _ *wmi.Client) error { // Duplicates will be detected where the metric name, labels and label values are identical. func duplicateMetricEntry(metricFamilies []*dto.MetricFamily) bool { uniqueMetrics := make(map[string]map[string]string) + for _, metricFamily := range metricFamilies { metricName := metricFamily.GetName() + for _, metric := range metricFamily.GetMetric() { metricLabels := metric.GetLabel() labels := make(map[string]string) + for _, label := range metricLabels { labels[label.GetName()] = label.GetValue() } @@ -138,17 +141,21 @@ func duplicateMetricEntry(metricFamilies []*dto.MetricFamily) bool { if mapContainsKey && reflect.DeepEqual(uniqueMetrics[metricName], labels) { return true } + uniqueMetrics[metricName] = labels } } + return false } -func (c *Collector) convertMetricFamily(logger log.Logger, metricFamily *dto.MetricFamily, ch chan<- prometheus.Metric) { +func (c *Collector) convertMetricFamily(logger *slog.Logger, metricFamily *dto.MetricFamily, ch chan<- prometheus.Metric) { var valType prometheus.ValueType + var val float64 allLabelNames := map[string]struct{}{} + for _, metric := range metricFamily.GetMetric() { labels := metric.GetLabel() for _, label := range labels { @@ -160,12 +167,15 @@ func (c *Collector) convertMetricFamily(logger log.Logger, metricFamily *dto.Met for _, metric := range metricFamily.GetMetric() { if metric.TimestampMs != nil { - _ = level.Warn(logger).Log("msg", fmt.Sprintf("Ignoring unsupported custom timestamp on textfile Collector metric %v", metric)) + logger.Warn(fmt.Sprintf("Ignoring unsupported custom timestamp on textfile Collector metric %v", metric)) } labels := metric.GetLabel() + var names []string + var values []string + for _, label := range labels { names = append(names, label.GetName()) values = append(values, label.GetValue()) @@ -173,12 +183,15 @@ func (c *Collector) convertMetricFamily(logger log.Logger, metricFamily *dto.Met for k := range allLabelNames { present := false + for _, name := range names { if k == name { present = true + break } } + if !present { names = append(names, k) values = append(values, "") @@ -230,9 +243,11 @@ func (c *Collector) convertMetricFamily(logger log.Logger, metricFamily *dto.Met buckets, values..., ) default: - _ = level.Error(logger).Log("msg", "unknown metric type for file") + logger.Error("unknown metric type for file") + continue } + if metricType == dto.MetricType_GAUGE || metricType == dto.MetricType_COUNTER || metricType == dto.MetricType_UNTYPED { ch <- prometheus.MustNewConstMetric( prometheus.NewDesc( @@ -254,6 +269,7 @@ func (c *Collector) exportMTimes(mTimes map[string]time.Time, ch chan<- promethe for filename := range mTimes { filenames = append(filenames, filename) } + sort.Strings(filenames) for _, filename := range filenames { @@ -280,6 +296,7 @@ func (cr carriageReturnFilteringReader) Read(p []byte) (int, error) { } pi := 0 + for i := range n { if buf[i] != '\r' { p[pi] = buf[i] @@ -291,8 +308,8 @@ func (cr carriageReturnFilteringReader) Read(p []byte) (int, error) { } // Collect implements the Collector interface. -func (c *Collector) Collect(_ *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) errorMetric := 0.0 mTimes := map[string]time.Time{} @@ -305,43 +322,68 @@ func (c *Collector) Collect(_ *types.ScrapeContext, logger log.Logger, ch chan<- for _, directory := range c.config.TextFileDirectories { err := filepath.WalkDir(directory, func(path string, dirEntry os.DirEntry, err error) error { if err != nil { - _ = level.Error(logger).Log("msg", "Error reading directory: "+path, "err", err) + logger.Error("Error reading directory: "+path, + slog.Any("err", err), + ) + errorMetric = 1.0 + return nil } + if !dirEntry.IsDir() && strings.HasSuffix(dirEntry.Name(), ".prom") { - _ = level.Debug(logger).Log("msg", "Processing file: "+path) + logger.Debug("Processing file: " + path) + families_array, err := scrapeFile(path, logger) if err != nil { - _ = level.Error(logger).Log("msg", fmt.Sprintf("Error scraping file: %q. Skip File.", path), "err", err) + logger.Error(fmt.Sprintf("Error scraping file: %q. Skip File.", path), + slog.Any("err", err), + ) + errorMetric = 1.0 + return nil } + fileInfo, err := os.Stat(path) if err != nil { - _ = level.Error(logger).Log("msg", fmt.Sprintf("Error reading file info: %q. Skip File.", path), "err", err) + logger.Error(fmt.Sprintf("Error reading file info: %q. Skip File.", path), + slog.Any("err", err), + ) + errorMetric = 1.0 + return nil } + if _, hasName := mTimes[fileInfo.Name()]; hasName { - _ = level.Error(logger).Log("msg", fmt.Sprintf("Duplicate filename detected: %q. Skip File.", path)) + logger.Error(fmt.Sprintf("Duplicate filename detected: %q. Skip File.", path)) + errorMetric = 1.0 + return nil } + mTimes[fileInfo.Name()] = fileInfo.ModTime() + metricFamilies = append(metricFamilies, families_array...) } + return nil }) if err != nil && directory != "" { - _ = level.Error(logger).Log("msg", "Error reading textfile Collector directory: "+directory, "err", err) + logger.Error("Error reading textfile Collector directory: "+directory, + slog.Any("err", err), + ) + errorMetric = 1.0 } } // If duplicates are detected across *multiple* files, return error. if duplicateMetricEntry(metricFamilies) { - _ = level.Error(logger).Log("msg", "Duplicate metrics detected across multiple files") + logger.Error("Duplicate metrics detected across multiple files") + errorMetric = 1.0 } else { for _, mf := range metricFamilies { @@ -359,24 +401,32 @@ func (c *Collector) Collect(_ *types.ScrapeContext, logger log.Logger, ch chan<- ), prometheus.GaugeValue, errorMetric, ) + return nil } -func scrapeFile(path string, log log.Logger) ([]*dto.MetricFamily, error) { +func scrapeFile(path string, logger *slog.Logger) ([]*dto.MetricFamily, error) { file, err := os.Open(path) if err != nil { return nil, err } + var parser expfmt.TextParser + r, encoding := utfbom.Skip(carriageReturnFilteringReader{r: file}) if err = checkBOM(encoding); err != nil { return nil, err } + parsedFamilies, err := parser.TextToMetricFamilies(r) + closeErr := file.Close() if closeErr != nil { - _ = level.Warn(log).Log("msg", fmt.Sprintf("Error closing file %q", path), "err", closeErr) + logger.Warn("error closing file "+path, + slog.Any("err", closeErr), + ) } + if err != nil { return nil, err } @@ -386,11 +436,13 @@ func scrapeFile(path string, log log.Logger) ([]*dto.MetricFamily, error) { for _, mf := range parsedFamilies { families_array = append(families_array, mf) + for _, m := range mf.GetMetric() { if m.TimestampMs != nil { return nil, errors.New("textfile contains unsupported client-side timestamps") } } + if mf.Help == nil { help := "Metric read from " + path mf.Help = &help @@ -401,6 +453,7 @@ func scrapeFile(path string, log log.Logger) ([]*dto.MetricFamily, error) { if duplicateMetricEntry(families_array) { return nil, errors.New("duplicate metrics detected") } + return families_array, nil } @@ -414,5 +467,6 @@ func checkBOM(encoding utfbom.Encoding) error { func getDefaultPath() string { execPath, _ := os.Executable() + return filepath.Join(filepath.Dir(execPath), "textfile_inputs") } diff --git a/pkg/collector/textfile/textfile_test.go b/pkg/collector/textfile/textfile_test.go index 5ff3007dd..44b20d393 100644 --- a/pkg/collector/textfile/textfile_test.go +++ b/pkg/collector/textfile/textfile_test.go @@ -14,6 +14,7 @@ func TestCRFilter(t *testing.T) { sr := strings.NewReader("line 1\r\nline 2") cr := carriageReturnFilteringReader{r: sr} + b, err := io.ReadAll(cr) if err != nil { t.Error(err) @@ -43,9 +44,11 @@ func TestCheckBOM(t *testing.T) { if d.err == "" && err != nil { t.Error(err) } + if d.err != "" && err == nil { t.Errorf("Missing expected error %s", d.err) } + if err != nil && !strings.Contains(err.Error(), d.err) { t.Error(err) } diff --git a/pkg/collector/textfile/textfile_test_test.go b/pkg/collector/textfile/textfile_test_test.go index b5de5c3a2..08b061166 100644 --- a/pkg/collector/textfile/textfile_test_test.go +++ b/pkg/collector/textfile/textfile_test_test.go @@ -2,11 +2,11 @@ package textfile_test import ( "fmt" - "os" + "io" + "log/slog" "strings" "testing" - "github.com/go-kit/log" "github.com/prometheus-community/windows_exporter/pkg/collector" "github.com/prometheus-community/windows_exporter/pkg/collector/textfile" "github.com/prometheus/client_golang/prometheus" @@ -18,7 +18,7 @@ var baseDir = "../../../tools/textfile-test" //nolint:paralleltest func TestMultipleDirectories(t *testing.T) { - logger := log.NewLogfmtLogger(os.Stdout) + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) testDir := baseDir + "/multiple-dirs" testDirs := fmt.Sprintf("%[1]s/dir1,%[1]s/dir2,%[1]s/dir3", testDir) @@ -33,16 +33,21 @@ func TestMultipleDirectories(t *testing.T) { if err != nil { t.Errorf("Unexpected error %s", err) } + metrics := make(chan prometheus.Metric) got := "" + go func() { for { var metric dto.Metric + val := <-metrics + err := val.Write(&metric) if err != nil { t.Errorf("Unexpected error %s", err) } + got += metric.String() } }() @@ -61,7 +66,7 @@ func TestMultipleDirectories(t *testing.T) { //nolint:paralleltest func TestDuplicateFileName(t *testing.T) { - logger := log.NewLogfmtLogger(os.Stdout) + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) testDir := baseDir + "/duplicate-filename" textFileCollector := textfile.New(&textfile.Config{ TextFileDirectories: []string{testDir}, @@ -74,19 +79,25 @@ func TestDuplicateFileName(t *testing.T) { if err != nil { t.Errorf("Unexpected error %s", err) } + metrics := make(chan prometheus.Metric) got := "" + go func() { for { var metric dto.Metric + val := <-metrics + err := val.Write(&metric) if err != nil { t.Errorf("Unexpected error %s", err) } + got += metric.String() } }() + err = textFileCollector.Collect(scrapeContext, logger, metrics) if err != nil { t.Errorf("Unexpected error %s", err) diff --git a/pkg/collector/thermalzone/thermalzone.go b/pkg/collector/thermalzone/thermalzone.go index 1a73dac57..ca5929523 100644 --- a/pkg/collector/thermalzone/thermalzone.go +++ b/pkg/collector/thermalzone/thermalzone.go @@ -4,10 +4,9 @@ package thermalzone import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -49,15 +48,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -87,17 +86,22 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { }, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collect(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting thermalzone metrics", "err", err) + logger.Error("failed collecting thermalzone metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/collector/time/time.go b/pkg/collector/time/time.go index 6a8cce7fc..0d08c0d06 100644 --- a/pkg/collector/time/time.go +++ b/pkg/collector/time/time.go @@ -4,15 +4,17 @@ package time import ( "errors" + "log/slog" + "time" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" + "github.com/prometheus-community/windows_exporter/pkg/headers/kernel32" "github.com/prometheus-community/windows_exporter/pkg/perflib" "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" + "golang.org/x/sys/windows" ) const Name = "time" @@ -25,6 +27,8 @@ var ConfigDefaults = Config{} type Collector struct { config Config + currentTime *prometheus.Desc + timezone *prometheus.Desc clockFrequencyAdjustmentPPBTotal *prometheus.Desc computedTimeOffset *prometheus.Desc ntpClientTimeSourceCount *prometheus.Desc @@ -53,19 +57,31 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{"Windows Time Service"}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { if winversion.WindowsVersionFloat() <= 6.1 { return errors.New("windows version older than Server 2016 detected. The time collector will not run and should be disabled via CLI flags or configuration file") } + c.currentTime = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "current_timestamp_seconds"), + "OperatingSystem.LocalDateTime", + nil, + nil, + ) + c.timezone = prometheus.NewDesc( + prometheus.BuildFQName(types.Namespace, Name, "timezone"), + "OperatingSystem.LocalDateTime", + []string{"timezone"}, + nil, + ) c.clockFrequencyAdjustmentPPBTotal = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "clock_frequency_adjustment_ppb_total"), "Total adjustment made to the local system clock frequency by W32Time in Parts Per Billion (PPB) units.", @@ -102,18 +118,34 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error { nil, nil, ) + return nil } // Collect sends the metric values for each metric // to the provided prometheus Metric channel. -func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) - if err := c.collect(ctx, logger, ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting time metrics", "err", err) - return err +func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + + errs := make([]error, 0, 2) + + if err := c.collectTime(ch); err != nil { + logger.Error("failed collecting time metrics", + slog.Any("err", err), + ) + + errs = append(errs, err) } - return nil + + if err := c.collectNTP(ctx, logger, ch); err != nil { + logger.Error("failed collecting time ntp metrics", + slog.Any("err", err), + ) + + errs = append(errs, err) + } + + return errors.Join(errs...) } // Perflib "Windows Time Service". @@ -121,18 +153,49 @@ type windowsTime struct { ClockFrequencyAdjustmentPPBTotal float64 `perflib:"Clock Frequency Adjustment (ppb)"` ComputedTimeOffset float64 `perflib:"Computed Time Offset"` NTPClientTimeSourceCount float64 `perflib:"NTP Client Time Source Count"` - NTPRoundtripDelay float64 `perflib:"NTP Roundtrip Delay"` + NTPRoundTripDelay float64 `perflib:"NTP Roundtrip Delay"` NTPServerIncomingRequestsTotal float64 `perflib:"NTP Server Incoming Requests"` NTPServerOutgoingResponsesTotal float64 `perflib:"NTP Server Outgoing Responses"` } -func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error { - logger = log.With(logger, "collector", Name) +func (c *Collector) collectTime(ch chan<- prometheus.Metric) error { + ch <- prometheus.MustNewConstMetric( + c.currentTime, + prometheus.GaugeValue, + float64(time.Now().Unix()), + ) + + timeZoneInfo, err := kernel32.GetDynamicTimeZoneInformation() + if err != nil { + return err + } + + // timeZoneKeyName contains the english name of the timezone. + timezoneName := windows.UTF16ToString(timeZoneInfo.TimeZoneKeyName[:]) + + ch <- prometheus.MustNewConstMetric( + c.timezone, + prometheus.GaugeValue, + 1.0, + timezoneName, + ) + + return nil +} + +func (c *Collector) collectNTP(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) + var dst []windowsTime // Single-instance class, array is required but will have single entry. + if err := perflib.UnmarshalObject(ctx.PerfObjects["Windows Time Service"], &dst, logger); err != nil { return err } + if len(dst) == 0 { + return errors.New("no data returned for Windows Time Service") + } + ch <- prometheus.MustNewConstMetric( c.clockFrequencyAdjustmentPPBTotal, prometheus.CounterValue, @@ -151,7 +214,7 @@ func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan ch <- prometheus.MustNewConstMetric( c.ntpRoundTripDelay, prometheus.GaugeValue, - dst[0].NTPRoundtripDelay/1000000, // microseconds -> seconds + dst[0].NTPRoundTripDelay/1000000, // microseconds -> seconds ) ch <- prometheus.MustNewConstMetric( c.ntpServerIncomingRequestsTotal, @@ -163,5 +226,6 @@ func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan prometheus.CounterValue, dst[0].NTPServerOutgoingResponsesTotal, ) + return nil } diff --git a/pkg/collector/types.go b/pkg/collector/types.go index ff9fb8fcd..9fd59230e 100644 --- a/pkg/collector/types.go +++ b/pkg/collector/types.go @@ -1,17 +1,18 @@ package collector import ( + "log/slog" + "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" ) -type Collectors struct { - collectors Map - wmiClient *wmi.Client - perfCounterQuery string +type MetricCollectors struct { + Collectors Map + WMIClient *wmi.Client + PerfCounterQuery string } type ( @@ -21,13 +22,13 @@ type ( // Collector interface that a collector has to implement. type Collector interface { - Build(logger log.Logger, wmiClient *wmi.Client) error + Build(logger *slog.Logger, wmiClient *wmi.Client) error // Close closes the collector - Close() error + Close(logger *slog.Logger) error // GetName get the name of the collector GetName() string // GetPerfCounter returns the perf counter required by the collector - GetPerfCounter(logger log.Logger) ([]string, error) + GetPerfCounter(logger *slog.Logger) ([]string, error) // Collect Get new metrics and expose them via prometheus registry. - Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) (err error) + Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) (err error) } diff --git a/pkg/collector/vmware/vmware.go b/pkg/collector/vmware/vmware.go index bcb4ad68b..e1676a26b 100644 --- a/pkg/collector/vmware/vmware.go +++ b/pkg/collector/vmware/vmware.go @@ -4,10 +4,9 @@ package vmware import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/perflib" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" @@ -67,15 +66,15 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { +func (c *Collector) Build(_ *slog.Logger, wmiClient *wmi.Client) error { if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") } @@ -197,21 +196,30 @@ func (c *Collector) Build(_ log.Logger, wmiClient *wmi.Client) error { nil, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectMem(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware memory metrics", "err", err) + logger.Error("failed collecting vmware memory metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectCpu(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware cpu metrics", "err", err) + logger.Error("failed collecting vmware cpu metrics", + slog.Any("err", err), + ) + return err } + return nil } @@ -245,6 +253,7 @@ func (c *Collector) collectMem(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM Win32_PerfRawData_vmGuestLib_VMem", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } @@ -333,6 +342,7 @@ func (c *Collector) collectCpu(ch chan<- prometheus.Metric) error { if err := c.wmiClient.Query("SELECT * FROM Win32_PerfRawData_vmGuestLib_VCPU", &dst); err != nil { return err } + if len(dst) == 0 { return errors.New("WMI query returned empty result set") } diff --git a/pkg/collector/vmware_blast/vmware_blast.go b/pkg/collector/vmware_blast/vmware_blast.go index c9ce63a18..f4d1c821b 100644 --- a/pkg/collector/vmware_blast/vmware_blast.go +++ b/pkg/collector/vmware_blast/vmware_blast.go @@ -4,10 +4,9 @@ package vmware_blast import ( "errors" + "log/slog" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus-community/windows_exporter/pkg/types" "github.com/prometheus/client_golang/prometheus" "github.com/yusufpapurcu/wmi" @@ -139,17 +138,18 @@ func (c *Collector) GetName() string { return Name } -func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) { +func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) { return []string{}, nil } -func (c *Collector) Close() error { +func (c *Collector) Close(_ *slog.Logger) error { return nil } -func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { - _ = level.Warn(logger). - Log("msg", "vmware_blast collector is deprecated and will be removed in the future.", "collector", Name) +func (c *Collector) Build(logger *slog.Logger, wmiClient *wmi.Client) error { + logger.Warn("vmware_blast collector is deprecated and will be removed in the future.", + slog.String("collector", Name), + ) if wmiClient == nil || wmiClient.SWbemServicesClient == nil { return errors.New("wmiClient or SWbemServicesClient is nil") @@ -582,61 +582,110 @@ func (c *Collector) Build(logger log.Logger, wmiClient *wmi.Client) error { nil, nil, ) + return nil } // Collect sends the metric values for each metric // 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) +func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error { + logger = logger.With(slog.String("collector", Name)) if err := c.collectAudio(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast audio metrics", "err", err) + logger.Error("failed collecting vmware blast audio metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectCdr(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast CDR metrics", "err", err) + logger.Error("failed collecting vmware blast CDR metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectClipboard(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast clipboard metrics", "err", err) + logger.Error("failed collecting vmware blast clipboard metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectHtml5Mmr(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast HTML5 MMR metrics", "err", err) + logger.Error("failed collecting vmware blast HTML5 MMR metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectImaging(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast imaging metrics", "err", err) + logger.Error("failed collecting vmware blast imaging metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectRtav(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast RTAV metrics", "err", err) + logger.Error("failed collecting vmware blast RTAV metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectSerialPortandScanner(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast serial port and scanner metrics", "err", err) + logger.Error("failed collecting vmware blast serial port and scanner metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectSession(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast metrics", "err", err) + logger.Error("failed collecting vmware blast metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectSkypeforBusinessControl(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast skype for business control metrics", "err", err) + logger.Error("failed collecting vmware blast skype for business control metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectThinPrint(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast thin print metrics", "err", err) + logger.Error("failed collecting vmware blast thin print metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectUsb(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast USB metrics", "err", err) + logger.Error("failed collecting vmware blast USB metrics", + slog.Any("err", err), + ) + return err } + if err := c.collectWindowsMediaMmr(ch); err != nil { - _ = level.Error(logger).Log("msg", "failed collecting vmware blast windows media MMR metrics", "err", err) + logger.Error("failed collecting vmware blast windows media MMR metrics", + slog.Any("err", err), + ) + return err } + return nil } diff --git a/pkg/config/config.go b/pkg/config/config.go index d620a3bb4..64284ea3a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -14,16 +14,16 @@ package config import ( + "context" "crypto/tls" "fmt" "io" + "log/slog" "net/http" "os" "strings" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "gopkg.in/yaml.v3" ) @@ -37,9 +37,11 @@ type Resolver struct { } // NewResolver returns a Resolver structure. -func NewResolver(file string, logger log.Logger, insecureSkipVerify bool) (*Resolver, error) { +func NewResolver(file string, logger *slog.Logger, insecureSkipVerify bool) (*Resolver, error) { flags := map[string]string{} + var fileBytes []byte + var err error if strings.HasPrefix(file, "http://") || strings.HasPrefix(file, "https://") { fileBytes, err = readFromURL(file, logger, insecureSkipVerify) @@ -57,7 +59,7 @@ func NewResolver(file string, logger log.Logger, insecureSkipVerify bool) (*Reso err = yaml.Unmarshal(fileBytes, &rawValues) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to unmarshal configuration file: %w", err) } // Flatten nested YAML values @@ -71,35 +73,42 @@ func NewResolver(file string, logger log.Logger, insecureSkipVerify bool) (*Reso return &Resolver{flags: flags}, nil } -func readFromFile(file string, logger log.Logger) ([]byte, error) { - _ = level.Info(logger).Log("msg", fmt.Sprintf("Loading configuration file: %v", file)) +func readFromFile(file string, logger *slog.Logger) ([]byte, error) { + logger.Info("Loading configuration file: " + file) + if _, err := os.Stat(file); err != nil { - return nil, err + return nil, fmt.Errorf("failed to read configuration file: %w", err) } fileBytes, err := os.ReadFile(file) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to read configuration file: %w", err) } - return fileBytes, err + return fileBytes, nil } -func readFromURL(file string, logger log.Logger, insecureSkipVerify bool) ([]byte, error) { - _ = level.Info(logger).Log("msg", fmt.Sprintf("Loading configuration file from URL: %v", file)) +func readFromURL(file string, logger *slog.Logger, insecureSkipVerify bool) ([]byte, error) { + logger.Info("Loading configuration file from URL: " + file) + tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify}, //nolint:gosec } if insecureSkipVerify { - _ = level.Warn(logger).Log("msg", "Loading configuration file with TLS verification disabled") + logger.Warn("Loading configuration file with TLS verification disabled") } client := &http.Client{Transport: tr} - resp, err := client.Get(file) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, file, nil) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create HTTP request: %w", err) + } + + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to read configuration file from URL: %w", err) } defer resp.Body.Close() @@ -130,6 +139,7 @@ func (c *Resolver) Bind(app *kingpin.Application, args []string) error { } c.setDefault(app) + if pc.SelectedCommand != nil { c.setDefault(pc.SelectedCommand) } diff --git a/pkg/config/flatten.go b/pkg/config/flatten.go index da09f6d7d..bc89d0c5b 100644 --- a/pkg/config/flatten.go +++ b/pkg/config/flatten.go @@ -12,6 +12,7 @@ import ( // or {"a": {"b":[1,2]}} => {"a.b.0":1, "a.b.1": 2}. func flatten(data map[string]interface{}) map[string]string { ret := make(map[string]string) + for k, v := range data { switch typed := v.(type) { case map[interface{}]interface{}: @@ -30,11 +31,13 @@ func flatten(data map[string]interface{}) map[string]string { ret[k] = fmt.Sprint(typed) } } + return ret } func flattenSlice(data []interface{}) map[string]string { ret := make(map[string]string) + for idx, v := range data { switch typed := v.(type) { case map[interface{}]interface{}: @@ -53,15 +56,18 @@ func flattenSlice(data []interface{}) map[string]string { ret[strconv.Itoa(idx)] = fmt.Sprint(typed) } } + return ret } func convertMap(originalMap map[interface{}]interface{}) map[string]interface{} { convertedMap := map[string]interface{}{} + for key, value := range originalMap { if keyString, ok := key.(string); ok { convertedMap[keyString] = value } } + return convertedMap } diff --git a/pkg/config/flatten_test.go b/pkg/config/flatten_test.go index f1bbb33d2..69d5c9dad 100644 --- a/pkg/config/flatten_test.go +++ b/pkg/config/flatten_test.go @@ -18,7 +18,9 @@ func TestConfigFlattening(t *testing.T) { log: level: debug`) + var data map[string]interface{} + err := yaml.Unmarshal(goodYamlConfig, &data) if err != nil { t.Error(err) diff --git a/pkg/headers/kernel32/kernel32.go b/pkg/headers/kernel32/kernel32.go index f0b37f914..17f2b6278 100644 --- a/pkg/headers/kernel32/kernel32.go +++ b/pkg/headers/kernel32/kernel32.go @@ -1,14 +1,16 @@ package kernel32 import ( - "syscall" "unsafe" + + "golang.org/x/sys/windows" ) var ( - kernel32 = syscall.NewLazyDLL("kernel32.dll") + kernel32 = windows.NewLazySystemDLL("kernel32.dll") procGetDynamicTimeZoneInformationSys = kernel32.NewProc("GetDynamicTimeZoneInformation") + kernelLocalFileTimeToFileTime = kernel32.NewProc("LocalFileTimeToFileTime") ) // SYSTEMTIME contains a date and time. @@ -50,3 +52,11 @@ func GetDynamicTimeZoneInformation() (DynamicTimezoneInformation, error) { return tzi, nil } + +func LocalFileTimeToFileTime(localFileTime, utcFileTime *windows.Filetime) uint32 { + ret, _, _ := kernelLocalFileTimeToFileTime.Call( + uintptr(unsafe.Pointer(localFileTime)), + uintptr(unsafe.Pointer(utcFileTime))) + + return uint32(ret) +} diff --git a/pkg/headers/netapi32/netapi32.go b/pkg/headers/netapi32/netapi32.go index b471ead02..e2eba152a 100644 --- a/pkg/headers/netapi32/netapi32.go +++ b/pkg/headers/netapi32/netapi32.go @@ -76,6 +76,7 @@ func netApiBufferFree(buffer *wKSTAInfo102) { // https://docs.microsoft.com/en-us/windows/win32/api/lmwksta/nf-lmwksta-netwkstagetinfo func netWkstaGetInfo() (wKSTAInfo102, uint32, error) { var lpwi *wKSTAInfo102 + pLevel := uintptr(102) r1, _, _ := procNetWkstaGetInfo.Call(0, pLevel, uintptr(unsafe.Pointer(&lpwi))) @@ -86,6 +87,7 @@ func netWkstaGetInfo() (wKSTAInfo102, uint32, error) { } deref := *lpwi + return deref, 0, nil } @@ -95,6 +97,7 @@ func GetWorkstationInfo() (WorkstationInfo, error) { if err != nil { return WorkstationInfo{}, err } + workstationInfo := WorkstationInfo{ PlatformId: info.wki102_platform_id, ComputerName: windows.UTF16PtrToString(info.wki102_computername), @@ -104,5 +107,6 @@ func GetWorkstationInfo() (WorkstationInfo, error) { LanRoot: windows.UTF16PtrToString(info.wki102_lanroot), LoggedOnUsers: info.wki102_logged_on_users, } + return workstationInfo, nil } diff --git a/pkg/headers/sysinfoapi/sysinfoapi.go b/pkg/headers/sysinfoapi/sysinfoapi.go index 21fa4acf0..2761f7b2b 100644 --- a/pkg/headers/sysinfoapi/sysinfoapi.go +++ b/pkg/headers/sysinfoapi/sysinfoapi.go @@ -132,7 +132,9 @@ func GlobalMemoryStatusEx() (MemoryStatus, error) { // https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo func GetSystemInfo() SystemInfo { var info lpSystemInfo + procGetSystemInfo.Call(uintptr(unsafe.Pointer(&info))) //nolint:errcheck + return SystemInfo{ Arch: ProcessorArchitecture(info.Arch.WProcessorArchitecture), PageSize: info.DwPageSize, @@ -153,12 +155,16 @@ func GetComputerName(f WinComputerNameFormat) (string, error) { // 1kb buffer to accept computer name. This should be more than enough as the maximum size // returned is the max length of a DNS name, which this author believes is 253 characters. size := 1024 + var buffer [1024]uint16 + r1, _, err := procGetComputerNameExW.Call(uintptr(f), uintptr(unsafe.Pointer(&buffer)), uintptr(unsafe.Pointer(&size))) if r1 == 0 { return "", err } + bytes := buffer[0:size] out := utf16.Decode(bytes) + return string(out), nil } diff --git a/pkg/headers/wtsapi32/wtsapi32.go b/pkg/headers/wtsapi32/wtsapi32.go index 87bde129d..5e0ca8f4f 100644 --- a/pkg/headers/wtsapi32/wtsapi32.go +++ b/pkg/headers/wtsapi32/wtsapi32.go @@ -2,11 +2,9 @@ package wtsapi32 import ( "fmt" - "syscall" + "log/slog" "unsafe" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "golang.org/x/sys/windows" ) @@ -105,30 +103,30 @@ var ( } ) -func WTSOpenServer(server string) (syscall.Handle, error) { +func WTSOpenServer(server string) (windows.Handle, error) { var ( err error serverName *uint16 ) if server != "" { - serverName, err = syscall.UTF16PtrFromString(server) + serverName, err = windows.UTF16PtrFromString(server) if err != nil { - return syscall.InvalidHandle, err + return windows.InvalidHandle, err } } r1, _, err := procWTSOpenServerEx.Call(uintptr(unsafe.Pointer(serverName))) - serverHandle := syscall.Handle(r1) + serverHandle := windows.Handle(r1) - if serverHandle == syscall.InvalidHandle { - return syscall.InvalidHandle, err + if serverHandle == windows.InvalidHandle { + return windows.InvalidHandle, err } return serverHandle, nil } -func WTSCloseServer(server syscall.Handle) error { +func WTSCloseServer(server windows.Handle) error { r1, _, err := procWTSCloseServer.Call(uintptr(server)) if r1 != 1 { @@ -152,8 +150,9 @@ func WTSFreeMemoryEx(class WTSTypeClass, pMemory uintptr, numberOfEntries uint32 return nil } -func WTSEnumerateSessionsEx(server syscall.Handle, logger log.Logger) ([]WTSSession, error) { +func WTSEnumerateSessionsEx(server windows.Handle, logger *slog.Logger) ([]WTSSession, error) { var sessionInfoPointer uintptr + var count uint32 pLevel := uint32(1) @@ -173,7 +172,7 @@ func WTSEnumerateSessionsEx(server syscall.Handle, logger log.Logger) ([]WTSSess defer func(class WTSTypeClass, pMemory uintptr, NumberOfEntries uint32) { err := WTSFreeMemoryEx(class, pMemory, NumberOfEntries) if err != nil { - _ = level.Error(logger).Log("msg", "failed to free memory", "err", fmt.Errorf("WTSEnumerateSessionsEx: %w", err)) + logger.Warn("failed to free memory", "err", fmt.Errorf("WTSEnumerateSessionsEx: %w", err)) } }(WTSTypeSessionInfoLevel1, sessionInfoPointer, count) } @@ -182,6 +181,7 @@ func WTSEnumerateSessionsEx(server syscall.Handle, logger log.Logger) ([]WTSSess sessionSize := unsafe.Sizeof(sizeTest) sessions := make([]WTSSession, 0, count) + for i := range count { curPtr := unsafe.Pointer(sessionInfoPointer + (uintptr(i) * sessionSize)) data := (*wtsSessionInfo1)(curPtr) diff --git a/pkg/httphandler/health.go b/pkg/httphandler/health.go new file mode 100644 index 000000000..56dcec3b4 --- /dev/null +++ b/pkg/httphandler/health.go @@ -0,0 +1,19 @@ +package httphandler + +import ( + "net/http" +) + +type HealthHandler struct{} + +// Interface guard. +var _ http.Handler = (*HealthHandler)(nil) + +func NewHealthHandler() HealthHandler { + return HealthHandler{} +} + +func (h HealthHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"status":"ok"}`)) +} diff --git a/pkg/httphandler/httphandler.go b/pkg/httphandler/httphandler.go new file mode 100644 index 000000000..8efa33203 --- /dev/null +++ b/pkg/httphandler/httphandler.go @@ -0,0 +1,192 @@ +package httphandler + +import ( + "fmt" + "log/slog" + "net/http" + "strconv" + "time" + + "github.com/google/uuid" + "github.com/prometheus-community/windows_exporter/pkg/collector" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" + "github.com/prometheus/client_golang/prometheus/collectors/version" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +// Interface guard. +var _ http.Handler = (*MetricsHTTPHandler)(nil) + +const defaultScrapeTimeout = 10.0 + +type MetricsHTTPHandler struct { + metricCollectors *collector.MetricCollectors + // exporterMetricsRegistry is a separate registry for the metrics about + // the exporter itself. + exporterMetricsRegistry *prometheus.Registry + + logger *slog.Logger + options Options + concurrencyCh chan struct{} +} + +type Options struct { + DisableExporterMetrics bool + TimeoutMargin float64 + MaxRequests int +} + +func New(logger *slog.Logger, metricCollectors *collector.MetricCollectors, options *Options) *MetricsHTTPHandler { + if options == nil { + options = &Options{ + DisableExporterMetrics: false, + TimeoutMargin: 0.5, + MaxRequests: 5, + } + } + + handler := &MetricsHTTPHandler{ + metricCollectors: metricCollectors, + logger: logger, + options: *options, + concurrencyCh: make(chan struct{}, options.MaxRequests), + } + + if !options.DisableExporterMetrics { + handler.exporterMetricsRegistry = prometheus.NewRegistry() + handler.exporterMetricsRegistry.MustRegister( + collectors.NewBuildInfoCollector(), + collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), + collectors.NewGoCollector(), + ) + } + + return handler +} + +func (c *MetricsHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + logger := c.logger.With( + slog.Any("remote", r.RemoteAddr), + slog.Any("correlation_id", uuid.New().String()), + ) + + scrapeTimeout := c.getScrapeTimeout(logger, r) + + handler, err := c.handlerFactory(logger, scrapeTimeout, r.URL.Query()["collect[]"]) + if err != nil { + logger.Warn("Couldn't create filtered metrics handler", + slog.Any("err", err), + ) + + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(fmt.Sprintf("Couldn't create filtered metrics handler: %s", err))) + + return + } + + handler.ServeHTTP(w, r) +} + +func (c *MetricsHTTPHandler) getScrapeTimeout(logger *slog.Logger, r *http.Request) time.Duration { + var timeoutSeconds float64 + + if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" { + var err error + + timeoutSeconds, err = strconv.ParseFloat(v, 64) + if err != nil { + logger.Warn(fmt.Sprintf("Couldn't parse X-Prometheus-Scrape-Timeout-Seconds: %q. Defaulting timeout to %f", v, defaultScrapeTimeout)) + } + } + + if timeoutSeconds == 0 { + timeoutSeconds = defaultScrapeTimeout + } + + timeoutSeconds -= c.options.TimeoutMargin + + return time.Duration(timeoutSeconds) * time.Second +} + +func (c *MetricsHTTPHandler) handlerFactory(logger *slog.Logger, scrapeTimeout time.Duration, requestedCollectors []string) (http.Handler, error) { + reg := prometheus.NewRegistry() + + var metricCollectors *collector.MetricCollectors + if len(requestedCollectors) == 0 { + metricCollectors = c.metricCollectors + } else { + filteredCollectors := make(collector.Map) + + for _, name := range requestedCollectors { + metricCollector, ok := c.metricCollectors.Collectors[name] + if !ok { + return nil, fmt.Errorf("couldn't find collector %s", name) + } + + filteredCollectors[name] = metricCollector + } + + metricCollectors = &collector.MetricCollectors{ + Collectors: filteredCollectors, + WMIClient: c.metricCollectors.WMIClient, + PerfCounterQuery: c.metricCollectors.PerfCounterQuery, + } + } + + reg.MustRegister(version.NewCollector("windows_exporter")) + + if err := reg.Register(metricCollectors.NewPrometheusCollector(scrapeTimeout, c.logger)); err != nil { + return nil, fmt.Errorf("couldn't register Prometheus collector: %w", err) + } + + var handler http.Handler + if c.exporterMetricsRegistry != nil { + handler = promhttp.HandlerFor( + prometheus.Gatherers{c.exporterMetricsRegistry, reg}, + promhttp.HandlerOpts{ + ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError), + ErrorHandling: promhttp.ContinueOnError, + MaxRequestsInFlight: c.options.MaxRequests, + Registry: c.exporterMetricsRegistry, + }, + ) + + // Note that we have to use h.exporterMetricsRegistry here to + // use the same promhttp metrics for all expositions. + handler = promhttp.InstrumentMetricHandler( + c.exporterMetricsRegistry, handler, + ) + } else { + handler = promhttp.HandlerFor( + reg, + promhttp.HandlerOpts{ + ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError), + ErrorHandling: promhttp.ContinueOnError, + MaxRequestsInFlight: c.options.MaxRequests, + }, + ) + } + + return c.withConcurrencyLimit(handler.ServeHTTP), nil +} + +func (c *MetricsHTTPHandler) withConcurrencyLimit(next http.HandlerFunc) http.HandlerFunc { + if c.options.MaxRequests <= 0 { + return next + } + + return func(w http.ResponseWriter, r *http.Request) { + select { + case c.concurrencyCh <- struct{}{}: + defer func() { <-c.concurrencyCh }() + default: + w.WriteHeader(http.StatusServiceUnavailable) + _, _ = w.Write([]byte("Too many concurrent requests")) + + return + } + + next(w, r) + } +} diff --git a/pkg/httphandler/version.go b/pkg/httphandler/version.go new file mode 100644 index 000000000..fd80ca287 --- /dev/null +++ b/pkg/httphandler/version.go @@ -0,0 +1,45 @@ +package httphandler + +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/prometheus/common/version" +) + +type VersionHandler struct{} + +// Same struct prometheus uses for their /version endpoint. +// Separate copy to avoid pulling all of prometheus as a dependency. +type prometheusVersion struct { + Version string `json:"version"` + Revision string `json:"revision"` + Branch string `json:"branch"` + BuildUser string `json:"buildUser"` + BuildDate string `json:"buildDate"` + GoVersion string `json:"goVersion"` +} + +// Interface guard. +var _ http.Handler = (*VersionHandler)(nil) + +func NewVersionHandler() VersionHandler { + return VersionHandler{} +} + +func (h VersionHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) { + // we can't use "version" directly as it is a package, and not an object that + // can be serialized. + err := json.NewEncoder(w).Encode(prometheusVersion{ + Version: version.Version, + Revision: version.Revision, + Branch: version.Branch, + BuildUser: version.BuildUser, + BuildDate: version.BuildDate, + GoVersion: version.GoVersion, + }) + if err != nil { + http.Error(w, fmt.Sprintf("error encoding JSON: %s", err), http.StatusInternalServerError) + } +} diff --git a/pkg/initiate/initiate.go b/pkg/initiate/initiate.go index 6efc1eea6..ced9332e6 100644 --- a/pkg/initiate/initiate.go +++ b/pkg/initiate/initiate.go @@ -50,7 +50,9 @@ func init() { if err != nil { os.Exit(2) } + _ = logger.Error(102, fmt.Sprintf("Failed to detect service: %v", err)) + os.Exit(1) } @@ -59,7 +61,9 @@ func init() { if err != nil { os.Exit(2) } + _ = logger.Info(100, "Attempting to start exporter service") + go func() { err = svc.Run(serviceName, &windowsExporterService{}) if err != nil { diff --git a/pkg/log/eventlog/eventlog.go b/pkg/log/eventlog/eventlog.go index 5df5a4fc8..63b4876de 100644 --- a/pkg/log/eventlog/eventlog.go +++ b/pkg/log/eventlog/eventlog.go @@ -1,21 +1,14 @@ //go:build windows -// +build windows // Package eventlog provides a Logger that writes to Windows Event Log. package eventlog import ( "bytes" - "errors" "fmt" "io" - "sync" - "syscall" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "golang.org/x/sys/windows" - goeventlog "golang.org/x/sys/windows/svc/eventlog" ) const ( @@ -25,109 +18,36 @@ const ( neLogOemCode = uint32(3299) ) -type Priority struct { - etype int -} - -// NewEventLogLogger returns a new Logger which writes to Windows EventLog in event log format. -// The body of the log message is the formatted output from the Logger returned -// by newLogger. -func NewEventLogLogger(w *goeventlog.Log, newLogger func(io.Writer) log.Logger) log.Logger { - l := &eventlogLogger{ - w: w, - newLogger: newLogger, - prioritySelector: defaultPrioritySelector, - bufPool: sync.Pool{New: func() interface{} { - return &loggerBuf{} - }}, - } +// Interface guard. +var _ io.Writer = (*Writer)(nil) - return l +type Writer struct { + handle windows.Handle } -type eventlogLogger struct { - w *goeventlog.Log - newLogger func(io.Writer) log.Logger - prioritySelector PrioritySelector - bufPool sync.Pool +// NewEventLogWriter returns a new Writer which writes to Windows EventLog. +func NewEventLogWriter(handle windows.Handle) *Writer { + return &Writer{handle: handle} } -func (l *eventlogLogger) Log(keyvals ...interface{}) error { - priority := l.prioritySelector(keyvals...) - - lb, err := l.getLoggerBuf() - if err != nil { - return err - } +func (w *Writer) Write(p []byte) (int, error) { + var eType uint16 - defer l.putLoggerBuf(lb) - if err := lb.logger.Log(keyvals...); err != nil { - return err + switch { + case bytes.Contains(p, []byte(" level=error")) || bytes.Contains(p, []byte(`"level":"error"`)): + eType = windows.EVENTLOG_ERROR_TYPE + case bytes.Contains(p, []byte(" level=warn")) || bytes.Contains(p, []byte(`"level":"warn"`)): + eType = windows.EVENTLOG_WARNING_TYPE + default: + eType = windows.EVENTLOG_INFORMATION_TYPE } - // golang.org/x/sys/windows/svc/eventlog does not provide func which allows to send more than one string. - // See: https://github.com/golang/go/issues/59780 - - msg, err := syscall.UTF16PtrFromString(lb.buf.String()) + msg, err := windows.UTF16PtrFromString(string(p)) if err != nil { - return fmt.Errorf("error convert string to UTF-16: %w", err) + return 0, fmt.Errorf("error convert string to UTF-16: %w", err) } ss := []*uint16{msg, nil, nil, nil, nil, nil, nil, nil, nil} - return windows.ReportEvent(l.w.Handle, uint16(priority.etype), 0, neLogOemCode, 0, 9, 0, &ss[0], nil) -} - -type loggerBuf struct { - buf *bytes.Buffer - logger log.Logger -} - -func (l *eventlogLogger) getLoggerBuf() (*loggerBuf, error) { - lb, ok := l.bufPool.Get().(*loggerBuf) - if !ok { - return nil, errors.New("failed to get loggerBuf from pool") - } - - if lb.buf == nil { - lb.buf = &bytes.Buffer{} - lb.logger = l.newLogger(lb.buf) - } else { - lb.buf.Reset() - } - return lb, nil -} - -func (l *eventlogLogger) putLoggerBuf(lb *loggerBuf) { - l.bufPool.Put(lb) -} - -// PrioritySelector inspects the list of keyvals and selects an eventlog priority. -type PrioritySelector func(keyvals ...interface{}) Priority - -// defaultPrioritySelector convert a kit/log level into a Windows Eventlog level. -func defaultPrioritySelector(keyvals ...interface{}) Priority { - l := len(keyvals) - - eType := windows.EVENTLOG_SUCCESS - - for i := 0; i < l; i += 2 { - if keyvals[i] == level.Key() { - var val interface{} - if i+1 < l { - val = keyvals[i+1] - } - if v, ok := val.(level.Value); ok { - switch v { - case level.ErrorValue(): - eType = windows.EVENTLOG_ERROR_TYPE - case level.WarnValue(): - eType = windows.EVENTLOG_WARNING_TYPE - case level.InfoValue(): - eType = windows.EVENTLOG_INFORMATION_TYPE - } - } - } - } - return Priority{etype: eType} + return len(p), windows.ReportEvent(w.handle, eType, 0, neLogOemCode, 0, 9, 0, &ss[0], nil) } diff --git a/pkg/log/flag/flag.go b/pkg/log/flag/flag.go index d16ffa784..d03df52a3 100644 --- a/pkg/log/flag/flag.go +++ b/pkg/log/flag/flag.go @@ -16,8 +16,8 @@ package flag import ( "github.com/alecthomas/kingpin/v2" "github.com/prometheus-community/windows_exporter/pkg/log" - "github.com/prometheus/common/promlog" - promlogflag "github.com/prometheus/common/promlog/flag" + "github.com/prometheus/common/promslog" + "github.com/prometheus/common/promslog/flag" ) // FileFlagName is the canonical flag name to configure the log file. @@ -29,15 +29,9 @@ const FileFlagHelp = "Output file of log messages. One of [stdout, stderr, event // AddFlags adds the flags used by this package to the Kingpin application. // To use the default Kingpin application, call AddFlags(kingpin.CommandLine). func AddFlags(a *kingpin.Application, config *log.Config) { - config.Level = &promlog.AllowedLevel{} - a.Flag(promlogflag.LevelFlagName, promlogflag.LevelFlagHelp). - Default("info").SetValue(config.Level) + config.Config = new(promslog.Config) + flag.AddFlags(a, config.Config) config.File = &log.AllowedFile{} - a.Flag(FileFlagName, FileFlagHelp). - Default("stderr").SetValue(config.File) - - config.Format = &promlog.AllowedFormat{} - a.Flag(promlogflag.FormatFlagName, promlogflag.FormatFlagHelp). - Default("logfmt").SetValue(config.Format) + a.Flag(FileFlagName, FileFlagHelp).Default("stderr").SetValue(config.File) } diff --git a/pkg/log/logger.go b/pkg/log/logger.go index 4bf89015b..ea18c675d 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -4,12 +4,12 @@ import ( "errors" "fmt" "io" + "log/slog" "os" - "github.com/go-kit/log" "github.com/prometheus-community/windows_exporter/pkg/log/eventlog" - "github.com/prometheus/common/promlog" - goeventlog "golang.org/x/sys/windows/svc/eventlog" + "github.com/prometheus/common/promslog" + "golang.org/x/sys/windows" ) // AllowedFile is a settable identifier for the output file that the logger can have. @@ -25,71 +25,45 @@ func (f *AllowedFile) String() string { // Set updates the value of the allowed format. func (f *AllowedFile) Set(s string) error { f.s = s + switch s { case "stdout": f.w = os.Stdout case "stderr": f.w = os.Stderr case "eventlog": - f.w = nil + handle, err := windows.RegisterEventSource(nil, windows.StringToUTF16Ptr("windows_exporter")) + if err != nil { + return fmt.Errorf("failed to open event log: %w", err) + } + + f.w = eventlog.NewEventLogWriter(handle) default: file, err := os.OpenFile(s, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o200) if err != nil { - return err + return fmt.Errorf("failed to open log file: %w", err) } + f.w = file } + return nil } // Config is a struct containing configurable settings for the logger. type Config struct { - promlog.Config + *promslog.Config File *AllowedFile } -func New(config *Config) (log.Logger, error) { +func New(config *Config) (*slog.Logger, error) { if config.File == nil { return nil, errors.New("log file undefined") } - if config.Format == nil { - return nil, errors.New("log format undefined") - } - - var ( - l log.Logger - loggerFunc func(io.Writer) log.Logger - ) - - switch config.Format.String() { - case "json": - loggerFunc = log.NewJSONLogger - case "logfmt": - loggerFunc = log.NewLogfmtLogger - default: - return nil, fmt.Errorf("unsupported log.format %q", config.Format.String()) - } - - switch { - case config.File.s == "eventlog": - - w, err := goeventlog.Open("windows_exporter") - if err != nil { - return nil, err - } - l = eventlog.NewEventLogLogger(w, loggerFunc) - case config.File.w == nil: - panic("logger: file writer is nil") - default: - l = loggerFunc(log.NewSyncWriter(config.File.w)) - } - - promlogConfig := promlog.Config{ - Format: config.Format, - Level: config.Level, - } + config.Config.Writer = config.File.w + config.Config.Style = promslog.GoKitStyle - return promlog.NewWithLogger(l, &promlogConfig), nil + return promslog.New(config.Config), nil } diff --git a/pkg/perfdata/collector.go b/pkg/perfdata/collector.go new file mode 100644 index 000000000..acd602646 --- /dev/null +++ b/pkg/perfdata/collector.go @@ -0,0 +1,236 @@ +//go:build windows + +package perfdata + +import ( + "errors" + "fmt" + "strings" + "time" + "unsafe" + + "github.com/prometheus/client_golang/prometheus" + "golang.org/x/sys/windows" +) + +const EmptyInstance = "------" + +type Collector struct { + time time.Time + object string + counters []Counter + handle pdhQueryHandle +} + +type Counter struct { + Name string + Instances map[string]pdhCounterHandle + Type uint32 + Frequency float64 +} + +type CounterValues struct { + Type prometheus.ValueType + FirstValue float64 + SecondValue float64 +} + +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 { + instances = []string{EmptyInstance} + } + + collector := &Collector{ + object: object, + counters: make([]Counter, 0, len(counters)), + handle: handle, + } + + 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 + + if counter.Type == 0 { + // Get the info with the current buffer size + bufLen := uint32(0) + + if ret := PdhGetCounterInfo(counterHandle, 0, &bufLen, nil); ret != PdhMoreData { + return nil, fmt.Errorf("PdhGetCounterInfo: %w", NewPdhError(ret)) + } + + buf := make([]byte, bufLen) + if ret := PdhGetCounterInfo(counterHandle, 0, &bufLen, &buf[0]); ret != ErrorSuccess { + return nil, fmt.Errorf("PdhGetCounterInfo: %w", NewPdhError(ret)) + } + + ci := (*PdhCounterInfo)(unsafe.Pointer(&buf[0])) + counter.Type = ci.DwType + + frequency := float64(0) + + if ret := PdhGetCounterTimeBase(counterHandle, &frequency); ret != ErrorSuccess { + return nil, fmt.Errorf("PdhGetCounterTimeBase: %w", NewPdhError(ret)) + } + + counter.Frequency = frequency + } + } + + collector.counters = append(collector.counters, counter) + } + + if len(collector.counters) == 0 { + return nil, errors.New("no counters configured") + } + + if _, err := collector.Collect(); err != nil { + return nil, fmt.Errorf("failed to collect initial data: %w", err) + } + + return collector, nil +} + +func (c *Collector) Collect() (map[string]map[string]CounterValues, error) { + if len(c.counters) == 0 { + return map[string]map[string]CounterValues{}, nil + } + + if ret := PdhCollectQueryData(c.handle); ret != ErrorSuccess { + return nil, fmt.Errorf("failed to collect query data: %w", NewPdhError(ret)) + } + + c.time = time.Now() + + var data map[string]map[string]CounterValues + + 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 + } + + items := (*[1 << 20]PdhRawCounterItem)(unsafe.Pointer(&buf[0]))[:itemCount] + + if data == nil { + data = make(map[string]map[string]CounterValues, itemCount) + } + + var metricType prometheus.ValueType + if val, ok := supportedCounterTypes[counter.Type]; ok { + metricType = val + } else { + metricType = prometheus.GaugeValue + } + + for _, item := range items { + if item.RawValue.CStatus == PdhCstatusValidData || item.RawValue.CStatus == PdhCstatusNewData { + instanceName := windows.UTF16PtrToString(item.SzName) + if strings.HasSuffix(instanceName, "_Total") { + continue + } + + if instanceName == "" { + instanceName = EmptyInstance + } + + if _, ok := data[instanceName]; !ok { + data[instanceName] = make(map[string]CounterValues, len(c.counters)) + } + + values := CounterValues{ + 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 + + switch counter.Type { + case PERF_ELAPSED_TIME: + values.FirstValue = float64(item.RawValue.FirstValue-WindowsEpoch) / counter.Frequency + values.SecondValue = float64(item.RawValue.SecondValue-WindowsEpoch) / counter.Frequency + case PERF_100NSEC_TIMER, PERF_PRECISION_100NS_TIMER: + values.FirstValue = float64(item.RawValue.FirstValue) * TicksToSecondScaleFactor + values.SecondValue = float64(item.RawValue.SecondValue) * TicksToSecondScaleFactor + default: + values.FirstValue = float64(item.RawValue.FirstValue) + values.SecondValue = float64(item.RawValue.SecondValue) + } + + data[instanceName][counter.Name] = values + } + } + } + } + + return data, nil +} + +func (c *Collector) Close() { + PdhCloseQuery(c.handle) +} + +func formatCounterPath(object, instance, counterName string) string { + var counterPath string + + if instance == EmptyInstance { + 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) +} diff --git a/pkg/perfdata/collector_bench_test.go b/pkg/perfdata/collector_bench_test.go new file mode 100644 index 000000000..d0a4aeb22 --- /dev/null +++ b/pkg/perfdata/collector_bench_test.go @@ -0,0 +1,49 @@ +package perfdata_test + +import ( + "testing" + + "github.com/prometheus-community/windows_exporter/pkg/perfdata" + "github.com/stretchr/testify/require" +) + +func BenchmarkTestCollector(b *testing.B) { + counters := []string{ + "% Processor Time", + "% Privileged Time", + "% User Time", + "Creating Process ID", + "Elapsed Time", + "Handle Count", + "ID Process", + "IO Data Bytes/sec", + "IO Data Operations/sec", + "IO Other Bytes/sec", + "IO Other Operations/sec", + "IO Read Bytes/sec", + "IO Read Operations/sec", + "IO Write Bytes/sec", + "IO Write Operations/sec", + "Page Faults/sec", + "Page File Bytes Peak", + "Page File Bytes", + "Pool Nonpaged Bytes", + "Pool Paged Bytes", + "Priority Base", + "Private Bytes", + "Thread Count", + "Virtual Bytes Peak", + "Virtual Bytes", + "Working Set - Private", + "Working Set Peak", + "Working Set", + } + performanceData, err := perfdata.NewCollector("Process", []string{"*"}, counters) + require.NoError(b, err) + + for i := 0; i < b.N; i++ { + _, _ = performanceData.Collect() + } + + performanceData.Close() +} diff --git a/pkg/perfdata/collector_test.go b/pkg/perfdata/collector_test.go new file mode 100644 index 000000000..6accd501f --- /dev/null +++ b/pkg/perfdata/collector_test.go @@ -0,0 +1,86 @@ +//go:build windows + +package perfdata_test + +import ( + "testing" + "time" + + "github.com/prometheus-community/windows_exporter/pkg/perfdata" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCollector(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + object string + instances []string + counters []string + }{ + { + object: "Memory", + counters: []string{ + "Available Bytes", + "Available KBytes", + "Available MBytes", + "Cache Bytes", + "Cache Bytes Peak", + "Cache Faults/sec", + "Commit Limit", + "Committed Bytes", + "Demand Zero Faults/sec", + "Free & Zero Page List Bytes", + "Free System Page Table Entries", + "Modified Page List Bytes", + "Page Reads/sec", + }, + }, { + object: "TCPv4", + counters: []string{ + "Connection Failures", + "Connections Active", + "Connections Established", + "Connections Passive", + "Connections Reset", + "Segments/sec", + "Segments Received/sec", + "Segments Retransmitted/sec", + "Segments Sent/sec", + }, + }, { + object: "Process", + instances: []string{"*"}, + counters: []string{ + "Thread Count", + "ID Process", + }, + }, + } { + t.Run(tc.object, func(t *testing.T) { + t.Parallel() + + performanceData, err := perfdata.NewCollector(tc.object, tc.instances, tc.counters) + require.NoError(t, err) + + time.Sleep(100 * time.Millisecond) + + data, err := performanceData.Collect() + require.NoError(t, err) + require.NotEmpty(t, data) + + for instance, d := range data { + require.NotEmpty(t, d) + + if instance == "Idle" || instance == "Secure System" { + continue + } + + for _, c := range tc.counters { + assert.NotZerof(t, d[c].FirstValue, "object: %s, instance: %s, counter: %s", tc.object, instance, c) + } + } + }) + } +} diff --git a/pkg/perfdata/const.go b/pkg/perfdata/const.go new file mode 100644 index 000000000..c618fd559 --- /dev/null +++ b/pkg/perfdata/const.go @@ -0,0 +1,78 @@ +//go:build windows + +package perfdata + +import "github.com/prometheus/client_golang/prometheus" + +// Conversion factors. +const ( + TicksToSecondScaleFactor = 1 / 1e7 + WindowsEpoch = 116444736000000000 +) + +// Based on https://github.com/leoluk/perflib_exporter/blob/master/collector/mapper.go +// +//goland:noinspection GoUnusedConst +const ( + PERF_COUNTER_RAWCOUNT_HEX = 0x00000000 + PERF_COUNTER_LARGE_RAWCOUNT_HEX = 0x00000100 + PERF_COUNTER_TEXT = 0x00000b00 + PERF_COUNTER_RAWCOUNT = 0x00010000 + PERF_COUNTER_LARGE_RAWCOUNT = 0x00010100 + PERF_DOUBLE_RAW = 0x00012000 + PERF_COUNTER_DELTA = 0x00400400 + PERF_COUNTER_LARGE_DELTA = 0x00400500 + PERF_SAMPLE_COUNTER = 0x00410400 + PERF_COUNTER_QUEUELEN_TYPE = 0x00450400 + PERF_COUNTER_LARGE_QUEUELEN_TYPE = 0x00450500 + PERF_COUNTER_100NS_QUEUELEN_TYPE = 0x00550500 + PERF_COUNTER_OBJ_TIME_QUEUELEN_TYPE = 0x00650500 + PERF_COUNTER_COUNTER = 0x10410400 + PERF_COUNTER_BULK_COUNT = 0x10410500 + PERF_RAW_FRACTION = 0x20020400 + PERF_LARGE_RAW_FRACTION = 0x20020500 + PERF_COUNTER_TIMER = 0x20410500 + PERF_PRECISION_SYSTEM_TIMER = 0x20470500 + PERF_100NSEC_TIMER = 0x20510500 + PERF_PRECISION_100NS_TIMER = 0x20570500 + PERF_OBJ_TIME_TIMER = 0x20610500 + PERF_PRECISION_OBJECT_TIMER = 0x20670500 + PERF_SAMPLE_FRACTION = 0x20c20400 + PERF_COUNTER_TIMER_INV = 0x21410500 + PERF_100NSEC_TIMER_INV = 0x21510500 + PERF_COUNTER_MULTI_TIMER = 0x22410500 + PERF_100NSEC_MULTI_TIMER = 0x22510500 + PERF_COUNTER_MULTI_TIMER_INV = 0x23410500 + PERF_100NSEC_MULTI_TIMER_INV = 0x23510500 + PERF_AVERAGE_TIMER = 0x30020400 + PERF_ELAPSED_TIME = 0x30240500 + PERF_COUNTER_NODATA = 0x40000200 + PERF_AVERAGE_BULK = 0x40020500 + PERF_SAMPLE_BASE = 0x40030401 + PERF_AVERAGE_BASE = 0x40030402 + PERF_RAW_BASE = 0x40030403 + PERF_PRECISION_TIMESTAMP = 0x40030500 + PERF_LARGE_RAW_BASE = 0x40030503 + PERF_COUNTER_MULTI_BASE = 0x42030500 + PERF_COUNTER_HISTOGRAM_TYPE = 0x80000000 +) + +var supportedCounterTypes = map[uint32]prometheus.ValueType{ + PERF_COUNTER_RAWCOUNT_HEX: prometheus.GaugeValue, + PERF_COUNTER_LARGE_RAWCOUNT_HEX: prometheus.GaugeValue, + PERF_COUNTER_RAWCOUNT: prometheus.GaugeValue, + PERF_COUNTER_LARGE_RAWCOUNT: prometheus.GaugeValue, + PERF_COUNTER_DELTA: prometheus.CounterValue, + PERF_COUNTER_COUNTER: prometheus.CounterValue, + PERF_COUNTER_BULK_COUNT: prometheus.CounterValue, + PERF_RAW_FRACTION: prometheus.GaugeValue, + PERF_LARGE_RAW_FRACTION: prometheus.GaugeValue, + PERF_100NSEC_TIMER: prometheus.CounterValue, + PERF_PRECISION_100NS_TIMER: prometheus.CounterValue, + PERF_SAMPLE_FRACTION: prometheus.GaugeValue, + PERF_100NSEC_TIMER_INV: prometheus.CounterValue, + PERF_ELAPSED_TIME: prometheus.GaugeValue, + PERF_SAMPLE_BASE: prometheus.GaugeValue, + PERF_RAW_BASE: prometheus.GaugeValue, + PERF_LARGE_RAW_BASE: prometheus.GaugeValue, +} diff --git a/pkg/perfdata/error.go b/pkg/perfdata/error.go new file mode 100644 index 000000000..46fa4fb90 --- /dev/null +++ b/pkg/perfdata/error.go @@ -0,0 +1,18 @@ +package perfdata + +// Error represents error returned from Performance Counters API. +type Error struct { + ErrorCode uint32 + errorText string +} + +func (m *Error) Error() string { + return m.errorText +} + +func NewPdhError(code uint32) error { + return &Error{ + ErrorCode: code, + errorText: PdhFormatError(code), + } +} diff --git a/pkg/perfdata/pdh.go b/pkg/perfdata/pdh.go new file mode 100644 index 000000000..21e4a232b --- /dev/null +++ b/pkg/perfdata/pdh.go @@ -0,0 +1,638 @@ +// Copyright (c) 2010-2024 The win Authors. All rights reserved. +// Copyright (c) 2024 The prometheus-community Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. The names of the authors may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// This is the official list of 'win' authors for copyright purposes. +// +// Alexander Neumann +// Joseph Watson +// Kevin Pors + +//go:build windows + +package perfdata + +import ( + "fmt" + "time" + "unsafe" + + "github.com/prometheus-community/windows_exporter/pkg/headers/kernel32" + "golang.org/x/sys/windows" +) + +// Error codes. +const ( + ErrorSuccess = 0 + ErrorFailure = 1 + ErrorInvalidFunction = 1 + EpochDifferenceMicros int64 = 11644473600000000 +) + +type ( + HANDLE uintptr +) + +// PDH error codes, which can be returned by all Pdh* functions. Taken from mingw-w64 pdhmsg.h + +const ( + PdhCstatusValidData = 0x00000000 // The returned data is valid. + PdhCstatusNewData = 0x00000001 // The return data value is valid and different from the last sample. + PdhCstatusNoMachine = 0x800007D0 // Unable to connect to the specified computer, or the computer is offline. + PdhCstatusNoInstance = 0x800007D1 + PdhMoreData = 0x800007D2 // The PdhGetFormattedCounterArray* function can return this if there's 'more data to be displayed'. + PdhCstatusItemNotValidated = 0x800007D3 + PdhRetry = 0x800007D4 + PdhNoData = 0x800007D5 // The query does not currently contain any counters (for example, limited access) + PdhCalcNegativeDenominator = 0x800007D6 + PdhCalcNegativeTimebase = 0x800007D7 + PdhCalcNegativeValue = 0x800007D8 + PdhDialogCancelled = 0x800007D9 + PdhEndOfLogFile = 0x800007DA + PdhAsyncQueryTimeout = 0x800007DB + PdhCannotSetDefaultRealtimeDatasource = 0x800007DC + PdhCstatusNoObject = 0xC0000BB8 + PdhCstatusNoCounter = 0xC0000BB9 // The specified counter could not be found. + PdhCstatusInvalidData = 0xC0000BBA // The counter was successfully found, but the data returned is not valid. + PdhMemoryAllocationFailure = 0xC0000BBB + PdhInvalidHandle = 0xC0000BBC + PdhInvalidArgument = 0xC0000BBD // Required argument is missing or incorrect. + PdhFunctionNotFound = 0xC0000BBE + PdhCstatusNoCountername = 0xC0000BBF + PdhCstatusBadCountername = 0xC0000BC0 // Unable to parse the counter path. Check the format and syntax of the specified path. + PdhInvalidBuffer = 0xC0000BC1 + PdhInsufficientBuffer = 0xC0000BC2 + PdhCannotConnectMachine = 0xC0000BC3 + PdhInvalidPath = 0xC0000BC4 + PdhInvalidInstance = 0xC0000BC5 + PdhInvalidData = 0xC0000BC6 // specified counter does not contain valid data or a successful status code. + PdhNoDialogData = 0xC0000BC7 + PdhCannotReadNameStrings = 0xC0000BC8 + PdhLogFileCreateError = 0xC0000BC9 + PdhLogFileOpenError = 0xC0000BCA + PdhLogTypeNotFound = 0xC0000BCB + PdhNoMoreData = 0xC0000BCC + PdhEntryNotInLogFile = 0xC0000BCD + PdhDataSourceIsLogFile = 0xC0000BCE + PdhDataSourceIsRealTime = 0xC0000BCF + PdhUnableReadLogHeader = 0xC0000BD0 + PdhFileNotFound = 0xC0000BD1 + PdhFileAlreadyExists = 0xC0000BD2 + PdhNotImplemented = 0xC0000BD3 + PdhStringNotFound = 0xC0000BD4 + PdhUnableMapNameFiles = 0x80000BD5 + PdhUnknownLogFormat = 0xC0000BD6 + PdhUnknownLogsvcCommand = 0xC0000BD7 + PdhLogsvcQueryNotFound = 0xC0000BD8 + PdhLogsvcNotOpened = 0xC0000BD9 + PdhWbemError = 0xC0000BDA + PdhAccessDenied = 0xC0000BDB + PdhLogFileTooSmall = 0xC0000BDC + PdhInvalidDatasource = 0xC0000BDD + PdhInvalidSqldb = 0xC0000BDE + PdhNoCounters = 0xC0000BDF + PdhSQLAllocFailed = 0xC0000BE0 + PdhSQLAllocconFailed = 0xC0000BE1 + PdhSQLExecDirectFailed = 0xC0000BE2 + PdhSQLFetchFailed = 0xC0000BE3 + PdhSQLRowcountFailed = 0xC0000BE4 + PdhSQLMoreResultsFailed = 0xC0000BE5 + PdhSQLConnectFailed = 0xC0000BE6 + PdhSQLBindFailed = 0xC0000BE7 + PdhCannotConnectWmiServer = 0xC0000BE8 + PdhPlaCollectionAlreadyRunning = 0xC0000BE9 + PdhPlaErrorScheduleOverlap = 0xC0000BEA + PdhPlaCollectionNotFound = 0xC0000BEB + PdhPlaErrorScheduleElapsed = 0xC0000BEC + PdhPlaErrorNostart = 0xC0000BED + PdhPlaErrorAlreadyExists = 0xC0000BEE + PdhPlaErrorTypeMismatch = 0xC0000BEF + PdhPlaErrorFilepath = 0xC0000BF0 + PdhPlaServiceError = 0xC0000BF1 + PdhPlaValidationError = 0xC0000BF2 + PdhPlaValidationWarning = 0x80000BF3 + PdhPlaErrorNameTooLong = 0xC0000BF4 + PdhInvalidSQLLogFormat = 0xC0000BF5 + PdhCounterAlreadyInQuery = 0xC0000BF6 + PdhBinaryLogCorrupt = 0xC0000BF7 + PdhLogSampleTooSmall = 0xC0000BF8 + PdhOsLaterVersion = 0xC0000BF9 + PdhOsEarlierVersion = 0xC0000BFA + PdhIncorrectAppendTime = 0xC0000BFB + PdhUnmatchedAppendCounter = 0xC0000BFC + PdhSQLAlterDetailFailed = 0xC0000BFD + PdhQueryPerfDataTimeout = 0xC0000BFE +) + +var PDHErrors = map[uint32]string{ + PdhCstatusValidData: "PDH_CSTATUS_VALID_DATA", + PdhCstatusNewData: "PDH_CSTATUS_NEW_DATA", + PdhCstatusNoMachine: "PDH_CSTATUS_NO_MACHINE", + PdhCstatusNoInstance: "PDH_CSTATUS_NO_INSTANCE", + PdhMoreData: "PDH_MORE_DATA", + PdhCstatusItemNotValidated: "PDH_CSTATUS_ITEM_NOT_VALIDATED", + PdhRetry: "PDH_RETRY", + PdhNoData: "PDH_NO_DATA", + PdhCalcNegativeDenominator: "PDH_CALC_NEGATIVE_DENOMINATOR", + PdhCalcNegativeTimebase: "PDH_CALC_NEGATIVE_TIMEBASE", + PdhCalcNegativeValue: "PDH_CALC_NEGATIVE_VALUE", + PdhDialogCancelled: "PDH_DIALOG_CANCELLED", + PdhEndOfLogFile: "PDH_END_OF_LOG_FILE", + PdhAsyncQueryTimeout: "PDH_ASYNC_QUERY_TIMEOUT", + PdhCannotSetDefaultRealtimeDatasource: "PDH_CANNOT_SET_DEFAULT_REALTIME_DATASOURCE", + PdhCstatusNoObject: "PDH_CSTATUS_NO_OBJECT", + PdhCstatusNoCounter: "PDH_CSTATUS_NO_COUNTER", + PdhCstatusInvalidData: "PDH_CSTATUS_INVALID_DATA", + PdhMemoryAllocationFailure: "PDH_MEMORY_ALLOCATION_FAILURE", + PdhInvalidHandle: "PDH_INVALID_HANDLE", + PdhInvalidArgument: "PDH_INVALID_ARGUMENT", + PdhFunctionNotFound: "PDH_FUNCTION_NOT_FOUND", + PdhCstatusNoCountername: "PDH_CSTATUS_NO_COUNTERNAME", + PdhCstatusBadCountername: "PDH_CSTATUS_BAD_COUNTERNAME", + PdhInvalidBuffer: "PDH_INVALID_BUFFER", + PdhInsufficientBuffer: "PDH_INSUFFICIENT_BUFFER", + PdhCannotConnectMachine: "PDH_CANNOT_CONNECT_MACHINE", + PdhInvalidPath: "PDH_INVALID_PATH", + PdhInvalidInstance: "PDH_INVALID_INSTANCE", + PdhInvalidData: "PDH_INVALID_DATA", + PdhNoDialogData: "PDH_NO_DIALOG_DATA", + PdhCannotReadNameStrings: "PDH_CANNOT_READ_NAME_STRINGS", + PdhLogFileCreateError: "PDH_LOG_FILE_CREATE_ERROR", + PdhLogFileOpenError: "PDH_LOG_FILE_OPEN_ERROR", + PdhLogTypeNotFound: "PDH_LOG_TYPE_NOT_FOUND", + PdhNoMoreData: "PDH_NO_MORE_DATA", + PdhEntryNotInLogFile: "PDH_ENTRY_NOT_IN_LOG_FILE", + PdhDataSourceIsLogFile: "PDH_DATA_SOURCE_IS_LOG_FILE", + PdhDataSourceIsRealTime: "PDH_DATA_SOURCE_IS_REAL_TIME", + PdhUnableReadLogHeader: "PDH_UNABLE_READ_LOG_HEADER", + PdhFileNotFound: "PDH_FILE_NOT_FOUND", + PdhFileAlreadyExists: "PDH_FILE_ALREADY_EXISTS", + PdhNotImplemented: "PDH_NOT_IMPLEMENTED", + PdhStringNotFound: "PDH_STRING_NOT_FOUND", + PdhUnableMapNameFiles: "PDH_UNABLE_MAP_NAME_FILES", + PdhUnknownLogFormat: "PDH_UNKNOWN_LOG_FORMAT", + PdhUnknownLogsvcCommand: "PDH_UNKNOWN_LOGSVC_COMMAND", + PdhLogsvcQueryNotFound: "PDH_LOGSVC_QUERY_NOT_FOUND", + PdhLogsvcNotOpened: "PDH_LOGSVC_NOT_OPENED", + PdhWbemError: "PDH_WBEM_ERROR", + PdhAccessDenied: "PDH_ACCESS_DENIED", + PdhLogFileTooSmall: "PDH_LOG_FILE_TOO_SMALL", + PdhInvalidDatasource: "PDH_INVALID_DATASOURCE", + PdhInvalidSqldb: "PDH_INVALID_SQLDB", + PdhNoCounters: "PDH_NO_COUNTERS", + PdhSQLAllocFailed: "PDH_SQL_ALLOC_FAILED", + PdhSQLAllocconFailed: "PDH_SQL_ALLOCCON_FAILED", + PdhSQLExecDirectFailed: "PDH_SQL_EXEC_DIRECT_FAILED", + PdhSQLFetchFailed: "PDH_SQL_FETCH_FAILED", + PdhSQLRowcountFailed: "PDH_SQL_ROWCOUNT_FAILED", + PdhSQLMoreResultsFailed: "PDH_SQL_MORE_RESULTS_FAILED", + PdhSQLConnectFailed: "PDH_SQL_CONNECT_FAILED", + PdhSQLBindFailed: "PDH_SQL_BIND_FAILED", + PdhCannotConnectWmiServer: "PDH_CANNOT_CONNECT_WMI_SERVER", + PdhPlaCollectionAlreadyRunning: "PDH_PLA_COLLECTION_ALREADY_RUNNING", + PdhPlaErrorScheduleOverlap: "PDH_PLA_ERROR_SCHEDULE_OVERLAP", + PdhPlaCollectionNotFound: "PDH_PLA_COLLECTION_NOT_FOUND", + PdhPlaErrorScheduleElapsed: "PDH_PLA_ERROR_SCHEDULE_ELAPSED", + PdhPlaErrorNostart: "PDH_PLA_ERROR_NOSTART", + PdhPlaErrorAlreadyExists: "PDH_PLA_ERROR_ALREADY_EXISTS", + PdhPlaErrorTypeMismatch: "PDH_PLA_ERROR_TYPE_MISMATCH", + PdhPlaErrorFilepath: "PDH_PLA_ERROR_FILEPATH", + PdhPlaServiceError: "PDH_PLA_SERVICE_ERROR", + PdhPlaValidationError: "PDH_PLA_VALIDATION_ERROR", + PdhPlaValidationWarning: "PDH_PLA_VALIDATION_WARNING", + PdhPlaErrorNameTooLong: "PDH_PLA_ERROR_NAME_TOO_LONG", + PdhInvalidSQLLogFormat: "PDH_INVALID_SQL_LOG_FORMAT", + PdhCounterAlreadyInQuery: "PDH_COUNTER_ALREADY_IN_QUERY", + PdhBinaryLogCorrupt: "PDH_BINARY_LOG_CORRUPT", + PdhLogSampleTooSmall: "PDH_LOG_SAMPLE_TOO_SMALL", + PdhOsLaterVersion: "PDH_OS_LATER_VERSION", + PdhOsEarlierVersion: "PDH_OS_EARLIER_VERSION", + PdhIncorrectAppendTime: "PDH_INCORRECT_APPEND_TIME", + PdhUnmatchedAppendCounter: "PDH_UNMATCHED_APPEND_COUNTER", + PdhSQLAlterDetailFailed: "PDH_SQL_ALTER_DETAIL_FAILED", + PdhQueryPerfDataTimeout: "PDH_QUERY_PERF_DATA_TIMEOUT", +} + +// Formatting options for GetFormattedCounterValue(). +// +//goland:noinspection GoUnusedConst +const ( + PdhFmtRaw = 0x00000010 + PdhFmtAnsi = 0x00000020 + PdhFmtUnicode = 0x00000040 + PdhFmtLong = 0x00000100 // Return data as a long int. + PdhFmtDouble = 0x00000200 // Return data as a double precision floating point real. + PdhFmtLarge = 0x00000400 // Return data as a 64 bit integer. + PdhFmtNoscale = 0x00001000 // can be OR-ed: Do not apply the counter's default scaling factor. + PdhFmt1000 = 0x00002000 // can be OR-ed: multiply the actual value by 1,000. + PdhFmtNodata = 0x00004000 // can be OR-ed: unknown what this is for, MSDN says nothing. + PdhFmtNocap100 = 0x00008000 // can be OR-ed: do not cap values > 100. + PerfDetailCostly = 0x00010000 + PerfDetailStandard = 0x0000FFFF +) + +type ( + pdhQueryHandle HANDLE // query handle + pdhCounterHandle HANDLE // counter handle +) + +var ( + libPdhDll = windows.NewLazySystemDLL("pdh.dll") + + pdhAddCounterW = libPdhDll.NewProc("PdhAddCounterW") + pdhAddEnglishCounterW = libPdhDll.NewProc("PdhAddEnglishCounterW") + pdhCloseQuery = libPdhDll.NewProc("PdhCloseQuery") + pdhCollectQueryData = libPdhDll.NewProc("PdhCollectQueryData") + pdhCollectQueryDataWithTime = libPdhDll.NewProc("PdhCollectQueryDataWithTime") + pdhGetFormattedCounterValue = libPdhDll.NewProc("PdhGetFormattedCounterValue") + pdhGetFormattedCounterArrayW = libPdhDll.NewProc("PdhGetFormattedCounterArrayW") + pdhOpenQuery = libPdhDll.NewProc("PdhOpenQuery") + pdhValidatePathW = libPdhDll.NewProc("PdhValidatePathW") + pdhExpandWildCardPathW = libPdhDll.NewProc("PdhExpandWildCardPathW") + pdhGetCounterInfoW = libPdhDll.NewProc("PdhGetCounterInfoW") + pdhGetRawCounterValue = libPdhDll.NewProc("PdhGetRawCounterValue") + pdhGetRawCounterArrayW = libPdhDll.NewProc("PdhGetRawCounterArrayW") + pdhPdhGetCounterTimeBase = libPdhDll.NewProc("PdhGetCounterTimeBase") +) + +// PdhAddCounter adds the specified counter to the query. This is the internationalized version. Preferably, use the +// function PdhAddEnglishCounter instead. hQuery is the query handle, which has been fetched by PdhOpenQuery. +// szFullCounterPath is a full, internationalized counter path (this will differ per Windows language version). +// dwUserData is a 'user-defined value', which becomes part of the counter information. To retrieve this value +// later, call PdhGetCounterInfo() and access dwQueryUserData of the PdhCounterInfo structure. +// +// Examples of szFullCounterPath (in an English version of Windows): +// +// \\Processor(_Total)\\% Idle Time +// \\Processor(_Total)\\% Processor Time +// \\LogicalDisk(C:)\% Free Space +// +// To view all (internationalized...) counters on a system, there are three non-programmatic ways: perfmon utility, +// the typeperf command, and the registry editor. perfmon.exe is perhaps the easiest way, because it's basically a +// full implementation of the pdh.dll API, except with a GUI and all that. The registry setting also provides an +// interface to the available counters, and can be found at the following key: +// +// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\CurrentLanguage +// +// This registry key contains several values as follows: +// +// 1 +// 1847 +// 2 +// System +// 4 +// Memory +// 6 +// % Processor Time +// ... many, many more +// +// Somehow, these numeric values can be used as szFullCounterPath too: +// +// \2\6 will correspond to \\System\% Processor Time +// +// The typeperf command may also be pretty easy. To find all performance counters, simply execute: +// +// typeperf -qx +func PdhAddCounter(hQuery pdhQueryHandle, szFullCounterPath string, dwUserData uintptr, phCounter *pdhCounterHandle) uint32 { + ptxt, _ := windows.UTF16PtrFromString(szFullCounterPath) + ret, _, _ := pdhAddCounterW.Call( + uintptr(hQuery), + uintptr(unsafe.Pointer(ptxt)), + dwUserData, + uintptr(unsafe.Pointer(phCounter))) + + return uint32(ret) +} + +// PdhAddEnglishCounterSupported returns true if PdhAddEnglishCounterW Win API function was found in pdh.dll. +// PdhAddEnglishCounterW function is not supported on pre-Windows Vista systems. +func PdhAddEnglishCounterSupported() bool { + return pdhAddEnglishCounterW != nil +} + +// PdhAddEnglishCounter adds the specified language-neutral counter to the query. See the PdhAddCounter function. This function only exists on +// Windows versions higher than Vista. +func PdhAddEnglishCounter(hQuery pdhQueryHandle, szFullCounterPath string, dwUserData uintptr, phCounter *pdhCounterHandle) uint32 { + if pdhAddEnglishCounterW == nil { + return ErrorInvalidFunction + } + + ptxt, _ := windows.UTF16PtrFromString(szFullCounterPath) + ret, _, _ := pdhAddEnglishCounterW.Call( + uintptr(hQuery), + uintptr(unsafe.Pointer(ptxt)), + dwUserData, + uintptr(unsafe.Pointer(phCounter))) + + return uint32(ret) +} + +// PdhCloseQuery closes all counters contained in the specified query, closes all handles related to the query, +// and frees all memory associated with the query. +func PdhCloseQuery(hQuery pdhQueryHandle) uint32 { + ret, _, _ := pdhCloseQuery.Call(uintptr(hQuery)) + + return uint32(ret) +} + +// PdhCollectQueryData collects the current raw data value for all counters in the specified query and updates the status +// code of each counter. With some counters, this function needs to be repeatedly called before the value +// of the counter can be extracted with PdhGetFormattedCounterValue(). For example, the following code +// requires at least two calls: +// +// var handle win.PDH_HQUERY +// var counterHandle win.PDH_HCOUNTER +// ret := win.PdhOpenQuery(0, 0, &handle) +// ret = win.PdhAddEnglishCounter(handle, "\\Processor(_Total)\\% Idle Time", 0, &counterHandle) +// var derp win.PDH_FMT_COUNTERVALUE_DOUBLE +// +// ret = win.PdhCollectQueryData(handle) +// fmt.Printf("Collect return code is %x\n", ret) // return code will be PDH_CSTATUS_INVALID_DATA +// ret = win.PdhGetFormattedCounterValueDouble(counterHandle, 0, &derp) +// +// ret = win.PdhCollectQueryData(handle) +// fmt.Printf("Collect return code is %x\n", ret) // return code will be ERROR_SUCCESS +// ret = win.PdhGetFormattedCounterValueDouble(counterHandle, 0, &derp) +// +// The PdhCollectQueryData will return an error in the first call because it needs two values for +// displaying the correct data for the processor idle time. The second call will have a 0 return code. +func PdhCollectQueryData(hQuery pdhQueryHandle) uint32 { + ret, _, _ := pdhCollectQueryData.Call(uintptr(hQuery)) + + return uint32(ret) +} + +// PdhCollectQueryDataWithTime queries data from perfmon, retrieving the device/windows timestamp from the node it was collected on. +// Converts the filetime structure to a GO time class and returns the native time. +func PdhCollectQueryDataWithTime(hQuery pdhQueryHandle) (uint32, time.Time) { + var localFileTime windows.Filetime + + ret, _, _ := pdhCollectQueryDataWithTime.Call(uintptr(hQuery), uintptr(unsafe.Pointer(&localFileTime))) + + if ret == ErrorSuccess { + var utcFileTime windows.Filetime + + if ret := kernel32.LocalFileTimeToFileTime(&localFileTime, &utcFileTime); ret == 0 { + return uint32(ErrorFailure), time.Now() + } + + retTime := time.Unix(0, utcFileTime.Nanoseconds()) + + return uint32(ErrorSuccess), retTime + } + + return uint32(ret), time.Now() +} + +// PdhGetFormattedCounterValueDouble formats the given hCounter using a 'double'. The result is set into the specialized union struct pValue. +// This function does not directly translate to a Windows counterpart due to union specialization tricks. +func PdhGetFormattedCounterValueDouble(hCounter pdhCounterHandle, lpdwType *uint32, pValue *PdhFmtCountervalueDouble) uint32 { + ret, _, _ := pdhGetFormattedCounterValue.Call( + uintptr(hCounter), + uintptr(PdhFmtDouble|PdhFmtNocap100), + uintptr(unsafe.Pointer(lpdwType)), + uintptr(unsafe.Pointer(pValue))) + + return uint32(ret) +} + +// PdhGetFormattedCounterArrayDouble returns an array of formatted counter values. Use this function when you want to format the counter values of a +// counter that contains a wildcard character for the instance name. The itemBuffer must a slice of type PdhFmtCountervalueItemDouble. +// An example of how this function can be used: +// +// okPath := "\\Process(*)\\% Processor Time" // notice the wildcard * character +// +// // omitted all necessary stuff ... +// +// var bufSize uint32 +// var bufCount uint32 +// var size uint32 = uint32(unsafe.Sizeof(win.PDH_FMT_COUNTERVALUE_ITEM_DOUBLE{})) +// var emptyBuf [1]win.PDH_FMT_COUNTERVALUE_ITEM_DOUBLE // need at least 1 addressable null ptr. +// +// for { +// // collect +// ret := win.PdhCollectQueryData(queryHandle) +// if ret == win.ERROR_SUCCESS { +// ret = win.PdhGetFormattedCounterArrayDouble(counterHandle, &bufSize, &bufCount, &emptyBuf[0]) // uses null ptr here according to MSDN. +// if ret == win.PDH_MORE_DATA { +// filledBuf := make([]win.PDH_FMT_COUNTERVALUE_ITEM_DOUBLE, bufCount*size) +// ret = win.PdhGetFormattedCounterArrayDouble(counterHandle, &bufSize, &bufCount, &filledBuf[0]) +// for i := 0; i < int(bufCount); i++ { +// c := filledBuf[i] +// var s string = win.UTF16PtrToString(c.SzName) +// fmt.Printf("Index %d -> %s, value %v\n", i, s, c.FmtValue.DoubleValue) +// } +// +// filledBuf = nil +// // Need to at least set bufSize to zero, because if not, the function will not +// // return PDH_MORE_DATA and will not set the bufSize. +// bufCount = 0 +// bufSize = 0 +// } +// +// time.Sleep(2000 * time.Millisecond) +// } +// } +func PdhGetFormattedCounterArrayDouble(hCounter pdhCounterHandle, lpdwBufferSize *uint32, lpdwBufferCount *uint32, itemBuffer *byte) uint32 { + ret, _, _ := pdhGetFormattedCounterArrayW.Call( + uintptr(hCounter), + uintptr(PdhFmtDouble|PdhFmtNocap100), + uintptr(unsafe.Pointer(lpdwBufferSize)), + uintptr(unsafe.Pointer(lpdwBufferCount)), + uintptr(unsafe.Pointer(itemBuffer))) + + return uint32(ret) +} + +// PdhOpenQuery creates a new query that is used to manage the collection of performance data. +// szDataSource is a null terminated string that specifies the name of the log file from which to +// retrieve the performance data. If 0, performance data is collected from a real-time data source. +// dwUserData is a user-defined value to associate with this query. To retrieve the user data later, +// call PdhGetCounterInfo and access dwQueryUserData of the PdhCounterInfo structure. phQuery is +// the handle to the query, and must be used in subsequent calls. This function returns a PDH_ +// constant error code, or ErrorSuccess if the call succeeded. +func PdhOpenQuery(szDataSource uintptr, dwUserData uintptr, phQuery *pdhQueryHandle) uint32 { + ret, _, _ := pdhOpenQuery.Call( + szDataSource, + dwUserData, + uintptr(unsafe.Pointer(phQuery))) + + return uint32(ret) +} + +// PdhExpandWildCardPath examines the specified computer or log file and returns those counter paths that match the given counter path +// which contains wildcard characters. The general counter path format is as follows: +// +// \\computer\object(parent/instance#index)\counter +// +// The parent, instance, index, and counter components of the counter path may contain either a valid name or a wildcard character. +// The computer, parent, instance, and index components are not necessary for all counters. +// +// The following is a list of the possible formats: +// +// \\computer\object(parent/instance#index)\counter +// \\computer\object(parent/instance)\counter +// \\computer\object(instance#index)\counter +// \\computer\object(instance)\counter +// \\computer\object\counter +// \object(parent/instance#index)\counter +// \object(parent/instance)\counter +// \object(instance#index)\counter +// \object(instance)\counter +// \object\counter +// Use an asterisk (*) as the wildcard character, for example, \object(*)\counter. +// +// If a wildcard character is specified in the parent name, all instances of the specified object +// that match the specified instance and counter fields will be returned. +// For example, \object(*/instance)\counter. +// +// If a wildcard character is specified in the instance name, all instances of the specified object and parent object will be returned if all instance names +// corresponding to the specified index match the wildcard character. For example, \object(parent/*)\counter. +// If the object does not contain an instance, an error occurs. +// +// If a wildcard character is specified in the counter name, all counters of the specified object are returned. +// +// Partial counter path string matches (for example, "pro*") are supported. +func PdhExpandWildCardPath(szWildCardPath string, mszExpandedPathList *uint16, pcchPathListLength *uint32) uint32 { + ptxt, _ := windows.UTF16PtrFromString(szWildCardPath) + flags := uint32(0) // expand instances and counters + ret, _, _ := pdhExpandWildCardPathW.Call( + 0, // search counters on local computer + uintptr(unsafe.Pointer(ptxt)), + uintptr(unsafe.Pointer(mszExpandedPathList)), + uintptr(unsafe.Pointer(pcchPathListLength)), + uintptr(unsafe.Pointer(&flags))) + + return uint32(ret) +} + +// PdhValidatePath validates a path. Will return ErrorSuccess when ok, or PdhCstatusBadCountername when the path is erroneous. +func PdhValidatePath(path string) uint32 { + ptxt, _ := windows.UTF16PtrFromString(path) + ret, _, _ := pdhValidatePathW.Call(uintptr(unsafe.Pointer(ptxt))) + + return uint32(ret) +} + +func PdhFormatError(msgID uint32) string { + var flags uint32 = windows.FORMAT_MESSAGE_FROM_HMODULE | windows.FORMAT_MESSAGE_ARGUMENT_ARRAY | windows.FORMAT_MESSAGE_IGNORE_INSERTS + + buf := make([]uint16, 300) + _, err := windows.FormatMessage(flags, libPdhDll.Handle(), msgID, 0, buf, nil) + + if err == nil { + return windows.UTF16PtrToString(&buf[0]) + } + + return fmt.Sprintf("(pdhErr=%d) %s", msgID, err.Error()) +} + +// PdhGetCounterInfo retrieves information about a counter, such as data size, counter type, path, and user-supplied data values +// hCounter [in] +// Handle of the counter from which you want to retrieve information. The PdhAddCounter function returns this handle. +// +// bRetrieveExplainText [in] +// Determines whether explain text is retrieved. If you set this parameter to TRUE, the explain text for the counter is retrieved. +// If you set this parameter to FALSE, the field in the returned buffer is NULL. +// +// pdwBufferSize [in, out] +// Size of the lpBuffer buffer, in bytes. If zero on input, the function returns PdhMoreData and sets this parameter to the required buffer size. +// If the buffer is larger than the required size, the function sets this parameter to the actual size of the buffer that was used. +// If the specified size on input is greater than zero but less than the required size, you should not rely on the returned size to reallocate the buffer. +// +// lpBuffer [out] +// Caller-allocated buffer that receives a PdhCounterInfo structure. +// The structure is variable-length, because the string data is appended to the end of the fixed-format portion of the structure. +// This is done so that all data is returned in a single buffer allocated by the caller. Set to NULL if pdwBufferSize is zero. +func PdhGetCounterInfo(hCounter pdhCounterHandle, bRetrieveExplainText int, pdwBufferSize *uint32, lpBuffer *byte) uint32 { + ret, _, _ := pdhGetCounterInfoW.Call( + uintptr(hCounter), + uintptr(bRetrieveExplainText), + uintptr(unsafe.Pointer(pdwBufferSize)), + uintptr(unsafe.Pointer(lpBuffer))) + + return uint32(ret) +} + +// PdhGetRawCounterValue returns the current raw value of the counter. +// If the specified counter instance does not exist, this function will return ErrorSuccess +// and the CStatus member of the PdhRawCounter structure will contain PdhCstatusNoInstance. +// +// hCounter [in] +// Handle of the counter from which to retrieve the current raw value. The PdhAddCounter function returns this handle. +// +// lpdwType [out] +// Receives the counter type. For a list of counter types, see the Counter Types section of the Windows Server 2003 Deployment Kit. +// This parameter is optional. +// +// pValue [out] +// A PdhRawCounter structure that receives the counter value. +func PdhGetRawCounterValue(hCounter pdhCounterHandle, lpdwType *uint32, pValue *PdhRawCounter) uint32 { + ret, _, _ := pdhGetRawCounterValue.Call( + uintptr(hCounter), + uintptr(unsafe.Pointer(lpdwType)), + uintptr(unsafe.Pointer(pValue))) + + return uint32(ret) +} + +// PdhGetRawCounterArray returns an array of raw values from the specified counter. Use this function when you want to retrieve the raw counter values +// of a counter that contains a wildcard character for the instance name. +// hCounter +// Handle of the counter for whose current raw instance values you want to retrieve. The PdhAddCounter function returns this handle. +// +// lpdwBufferSize +// Size of the ItemBuffer buffer, in bytes. If zero on input, the function returns PdhMoreData and sets this parameter to the required buffer size. +// If the buffer is larger than the required size, the function sets this parameter to the actual size of the buffer that was used. +// If the specified size on input is greater than zero but less than the required size, you should not rely on the returned size to reallocate the buffer. +// +// lpdwItemCount +// Number of raw counter values in the ItemBuffer buffer. +// +// ItemBuffer +// Caller-allocated buffer that receives the array of PdhRawCounterItem structures; the structures contain the raw instance counter values. +// Set to NULL if lpdwBufferSize is zero. +func PdhGetRawCounterArray(hCounter pdhCounterHandle, lpdwBufferSize *uint32, lpdwBufferCount *uint32, itemBuffer *byte) uint32 { + ret, _, _ := pdhGetRawCounterArrayW.Call( + uintptr(hCounter), + uintptr(unsafe.Pointer(lpdwBufferSize)), + uintptr(unsafe.Pointer(lpdwBufferCount)), + uintptr(unsafe.Pointer(itemBuffer))) + + return uint32(ret) +} + +// PdhGetCounterTimeBase returns the time base of the specified counter. +// hCounter +// Handle of the counter for whose current raw instance values you want to retrieve. The PdhAddCounter function returns this handle. +// +// lpdwItemCount +// Time base that specifies the number of performance values a counter samples per second. +func PdhGetCounterTimeBase(hCounter pdhCounterHandle, pTimeBase *float64) uint32 { + ret, _, _ := pdhPdhGetCounterTimeBase.Call( + uintptr(hCounter), + uintptr(unsafe.Pointer(pTimeBase))) + + return uint32(ret) +} diff --git a/pkg/perfdata/pdh_amd64.go b/pkg/perfdata/pdh_amd64.go new file mode 100644 index 000000000..02ce5ec00 --- /dev/null +++ b/pkg/perfdata/pdh_amd64.go @@ -0,0 +1,143 @@ +// Copyright (c) 2010-2024 The win Authors. All rights reserved. +// Copyright (c) 2024 The prometheus-community Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. The names of the authors may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// This is the official list of 'win' authors for copyright purposes. +// +// Alexander Neumann +// Joseph Watson +// Kevin Pors + +//go:build windows + +package perfdata + +import "golang.org/x/sys/windows" + +// PdhFmtCountervalueDouble is a union specialization for double values. +type PdhFmtCountervalueDouble struct { + CStatus uint32 + DoubleValue float64 +} + +// PdhFmtCounterValueLarge is a union specialization for 64-bit integer values. +type PdhFmtCounterValueLarge struct { + CStatus uint32 + LargeValue int64 +} + +// PdhFmtCounterValueLong is a union specialization for long values. +type PdhFmtCounterValueLong struct { + CStatus uint32 + LongValue int32 + padding [4]byte //nolint:unused // Memory reservation +} + +// PdhFmtCountervalueItemDouble is a union specialization for double values, used by PdhGetFormattedCounterArrayDouble. +type PdhFmtCountervalueItemDouble struct { + SzName *uint16 + FmtValue PdhFmtCountervalueDouble +} + +// PdhFmtCounterValueItemLarge is a union specialization for 'large' values, used by PdhGetFormattedCounterArrayLarge(). +type PdhFmtCounterValueItemLarge struct { + SzName *uint16 // pointer to a string + FmtValue PdhFmtCounterValueLarge +} + +// PdhFmtCounterValueItemLong is a union specialization for long values, used by PdhGetFormattedCounterArrayLong(). +type PdhFmtCounterValueItemLong struct { + SzName *uint16 // pointer to a string + FmtValue PdhFmtCounterValueLong +} + +// PdhCounterInfo structure contains information describing the properties of a counter. This information also includes the counter path. +type PdhCounterInfo struct { + // Size of the structure, including the appended strings, in bytes. + DwLength uint32 + // Counter type. For a list of counter types, + // see the Counter Types section of the Windows Server 2003 Deployment Kit. + // The counter type constants are defined in Winperf.h. + DwType uint32 + // Counter version information. Not used. + CVersion uint32 + // Counter status that indicates if the counter value is valid. For a list of possible values, + // see Checking PDH Interface Return Values. + CStatus uint32 + // Scale factor to use when computing the displayable value of the counter. The scale factor is a power of ten. + // The valid range of this parameter is PDH_MIN_SCALE (–7) (the returned value is the actual value times 10–⁷) to + // Pdh_MAX_SCALE (+7) (the returned value is the actual value times 10⁺⁷). A value of zero will set the scale to one, so that the actual value is returned. + LScale int32 + // Default scale factor as suggested by the counter's provider. + LDefaultScale int32 + // The value passed in the dwUserData parameter when calling PdhAddCounter. + DwUserData *uint32 + // The value passed in the dwUserData parameter when calling PdhOpenQuery. + DwQueryUserData *uint32 + // Null-terminated string that specifies the full counter path. The string follows this structure in memory. + SzFullPath *uint16 // pointer to a string + // Null-terminated string that contains the name of the computer specified in the counter path. Is NULL, if the path does not specify a computer. + // The string follows this structure in memory. + SzMachineName *uint16 // pointer to a string + // Null-terminated string that contains the name of the performance object specified in the counter path. The string follows this structure in memory. + SzObjectName *uint16 // pointer to a string + // Null-terminated string that contains the name of the object instance specified in the counter path. Is NULL, if the path does not specify an instance. + // The string follows this structure in memory. + SzInstanceName *uint16 // pointer to a string + // Null-terminated string that contains the name of the parent instance specified in the counter path. + // Is NULL, if the path does not specify a parent instance. The string follows this structure in memory. + SzParentInstance *uint16 // pointer to a string + // Instance index specified in the counter path. Is 0, if the path does not specify an instance index. + DwInstanceIndex uint32 // pointer to a string + // Null-terminated string that contains the counter name. The string follows this structure in memory. + SzCounterName *uint16 // pointer to a string + // Help text that describes the counter. Is NULL if the source is a log file. + SzExplainText *uint16 // pointer to a string + // Start of the string data that is appended to the structure. + DataBuffer [1]uint32 // pointer to an extra space +} + +// The PdhRawCounter structure returns the data as it was collected from the counter provider. +// No translation, formatting, or other interpretation is performed on the data. +type PdhRawCounter struct { + // Counter status that indicates if the counter value is valid. Check this member before using the data in a calculation or displaying its value. + // For a list of possible values, see https://docs.microsoft.com/windows/desktop/PerfCtrs/checking-pdh-interface-return-values + CStatus uint32 + // Local time for when the data was collected + TimeStamp windows.Filetime + // First raw counter value. + FirstValue int64 + // Second raw counter value. Rate counters require two values in order to compute a displayable value. + SecondValue int64 + // If the counter type contains the PERF_MULTI_COUNTER flag, this member contains the additional counter data used in the calculation. + // For example, the PERF_100NSEC_MULTI_TIMER counter type contains the PERF_MULTI_COUNTER flag. + MultiCount uint32 +} + +type PdhRawCounterItem struct { + // Pointer to a null-terminated string that specifies the instance name of the counter. The string is appended to the end of this structure. + SzName *uint16 + // A PdhRawCounter structure that contains the raw counter value of the instance + RawValue PdhRawCounter +} diff --git a/pkg/perfdata/pdh_arm64.go b/pkg/perfdata/pdh_arm64.go new file mode 100644 index 000000000..0047e6acd --- /dev/null +++ b/pkg/perfdata/pdh_arm64.go @@ -0,0 +1,143 @@ +// Copyright (c) 2010-2024 The win Authors. All rights reserved. +// Copyright (c) 2024 The prometheus-community Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. The names of the authors may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// This is the official list of 'win' authors for copyright purposes. +// +// Alexander Neumann +// Joseph Watson +// Kevin Pors + +//go:build windows + +package perfdata + +import "golang.org/x/sys/windows" + +// PdhFmtCountervalueDouble is a union specialization for double values. +type PdhFmtCountervalueDouble struct { + CStatus uint32 + DoubleValue float64 +} + +// PdhFmtCounterValueLarge is a union specialization for 64-bit integer values. +type PdhFmtCountervalueLarge struct { + CStatus uint32 + LargeValue int64 +} + +// PdhFmtCounterValueLong is a union specialization for long values. +type PdhFmtCountervalueLong struct { + CStatus uint32 + LongValue int32 + padding [4]byte //nolint:unused // Memory reservation +} + +type PdhFmtCountervalueItemDouble struct { + SzName *uint16 + FmtValue PdhFmtCountervalueDouble +} + +// PdhFmtCounterValueItemLarge is a union specialization for 'large' values, used by PdhGetFormattedCounterArrayLarge(). +type PdhFmtCountervalueItemLarge struct { + SzName *uint16 // pointer to a string + FmtValue PdhFmtCountervalueLarge +} + +// PdhFmtCounterValueItemLong is a union specialization for long values, used by PdhGetFormattedCounterArrayLong(). +type PdhFmtCountervalueItemLong struct { + SzName *uint16 // pointer to a string + FmtValue PdhFmtCountervalueLong +} + +// PdhCounterInfo structure contains information describing the properties of a counter. This information also includes the counter path. +type PdhCounterInfo struct { + // Size of the structure, including the appended strings, in bytes. + DwLength uint32 + // Counter type. For a list of counter types, see the Counter Types section + // of the Windows Server 2003 Deployment Kit (http://go.microsoft.com/fwlink/p/?linkid=84422). + // The counter type constants are defined in Winperf.h. + DwType uint32 + // Counter version information. Not used. + CVersion uint32 + // Counter status that indicates if the counter value is valid. For a list of possible values, + // see Checking PDH Interface Return Values. + CStatus uint32 + // Scale factor to use when computing the displayable value of the counter. The scale factor is a power of ten. + // The valid range of this parameter is PDH_MIN_SCALE (–7) (the returned value is the actual value times 10–⁷) to + // Pdh_MAX_SCALE (+7) (the returned value is the actual value times 10⁺⁷). A value of zero will set the scale to one, so that the actual value is returned. + LScale int32 + // Default scale factor as suggested by the counter's provider. + LDefaultScale int32 + // The value passed in the dwUserData parameter when calling PdhAddCounter. + DwUserData *uint32 + // The value passed in the dwUserData parameter when calling PdhOpenQuery. + DwQueryUserData *uint32 + // Null-terminated string that specifies the full counter path. The string follows this structure in memory. + SzFullPath *uint16 // pointer to a string + // Null-terminated string that contains the name of the computer specified in the counter path. Is NULL, if the path does not specify a computer. + // The string follows this structure in memory. + SzMachineName *uint16 // pointer to a string + // Null-terminated string that contains the name of the performance object specified in the counter path. The string follows this structure in memory. + SzObjectName *uint16 // pointer to a string + // Null-terminated string that contains the name of the object instance specified in the counter path. Is NULL, if the path does not specify an instance. + // The string follows this structure in memory. + SzInstanceName *uint16 // pointer to a string + // Null-terminated string that contains the name of the parent instance specified in the counter path. + // Is NULL, if the path does not specify a parent instance. + // The string follows this structure in memory. + SzParentInstance *uint16 // pointer to a string + // Instance index specified in the counter path. Is 0, if the path does not specify an instance index. + DwInstanceIndex uint32 // pointer to a string + // Null-terminated string that contains the counter name. The string follows this structure in memory. + SzCounterName *uint16 // pointer to a string + // Help text that describes the counter. Is NULL if the source is a log file. + SzExplainText *uint16 // pointer to a string + // Start of the string data that is appended to the structure. + DataBuffer [1]uint32 // pointer to an extra space +} + +// The PdhRawCounter structure returns the data as it was collected from the counter provider. +// No translation, formatting, or other interpretation is performed on the data. +type PdhRawCounter struct { + // Counter status that indicates if the counter value is valid. Check this member before using the data in a calculation or displaying its value. + // For a list of possible values, see https://docs.microsoft.com/windows/desktop/PerfCtrs/checking-pdh-interface-return-values + CStatus uint32 + // Local time for when the data was collected + TimeStamp windows.Filetime + // First raw counter value. + FirstValue int64 + // Second raw counter value. Rate counters require two values in order to compute a displayable value. + SecondValue int64 + // If the counter type contains the PERF_MULTI_COUNTER flag, this member contains the additional counter data used in the calculation. + // For example, the PERF_100NSEC_MULTI_TIMER counter type contains the PERF_MULTI_COUNTER flag. + MultiCount uint32 +} + +type PdhRawCounterItem struct { + // Pointer to a null-terminated string that specifies the instance name of the counter. The string is appended to the end of this structure. + SzName *uint16 + // A PdhRawCounter structure that contains the raw counter value of the instance + RawValue PdhRawCounter +} diff --git a/pkg/perflib/nametable.go b/pkg/perflib/nametable.go index b10a76103..75065f7d4 100644 --- a/pkg/perflib/nametable.go +++ b/pkg/perflib/nametable.go @@ -31,11 +31,13 @@ type NameTable struct { func (t *NameTable) LookupString(index uint32) string { t.initialize() + return t.table.index[index] } func (t *NameTable) LookupIndex(str string) uint32 { t.initialize() + return t.table.string[str] } @@ -56,7 +58,9 @@ func (t *NameTable) initialize() { if err != nil { panic(err) } + r := bytes.NewReader(buffer) + for { index, err := readUTF16String(r) if err != nil { diff --git a/pkg/perflib/perflib.go b/pkg/perflib/perflib.go index b2b7042fd..58b650b87 100644 --- a/pkg/perflib/perflib.go +++ b/pkg/perflib/perflib.go @@ -119,6 +119,8 @@ import ( "strings" "syscall" "unsafe" + + "golang.org/x/sys/windows" ) // TODO: There's a LittleEndian field in the PERF header - we ought to check it. @@ -204,7 +206,7 @@ func queryRawData(query string) ([]byte, error) { buffer = make([]byte, bufLen) - name, err := syscall.UTF16PtrFromString(query) + name, err := windows.UTF16PtrFromString(query) if err != nil { return nil, fmt.Errorf("failed to encode query string: %w", err) } @@ -212,21 +214,23 @@ func queryRawData(query string) ([]byte, error) { for { bufLen := uint32(len(buffer)) + //nolint:forbidigo // Legacy Code err := syscall.RegQueryValueEx( - syscall.HKEY_PERFORMANCE_DATA, + windows.HKEY_PERFORMANCE_DATA, name, nil, &valType, (*byte)(unsafe.Pointer(&buffer[0])), &bufLen) - if errors.Is(err, error(syscall.ERROR_MORE_DATA)) { + if errors.Is(err, error(syscall.ERROR_MORE_DATA)) { //nolint:forbidigo // Legacy Code newBuffer := make([]byte, len(buffer)+16384) copy(newBuffer, buffer) buffer = newBuffer + continue } else if err != nil { - var errNo syscall.Errno + var errNo syscall.Errno //nolint:forbidigo // Legacy Code if errors.As(err, &errNo) { return nil, fmt.Errorf("ReqQueryValueEx failed: %w errno %d", err, uint(errNo)) } @@ -276,6 +280,7 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) { // Read global header header := new(perfDataBlock) + err = header.BinaryReadFrom(r) if err != nil { return nil, fmt.Errorf("failed to read performance data block for %q with: %w", query, err) @@ -300,6 +305,7 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) { } obj := new(perfObjectType) + err = obj.BinaryReadFrom(r) if err != nil { return nil, err @@ -329,6 +335,7 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) { for i := range numCounterDefs { def := new(perfCounterDefinition) + err := def.BinaryReadFrom(r) if err != nil { return nil, err @@ -410,7 +417,9 @@ func parseCounterBlock(b []byte, r io.ReadSeeker, pos int64, defs []*PerfCounter if err != nil { return 0, nil, err } + block := new(perfCounterBlock) + err = block.BinaryReadFrom(r) if err != nil { return 0, nil, err @@ -453,7 +462,6 @@ func convertCounterValue(counterDef *perfCounterDefinition, buffer []byte, value 272696576 64bit rate */ - switch counterDef.CounterSize { case 4: value = int64(bo.Uint32(buffer[valueOffset:(valueOffset + 4)])) diff --git a/pkg/perflib/raw_types.go b/pkg/perflib/raw_types.go index 5939b7da2..fbd509220 100644 --- a/pkg/perflib/raw_types.go +++ b/pkg/perflib/raw_types.go @@ -3,7 +3,8 @@ package perflib import ( "encoding/binary" "io" - "syscall" + + "golang.org/x/sys/windows" ) /* @@ -36,7 +37,7 @@ type perfDataBlock struct { HeaderLength uint32 NumObjectTypes uint32 DefaultObject int32 - SystemTime syscall.Systemtime + SystemTime windows.Systemtime _ uint32 // TODO PerfTime int64 PerfFreq int64 diff --git a/pkg/perflib/unmarshal.go b/pkg/perflib/unmarshal.go index 0219609bc..f4e0b31a7 100644 --- a/pkg/perflib/unmarshal.go +++ b/pkg/perflib/unmarshal.go @@ -3,11 +3,9 @@ package perflib import ( "errors" "fmt" + "log/slog" "reflect" "strings" - - "github.com/go-kit/log" - "github.com/go-kit/log/level" ) // Conversion factors. @@ -16,14 +14,16 @@ const ( WindowsEpoch = 116444736000000000 ) -func UnmarshalObject(obj *PerfObject, vs interface{}, logger log.Logger) error { +func UnmarshalObject(obj *PerfObject, vs interface{}, logger *slog.Logger) error { if obj == nil { return errors.New("counter not found") } + rv := reflect.ValueOf(vs) if rv.Kind() != reflect.Ptr || rv.IsNil() { return fmt.Errorf("%v is nil or not a pointer to slice", reflect.TypeOf(vs)) } + ev := rv.Elem() if ev.Kind() != reflect.Slice { return fmt.Errorf("%v is not slice", reflect.TypeOf(vs)) @@ -40,6 +40,7 @@ func UnmarshalObject(obj *PerfObject, vs interface{}, logger log.Logger) error { rt := target.Type() counters := make(map[string]*PerfCounter, len(instance.Counters)) + for _, ctr := range instance.Counters { if ctr.Def.IsBaseValue && !ctr.Def.IsNanosecondCounter { counters[ctr.Def.Name+"_Base"] = ctr @@ -50,10 +51,12 @@ func UnmarshalObject(obj *PerfObject, vs interface{}, logger log.Logger) error { for i := range target.NumField() { f := rt.Field(i) + tag := f.Tag.Get("perflib") if tag == "" { continue } + secondValue := false st := strings.Split(tag, ",") @@ -67,12 +70,15 @@ func UnmarshalObject(obj *PerfObject, vs interface{}, logger log.Logger) error { ctr, found := counters[tag] if !found { - _ = level.Debug(logger).Log("msg", fmt.Sprintf("missing counter %q, have %v", tag, counterMapKeys(counters))) + logger.Debug(fmt.Sprintf("missing counter %q, have %v", tag, counterMapKeys(counters))) + continue } + if !target.Field(i).CanSet() { return fmt.Errorf("tagged field %v cannot be written to", f.Name) } + if fieldType := target.Field(i).Type(); fieldType != reflect.TypeOf((*float64)(nil)).Elem() { return fmt.Errorf("tagged field %v has wrong type %v, must be float64", f.Name, fieldType) } @@ -81,7 +87,9 @@ func UnmarshalObject(obj *PerfObject, vs interface{}, logger log.Logger) error { if !ctr.Def.HasSecondValue { return fmt.Errorf("tagged field %v expected a SecondValue, which was not present", f.Name) } + target.Field(i).SetFloat(float64(ctr.SecondValue)) + continue } @@ -108,5 +116,6 @@ func counterMapKeys(m map[string]*PerfCounter) []string { for k := range m { keys = append(keys, k) } + return keys } diff --git a/pkg/perflib/utf16.go b/pkg/perflib/utf16.go index 243199baa..81e2c324f 100644 --- a/pkg/perflib/utf16.go +++ b/pkg/perflib/utf16.go @@ -3,12 +3,14 @@ package perflib import ( "encoding/binary" "io" - "syscall" + + "golang.org/x/sys/windows" ) // readUTF16StringAtPos Read an unterminated UTF16 string at a given position, specifying its length. func readUTF16StringAtPos(r io.ReadSeeker, absPos int64, length uint32) (string, error) { value := make([]uint16, length/2) + _, err := r.Seek(absPos, io.SeekStart) if err != nil { return "", err @@ -19,7 +21,7 @@ func readUTF16StringAtPos(r io.ReadSeeker, absPos int64, length uint32) (string, return "", err } - return syscall.UTF16ToString(value), nil + return windows.UTF16ToString(value), nil } // readUTF16String Reads a null-terminated UTF16 string at the current offset. @@ -43,5 +45,5 @@ func readUTF16String(r io.Reader) (string, error) { return "", err } - return syscall.UTF16ToString(out), nil + return windows.UTF16ToString(out), nil } diff --git a/pkg/perflib/utils.go b/pkg/perflib/utils.go index bca5011f7..ab101a9c7 100644 --- a/pkg/perflib/utils.go +++ b/pkg/perflib/utils.go @@ -18,5 +18,6 @@ func GetPerflibSnapshot(objNames string) (map[string]*PerfObject, error) { for _, obj := range objects { indexed[obj.Name] = obj } + return indexed, nil } diff --git a/pkg/perflib/utils_test.go b/pkg/perflib/utils_test.go index a60c9d8f1..828438341 100644 --- a/pkg/perflib/utils_test.go +++ b/pkg/perflib/utils_test.go @@ -1,10 +1,10 @@ package perflib import ( + "io" + "log/slog" "reflect" "testing" - - "github.com/go-kit/log" ) type simple struct { @@ -114,11 +114,14 @@ func TestUnmarshalPerflib(t *testing.T) { t.Run(c.name, func(t *testing.T) { t.Parallel() + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) output := make([]simple, 0) - err := UnmarshalObject(c.obj, &output, log.NewNopLogger()) + + err := UnmarshalObject(c.obj, &output, logger) if err != nil && !c.expectError { t.Errorf("Did not expect error, got %q", err) } + if err == nil && c.expectError { t.Errorf("Expected an error, but got ok") } diff --git a/pkg/testutils/testutils.go b/pkg/testutils/testutils.go index 5120b02a6..d4a1fa5c6 100644 --- a/pkg/testutils/testutils.go +++ b/pkg/testutils/testutils.go @@ -3,10 +3,11 @@ package testutils import ( + "io" + "log/slog" "testing" "github.com/alecthomas/kingpin/v2" - "github.com/go-kit/log" "github.com/prometheus-community/windows_exporter/pkg/collector" "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/require" @@ -15,7 +16,7 @@ import ( func FuncBenchmarkCollector[C collector.Collector](b *testing.B, name string, collectFunc collector.BuilderWithFlags[C]) { b.Helper() - logger := log.NewNopLogger() + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) c := collectFunc(kingpin.CommandLine) collectors := collector.New(map[string]collector.Collector{name: c}) @@ -28,6 +29,7 @@ func FuncBenchmarkCollector[C collector.Collector](b *testing.B, name string, co require.NoError(b, err) metrics := make(chan prometheus.Metric) + go func() { for { <-metrics diff --git a/pkg/types/const.go b/pkg/types/const.go index eca2931c3..e108970c6 100644 --- a/pkg/types/const.go +++ b/pkg/types/const.go @@ -3,7 +3,7 @@ package types const ( - DefaultCollectors = "cpu,cs,logical_disk,physical_disk,net,os,service,system" + DefaultCollectors = "cpu,cs,memory,logical_disk,physical_disk,net,os,service,system" DefaultCollectorsPlaceholder = "[defaults]" Namespace = "windows" ) diff --git a/pkg/utils/collector.go b/pkg/utils/collector.go index 766712930..d62ebec63 100644 --- a/pkg/utils/collector.go +++ b/pkg/utils/collector.go @@ -12,14 +12,17 @@ func ExpandEnabledCollectors(enabled string) []string { expanded := strings.ReplaceAll(enabled, types.DefaultCollectorsPlaceholder, types.DefaultCollectors) separated := strings.Split(expanded, ",") 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) } + return result } diff --git a/pkg/utils/collector_test.go b/pkg/utils/collector_test.go index 9865d7b62..98ba0d8fe 100644 --- a/pkg/utils/collector_test.go +++ b/pkg/utils/collector_test.go @@ -38,13 +38,16 @@ func TestExpandEnabled(t *testing.T) { success = false } else { sort.Strings(testCase.expectedOutput) + for idx := range output { if output[idx] != testCase.expectedOutput[idx] { success = false + break } } } + if !success { t.Error("For", testCase.input, "expected", testCase.expectedOutput, "got", output) } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 0bc79e7a0..1823ad535 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -10,11 +10,8 @@ func BoolToFloat(b bool) float64 { if b { return 1.0 } - return 0.0 -} -func IsEmpty(v *string) bool { - return v == nil || *v == "" + return 0.0 } func ToPTR[t any](v t) *t { diff --git a/tools/e2e-output.txt b/tools/e2e-output.txt index 084d0808b..ea2681c78 100644 --- a/tools/e2e-output.txt +++ b/tools/e2e-output.txt @@ -1,4 +1,4 @@ -# HELP test_alpha_total Some random metric. +# HELP test_alpha_total Some random metric. # TYPE test_alpha_total counter test_alpha_total 42 # HELP windows_cpu_clock_interrupts_total Total number of received and serviced clock tick interrupts @@ -27,26 +27,30 @@ test_alpha_total 42 # TYPE windows_cpu_info_thread gauge # HELP windows_cpu_interrupts_total Total number of received and serviced hardware interrupts # TYPE windows_cpu_interrupts_total counter +# HELP windows_cpu_logical_processor Total number of logical processors +# TYPE windows_cpu_logical_processor gauge # HELP windows_cpu_parking_status Parking Status represents whether a processor is parked or not # TYPE windows_cpu_parking_status gauge -# HELP windows_cpu_processor_performance_total Processor Performance is the average performance of the processor while it is executing instructions, as a percentage of the nominal performance of the processor. On some processors, Processor Performance may exceed 100% -# TYPE windows_cpu_processor_performance_total counter -# HELP windows_cpu_time_total Time that processor spent in different modes (dpc, idle, interrupt, privileged, user) -# TYPE windows_cpu_time_total counter # HELP windows_cpu_processor_mperf_total Processor MPerf is the number of TSC ticks incremented while executing instructions # TYPE windows_cpu_processor_mperf_total counter +# HELP windows_cpu_processor_performance_total Processor Performance is the average performance of the processor while it is executing instructions, as a percentage of the nominal performance of the processor. On some processors, Processor Performance may exceed 100% +# TYPE windows_cpu_processor_performance_total counter # HELP windows_cpu_processor_privileged_utility_total Processor Privileged Utility represents is the amount of time the core has spent executing instructions inside the kernel # TYPE windows_cpu_processor_privileged_utility_total counter # HELP windows_cpu_processor_rtc_total Processor RTC represents the number of RTC ticks made since the system booted. It should consistently be 64e6, and can be used to properly derive Processor Utility Rate # TYPE windows_cpu_processor_rtc_total counter # HELP windows_cpu_processor_utility_total Processor Utility represents is the amount of time the core spends executing instructions # TYPE windows_cpu_processor_utility_total counter -# HELP windows_cs_hostname Labelled system hostname information as provided by ComputerSystem.DNSHostName and ComputerSystem.Domain +# HELP windows_cpu_time_total Time that processor spent in different modes (dpc, idle, interrupt, privileged, user) +# TYPE windows_cpu_time_total counter +# HELP windows_cs_hostname Deprecated: Use windows_os_hostname instead # TYPE windows_cs_hostname gauge -# HELP windows_cs_logical_processors ComputerSystem.NumberOfLogicalProcessors +# HELP windows_cs_logical_processors Deprecated: Use windows_cpu_logical_processor instead # TYPE windows_cs_logical_processors gauge -# HELP windows_cs_physical_memory_bytes ComputerSystem.TotalPhysicalMemory +# HELP windows_cs_physical_memory_bytes Deprecated: Use windows_physical_memory_total_bytes instead # TYPE windows_cs_physical_memory_bytes gauge +# HELP windows_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, goversion from which windows_exporter was built, and the goos and goarch for the build. +# TYPE windows_exporter_build_info gauge # HELP windows_exporter_collector_duration_seconds windows_exporter: Duration of a collection. # TYPE windows_exporter_collector_duration_seconds gauge # HELP windows_exporter_collector_success windows_exporter: Whether the collector was successful. @@ -55,9 +59,11 @@ windows_exporter_collector_success{collector="cpu"} 1 windows_exporter_collector_success{collector="cpu_info"} 1 windows_exporter_collector_success{collector="cs"} 1 windows_exporter_collector_success{collector="logical_disk"} 1 -windows_exporter_collector_success{collector="physical_disk"} 1 +windows_exporter_collector_success{collector="memory"} 1 windows_exporter_collector_success{collector="net"} 1 windows_exporter_collector_success{collector="os"} 1 +windows_exporter_collector_success{collector="perfdata"} 1 +windows_exporter_collector_success{collector="physical_disk"} 1 windows_exporter_collector_success{collector="process"} 1 windows_exporter_collector_success{collector="scheduled_task"} 1 windows_exporter_collector_success{collector="service"} 1 @@ -69,9 +75,11 @@ windows_exporter_collector_timeout{collector="cpu"} 0 windows_exporter_collector_timeout{collector="cpu_info"} 0 windows_exporter_collector_timeout{collector="cs"} 0 windows_exporter_collector_timeout{collector="logical_disk"} 0 -windows_exporter_collector_timeout{collector="physical_disk"} 0 +windows_exporter_collector_timeout{collector="memory"} 0 windows_exporter_collector_timeout{collector="net"} 0 windows_exporter_collector_timeout{collector="os"} 0 +windows_exporter_collector_timeout{collector="perfdata"} 0 +windows_exporter_collector_timeout{collector="physical_disk"} 0 windows_exporter_collector_timeout{collector="process"} 0 windows_exporter_collector_timeout{collector="scheduled_task"} 0 windows_exporter_collector_timeout{collector="service"} 0 @@ -79,6 +87,12 @@ windows_exporter_collector_timeout{collector="system"} 0 windows_exporter_collector_timeout{collector="textfile"} 0 # HELP windows_exporter_perflib_snapshot_duration_seconds Duration of perflib snapshot capture # TYPE windows_exporter_perflib_snapshot_duration_seconds gauge +# HELP windows_exporter_scrape_duration_seconds windows_exporter: Total scrape duration. +# TYPE windows_exporter_scrape_duration_seconds gauge +# HELP windows_logical_disk_avg_read_requests_queued Average number of read requests that were queued for the selected disk during the sample interval (LogicalDisk.AvgDiskReadQueueLength) +# TYPE windows_logical_disk_avg_read_requests_queued gauge +# HELP windows_logical_disk_avg_write_requests_queued Average number of write requests that were queued for the selected disk during the sample interval (LogicalDisk.AvgDiskWriteQueueLength) +# TYPE windows_logical_disk_avg_write_requests_queued gauge # HELP windows_logical_disk_free_bytes Free space in bytes, updates every 10-15 min (LogicalDisk.PercentFreeSpace) # TYPE windows_logical_disk_free_bytes gauge # HELP windows_logical_disk_idle_seconds_total Seconds that the disk was idle (LogicalDisk.PercentIdleTime) @@ -97,10 +111,6 @@ windows_exporter_collector_timeout{collector="textfile"} 0 # TYPE windows_logical_disk_reads_total counter # HELP windows_logical_disk_requests_queued The number of requests queued to the disk (LogicalDisk.CurrentDiskQueueLength) # TYPE windows_logical_disk_requests_queued gauge -# HELP windows_logical_disk_avg_read_requests_queued Average number of read requests that were queued for the selected disk during the sample interval (LogicalDisk.AvgDiskReadQueueLength) -# TYPE windows_logical_disk_avg_read_requests_queued gauge -# HELP windows_logical_disk_avg_write_requests_queued Average number of write requests that were queued for the selected disk during the sample interval (LogicalDisk.AvgDiskWriteQueueLength) -# TYPE windows_logical_disk_avg_write_requests_queued gauge # HELP windows_logical_disk_size_bytes Total space in bytes, updates every 10-15 min (LogicalDisk.PercentFreeSpace_Base) # TYPE windows_logical_disk_size_bytes gauge # HELP windows_logical_disk_split_ios_total The number of I/Os to the disk were split into multiple I/Os (LogicalDisk.SplitIOPerSec) @@ -113,40 +123,86 @@ windows_exporter_collector_timeout{collector="textfile"} 0 # TYPE windows_logical_disk_write_seconds_total counter # HELP windows_logical_disk_writes_total The number of write operations on the disk (LogicalDisk.DiskWritesPerSec) # TYPE windows_logical_disk_writes_total counter -# HELP windows_physical_disk_idle_seconds_total Seconds that the disk was idle (PhysicalDisk.PercentIdleTime) -# TYPE windows_physical_disk_idle_seconds_total counter -# HELP windows_physical_disk_read_bytes_total The number of bytes transferred from the disk during read operations (PhysicalDisk.DiskReadBytesPerSec) -# TYPE windows_physical_disk_read_bytes_total counter -# HELP windows_physical_disk_read_latency_seconds_total Shows the average time, in seconds, of a read operation from the disk (PhysicalDisk.AvgDiskSecPerRead) -# TYPE windows_physical_disk_read_latency_seconds_total counter -# HELP windows_physical_disk_read_seconds_total Seconds that the disk was busy servicing read requests (PhysicalDisk.PercentDiskReadTime) -# TYPE windows_physical_disk_read_seconds_total counter -# HELP windows_physical_disk_read_write_latency_seconds_total Shows the time, in seconds, of the average disk transfer (PhysicalDisk.AvgDiskSecPerTransfer) -# TYPE windows_physical_disk_read_write_latency_seconds_total counter -# HELP windows_physical_disk_reads_total The number of read operations on the disk (PhysicalDisk.DiskReadsPerSec) -# TYPE windows_physical_disk_reads_total counter -# HELP windows_physical_disk_requests_queued The number of requests queued to the disk (PhysicalDisk.CurrentDiskQueueLength) -# TYPE windows_physical_disk_requests_queued gauge -# HELP windows_physical_disk_split_ios_total The number of I/Os to the disk were split into multiple I/Os (PhysicalDisk.SplitIOPerSec) -# TYPE windows_physical_disk_split_ios_total counter -# HELP windows_physical_disk_write_bytes_total The number of bytes transferred to the disk during write operations (PhysicalDisk.DiskWriteBytesPerSec) -# TYPE windows_physical_disk_write_bytes_total counter -# HELP windows_physical_disk_write_latency_seconds_total Shows the average time, in seconds, of a write operation to the disk (PhysicalDisk.AvgDiskSecPerWrite) -# TYPE windows_physical_disk_write_latency_seconds_total counter -# HELP windows_physical_disk_write_seconds_total Seconds that the disk was busy servicing write requests (PhysicalDisk.PercentDiskWriteTime) -# TYPE windows_physical_disk_write_seconds_total counter -# HELP windows_physical_disk_writes_total The number of write operations on the disk (PhysicalDisk.DiskWritesPerSec) -# TYPE windows_physical_disk_writes_total counter +# HELP windows_memory_available_bytes The amount of physical memory immediately available for allocation to a process or for system use. It is equal to the sum of memory assigned to the standby (cached), free and zero page lists (AvailableBytes) +# TYPE windows_memory_available_bytes gauge +# HELP windows_memory_cache_bytes (CacheBytes) +# TYPE windows_memory_cache_bytes gauge +# HELP windows_memory_cache_bytes_peak (CacheBytesPeak) +# TYPE windows_memory_cache_bytes_peak gauge +# HELP windows_memory_cache_faults_total Number of faults which occur when a page sought in the file system cache is not found there and must be retrieved from elsewhere in memory (soft fault) or from disk (hard fault) (Cache Faults/sec) +# TYPE windows_memory_cache_faults_total counter +# HELP windows_memory_commit_limit (CommitLimit) +# TYPE windows_memory_commit_limit gauge +# HELP windows_memory_committed_bytes (CommittedBytes) +# TYPE windows_memory_committed_bytes gauge +# HELP windows_memory_demand_zero_faults_total The number of zeroed pages required to satisfy faults. Zeroed pages, pages emptied of previously stored data and filled with zeros, are a security feature of Windows that prevent processes from seeing data stored by earlier processes that used the memory space (Demand Zero Faults/sec) +# TYPE windows_memory_demand_zero_faults_total counter +# HELP windows_memory_free_and_zero_page_list_bytes The amount of physical memory, in bytes, that is assigned to the free and zero page lists. This memory does not contain cached data. It is immediately available for allocation to a process or for system use (FreeAndZeroPageListBytes) +# TYPE windows_memory_free_and_zero_page_list_bytes gauge +# HELP windows_memory_free_system_page_table_entries (FreeSystemPageTableEntries) +# TYPE windows_memory_free_system_page_table_entries gauge +# HELP windows_memory_modified_page_list_bytes The amount of physical memory, in bytes, that is assigned to the modified page list. This memory contains cached data and code that is not actively in use by processes, the system and the system cache (ModifiedPageListBytes) +# TYPE windows_memory_modified_page_list_bytes gauge +# HELP windows_memory_page_faults_total Overall rate at which faulted pages are handled by the processor (Page Faults/sec) +# TYPE windows_memory_page_faults_total counter +# HELP windows_memory_physical_free_bytes The amount of physical memory currently available, in bytes. This is the amount of physical memory that can be immediately reused without having to write its contents to disk first. It is the sum of the size of the standby, free, and zero lists. +# TYPE windows_memory_physical_free_bytes gauge +# HELP windows_memory_physical_total_bytes The amount of actual physical memory, in bytes. +# TYPE windows_memory_physical_total_bytes gauge +# HELP windows_memory_pool_nonpaged_allocs_total The number of calls to allocate space in the nonpaged pool. The nonpaged pool is an area of system memory area for objects that cannot be written to disk, and must remain in physical memory as long as they are allocated (PoolNonpagedAllocs) +# TYPE windows_memory_pool_nonpaged_allocs_total gauge +# HELP windows_memory_pool_nonpaged_bytes Number of bytes in the non-paged pool, an area of the system virtual memory that is used for objects that cannot be written to disk, but must remain in physical memory as long as they are allocated (PoolNonpagedBytes) +# TYPE windows_memory_pool_nonpaged_bytes gauge +# HELP windows_memory_pool_paged_allocs_total Number of calls to allocate space in the paged pool, regardless of the amount of space allocated in each call (PoolPagedAllocs) +# TYPE windows_memory_pool_paged_allocs_total counter +# HELP windows_memory_pool_paged_bytes (PoolPagedBytes) +# TYPE windows_memory_pool_paged_bytes gauge +# HELP windows_memory_pool_paged_resident_bytes The size, in bytes, of the portion of the paged pool that is currently resident and active in physical memory. The paged pool is an area of the system virtual memory that is used for objects that can be written to disk when they are not being used (PoolPagedResidentBytes) +# TYPE windows_memory_pool_paged_resident_bytes gauge +# HELP windows_memory_process_memory_limit_bytes The size of the user-mode portion of the virtual address space of the calling process, in bytes. This value depends on the type of process, the type of processor, and the configuration of the operating system. +# TYPE windows_memory_process_memory_limit_bytes gauge +# HELP windows_memory_standby_cache_core_bytes The amount of physical memory, in bytes, that is assigned to the core standby cache page lists. This memory contains cached data and code that is not actively in use by processes, the system and the system cache (StandbyCacheCoreBytes) +# TYPE windows_memory_standby_cache_core_bytes gauge +# HELP windows_memory_standby_cache_normal_priority_bytes The amount of physical memory, in bytes, that is assigned to the normal priority standby cache page lists. This memory contains cached data and code that is not actively in use by processes, the system and the system cache (StandbyCacheNormalPriorityBytes) +# TYPE windows_memory_standby_cache_normal_priority_bytes gauge +# HELP windows_memory_standby_cache_reserve_bytes The amount of physical memory, in bytes, that is assigned to the reserve standby cache page lists. This memory contains cached data and code that is not actively in use by processes, the system and the system cache (StandbyCacheReserveBytes) +# TYPE windows_memory_standby_cache_reserve_bytes gauge +# HELP windows_memory_swap_page_operations_total Total number of swap page read and writes (PagesPersec) +# TYPE windows_memory_swap_page_operations_total counter +# HELP windows_memory_swap_page_reads_total Number of disk page reads (a single read operation reading several pages is still only counted once) (PageReadsPersec) +# TYPE windows_memory_swap_page_reads_total counter +# HELP windows_memory_swap_page_writes_total Number of disk page writes (a single write operation writing several pages is still only counted once) (PageWritesPersec) +# TYPE windows_memory_swap_page_writes_total counter +# HELP windows_memory_swap_pages_read_total Number of pages read across all page reads (ie counting all pages read even if they are read in a single operation) (PagesInputPersec) +# TYPE windows_memory_swap_pages_read_total counter +# HELP windows_memory_swap_pages_written_total Number of pages written across all page writes (ie counting all pages written even if they are written in a single operation) (PagesOutputPersec) +# TYPE windows_memory_swap_pages_written_total counter +# HELP windows_memory_system_cache_resident_bytes The size, in bytes, of the portion of the system file cache which is currently resident and active in physical memory (SystemCacheResidentBytes) +# TYPE windows_memory_system_cache_resident_bytes gauge +# HELP windows_memory_system_code_resident_bytes The size, in bytes, of the pageable operating system code that is currently resident and active in physical memory (SystemCodeResidentBytes) +# TYPE windows_memory_system_code_resident_bytes gauge +# HELP windows_memory_system_code_total_bytes The size, in bytes, of the pageable operating system code currently mapped into the system virtual address space (SystemCodeTotalBytes) +# TYPE windows_memory_system_code_total_bytes gauge +# HELP windows_memory_system_driver_resident_bytes The size, in bytes, of the pageable physical memory being used by device drivers. It is the working set (physical memory area) of the drivers (SystemDriverResidentBytes) +# TYPE windows_memory_system_driver_resident_bytes gauge +# HELP windows_memory_system_driver_total_bytes The size, in bytes, of the pageable virtual memory currently being used by device drivers. Pageable memory can be written to disk when it is not being used (SystemDriverTotalBytes) +# TYPE windows_memory_system_driver_total_bytes gauge +# HELP windows_memory_transition_faults_total Number of faults rate at which page faults are resolved by recovering pages that were being used by another process sharing the page, or were on the modified page list or the standby list, or were being written to disk at the time of the page fault (TransitionFaultsPersec) +# TYPE windows_memory_transition_faults_total counter +# HELP windows_memory_transition_pages_repurposed_total Transition Pages RePurposed is the rate at which the number of transition cache pages were reused for a different purpose (TransitionPagesRePurposedPersec) +# TYPE windows_memory_transition_pages_repurposed_total counter +# HELP windows_memory_write_copies_total The number of page faults caused by attempting to write that were satisfied by copying the page from elsewhere in physical memory (WriteCopiesPersec) +# TYPE windows_memory_write_copies_total counter # HELP windows_net_bytes_received_total (Network.BytesReceivedPerSec) # TYPE windows_net_bytes_received_total counter # HELP windows_net_bytes_sent_total (Network.BytesSentPerSec) # TYPE windows_net_bytes_sent_total counter # HELP windows_net_bytes_total (Network.BytesTotalPerSec) # TYPE windows_net_bytes_total counter -# HELP windows_net_output_queue_length_packets (Network.OutputQueueLength) -# TYPE windows_net_output_queue_length_packets gauge # HELP windows_net_current_bandwidth_bytes (Network.CurrentBandwidth) # TYPE windows_net_current_bandwidth_bytes gauge +# HELP windows_net_output_queue_length_packets (Network.OutputQueueLength) +# TYPE windows_net_output_queue_length_packets gauge # HELP windows_net_packets_outbound_discarded_total (Network.PacketsOutboundDiscarded) # TYPE windows_net_packets_outbound_discarded_total counter # HELP windows_net_packets_outbound_errors_total (Network.PacketsOutboundErrors) @@ -163,32 +219,64 @@ windows_exporter_collector_timeout{collector="textfile"} 0 # TYPE windows_net_packets_sent_total counter # HELP windows_net_packets_total (Network.PacketsPerSec) # TYPE windows_net_packets_total counter -# HELP windows_os_info OperatingSystem.Caption, OperatingSystem.Version +# HELP windows_os_hostname Labelled system hostname information as provided by ComputerSystem.DNSHostName and ComputerSystem.Domain +# TYPE windows_os_hostname gauge +# HELP windows_os_info Contains full product name & version in labels. Note that the "major_version" for Windows 11 is \\"10\\"; a build number greater than 22000 represents Windows 11. # TYPE windows_os_info gauge # HELP windows_os_paging_free_bytes OperatingSystem.FreeSpaceInPagingFiles # TYPE windows_os_paging_free_bytes gauge # HELP windows_os_paging_limit_bytes OperatingSystem.SizeStoredInPagingFiles # TYPE windows_os_paging_limit_bytes gauge -# HELP windows_os_physical_memory_free_bytes OperatingSystem.FreePhysicalMemory +# HELP windows_os_physical_memory_free_bytes Deprecated: Use `windows_memory_physical_free_bytes` instead. # TYPE windows_os_physical_memory_free_bytes gauge -# HELP windows_os_process_memory_limit_bytes OperatingSystem.MaxProcessMemorySize +# HELP windows_os_process_memory_limit_bytes Deprecated: Use `windows_memory_process_memory_limit_bytes` instead. # TYPE windows_os_process_memory_limit_bytes gauge -# HELP windows_os_processes OperatingSystem.NumberOfProcesses +# HELP windows_os_processes Deprecated: Use `windows_system_processes` instead. # TYPE windows_os_processes gauge -# HELP windows_os_processes_limit OperatingSystem.MaxNumberOfProcesses +# HELP windows_os_processes_limit Deprecated: Use `windows_system_process_limit` instead. # TYPE windows_os_processes_limit gauge -# HELP windows_os_time OperatingSystem.LocalDateTime +# HELP windows_os_time Deprecated: Use windows_time_current_timestamp_seconds instead. # TYPE windows_os_time gauge -# HELP windows_os_timezone OperatingSystem.LocalDateTime +# HELP windows_os_timezone Deprecated: Use windows_time_timezone instead. # TYPE windows_os_timezone gauge -# HELP windows_os_users OperatingSystem.NumberOfUsers +# HELP windows_os_users Deprecated: Use `count(windows_logon_logon_type)` instead. # TYPE windows_os_users gauge -# HELP windows_os_virtual_memory_bytes OperatingSystem.TotalVirtualMemorySize +# HELP windows_os_virtual_memory_bytes Deprecated: Use `windows_memory_commit_limit` instead. # TYPE windows_os_virtual_memory_bytes gauge -# HELP windows_os_virtual_memory_free_bytes OperatingSystem.FreeVirtualMemory +# HELP windows_os_virtual_memory_free_bytes Deprecated: Use `windows_memory_commit_limit - windows_memory_committed_bytes` instead. # TYPE windows_os_virtual_memory_free_bytes gauge -# HELP windows_os_visible_memory_bytes OperatingSystem.TotalVisibleMemorySize +# HELP windows_os_visible_memory_bytes Deprecated: Use `windows_memory_physical_total_bytes` instead. # TYPE windows_os_visible_memory_bytes gauge +# HELP windows_perfdata_memory_cache_faults_sec Performance data for \\Memory\\Cache Faults/sec +# TYPE windows_perfdata_memory_cache_faults_sec counter +# HELP windows_perfdata_processor_information__privileged_time Performance data for \\Processor Information\\% Privileged Time +# TYPE windows_perfdata_processor_information__privileged_time counter +# HELP windows_perfdata_processor_information__processor_time Performance data for \\Processor Information\\% Processor Time +# TYPE windows_perfdata_processor_information__processor_time counter +# HELP windows_physical_disk_idle_seconds_total Seconds that the disk was idle (PhysicalDisk.PercentIdleTime) +# TYPE windows_physical_disk_idle_seconds_total counter +# HELP windows_physical_disk_read_bytes_total The number of bytes transferred from the disk during read operations (PhysicalDisk.DiskReadBytesPerSec) +# TYPE windows_physical_disk_read_bytes_total counter +# HELP windows_physical_disk_read_latency_seconds_total Shows the average time, in seconds, of a read operation from the disk (PhysicalDisk.AvgDiskSecPerRead) +# TYPE windows_physical_disk_read_latency_seconds_total counter +# HELP windows_physical_disk_read_seconds_total Seconds that the disk was busy servicing read requests (PhysicalDisk.PercentDiskReadTime) +# TYPE windows_physical_disk_read_seconds_total counter +# HELP windows_physical_disk_read_write_latency_seconds_total Shows the time, in seconds, of the average disk transfer (PhysicalDisk.AvgDiskSecPerTransfer) +# TYPE windows_physical_disk_read_write_latency_seconds_total counter +# HELP windows_physical_disk_reads_total The number of read operations on the disk (PhysicalDisk.DiskReadsPerSec) +# TYPE windows_physical_disk_reads_total counter +# HELP windows_physical_disk_requests_queued The number of requests queued to the disk (PhysicalDisk.CurrentDiskQueueLength) +# TYPE windows_physical_disk_requests_queued gauge +# HELP windows_physical_disk_split_ios_total The number of I/Os to the disk were split into multiple I/Os (PhysicalDisk.SplitIOPerSec) +# TYPE windows_physical_disk_split_ios_total counter +# HELP windows_physical_disk_write_bytes_total The number of bytes transferred to the disk during write operations (PhysicalDisk.DiskWriteBytesPerSec) +# TYPE windows_physical_disk_write_bytes_total counter +# HELP windows_physical_disk_write_latency_seconds_total Shows the average time, in seconds, of a write operation to the disk (PhysicalDisk.AvgDiskSecPerWrite) +# TYPE windows_physical_disk_write_latency_seconds_total counter +# HELP windows_physical_disk_write_seconds_total Seconds that the disk was busy servicing write requests (PhysicalDisk.PercentDiskWriteTime) +# TYPE windows_physical_disk_write_seconds_total counter +# HELP windows_physical_disk_writes_total The number of write operations on the disk (PhysicalDisk.DiskWritesPerSec) +# TYPE windows_physical_disk_writes_total counter # HELP windows_scheduled_task_state The current state of a scheduled task # TYPE windows_scheduled_task_state gauge windows_scheduled_task_state{state="disabled",task="/Microsoft/Windows/Maintenance/WinSAT"} 1 @@ -198,16 +286,33 @@ windows_scheduled_task_state{state="running",task="/Microsoft/Windows/Maintenanc windows_scheduled_task_state{state="unknown",task="/Microsoft/Windows/Maintenance/WinSAT"} 0 # HELP windows_service_info A metric with a constant '1' value labeled with service information # TYPE windows_service_info gauge +windows_service_info{display_name="Themes",name="Themes",path_name="C:\\WINDOWS\\System32\\svchost.exe -k netsvcs -p",run_as="LocalSystem"} 1 +# HELP windows_service_process Process of started service. The value is the creation time of the process as a unix timestamp. +# TYPE windows_service_process gauge # HELP windows_service_start_mode The start mode of the service (StartMode) # TYPE windows_service_start_mode gauge +windows_service_start_mode{name="Themes",start_mode="auto"} 1 +windows_service_start_mode{name="Themes",start_mode="boot"} 0 +windows_service_start_mode{name="Themes",start_mode="disabled"} 0 +windows_service_start_mode{name="Themes",start_mode="manual"} 0 +windows_service_start_mode{name="Themes",start_mode="system"} 0 # HELP windows_service_state The state of the service (State) # TYPE windows_service_state gauge -# HELP windows_service_status The status of the service (Status) -# TYPE windows_service_status gauge +windows_service_state{name="Themes",status="continue pending"} 0 +windows_service_state{name="Themes",status="pause pending"} 0 +windows_service_state{name="Themes",status="paused"} 0 +windows_service_state{name="Themes",status="running"} 1 +windows_service_state{name="Themes",status="start pending"} 0 +windows_service_state{name="Themes",status="stop pending"} 0 +windows_service_state{name="Themes",status="stopped"} 0 # HELP windows_system_context_switches_total Total number of context switches (WMI source: PerfOS_System.ContextSwitchesPersec) # TYPE windows_system_context_switches_total counter # HELP windows_system_exception_dispatches_total Total number of exceptions dispatched (WMI source: PerfOS_System.ExceptionDispatchesPersec) # TYPE windows_system_exception_dispatches_total counter +# HELP windows_system_processes Current number of processes (WMI source: PerfOS_System.Processes) +# TYPE windows_system_processes gauge +# HELP windows_system_processes_limit Maximum number of processes. +# TYPE windows_system_processes_limit gauge # HELP windows_system_processor_queue_length Length of processor queue (WMI source: PerfOS_System.ProcessorQueueLength) # TYPE windows_system_processor_queue_length gauge # HELP windows_system_system_calls_total Total number of system calls (WMI source: PerfOS_System.SystemCallsPersec) diff --git a/tools/end-to-end-test.ps1 b/tools/end-to-end-test.ps1 index d1597ff55..5076e42e2 100644 --- a/tools/end-to-end-test.ps1 +++ b/tools/end-to-end-test.ps1 @@ -1,15 +1,15 @@ $ErrorActionPreference = 'Stop' Set-StrictMode -Version 3 -if (-not (Test-Path -Path '.\windows_exporter.exe')) { - Write-Output ".\windows_exporter.exe not found. Consider running \`go build\` first" -} - # cd to location of script $script_path = $MyInvocation.MyCommand.Path $working_dir = Split-Path $script_path Push-Location $working_dir +if (-not (Test-Path -Path '..\windows_exporter.exe')) { + Write-Output "..\windows_exporter.exe not found. Consider running \`go build\` first" +} + $temp_dir = Join-Path $env:TEMP $(New-Guid) | ForEach-Object { mkdir $_ } # Create temporary directory for textfile collector @@ -18,20 +18,22 @@ mkdir $textfile_dir | Out-Null Copy-Item 'e2e-textfile.prom' -Destination "$($textfile_dir)/e2e-textfile.prom" # Omit dynamic collector information that will change after each run -$skip_re = "^(go_|windows_exporter_build_info|windows_exporter_collector_duration_seconds|windows_exporter_perflib_snapshot_duration_seconds|process_|windows_textfile_mtime_seconds|windows_cpu|windows_cs|windows_logical_disk|windows_physical_disk|windows_net|windows_os|windows_process|windows_service|windows_system|windows_textfile_mtime_seconds)" +$skip_re = "^(go_|windows_exporter_build_info|windows_exporter_collector_duration_seconds|windows_exporter_perflib_snapshot_duration_seconds|windows_exporter_scrape_duration_seconds|process_|windows_textfile_mtime_seconds|windows_cpu|windows_cs|windows_logical_disk|windows_physical_disk|windows_memory|windows_net|windows_os|windows_process|windows_service_process|windows_system|windows_perfdata|windows_textfile_mtime_seconds)" # Start process in background, awaiting HTTP requests. # Use default collectors, port and address: http://localhost:9182/metrics $exporter_proc = Start-Process ` -PassThru ` -FilePath ..\windows_exporter.exe ` - -ArgumentList "--log.level=debug --web.disable-exporter-metrics --collectors.enabled=[defaults],cpu_info,textfile,process,scheduled_task --collector.process.include=explorer.exe --collector.scheduled_task.include=.*WinSAT --collector.textfile.directories=$($textfile_dir)" ` + -ArgumentList "--log.level=debug","--web.disable-exporter-metrics","--collectors.enabled=[defaults],cpu_info,textfile,process,perfdata,scheduled_task","--collector.process.include=explorer.exe","--collector.scheduled_task.include=.*WinSAT","--collector.service.include=Themes","--collector.textfile.directories=$($textfile_dir)",@" +--collector.perfdata.objects="[{\"object\":\"Processor Information\",\"instance_label\":\"core\",\"instances\":[\"*\"],\"counters\":{\"% Processor Time\":{},\"% Privileged Time\":{}}},{\"object\":\"Memory\",\"counters\":{\"Cache Faults/sec\":{\"type\":\"counter\"}}}]" +"@ ` -WindowStyle Hidden ` -RedirectStandardOutput "$($temp_dir)/windows_exporter.log" ` -RedirectStandardError "$($temp_dir)/windows_exporter_error.log" # Exporter can take some time to start -for ($i=1; $i -le 5; $i++) { +for ($i=1; $i -le 1; $i++) { Start-Sleep 10 $netstat_output = netstat -anp tcp | Select-String 'listening' @@ -53,17 +55,29 @@ try { } # Response output must be split and saved as UTF-8. $response.content -split "[`r`n]"| Select-String -NotMatch $skip_re | Set-Content -Encoding utf8 "$($temp_dir)/e2e-output.txt" -Stop-Process -Id $exporter_proc.Id +try { + Stop-Process -Id $exporter_proc.Id +} catch { + Write-Host "STDOUT" + Get-Content "$($temp_dir)/windows_exporter.log" + Write-Host "STDERR" + Get-Content "$($temp_dir)/windows_exporter_error.log" + + throw $_ +} + $output_diff = Compare-Object (Get-Content 'e2e-output.txt') (Get-Content "$($temp_dir)/e2e-output.txt") # Fail if differences in output are detected if (-not ($null -eq $output_diff)) { - $output_diff | Format-Table + $output_diff | Format-Table -AutoSize | Out-String -Width 10000 Write-Host "STDOUT" Get-Content "$($temp_dir)/windows_exporter.log" Write-Host "STDERR" Get-Content "$($temp_dir)/windows_exporter_error.log" + (Get-Content "$($temp_dir)/e2e-output.txt") | Set-Content -Encoding utf8 "e2e-output.txt" + exit 1 } diff --git a/tools/promtool.ps1 b/tools/promtool.ps1 index 89d7ba2ff..948153d94 100644 --- a/tools/promtool.ps1 +++ b/tools/promtool.ps1 @@ -110,7 +110,8 @@ for ($i=1; $i -le 5; $i++) { } # Omit metrics from client_golang library; we're not responsible for these -$skip_re = "^[#]?\s*(HELP|TYPE)?\s*go_" +# windows_memory_pool_nonpaged_allocs_total is wrong for years. It's not a gauge, but a counter. +$skip_re = "^([#]?\s*(HELP|TYPE)?\s*go_|windows_memory_pool_nonpaged_allocs_total)" # Need to remove carriage returns, as promtool expects LF line endings $output = ((Invoke-WebRequest -UseBasicParsing -URI http://127.0.0.1:9183/metrics).Content) -Split "`r?`n" | Select-String -NotMatch $skip_re | Join-String -Separator "`n"