mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-08 14:06:38 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a20e1854d1 | ||
|
|
fe21cb44f6 | ||
|
|
71ec0bd6a3 | ||
|
|
8bff623393 | ||
|
|
3eabd0a00c | ||
|
|
73186cde48 | ||
|
|
25e04fc947 | ||
|
|
6b7201856c | ||
|
|
608b83cfd8 | ||
|
|
40a42ca457 | ||
|
|
423c8a787e | ||
|
|
6cefbed7f7 | ||
|
|
5836a7dbf2 | ||
|
|
d31ce0507c | ||
|
|
faa98d2708 | ||
|
|
f0f3d0d96e | ||
|
|
f73a74b678 | ||
|
|
b6f89ad92f |
38
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
38
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
<!--
|
||||
Please give your PR a title in the form "area: short description". For example "cpu: reduce usage by 95%" or "docs: fix typo in installation.md".
|
||||
|
||||
If your PR is to fix an issue, put "Fixes #issue-number" in the description.
|
||||
|
||||
Don't forget!
|
||||
|
||||
- Please sign CNCF's Developer Certificate of Origin and sign-off your commits by adding the -s / --signoff flag to `git commit`. See https://github.com/apps/dco for more information.
|
||||
|
||||
- If the PR adds or changes a behaviour or fixes a bug of an exported API it would need a unit/e2e test.
|
||||
|
||||
- Performance improvements would need a benchmark test to prove it.
|
||||
|
||||
- All comments should start with a capital letter and end with a full stop.
|
||||
-->
|
||||
|
||||
|
||||
#### What this PR does / why we need it
|
||||
|
||||
#### Which issue this PR fixes
|
||||
|
||||
*(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #
|
||||
|
||||
- fixes #
|
||||
|
||||
#### Special notes for your reviewer
|
||||
|
||||
#### Particularly user-facing changes
|
||||
|
||||
#### Checklist
|
||||
|
||||
Complete these before marking the PR as `ready to review`:
|
||||
|
||||
<!-- [Place an '[x]' (no spaces) in all applicable fields.] -->
|
||||
|
||||
- [ ] [DCO](https://github.com/prometheus-community/helm-charts/blob/main/CONTRIBUTING.md#sign-off-your-work) signed
|
||||
- [ ] The PR title has a summary of the changes and the area they affect
|
||||
- [ ] The PR body has a summary to reflect any significant (and particularly user-facing) changes introduced by this PR
|
||||
2
.github/workflows/stale-close.yml
vendored
2
.github/workflows/stale-close.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
||||
if: github.repository_owner == 'prometheus' || github.repository_owner == 'prometheus-community' # Don't run this workflow on forks.
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@28ca1036281a5e5922ead5184a1bbf96e5fc984e # v9.0.0
|
||||
- uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9.1.0
|
||||
with:
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
# opt out of defaults to avoid marking issues as stale and closing them
|
||||
|
||||
2
.github/workflows/stale.yml
vendored
2
.github/workflows/stale.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
||||
if: github.repository_owner == 'prometheus' || github.repository_owner == 'prometheus-community' # Don't run this workflow on forks.
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@28ca1036281a5e5922ead5184a1bbf96e5fc984e # v9.0.0
|
||||
- uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9.1.0
|
||||
with:
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
# opt out of defaults to avoid marking issues as stale and closing them
|
||||
|
||||
82
CONTRIBUTING.md
Normal file
82
CONTRIBUTING.md
Normal file
@@ -0,0 +1,82 @@
|
||||
# Contributing
|
||||
|
||||
windows_exporter uses GitHub to manage reviews of pull requests.
|
||||
|
||||
* If you are a new contributor see: [Steps to Contribute](#steps-to-contribute)
|
||||
|
||||
* If you have a trivial fix or improvement, go ahead and create a pull request,
|
||||
addressing (with `@...`) a suitable maintainer of this repository (see
|
||||
[MAINTAINERS.md](MAINTAINERS.md)) in the description of the pull request.
|
||||
|
||||
* If you plan to do something more involved, first discuss your ideas
|
||||
as [github issue](https://github.com/prometheus-community/windows_exporter/issues).
|
||||
This will avoid unnecessary work and surely give you and us a good deal
|
||||
of inspiration. New collectors are unlikely to be accepted, since the
|
||||
`performancecounter` collector is the preferred way to collect metrics.
|
||||
|
||||
* Relevant coding style guidelines are the [Go Code Review
|
||||
Comments](https://code.google.com/p/go-wiki/wiki/CodeReviewComments)
|
||||
and the _Formatting and style_ section of Peter Bourgon's [Go: Best
|
||||
Practices for Production
|
||||
Environments](https://peter.bourgon.org/go-in-production/#formatting-and-style).
|
||||
gofmt and [golangci-lint](https://github.com/golangci/golangci-lint) are your friends.
|
||||
|
||||
* Be sure to sign off on the [DCO](https://github.com/probot/dco#how-it-works).
|
||||
|
||||
## Steps to Contribute
|
||||
|
||||
Should you wish to work on an issue, please claim it first by commenting on the GitHub issue that you want to work on it. This is to prevent duplicated efforts from contributors on the same issue.
|
||||
For quickly compiling and testing your changes do:
|
||||
|
||||
```bash
|
||||
# For building.
|
||||
go build -o windows_exporter.exe ./cmd/windows_exporter/
|
||||
./windows_exporter.exe
|
||||
|
||||
# For testing.
|
||||
make test # Make sure all the tests pass before you commit and push :)
|
||||
```
|
||||
|
||||
To run a collection of Go linters through [`golangci-lint`](https://github.com/golangci/golangci-lint), do:
|
||||
```bash
|
||||
make lint
|
||||
```
|
||||
|
||||
If it reports an issue and you think that the warning needs to be disregarded or is a false-positive, you can add a special comment `//nolint:linter1[,linter2,...]` before the offending line. Use this sparingly though, fixing the code to comply with the linter's recommendation is in general the preferred course of action. See [this section of the golangci-lint documentation](https://golangci-lint.run/usage/false-positives/#nolint-directive) for more information.
|
||||
|
||||
All our issues are regularly tagged so that you can also filter down the issues involving the components you want to work on. For our labeling policy refer [the wiki page](https://github.com/prometheus/prometheus/wiki/Label-Names-and-Descriptions).
|
||||
|
||||
## Pull Request Checklist
|
||||
|
||||
* Branch from the main branch and, if needed, rebase to the current main branch before submitting your pull request. If it doesn't merge cleanly with main you may be asked to rebase your changes.
|
||||
|
||||
* Commits should be as small as possible, while ensuring that each commit is correct independently (i.e., each commit should compile and pass tests).
|
||||
|
||||
* The PR title should be of the format: `subsystem: what this PR does` (for example, `cpu: Add support for thing` or `docs: fix typo`).
|
||||
|
||||
* If your patch is not getting reviewed or you need a specific person to review it, you can @-reply a reviewer asking for a review in the pull request or a comment.
|
||||
|
||||
* Add tests relevant to the fixed bug or new feature.
|
||||
|
||||
## Dependency management
|
||||
|
||||
The Prometheus project uses [Go modules](https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more) to manage dependencies on external packages.
|
||||
|
||||
To add or update a new dependency, use the `go get` command:
|
||||
|
||||
```bash
|
||||
# Pick the latest tagged release.
|
||||
go get example.com/some/module/pkg@latest
|
||||
|
||||
# Pick a specific version.
|
||||
go get example.com/some/module/pkg@vX.Y.Z
|
||||
```
|
||||
|
||||
Tidy up the `go.mod` and `go.sum` files:
|
||||
|
||||
```bash
|
||||
# The GO111MODULE variable can be omitted when the code isn't located in GOPATH.
|
||||
GO111MODULE=on go mod tidy
|
||||
```
|
||||
|
||||
You have to commit the changes to `go.mod` and `go.sum` before submitting the pull request.
|
||||
1
LICENSE
1
LICENSE
@@ -1,6 +1,7 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Martin Lindhe
|
||||
Copyright (c) 2021 The Prometheus Authors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -88,7 +88,7 @@ windows_exporter accepts flags to configure certain behaviours. The ones configu
|
||||
| `--web.listen-address` | host:port for exporter. | `:9182` |
|
||||
| `--telemetry.path` | URL path for surfacing collected metrics. | `/metrics` |
|
||||
| `--telemetry.max-requests` | Maximum number of concurrent requests. 0 to disable. | `5` |
|
||||
| `--collectors.enabled` | Comma-separated list of collectors to use. Use `[defaults]` as a placeholder which gets expanded containing all the collectors enabled by default." | `[defaults]` |
|
||||
| `--collectors.enabled` | Comma-separated list of collectors to use. Use `[defaults]` as a placeholder which gets expanded containing all the collectors enabled by default. | `[defaults]` |
|
||||
| `--collectors.print` | If true, print available collectors and exit. | |
|
||||
| `--scrape.timeout-margin` | Seconds to subtract from the timeout allowed by the client. Tune to allow for overhead or high loads. | `0.5` |
|
||||
| `--web.config.file` | A [web config][web_config] for setting up TLS and Auth | None |
|
||||
|
||||
147
cmd/windows_exporter/0_service.go
Normal file
147
cmd/windows_exporter/0_service.go
Normal file
@@ -0,0 +1,147 @@
|
||||
// Copyright 2025 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/svc"
|
||||
"golang.org/x/sys/windows/svc/eventlog"
|
||||
)
|
||||
|
||||
const serviceName = "windows_exporter"
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
// exitCodeCh is a channel to send an exit code from the main function to the service manager.
|
||||
// Additionally, if there is an error in the IsService var declaration,
|
||||
// the exit code is sent to the service manager as well.
|
||||
exitCodeCh = make(chan int, 1)
|
||||
|
||||
// stopCh is a channel to send a signal to the service manager that the service is stopping.
|
||||
stopCh = make(chan struct{})
|
||||
)
|
||||
|
||||
// IsService variable declaration allows initiating time-sensitive components like registering the Windows service
|
||||
// as early as possible in the startup process.
|
||||
// init functions are called in the order they are declared, so this package should be imported first.
|
||||
//
|
||||
// Ref: https://github.com/prometheus-community/windows_exporter/issues/551#issuecomment-1220774835
|
||||
//
|
||||
// Declare imports on this package should be avoided where possible.
|
||||
// var declaration run before init function, so it guarantees that windows_exporter respond to service manager early
|
||||
// and avoid timeout.
|
||||
// The order of the var declaration and init functions depends on the filename as well. The filename should be 0_service.go
|
||||
// Ref: https://medium.com/@markbates/go-init-order-dafa89fcef22
|
||||
//
|
||||
//nolint:gochecknoglobals
|
||||
var IsService = func() bool {
|
||||
defer func() {
|
||||
go func() {
|
||||
err := svc.Run(serviceName, &windowsExporterService{})
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
_ = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("failed to start service: %v", err))
|
||||
}()
|
||||
}()
|
||||
|
||||
var err error
|
||||
|
||||
isService, err := svc.IsWindowsService()
|
||||
if err != nil {
|
||||
_ = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("failed to detect service: %v", err))
|
||||
|
||||
exitCodeCh <- 1
|
||||
}
|
||||
|
||||
if !isService {
|
||||
return false
|
||||
}
|
||||
|
||||
if err := logToEventToLog(windows.EVENTLOG_INFORMATION_TYPE, "attempting to start exporter service"); err != nil {
|
||||
//nolint:gosec
|
||||
_ = os.WriteFile("C:\\Program Files\\windows_exporter\\start-service.error.log", []byte(fmt.Sprintf("failed sent log to event log: %v", err)), 0o644)
|
||||
|
||||
exitCodeCh <- 2
|
||||
}
|
||||
|
||||
return true
|
||||
}()
|
||||
|
||||
type windowsExporterService struct{}
|
||||
|
||||
// Execute is the entry point for the Windows service manager.
|
||||
func (s *windowsExporterService) Execute(_ []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
|
||||
changes <- svc.Status{State: svc.StartPending}
|
||||
changes <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown}
|
||||
|
||||
for {
|
||||
select {
|
||||
case exitCodeCh := <-exitCodeCh:
|
||||
// Stop the service if an exit code from the main function is received.
|
||||
changes <- svc.Status{State: svc.StopPending}
|
||||
|
||||
return true, uint32(exitCodeCh)
|
||||
case c := <-r:
|
||||
// Handle the service control request.
|
||||
switch c.Cmd {
|
||||
case svc.Interrogate:
|
||||
changes <- c.CurrentStatus
|
||||
case svc.Stop, svc.Shutdown:
|
||||
// Stop the service if a stop or shutdown request is received.
|
||||
_ = logToEventToLog(windows.EVENTLOG_INFORMATION_TYPE, "service stop received")
|
||||
|
||||
changes <- svc.Status{State: svc.StopPending}
|
||||
|
||||
// Send a signal to the main function to stop the service.
|
||||
stopCh <- struct{}{}
|
||||
|
||||
// Wait for the main function to stop the service.
|
||||
return false, uint32(<-exitCodeCh)
|
||||
default:
|
||||
_ = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("unexpected control request #%d", c))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// logToEventToLog logs a message to the Windows event log.
|
||||
func logToEventToLog(eType uint16, msg string) error {
|
||||
eventLog, err := eventlog.Open("windows_exporter")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open event log: %w", err)
|
||||
}
|
||||
defer func(eventLog *eventlog.Log) {
|
||||
_ = eventLog.Close()
|
||||
}(eventLog)
|
||||
|
||||
p, err := windows.UTF16PtrFromString(msg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error convert string to UTF-16: %w", err)
|
||||
}
|
||||
|
||||
zero := uint16(0)
|
||||
ss := []*uint16{p, &zero, &zero, &zero, &zero, &zero, &zero, &zero, &zero}
|
||||
|
||||
err = windows.ReportEvent(eventLog.Handle, eType, 0, 3299, 0, 9, 0, &ss[0], nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error report event: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -17,12 +17,7 @@
|
||||
|
||||
package main
|
||||
|
||||
//goland:noinspection GoUnsortedImport
|
||||
//nolint:gofumpt
|
||||
import (
|
||||
// Its important that we do these first so that we can register with the Windows service control ASAP to avoid timeouts.
|
||||
"github.com/prometheus-community/windows_exporter/internal/windowsservice"
|
||||
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -55,14 +50,14 @@ func main() {
|
||||
exitCode := run()
|
||||
|
||||
// If we are running as a service, we need to signal the service control manager that we are done.
|
||||
if !windowsservice.IsService {
|
||||
if !IsService {
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
windowsservice.ExitCodeCh <- exitCode
|
||||
exitCodeCh <- exitCode
|
||||
|
||||
// Wait for the service control manager to signal that we are done.
|
||||
<-windowsservice.StopCh
|
||||
<-stopCh
|
||||
}
|
||||
|
||||
func run() int {
|
||||
@@ -114,7 +109,7 @@ func run() int {
|
||||
logFile := &log.AllowedFile{}
|
||||
|
||||
_ = logFile.Set("stdout")
|
||||
if windowsservice.IsService {
|
||||
if IsService {
|
||||
_ = logFile.Set("eventlog")
|
||||
}
|
||||
|
||||
@@ -276,7 +271,7 @@ func run() int {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
logger.Info("Shutting down windows_exporter via kill signal")
|
||||
case <-windowsservice.StopCh:
|
||||
case <-stopCh:
|
||||
logger.Info("Shutting down windows_exporter via service control")
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
@@ -330,7 +325,7 @@ func setPriorityWindows(logger *slog.Logger, pid int, priority string) error {
|
||||
winPriority, ok := priorityStringToInt[priority]
|
||||
|
||||
// Only set process priority if a non-default and valid value has been set
|
||||
if !ok || winPriority != windows.NORMAL_PRIORITY_CLASS {
|
||||
if !ok || winPriority == windows.NORMAL_PRIORITY_CLASS {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -2,49 +2,177 @@
|
||||
|
||||
The dhcp collector exposes DHCP Server metrics
|
||||
|
||||
|||
|
||||
-|-
|
||||
Metric name prefix | `dhcp`
|
||||
Data source | Perflib
|
||||
Classes | `DHCP Server`
|
||||
Enabled by default? | No
|
||||
| | |
|
||||
|---------------------|---------------|
|
||||
| Metric name prefix | `dhcp` |
|
||||
| Data source | Perflib |
|
||||
| Classes | `DHCP Server` |
|
||||
| Enabled by default? | No |
|
||||
|
||||
## Flags
|
||||
|
||||
None
|
||||
### `--collector.dhcp.enabled`
|
||||
|
||||
Comma-separated list of collectors to use. Defaults to all, if not specified.
|
||||
|
||||
## Metrics
|
||||
|
||||
Name | Description | Type | Labels
|
||||
-----|-------------|------|-------
|
||||
`packets_received_total` | Total number of packets received by the DHCP server | counter | None
|
||||
`duplicates_dropped_total` | Total number of duplicate packets received by the DHCP server | counter | None
|
||||
`packets_expired_total` | Total number of packets expired in the DHCP server message queue | counter | None
|
||||
`active_queue_length` | Number of packets in the processing queue of the DHCP server | gauge | None
|
||||
`conflict_check_queue_length` | Number of packets in the DHCP server queue waiting on conflict detection (ping) | gauge | None
|
||||
`discovers_total` | Total DHCP Discovers received by the DHCP server | counter | None
|
||||
`offers_total` | Total DHCP Offers sent by the DHCP server | counter | None
|
||||
`requests_total` | Total DHCP Requests received by the DHCP server | counter | None
|
||||
`informs_total` | Total DHCP Informs received by the DHCP server | counter | None
|
||||
`acks_total` | Total DHCP Acks sent by the DHCP server | counter | None
|
||||
`nacks_total` | Total DHCP Nacks sent by the DHCP server | counter | None
|
||||
`declines_total` | Total DHCP Declines received by the DHCP server | counter | None
|
||||
`releases_total` | Total DHCP Releases received by the DHCP server | counter | None
|
||||
`offer_queue_length` | Number of packets in the offer queue of the DHCP server | gauge | None
|
||||
`denied_due_to_match_total` | Total number of DHCP requests denied, based on matches from the Deny List | gauge | None
|
||||
`denied_due_to_nonmatch_total` | Total number of DHCP requests denied, based on non-matches from the Allow List | gauge | None
|
||||
`failover_bndupd_sent_total` | Number of DHCP failover Binding Update messages sent | counter | None
|
||||
`failover_bndupd_received_total` | Number of DHCP failover Binding Update messages received | counter | None
|
||||
`failover_bndack_sent_total` | Number of DHCP failover Binding Ack messages sent | counter | None
|
||||
`failover_bndack_received_total` | Number of DHCP failover Binding Ack messages received | counter | None
|
||||
`failover_bndupd_pending_in_outbound_queue` | Number of pending outbound DHCP failover Binding Update messages | counter | None
|
||||
`failover_transitions_communicationinterrupted_state_total` | Total number of transitions into COMMUNICATION INTERRUPTED state | counter | None
|
||||
`failover_transitions_partnerdown_state_total` | Total number of transitions into PARTNER DOWN state | counter | None
|
||||
`failover_transitions_recover_total` | Total number of transitions into RECOVER state | counter | None
|
||||
`failover_bndupd_dropped_total` | Total number of DHCP faileover Binding Updates dropped | counter | None
|
||||
| Name | Description | Type | Labels |
|
||||
|--------------------------------------------------------------------------|--------------------------------------------------------------------------------|---------|-----------------------------------------------------|
|
||||
| `windows_dhcp_ack_total` | Total DHCP Acks sent by the DHCP server | counter | None |
|
||||
| `windows_dhcp_denied_due_to_match_total` | Total number of DHCP requests denied, based on matches from the Deny List | gauge | None |
|
||||
| `windows_dhcp_denied_due_to_nonmatch_total` | Total number of DHCP requests denied, based on non-matches from the Allow List | gauge | None |
|
||||
| `windows_dhcp_declines_total` | Total DHCP Declines received by the DHCP server | counter | None |
|
||||
| `windows_dhcp_discovers_total` | Total DHCP Discovers received by the DHCP server | counter | None |
|
||||
| `windows_dhcp_failover_bndack_received_total` | Number of DHCP failover Binding Ack messages received | counter | None |
|
||||
| `windows_dhcp_failover_bndack_sent_total` | Number of DHCP failover Binding Ack messages sent | counter | None |
|
||||
| `windows_dhcp_failover_bndupd_dropped_total` | Total number of DHCP failover Binding Updates dropped | counter | None |
|
||||
| `windows_dhcp_failover_bndupd_received_total` | Number of DHCP failover Binding Update messages received | counter | None |
|
||||
| `windows_dhcp_failover_bndupd_sent_total` | Number of DHCP failover Binding Update messages sent | counter | None |
|
||||
| `windows_dhcp_failover_bndupd_pending_in_outbound_queue` | Number of pending outbound DHCP failover Binding Update messages | counter | None |
|
||||
| `windows_dhcp_failover_transitions_communicationinterrupted_state_total` | Total number of transitions into COMMUNICATION INTERRUPTED state | counter | None |
|
||||
| `windows_dhcp_failover_transitions_partnerdown_state_total` | Total number of transitions into PARTNER DOWN state | counter | None |
|
||||
| `windows_dhcp_failover_transitions_recover_total` | Total number of transitions into RECOVER state | counter | None |
|
||||
| `windows_dhcp_informs_total` | Total DHCP Informs received by the DHCP server | counter | None |
|
||||
| `windows_dhcp_nacks_total` | Total DHCP Nacks sent by the DHCP server | counter | None |
|
||||
| `windows_dhcp_offers_total` | Total DHCP Offers sent by the DHCP server | counter | None |
|
||||
| `windows_dhcp_packets_expired_total` | Total number of packets expired in the DHCP server message queue | counter | None |
|
||||
| `windows_dhcp_packets_received_total` | Total number of packets received by the DHCP server | counter | None |
|
||||
| `windows_dhcp_pending_offers_total` | Total number of pending offers in the DHCP server | counter | None |
|
||||
| `windows_dhcp_releases_total` | Total DHCP Releases received by the DHCP server | counter | None |
|
||||
| `windows_dhcp_requests_total` | Total DHCP Requests received by the DHCP server | counter | None |
|
||||
| `windows_dhcp_scope_addresses_free_on_this_server` | DHCP Scope free addresses on this server | gauge | `scope` |
|
||||
| `windows_dhcp_scope_addresses_free_on_partner_server` | DHCP Scope free addresses on partner server | gauge | `scope` |
|
||||
| `windows_dhcp_scope_addresses_free` | DHCP Scope free addresses | gauge | `scope` |
|
||||
| `windows_dhcp_scope_addresses_in_use_on_this_server` | DHCP Scope addresses in use on this server | gauge | `scope` |
|
||||
| `windows_dhcp_scope_addresses_in_use_on_partner_server` | DHCP Scope addresses in use on partner server | gauge | `scope` |
|
||||
| `windows_dhcp_scope_addresses_in_use` | DHCP Scope addresses in use | gauge | `scope` |
|
||||
| `windows_dhcp_scope_info` | DHCP Scope information | gauge | `name`, `superscope_name`, `superscope_id`, `scope` |
|
||||
| `windows_dhcp_scope_pending_offers` | DHCP Scope pending offers | gauge | `scope` |
|
||||
| `windows_dhcp_scope_reserved_address` | DHCP Scope reserved addresses | gauge | `scope` |
|
||||
| `windows_dhcp_scope_state` | DHCP Scope state | gauge | `scope`, `state` |
|
||||
|
||||
|
||||
### Example metric
|
||||
_This collector does not yet have explained examples, we would appreciate your help adding them!_
|
||||
```
|
||||
# HELP windows_dhcp_acks_total Total DHCP Acks sent by the DHCP server (AcksTotal)
|
||||
# TYPE windows_dhcp_acks_total counter
|
||||
windows_dhcp_acks_total 0
|
||||
# HELP windows_dhcp_active_queue_length Number of packets in the processing queue of the DHCP server (ActiveQueueLength)
|
||||
# TYPE windows_dhcp_active_queue_length gauge
|
||||
windows_dhcp_active_queue_length 0
|
||||
# HELP windows_dhcp_conflict_check_queue_length Number of packets in the DHCP server queue waiting on conflict detection (ping). (ConflictCheckQueueLength)
|
||||
# TYPE windows_dhcp_conflict_check_queue_length gauge
|
||||
windows_dhcp_conflict_check_queue_length 0
|
||||
# HELP windows_dhcp_declines_total Total DHCP Declines received by the DHCP server (DeclinesTotal)
|
||||
# TYPE windows_dhcp_declines_total counter
|
||||
windows_dhcp_declines_total 0
|
||||
# HELP windows_dhcp_denied_due_to_match_total Total number of DHCP requests denied, based on matches from the Deny list (DeniedDueToMatch)
|
||||
# TYPE windows_dhcp_denied_due_to_match_total counter
|
||||
windows_dhcp_denied_due_to_match_total 0
|
||||
# HELP windows_dhcp_denied_due_to_nonmatch_total Total number of DHCP requests denied, based on non-matches from the Allow list (DeniedDueToNonMatch)
|
||||
# TYPE windows_dhcp_denied_due_to_nonmatch_total counter
|
||||
windows_dhcp_denied_due_to_nonmatch_total 0
|
||||
# HELP windows_dhcp_discovers_total Total DHCP Discovers received by the DHCP server (DiscoversTotal)
|
||||
# TYPE windows_dhcp_discovers_total counter
|
||||
windows_dhcp_discovers_total 0
|
||||
# HELP windows_dhcp_duplicates_dropped_total Total number of duplicate packets received by the DHCP server (DuplicatesDroppedTotal)
|
||||
# TYPE windows_dhcp_duplicates_dropped_total counter
|
||||
windows_dhcp_duplicates_dropped_total 0
|
||||
# HELP windows_dhcp_failover_bndack_received_total Number of DHCP fail over Binding Ack messages received (FailoverBndackReceivedTotal)
|
||||
# TYPE windows_dhcp_failover_bndack_received_total counter
|
||||
windows_dhcp_failover_bndack_received_total 0
|
||||
# HELP windows_dhcp_failover_bndack_sent_total Number of DHCP fail over Binding Ack messages sent (FailoverBndackSentTotal)
|
||||
# TYPE windows_dhcp_failover_bndack_sent_total counter
|
||||
windows_dhcp_failover_bndack_sent_total 0
|
||||
# HELP windows_dhcp_failover_bndupd_dropped_total Total number of DHCP fail over Binding Updates dropped (FailoverBndupdDropped)
|
||||
# TYPE windows_dhcp_failover_bndupd_dropped_total counter
|
||||
windows_dhcp_failover_bndupd_dropped_total 0
|
||||
# HELP windows_dhcp_failover_bndupd_pending_in_outbound_queue Number of pending outbound DHCP fail over Binding Update messages (FailoverBndupdPendingOutboundQueue)
|
||||
# TYPE windows_dhcp_failover_bndupd_pending_in_outbound_queue gauge
|
||||
windows_dhcp_failover_bndupd_pending_in_outbound_queue 0
|
||||
# HELP windows_dhcp_failover_bndupd_received_total Number of DHCP fail over Binding Update messages received (FailoverBndupdReceivedTotal)
|
||||
# TYPE windows_dhcp_failover_bndupd_received_total counter
|
||||
windows_dhcp_failover_bndupd_received_total 0
|
||||
# HELP windows_dhcp_failover_bndupd_sent_total Number of DHCP fail over Binding Update messages sent (FailoverBndupdSentTotal)
|
||||
# TYPE windows_dhcp_failover_bndupd_sent_total counter
|
||||
windows_dhcp_failover_bndupd_sent_total 0
|
||||
# HELP windows_dhcp_failover_transitions_communicationinterrupted_state_total Total number of transitions into COMMUNICATION INTERRUPTED state (FailoverTransitionsCommunicationinterruptedState)
|
||||
# TYPE windows_dhcp_failover_transitions_communicationinterrupted_state_total counter
|
||||
windows_dhcp_failover_transitions_communicationinterrupted_state_total 0
|
||||
# HELP windows_dhcp_failover_transitions_partnerdown_state_total Total number of transitions into PARTNER DOWN state (FailoverTransitionsPartnerdownState)
|
||||
# TYPE windows_dhcp_failover_transitions_partnerdown_state_total counter
|
||||
windows_dhcp_failover_transitions_partnerdown_state_total 0
|
||||
# HELP windows_dhcp_failover_transitions_recover_total Total number of transitions into RECOVER state (FailoverTransitionsRecoverState)
|
||||
# TYPE windows_dhcp_failover_transitions_recover_total counter
|
||||
windows_dhcp_failover_transitions_recover_total 0
|
||||
# HELP windows_dhcp_informs_total Total DHCP Informs received by the DHCP server (InformsTotal)
|
||||
# TYPE windows_dhcp_informs_total counter
|
||||
windows_dhcp_informs_total 0
|
||||
# HELP windows_dhcp_nacks_total Total DHCP Nacks sent by the DHCP server (NacksTotal)
|
||||
# TYPE windows_dhcp_nacks_total counter
|
||||
windows_dhcp_nacks_total 0
|
||||
# HELP windows_dhcp_offer_queue_length Number of packets in the offer queue of the DHCP server (OfferQueueLength)
|
||||
# TYPE windows_dhcp_offer_queue_length gauge
|
||||
windows_dhcp_offer_queue_length 0
|
||||
# HELP windows_dhcp_offers_total Total DHCP Offers sent by the DHCP server (OffersTotal)
|
||||
# TYPE windows_dhcp_offers_total counter
|
||||
windows_dhcp_offers_total 0
|
||||
# HELP windows_dhcp_packets_expired_total Total number of packets expired in the DHCP server message queue (PacketsExpiredTotal)
|
||||
# TYPE windows_dhcp_packets_expired_total counter
|
||||
windows_dhcp_packets_expired_total 0
|
||||
# HELP windows_dhcp_packets_received_total Total number of packets received by the DHCP server (PacketsReceivedTotal)
|
||||
# TYPE windows_dhcp_packets_received_total counter
|
||||
windows_dhcp_packets_received_total 0
|
||||
# HELP windows_dhcp_releases_total Total DHCP Releases received by the DHCP server (ReleasesTotal)
|
||||
# TYPE windows_dhcp_releases_total counter
|
||||
windows_dhcp_releases_total 0
|
||||
# HELP windows_dhcp_requests_total Total DHCP Requests received by the DHCP server (RequestsTotal)
|
||||
# TYPE windows_dhcp_requests_total counter
|
||||
windows_dhcp_requests_total 0
|
||||
# HELP windows_dhcp_scope_addresses_free_total DHCP Scope free addresses
|
||||
# TYPE windows_dhcp_scope_addresses_free_total gauge
|
||||
windows_dhcp_scope_addresses_free_total{scope="10.11.12.0/25"} 0
|
||||
windows_dhcp_scope_addresses_free_total{scope="172.16.0.0/24"} 0
|
||||
windows_dhcp_scope_addresses_free_total{scope="192.168.0.0/24"} 231
|
||||
# HELP windows_dhcp_scope_addresses_in_use_total DHCP Scope addresses in use
|
||||
# TYPE windows_dhcp_scope_addresses_in_use_total gauge
|
||||
windows_dhcp_scope_addresses_in_use_total{scope="10.11.12.0/25"} 0
|
||||
windows_dhcp_scope_addresses_in_use_total{scope="172.16.0.0/24"} 0
|
||||
windows_dhcp_scope_addresses_in_use_total{scope="192.168.0.0/24"} 0
|
||||
# HELP windows_dhcp_scope_info DHCP Scope information
|
||||
# TYPE windows_dhcp_scope_info gauge
|
||||
windows_dhcp_scope_info{name="SUBSUPERSCOPE",scope="172.16.0.0/24",superscope_id="2",superscope_name="SUPERSCOPE"} 1
|
||||
windows_dhcp_scope_info{name="TEST",scope="192.168.0.0/24",superscope_id="0",superscope_name=""} 1
|
||||
windows_dhcp_scope_info{name="TEST2",scope="10.11.12.0/25",superscope_id="2",superscope_name="SUPERSCOPE"} 1
|
||||
# HELP windows_dhcp_scope_pending_offers_total DHCP Scope pending offers
|
||||
# TYPE windows_dhcp_scope_pending_offers_total gauge
|
||||
windows_dhcp_scope_pending_offers_total{scope="10.11.12.0/25"} 0
|
||||
windows_dhcp_scope_pending_offers_total{scope="172.16.0.0/24"} 0
|
||||
windows_dhcp_scope_pending_offers_total{scope="192.168.0.0/24"} 0
|
||||
# HELP windows_dhcp_scope_reserved_address_total DHCP Scope reserved addresses
|
||||
# TYPE windows_dhcp_scope_reserved_address_total gauge
|
||||
windows_dhcp_scope_reserved_address_total{scope="10.11.12.0/25"} 0
|
||||
windows_dhcp_scope_reserved_address_total{scope="172.16.0.0/24"} 0
|
||||
windows_dhcp_scope_reserved_address_total{scope="192.168.0.0/24"} 2
|
||||
# HELP windows_dhcp_scope_state DHCP Scope state
|
||||
# TYPE windows_dhcp_scope_state gauge
|
||||
windows_dhcp_scope_state{scope="10.11.12.0/25",state="Disabled"} 1
|
||||
windows_dhcp_scope_state{scope="10.11.12.0/25",state="DisabledSwitched"} 0
|
||||
windows_dhcp_scope_state{scope="10.11.12.0/25",state="Enabled"} 0
|
||||
windows_dhcp_scope_state{scope="10.11.12.0/25",state="EnabledSwitched"} 0
|
||||
windows_dhcp_scope_state{scope="10.11.12.0/25",state="InvalidState"} 0
|
||||
windows_dhcp_scope_state{scope="172.16.0.0/24",state="Disabled"} 1
|
||||
windows_dhcp_scope_state{scope="172.16.0.0/24",state="DisabledSwitched"} 0
|
||||
windows_dhcp_scope_state{scope="172.16.0.0/24",state="Enabled"} 0
|
||||
windows_dhcp_scope_state{scope="172.16.0.0/24",state="EnabledSwitched"} 0
|
||||
windows_dhcp_scope_state{scope="172.16.0.0/24",state="InvalidState"} 0
|
||||
windows_dhcp_scope_state{scope="192.168.0.0/24",state="Disabled"} 0
|
||||
windows_dhcp_scope_state{scope="192.168.0.0/24",state="DisabledSwitched"} 0
|
||||
windows_dhcp_scope_state{scope="192.168.0.0/24",state="Enabled"} 1
|
||||
windows_dhcp_scope_state{scope="192.168.0.0/24",state="EnabledSwitched"} 0
|
||||
windows_dhcp_scope_state{scope="192.168.0.0/24",state="InvalidState"} 0
|
||||
```
|
||||
|
||||
## Useful queries
|
||||
_This collector does not yet have any useful queries added, we would appreciate your help adding them!_
|
||||
|
||||
@@ -19,6 +19,10 @@ If given, an interface name needs to match the include regexp in order for the c
|
||||
|
||||
If given, an interface name needs to *not* match the exclude regexp in order for the corresponding metrics to be reported
|
||||
|
||||
### `--collector.net.enabled`
|
||||
|
||||
Comma-separated list of collectors to use. Defaults to all, if not specified.
|
||||
|
||||
## Metrics
|
||||
|
||||
Name | Description | Type | Labels
|
||||
|
||||
@@ -10,7 +10,9 @@ The netframework collector exposes metrics about dotnet framework.
|
||||
|
||||
## Flags
|
||||
|
||||
None
|
||||
### `--collector.netframework.enabled`
|
||||
|
||||
Comma-separated list of collectors to use. Defaults to all, if not specified.
|
||||
|
||||
## Metrics
|
||||
|
||||
|
||||
4
go.mod
4
go.mod
@@ -5,7 +5,7 @@ go 1.23
|
||||
require (
|
||||
github.com/Microsoft/hcsshim v0.12.9
|
||||
github.com/alecthomas/kingpin/v2 v2.4.0
|
||||
github.com/bmatcuk/doublestar/v4 v4.7.1
|
||||
github.com/bmatcuk/doublestar/v4 v4.8.1
|
||||
github.com/dimchansky/utfbom v1.1.1
|
||||
github.com/go-ole/go-ole v1.3.0
|
||||
github.com/google/uuid v1.6.0
|
||||
@@ -44,7 +44,7 @@ require (
|
||||
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
|
||||
go.opencensus.io v0.24.0 // indirect
|
||||
golang.org/x/crypto v0.31.0 // indirect
|
||||
golang.org/x/net v0.32.0 // indirect
|
||||
golang.org/x/net v0.33.0 // indirect
|
||||
golang.org/x/oauth2 v0.24.0 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
|
||||
8
go.sum
8
go.sum
@@ -10,8 +10,8 @@ github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b h1:mimo19zliBX/vS
|
||||
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b/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/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0CXv75Q=
|
||||
github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
|
||||
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
|
||||
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
@@ -145,8 +145,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
|
||||
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
|
||||
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
|
||||
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
|
||||
|
||||
@@ -28,7 +28,7 @@ Copy-Item -Force $PathToExecutable Work/windows_exporter.exe
|
||||
Write-Verbose "Creating windows_exporter-${Version}-${Arch}.msi"
|
||||
$wixArch = @{"amd64" = "x64"; "arm64" = "arm64"}[$Arch]
|
||||
|
||||
Invoke-Expression "wix build -arch $wixArch -o .\windows_exporter-$($Version)-$($Arch).msi .\files.wxs .\main.wxs -d ProductName=windows_exporter -d Version=$($MsiVersion) -ext WixToolset.Firewall.wixext -ext WixToolset.UI.wixext -ext WixToolset.Util.wixext"
|
||||
Invoke-Expression "wix build -sw1149 -arch $wixArch -o .\windows_exporter-$($Version)-$($Arch).msi .\files.wxs .\main.wxs -d ProductName=windows_exporter -d Version=$($MsiVersion) -ext WixToolset.Firewall.wixext -ext WixToolset.UI.wixext -ext WixToolset.Util.wixext"
|
||||
|
||||
Write-Verbose "Done!"
|
||||
Pop-Location
|
||||
|
||||
@@ -27,10 +27,15 @@
|
||||
ErrorControl="normal"
|
||||
Start="auto"
|
||||
Type="ownProcess"
|
||||
Interactive="no"
|
||||
Vital="yes"
|
||||
Arguments="[ConfigFileFlag] [CollectorsFlag] [ListenFlag] [MetricsPathFlag] [TextfileDirsFlag] [ExtraFlags]">
|
||||
<ServiceConfig
|
||||
DelayedAutoStart="yes"
|
||||
OnInstall="yes"
|
||||
OnReinstall="yes" />
|
||||
<util:ServiceConfig
|
||||
ResetPeriodInDays="1"
|
||||
ResetPeriodInDays="0"
|
||||
FirstFailureActionType="restart"
|
||||
SecondFailureActionType="restart"
|
||||
ThirdFailureActionType="restart"
|
||||
|
||||
@@ -43,6 +43,9 @@
|
||||
Property="OLDERVERSIONBEINGUPGRADED" />
|
||||
</Upgrade>
|
||||
|
||||
<CustomAction Id="CheckExtraFlags"
|
||||
Error="The parameter '--config.file' must not be included in EXTRA_FLAGS. Use CONFIG_FILE instead. Please remove it and try again." />
|
||||
|
||||
<CustomAction Id="set_maintenance" Property="MAINTENANCE" Value="true" />
|
||||
|
||||
<!-- Set to reinstall all features. -->
|
||||
@@ -54,12 +57,12 @@
|
||||
<CustomAction Id="set_reinstallmode_property"
|
||||
Property="REINSTALLMODE"
|
||||
Value="amus" />
|
||||
<!-- START CUSTOM ACTION FOR CONFIG FILE CREATION -->
|
||||
<SetProperty
|
||||
Id="CreateConfigFile"
|
||||
Value=""[%ComSpec]" /c TYPE NUL >>"[ConfigFile_NonDefault][ConfigFile_Default]""
|
||||
Before="CreateConfigFile"
|
||||
Sequence="execute"
|
||||
Condition="ConfigFile_NonDefault OR ConfigFile_Default"
|
||||
/>
|
||||
<CustomAction
|
||||
Id="CreateConfigFile"
|
||||
@@ -69,6 +72,24 @@
|
||||
Return="check"
|
||||
Impersonate="no"
|
||||
/>
|
||||
<!-- END CUSTOM ACTION FOR CONFIG FILE CREATION -->
|
||||
|
||||
<!-- START CUSTOM ACTION FOR KILLING THE PROCESS -->
|
||||
<SetProperty
|
||||
Id="KillProcess"
|
||||
Value=""[WindowsFolder]\System32\taskkill.exe" /T /F /IM windows_exporter.exe"
|
||||
Before="KillProcess"
|
||||
Sequence="execute"
|
||||
/>
|
||||
<CustomAction
|
||||
Id="KillProcess"
|
||||
BinaryRef="Wix4UtilCA_$(sys.BUILDARCHSHORT)"
|
||||
DllEntry="WixQuietExec"
|
||||
Execute="deferred"
|
||||
Return="ignore"
|
||||
Impersonate="no"
|
||||
/>
|
||||
<!-- END CUSTOM ACTION FOR KILLING THE PROCESS -->
|
||||
|
||||
<InstallExecuteSequence>
|
||||
<!-- Set REINSTALL=all and REINSTALLMODE=amus if the user reruns the
|
||||
@@ -78,6 +99,10 @@
|
||||
<Custom Action="set_reinstall_all_property" Before="set_reinstallmode_property" Condition="MAINTENANCE"/>
|
||||
<Custom Action="set_reinstallmode_property" Before="LaunchConditions" Condition="MAINTENANCE"/>
|
||||
<Custom Action="CreateConfigFile" Before="InstallServices" Condition="ConfigFile_NonDefault OR ConfigFile_Default" />
|
||||
<Custom Action="KillProcess" Before="RemoveFiles" />
|
||||
|
||||
<Custom Action="CheckExtraFlags" Before="InstallInitialize"
|
||||
Condition="EXTRA_FLAGS AND (EXTRA_FLAGS><"--config.file")" />
|
||||
</InstallExecuteSequence>
|
||||
|
||||
<Media Id="1" Cabinet="windows_exporter.cab" EmbedCab="yes" />
|
||||
@@ -90,9 +115,10 @@
|
||||
<SetProperty Id="ExtraFlags" After="InstallFiles" Sequence="execute" Value="[EXTRA_FLAGS]" Condition="EXTRA_FLAGS" />
|
||||
|
||||
<Property Id="CONFIG_FILE" Secure="yes" Value="config.yaml" />
|
||||
<SetProperty Id="ConfigFile_NonDefault" After="InstallFiles" Sequence="execute" Value="[CONFIG_FILE]" Condition="CONFIG_FILE AND CONFIG_FILE<>"config.yaml"" />
|
||||
<SetProperty Id="ConfigFile_Remote" After="InstallFiles" Sequence="execute" Value="[CONFIG_FILE]" Condition="CONFIG_FILE AND (CONFIG_FILE<<"http://" OR CONFIG_FILE<<"https://")" />
|
||||
<SetProperty Id="ConfigFile_NonDefault" After="InstallFiles" Sequence="execute" Value="[CONFIG_FILE]" Condition="CONFIG_FILE AND CONFIG_FILE<>"config.yaml" AND NOT (CONFIG_FILE<<"http://" OR CONFIG_FILE<<"https://")" />
|
||||
<SetProperty Id="ConfigFile_Default" After="InstallFiles" Sequence="execute" Value="[APPLICATIONFOLDER]config.yaml" Condition="CONFIG_FILE="config.yaml"" />
|
||||
<SetProperty Id="ConfigFileFlag" After="InstallFiles" Sequence="execute" Value="--config.file="[ConfigFile_NonDefault][ConfigFile_Default]"" Condition="ConfigFile_NonDefault OR ConfigFile_Default" />
|
||||
<SetProperty Id="ConfigFileFlag" After="InstallFiles" Sequence="execute" Value="--config.file="[ConfigFile_Remote][ConfigFile_NonDefault][ConfigFile_Default]"" Condition="ConfigFile_Remote OR ConfigFile_NonDefault OR ConfigFile_Default" />
|
||||
|
||||
<Property Id="LISTEN_PORT" Secure="yes" Value="9182" />
|
||||
<SetProperty Id="ListenFlag" After="InstallFiles" Sequence="execute" Value="--web.listen-address [LISTEN_ADDR]:[LISTEN_PORT]" Condition="LISTEN_ADDR<>"" OR LISTEN_PORT<>9182" />
|
||||
|
||||
@@ -16,22 +16,39 @@
|
||||
package dhcp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
"github.com/prometheus-community/windows_exporter/internal/headers/dhcpsapi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/pdh"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
const Name = "dhcp"
|
||||
const (
|
||||
Name = "dhcp"
|
||||
|
||||
type Config struct{}
|
||||
subCollectorServerMetrics = "server_metrics"
|
||||
subCollectorScopeMetrics = "scope_metrics"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
CollectorsEnabled []string `yaml:"collectors_enabled"`
|
||||
}
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var ConfigDefaults = Config{}
|
||||
var ConfigDefaults = Config{
|
||||
CollectorsEnabled: []string{
|
||||
subCollectorServerMetrics,
|
||||
subCollectorScopeMetrics,
|
||||
},
|
||||
}
|
||||
|
||||
// A Collector is a Prometheus Collector perflib DHCP metrics.
|
||||
type Collector struct {
|
||||
@@ -65,6 +82,17 @@ type Collector struct {
|
||||
packetsReceivedTotal *prometheus.Desc
|
||||
releasesTotal *prometheus.Desc
|
||||
requestsTotal *prometheus.Desc
|
||||
|
||||
scopeInfo *prometheus.Desc
|
||||
scopeState *prometheus.Desc
|
||||
scopeAddressesFreeTotal *prometheus.Desc
|
||||
scopeAddressesFreeOnPartnerServerTotal *prometheus.Desc
|
||||
scopeAddressesFreeOnThisServerTotal *prometheus.Desc
|
||||
scopeAddressesInUseTotal *prometheus.Desc
|
||||
scopeAddressesInUseOnPartnerServerTotal *prometheus.Desc
|
||||
scopeAddressesInUseOnThisServerTotal *prometheus.Desc
|
||||
scopePendingOffersTotal *prometheus.Desc
|
||||
scopeReservedAddressTotal *prometheus.Desc
|
||||
}
|
||||
|
||||
func New(config *Config) *Collector {
|
||||
@@ -72,6 +100,10 @@ func New(config *Config) *Collector {
|
||||
config = &ConfigDefaults
|
||||
}
|
||||
|
||||
if config.CollectorsEnabled == nil {
|
||||
config.CollectorsEnabled = ConfigDefaults.CollectorsEnabled
|
||||
}
|
||||
|
||||
c := &Collector{
|
||||
config: *config,
|
||||
}
|
||||
@@ -79,8 +111,26 @@ func New(config *Config) *Collector {
|
||||
return c
|
||||
}
|
||||
|
||||
func NewWithFlags(_ *kingpin.Application) *Collector {
|
||||
return &Collector{}
|
||||
func NewWithFlags(app *kingpin.Application) *Collector {
|
||||
c := &Collector{
|
||||
config: ConfigDefaults,
|
||||
}
|
||||
c.config.CollectorsEnabled = make([]string, 0)
|
||||
|
||||
var collectorsEnabled string
|
||||
|
||||
app.Flag(
|
||||
"collector.dhcp.enabled",
|
||||
"Comma-separated list of collectors to use. Defaults to all, if not specified.",
|
||||
).Default(strings.Join(ConfigDefaults.CollectorsEnabled, ",")).StringVar(&collectorsEnabled)
|
||||
|
||||
app.Action(func(*kingpin.ParseContext) error {
|
||||
c.config.CollectorsEnabled = strings.Split(collectorsEnabled, ",")
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Collector) GetName() string {
|
||||
@@ -88,7 +138,9 @@ func (c *Collector) GetName() string {
|
||||
}
|
||||
|
||||
func (c *Collector) Close() error {
|
||||
c.perfDataCollector.Close()
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorServerMetrics) {
|
||||
c.perfDataCollector.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -96,166 +148,258 @@ func (c *Collector) Close() error {
|
||||
func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
|
||||
var err error
|
||||
|
||||
c.perfDataCollector, err = pdh.NewCollector[perfDataCounterValues](pdh.CounterTypeRaw, "DHCP Server", nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create DHCP Server collector: %w", err)
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorServerMetrics) {
|
||||
c.perfDataCollector, err = pdh.NewCollector[perfDataCounterValues](pdh.CounterTypeRaw, "DHCP Server", nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create DHCP Server collector: %w", err)
|
||||
}
|
||||
|
||||
c.packetsReceivedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "packets_received_total"),
|
||||
"Total number of packets received by the DHCP server (PacketsReceivedTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.duplicatesDroppedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "duplicates_dropped_total"),
|
||||
"Total number of duplicate packets received by the DHCP server (DuplicatesDroppedTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.packetsExpiredTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "packets_expired_total"),
|
||||
"Total number of packets expired in the DHCP server message queue (PacketsExpiredTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.activeQueueLength = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "active_queue_length"),
|
||||
"Number of packets in the processing queue of the DHCP server (ActiveQueueLength)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.conflictCheckQueueLength = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "conflict_check_queue_length"),
|
||||
"Number of packets in the DHCP server queue waiting on conflict detection (ping). (ConflictCheckQueueLength)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.discoversTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "discovers_total"),
|
||||
"Total DHCP Discovers received by the DHCP server (DiscoversTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.offersTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "offers_total"),
|
||||
"Total DHCP Offers sent by the DHCP server (OffersTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.requestsTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "requests_total"),
|
||||
"Total DHCP Requests received by the DHCP server (RequestsTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.informsTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "informs_total"),
|
||||
"Total DHCP Informs received by the DHCP server (InformsTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.acksTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "acks_total"),
|
||||
"Total DHCP Acks sent by the DHCP server (AcksTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.nACKsTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "nacks_total"),
|
||||
"Total DHCP Nacks sent by the DHCP server (NacksTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.declinesTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "declines_total"),
|
||||
"Total DHCP Declines received by the DHCP server (DeclinesTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.releasesTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "releases_total"),
|
||||
"Total DHCP Releases received by the DHCP server (ReleasesTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.offerQueueLength = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "offer_queue_length"),
|
||||
"Number of packets in the offer queue of the DHCP server (OfferQueueLength)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.deniedDueToMatch = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "denied_due_to_match_total"),
|
||||
"Total number of DHCP requests denied, based on matches from the Deny list (DeniedDueToMatch)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.deniedDueToNonMatch = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "denied_due_to_nonmatch_total"),
|
||||
"Total number of DHCP requests denied, based on non-matches from the Allow list (DeniedDueToNonMatch)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndUpdSentTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndupd_sent_total"),
|
||||
"Number of DHCP fail over Binding Update messages sent (FailoverBndupdSentTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndUpdReceivedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndupd_received_total"),
|
||||
"Number of DHCP fail over Binding Update messages received (FailoverBndupdReceivedTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndAckSentTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndack_sent_total"),
|
||||
"Number of DHCP fail over Binding Ack messages sent (FailoverBndackSentTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndAckReceivedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndack_received_total"),
|
||||
"Number of DHCP fail over Binding Ack messages received (FailoverBndackReceivedTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndUpdPendingOutboundQueue = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndupd_pending_in_outbound_queue"),
|
||||
"Number of pending outbound DHCP fail over Binding Update messages (FailoverBndupdPendingOutboundQueue)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverTransitionsCommunicationInterruptedState = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_transitions_communicationinterrupted_state_total"),
|
||||
"Total number of transitions into COMMUNICATION INTERRUPTED state (FailoverTransitionsCommunicationinterruptedState)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverTransitionsPartnerDownState = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_transitions_partnerdown_state_total"),
|
||||
"Total number of transitions into PARTNER DOWN state (FailoverTransitionsPartnerdownState)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverTransitionsRecoverState = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_transitions_recover_total"),
|
||||
"Total number of transitions into RECOVER state (FailoverTransitionsRecoverState)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndUpdDropped = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndupd_dropped_total"),
|
||||
"Total number of DHCP fail over Binding Updates dropped (FailoverBndupdDropped)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
||||
c.packetsReceivedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "packets_received_total"),
|
||||
"Total number of packets received by the DHCP server (PacketsReceivedTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.duplicatesDroppedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "duplicates_dropped_total"),
|
||||
"Total number of duplicate packets received by the DHCP server (DuplicatesDroppedTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.packetsExpiredTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "packets_expired_total"),
|
||||
"Total number of packets expired in the DHCP server message queue (PacketsExpiredTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.activeQueueLength = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "active_queue_length"),
|
||||
"Number of packets in the processing queue of the DHCP server (ActiveQueueLength)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.conflictCheckQueueLength = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "conflict_check_queue_length"),
|
||||
"Number of packets in the DHCP server queue waiting on conflict detection (ping). (ConflictCheckQueueLength)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.discoversTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "discovers_total"),
|
||||
"Total DHCP Discovers received by the DHCP server (DiscoversTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.offersTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "offers_total"),
|
||||
"Total DHCP Offers sent by the DHCP server (OffersTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.requestsTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "requests_total"),
|
||||
"Total DHCP Requests received by the DHCP server (RequestsTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.informsTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "informs_total"),
|
||||
"Total DHCP Informs received by the DHCP server (InformsTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.acksTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "acks_total"),
|
||||
"Total DHCP Acks sent by the DHCP server (AcksTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.nACKsTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "nacks_total"),
|
||||
"Total DHCP Nacks sent by the DHCP server (NacksTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.declinesTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "declines_total"),
|
||||
"Total DHCP Declines received by the DHCP server (DeclinesTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.releasesTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "releases_total"),
|
||||
"Total DHCP Releases received by the DHCP server (ReleasesTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.offerQueueLength = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "offer_queue_length"),
|
||||
"Number of packets in the offer queue of the DHCP server (OfferQueueLength)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.deniedDueToMatch = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "denied_due_to_match_total"),
|
||||
"Total number of DHCP requests denied, based on matches from the Deny list (DeniedDueToMatch)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.deniedDueToNonMatch = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "denied_due_to_nonmatch_total"),
|
||||
"Total number of DHCP requests denied, based on non-matches from the Allow list (DeniedDueToNonMatch)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndUpdSentTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndupd_sent_total"),
|
||||
"Number of DHCP fail over Binding Update messages sent (FailoverBndupdSentTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndUpdReceivedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndupd_received_total"),
|
||||
"Number of DHCP fail over Binding Update messages received (FailoverBndupdReceivedTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndAckSentTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndack_sent_total"),
|
||||
"Number of DHCP fail over Binding Ack messages sent (FailoverBndackSentTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndAckReceivedTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndack_received_total"),
|
||||
"Number of DHCP fail over Binding Ack messages received (FailoverBndackReceivedTotal)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndUpdPendingOutboundQueue = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndupd_pending_in_outbound_queue"),
|
||||
"Number of pending outbound DHCP fail over Binding Update messages (FailoverBndupdPendingOutboundQueue)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverTransitionsCommunicationInterruptedState = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_transitions_communicationinterrupted_state_total"),
|
||||
"Total number of transitions into COMMUNICATION INTERRUPTED state (FailoverTransitionsCommunicationinterruptedState)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverTransitionsPartnerDownState = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_transitions_partnerdown_state_total"),
|
||||
"Total number of transitions into PARTNER DOWN state (FailoverTransitionsPartnerdownState)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverTransitionsRecoverState = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_transitions_recover_total"),
|
||||
"Total number of transitions into RECOVER state (FailoverTransitionsRecoverState)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
c.failoverBndUpdDropped = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "failover_bndupd_dropped_total"),
|
||||
"Total number of DHCP fail over Binding Updates dropped (FailoverBndupdDropped)",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorScopeMetrics) {
|
||||
c.scopeInfo = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_info"),
|
||||
"DHCP Scope information",
|
||||
[]string{"name", "superscope_name", "superscope_id", "scope"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.scopeState = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_state"),
|
||||
"DHCP Scope state",
|
||||
[]string{"scope", "state"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.scopeAddressesFreeTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_addresses_free"),
|
||||
"DHCP Scope free addresses",
|
||||
[]string{"scope"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.scopeAddressesFreeOnPartnerServerTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_addresses_free_on_partner_server"),
|
||||
"DHCP Scope free addresses on partner server",
|
||||
[]string{"scope"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.scopeAddressesFreeOnThisServerTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_addresses_free_on_this_server"),
|
||||
"DHCP Scope free addresses on this server",
|
||||
[]string{"scope"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.scopeAddressesInUseTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_addresses_in_use"),
|
||||
"DHCP Scope addresses in use",
|
||||
[]string{"scope"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.scopeAddressesInUseOnPartnerServerTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_addresses_in_use_on_partner_server"),
|
||||
"DHCP Scope addresses in use on partner server",
|
||||
[]string{"scope"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.scopeAddressesInUseOnThisServerTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_addresses_in_use_on_this_server"),
|
||||
"DHCP Scope addresses in use on this server",
|
||||
[]string{"scope"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.scopePendingOffersTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_pending_offers"),
|
||||
"DHCP Scope pending offers",
|
||||
[]string{"scope"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.scopeReservedAddressTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "scope_reserved_address"),
|
||||
"DHCP Scope reserved addresses",
|
||||
[]string{"scope"},
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
var errs []error
|
||||
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorServerMetrics) {
|
||||
if err := c.collectServerMetrics(ch); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorScopeMetrics) {
|
||||
if err := c.collectScopeMetrics(ch); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (c *Collector) collectServerMetrics(ch chan<- prometheus.Metric) error {
|
||||
err := c.perfDataCollector.Collect(&c.perfDataObject)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect DHCP Server metrics: %w", err)
|
||||
@@ -413,3 +557,113 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Collector) collectScopeMetrics(ch chan<- prometheus.Metric) error {
|
||||
dhcpScopes, err := dhcpsapi.GetDHCPV4ScopeStatistics()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get DHCP scopes: %w", err)
|
||||
}
|
||||
|
||||
for _, scope := range dhcpScopes {
|
||||
scopeID := scope.ScopeIPAddress.String()
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopeInfo,
|
||||
prometheus.GaugeValue,
|
||||
1,
|
||||
scope.Name,
|
||||
scope.SuperScopeName,
|
||||
strconv.Itoa(int(scope.SuperScopeNumber)),
|
||||
scopeID,
|
||||
)
|
||||
|
||||
for state, name := range dhcpsapi.DHCP_SUBNET_STATE_NAMES {
|
||||
metric := 0.0
|
||||
if state == scope.State {
|
||||
metric = 1.0
|
||||
}
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopeState,
|
||||
prometheus.GaugeValue,
|
||||
metric,
|
||||
scopeID,
|
||||
name,
|
||||
)
|
||||
}
|
||||
|
||||
if scope.AddressesFree != -1 {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopeAddressesFreeTotal,
|
||||
prometheus.GaugeValue,
|
||||
scope.AddressesFree,
|
||||
scopeID,
|
||||
)
|
||||
}
|
||||
|
||||
if scope.AddressesFreeOnPartnerServer != -1 {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopeAddressesFreeOnPartnerServerTotal,
|
||||
prometheus.GaugeValue,
|
||||
scope.AddressesFreeOnPartnerServer,
|
||||
scopeID,
|
||||
)
|
||||
}
|
||||
|
||||
if scope.AddressesFreeOnThisServer != -1 {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopeAddressesFreeOnThisServerTotal,
|
||||
prometheus.GaugeValue,
|
||||
scope.AddressesFreeOnThisServer,
|
||||
scopeID,
|
||||
)
|
||||
}
|
||||
|
||||
if scope.AddressesInUse != -1 {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopeAddressesInUseTotal,
|
||||
prometheus.GaugeValue,
|
||||
scope.AddressesInUse,
|
||||
scopeID,
|
||||
)
|
||||
}
|
||||
|
||||
if scope.AddressesInUseOnPartnerServer != -1 {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopeAddressesInUseOnPartnerServerTotal,
|
||||
prometheus.GaugeValue,
|
||||
scope.AddressesInUseOnPartnerServer,
|
||||
scopeID,
|
||||
)
|
||||
}
|
||||
|
||||
if scope.AddressesInUseOnThisServer != -1 {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopeAddressesInUseOnThisServerTotal,
|
||||
prometheus.GaugeValue,
|
||||
scope.AddressesInUseOnThisServer,
|
||||
scopeID,
|
||||
)
|
||||
}
|
||||
|
||||
if scope.PendingOffers != -1 {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopePendingOffersTotal,
|
||||
prometheus.GaugeValue,
|
||||
scope.PendingOffers,
|
||||
scopeID,
|
||||
)
|
||||
}
|
||||
|
||||
if scope.ReservedAddress != -1 {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.scopeReservedAddressTotal,
|
||||
prometheus.GaugeValue,
|
||||
scope.ReservedAddress,
|
||||
scopeID,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -391,7 +391,7 @@ func (c *Collector) collectNICAddresses(ch chan<- prometheus.Metric) error {
|
||||
return err
|
||||
}
|
||||
|
||||
convertNicName := strings.NewReplacer("(", "[", ")", "]")
|
||||
convertNicName := strings.NewReplacer("(", "[", ")", "]", "#", "_")
|
||||
|
||||
for _, nicAdapterAddress := range nicAdapterAddresses {
|
||||
friendlyName := windows.UTF16PtrToString(nicAdapterAddress.FriendlyName)
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
@@ -143,8 +144,26 @@ func New(config *Config) *Collector {
|
||||
return c
|
||||
}
|
||||
|
||||
func NewWithFlags(_ *kingpin.Application) *Collector {
|
||||
return &Collector{}
|
||||
func NewWithFlags(app *kingpin.Application) *Collector {
|
||||
c := &Collector{
|
||||
config: ConfigDefaults,
|
||||
}
|
||||
c.config.CollectorsEnabled = make([]string, 0)
|
||||
|
||||
var collectorsEnabled string
|
||||
|
||||
app.Flag(
|
||||
"collector.netframework.enabled",
|
||||
"Comma-separated list of collectors to use. Defaults to all, if not specified.",
|
||||
).Default(strings.Join(ConfigDefaults.CollectorsEnabled, ",")).StringVar(&collectorsEnabled)
|
||||
|
||||
app.Action(func(*kingpin.ParseContext) error {
|
||||
c.config.CollectorsEnabled = strings.Split(collectorsEnabled, ",")
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Collector) GetName() string {
|
||||
|
||||
@@ -63,7 +63,7 @@ type Win32_PerfRawData_NETFramework_NETCLRExceptions struct {
|
||||
|
||||
func (c *Collector) collectClrExceptions(ch chan<- prometheus.Metric) error {
|
||||
var dst []Win32_PerfRawData_NETFramework_NETCLRExceptions
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * Win32_PerfRawData_NETFramework_NETCLRExceptions"))); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * FROM Win32_PerfRawData_NETFramework_NETCLRExceptions"))); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ type Win32_PerfRawData_NETFramework_NETCLRInterop struct {
|
||||
|
||||
func (c *Collector) collectClrInterop(ch chan<- prometheus.Metric) error {
|
||||
var dst []Win32_PerfRawData_NETFramework_NETCLRInterop
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * Win32_PerfRawData_NETFramework_NETCLRInterop"))); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * FROM Win32_PerfRawData_NETFramework_NETCLRInterop"))); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ type Win32_PerfRawData_NETFramework_NETCLRJit struct {
|
||||
|
||||
func (c *Collector) collectClrJIT(ch chan<- prometheus.Metric) error {
|
||||
var dst []Win32_PerfRawData_NETFramework_NETCLRJit
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * Win32_PerfRawData_NETFramework_NETCLRJit"))); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * FROM Win32_PerfRawData_NETFramework_NETCLRJit"))); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ type Win32_PerfRawData_NETFramework_NETCLRLoading struct {
|
||||
|
||||
func (c *Collector) collectClrLoading(ch chan<- prometheus.Metric) error {
|
||||
var dst []Win32_PerfRawData_NETFramework_NETCLRLoading
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * Win32_PerfRawData_NETFramework_NETCLRLoading"))); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * FROM Win32_PerfRawData_NETFramework_NETCLRLoading"))); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ type Win32_PerfRawData_NETFramework_NETCLRLocksAndThreads struct {
|
||||
|
||||
func (c *Collector) collectClrLocksAndThreads(ch chan<- prometheus.Metric) error {
|
||||
var dst []Win32_PerfRawData_NETFramework_NETCLRLocksAndThreads
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * Win32_PerfRawData_NETFramework_NETCLRLocksAndThreads"))); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * FROM Win32_PerfRawData_NETFramework_NETCLRLocksAndThreads"))); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ type Win32_PerfRawData_NETFramework_NETCLRMemory struct {
|
||||
|
||||
func (c *Collector) collectClrMemory(ch chan<- prometheus.Metric) error {
|
||||
var dst []Win32_PerfRawData_NETFramework_NETCLRMemory
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * Win32_PerfRawData_NETFramework_NETCLRMemory"))); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * FROM Win32_PerfRawData_NETFramework_NETCLRMemory"))); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ type Win32_PerfRawData_NETFramework_NETCLRRemoting struct {
|
||||
|
||||
func (c *Collector) collectClrRemoting(ch chan<- prometheus.Metric) error {
|
||||
var dst []Win32_PerfRawData_NETFramework_NETCLRRemoting
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * Win32_PerfRawData_NETFramework_NETCLRRemoting"))); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * FROM Win32_PerfRawData_NETFramework_NETCLRRemoting"))); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ type Win32_PerfRawData_NETFramework_NETCLRSecurity struct {
|
||||
|
||||
func (c *Collector) collectClrSecurity(ch chan<- prometheus.Metric) error {
|
||||
var dst []Win32_PerfRawData_NETFramework_NETCLRSecurity
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * Win32_PerfRawData_NETFramework_NETCLRSecurity"))); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, utils.Must(mi.NewQuery("SELECT * FROM Win32_PerfRawData_NETFramework_NETCLRSecurity"))); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -28,5 +28,7 @@ func BenchmarkCollector(b *testing.B) {
|
||||
}
|
||||
|
||||
func TestCollector(t *testing.T) {
|
||||
t.Skip("Skipping test as it requires WMI data")
|
||||
|
||||
testutils.TestCollector(t, netframework.New, nil)
|
||||
}
|
||||
|
||||
@@ -76,6 +76,8 @@ type Collector struct {
|
||||
poolBytes *prometheus.Desc
|
||||
priorityBase *prometheus.Desc
|
||||
privateBytes *prometheus.Desc
|
||||
// Deprecated: Use start_time_seconds_timestamp instead
|
||||
startTimeOld *prometheus.Desc
|
||||
startTime *prometheus.Desc
|
||||
threadCount *prometheus.Desc
|
||||
virtualBytes *prometheus.Desc
|
||||
@@ -214,8 +216,15 @@ func (c *Collector) Build(logger *slog.Logger, miSession *mi.Session) error {
|
||||
nil,
|
||||
)
|
||||
|
||||
c.startTime = prometheus.NewDesc(
|
||||
c.startTimeOld = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "start_time"),
|
||||
"DEPRECATED: Use start_time_seconds_timestamp instead",
|
||||
[]string{"process", "process_id"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.startTime = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "start_time_seconds_timestamp"),
|
||||
"Time of process start.",
|
||||
[]string{"process", "process_id"},
|
||||
nil,
|
||||
|
||||
@@ -141,6 +141,13 @@ func (c *Collector) collectWorkerV1() {
|
||||
name, pidString,
|
||||
)
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.startTimeOld,
|
||||
prometheus.GaugeValue,
|
||||
data.ElapsedTime,
|
||||
name, pidString,
|
||||
)
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.handleCount,
|
||||
prometheus.GaugeValue,
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/pdh"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
@@ -134,10 +135,19 @@ func (c *Collector) collectWorkerV2() {
|
||||
name, pidString, parentPID, strconv.Itoa(int(processGroupID)), processOwner, cmdLine,
|
||||
)
|
||||
|
||||
startTime := float64(time.Now().Unix() - int64(data.ElapsedTime))
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.startTimeOld,
|
||||
prometheus.GaugeValue,
|
||||
startTime,
|
||||
name, pidString,
|
||||
)
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.startTime,
|
||||
prometheus.GaugeValue,
|
||||
data.ElapsedTime,
|
||||
startTime,
|
||||
name, pidString,
|
||||
)
|
||||
|
||||
|
||||
@@ -64,7 +64,8 @@ type Collector struct {
|
||||
// ref: https://victoriametrics.com/blog/go-sync-pool/
|
||||
serviceConfigPoolBytes sync.Pool
|
||||
|
||||
serviceManagerHandle *mgr.Mgr
|
||||
serviceManagerHandle *mgr.Mgr
|
||||
queryAllServicesBuffer []byte
|
||||
}
|
||||
|
||||
func New(config *Config) *Collector {
|
||||
@@ -140,6 +141,8 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
|
||||
},
|
||||
}
|
||||
|
||||
c.queryAllServicesBuffer = make([]byte, 1024*100)
|
||||
|
||||
c.info = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "info"),
|
||||
"A metric with a constant '1' value labeled with service information",
|
||||
@@ -209,7 +212,7 @@ func (c *Collector) Close() error {
|
||||
func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
services, err := c.queryAllServices()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to query services: %w", err)
|
||||
return fmt.Errorf("failed to query all services: %w", err)
|
||||
}
|
||||
|
||||
servicesCh := make(chan windows.ENUM_SERVICE_STATUS_PROCESS, len(services))
|
||||
@@ -368,16 +371,16 @@ func (c *Collector) queryAllServices() ([]windows.ENUM_SERVICE_STATUS_PROCESS, e
|
||||
err error
|
||||
)
|
||||
|
||||
buf := make([]byte, 1024*100)
|
||||
|
||||
for {
|
||||
currentBufferSize := uint32(cap(c.queryAllServicesBuffer))
|
||||
|
||||
err = windows.EnumServicesStatusEx(
|
||||
c.serviceManagerHandle.Handle,
|
||||
windows.SC_STATUS_PROCESS_INFO,
|
||||
windows.SERVICE_WIN32,
|
||||
windows.SERVICE_STATE_ALL,
|
||||
&buf[0],
|
||||
uint32(len(buf)),
|
||||
&c.queryAllServicesBuffer[0],
|
||||
currentBufferSize,
|
||||
&bytesNeeded,
|
||||
&servicesReturned,
|
||||
nil,
|
||||
@@ -392,18 +395,18 @@ func (c *Collector) queryAllServices() ([]windows.ENUM_SERVICE_STATUS_PROCESS, e
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if bytesNeeded <= uint32(len(buf)) {
|
||||
return nil, err
|
||||
if bytesNeeded <= currentBufferSize {
|
||||
return nil, fmt.Errorf("windows.EnumServicesStatusEx reports buffer too small (%d), but buffer is large enough (%d)", currentBufferSize, bytesNeeded)
|
||||
}
|
||||
|
||||
buf = make([]byte, bytesNeeded)
|
||||
c.queryAllServicesBuffer = make([]byte, bytesNeeded)
|
||||
}
|
||||
|
||||
if servicesReturned == 0 {
|
||||
return []windows.ENUM_SERVICE_STATUS_PROCESS{}, nil
|
||||
}
|
||||
|
||||
services := unsafe.Slice((*windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0])), int(servicesReturned))
|
||||
services := unsafe.Slice((*windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&c.queryAllServicesBuffer[0])), int(servicesReturned))
|
||||
|
||||
return services, nil
|
||||
}
|
||||
|
||||
@@ -42,10 +42,14 @@ type Resolver struct {
|
||||
func NewResolver(ctx context.Context, file string, logger *slog.Logger, insecureSkipVerify bool) (*Resolver, error) {
|
||||
flags := map[string]string{}
|
||||
|
||||
var fileBytes []byte
|
||||
var (
|
||||
err error
|
||||
fileBytes []byte
|
||||
)
|
||||
|
||||
var err error
|
||||
if strings.HasPrefix(file, "http://") || strings.HasPrefix(file, "https://") {
|
||||
logger.WarnContext(ctx, "Loading configuration file from URL is deprecated and will be removed in 0.31.0. Use a local file instead.")
|
||||
|
||||
fileBytes, err = readFromURL(ctx, file, logger, insecureSkipVerify)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
214
internal/headers/dhcpsapi/dhcpsapi.go
Normal file
214
internal/headers/dhcpsapi/dhcpsapi.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package dhcpsapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
modDhcpServer = windows.NewLazySystemDLL("dhcpsapi.dll")
|
||||
procDhcpGetSubnetInfo = modDhcpServer.NewProc("DhcpGetSubnetInfo")
|
||||
procDhcpGetSuperScopeInfoV4 = modDhcpServer.NewProc("DhcpGetSuperScopeInfoV4")
|
||||
procDhcpRpcFreeMemory = modDhcpServer.NewProc("DhcpRpcFreeMemory")
|
||||
procDhcpV4EnumSubnetReservations = modDhcpServer.NewProc("DhcpV4EnumSubnetReservations")
|
||||
procDhcpV4FailoverGetScopeStatistics = modDhcpServer.NewProc("DhcpV4FailoverGetScopeStatistics")
|
||||
procDhcpGetMibInfoV5 = modDhcpServer.NewProc("DhcpGetMibInfoV5")
|
||||
)
|
||||
|
||||
func GetDHCPV4ScopeStatistics() ([]DHCPV4Scope, error) {
|
||||
var mibInfo *DHCP_MIB_INFO_V5
|
||||
|
||||
if err := dhcpGetMibInfoV5(&mibInfo); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer dhcpRpcFreeMemory(unsafe.Pointer(mibInfo))
|
||||
|
||||
subnetScopeInfos := make(map[DHCP_IP_ADDRESS]DHCP_SUBNET_MIB_INFO_V5, mibInfo.Scopes)
|
||||
subnetMIBScopeInfos := unsafe.Slice(mibInfo.ScopeInfo, mibInfo.Scopes)
|
||||
|
||||
for _, subnetMIBScopeInfo := range subnetMIBScopeInfos {
|
||||
subnetScopeInfos[subnetMIBScopeInfo.Subnet] = subnetMIBScopeInfo
|
||||
}
|
||||
|
||||
var superScopeTable *DHCP_SUPER_SCOPE_TABLE
|
||||
|
||||
if err := dhcpGetSuperScopeInfoV4(&superScopeTable); err != nil {
|
||||
return nil, err
|
||||
} else if superScopeTable == nil {
|
||||
return nil, errors.New("dhcpGetSuperScopeInfoV4 returned nil")
|
||||
}
|
||||
|
||||
defer dhcpRpcFreeMemory(unsafe.Pointer(superScopeTable))
|
||||
|
||||
scopes := make([]DHCPV4Scope, 0, superScopeTable.Count)
|
||||
subnets := unsafe.Slice(superScopeTable.Entries, superScopeTable.Count)
|
||||
|
||||
var errs []error
|
||||
|
||||
for _, subnet := range subnets {
|
||||
if err := (func() error {
|
||||
var subnetInfo *DHCP_SUBNET_INFO
|
||||
err := dhcpGetSubnetInfo(subnet.SubnetAddress, &subnetInfo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get subnet info: %w", err)
|
||||
}
|
||||
|
||||
defer dhcpRpcFreeMemory(unsafe.Pointer(subnetInfo))
|
||||
|
||||
scope := DHCPV4Scope{
|
||||
Name: subnetInfo.SubnetName.String(),
|
||||
SuperScopeName: subnet.SuperScopeName.String(),
|
||||
ScopeIPAddress: net.IPNet{IP: subnetInfo.SubnetAddress.IPv4(), Mask: subnetInfo.SubnetMask.IPv4Mask()},
|
||||
SuperScopeNumber: subnet.SuperScopeNumber,
|
||||
State: subnetInfo.SubnetState,
|
||||
|
||||
AddressesFree: -1,
|
||||
AddressesFreeOnPartnerServer: -1,
|
||||
AddressesFreeOnThisServer: -1,
|
||||
AddressesInUse: -1,
|
||||
AddressesInUseOnPartnerServer: -1,
|
||||
AddressesInUseOnThisServer: -1,
|
||||
PendingOffers: -1,
|
||||
ReservedAddress: -1,
|
||||
}
|
||||
|
||||
if subnetScopeInfo, ok := subnetScopeInfos[subnetInfo.SubnetAddress]; ok {
|
||||
scope.AddressesInUse = float64(subnetScopeInfo.NumAddressesInUse)
|
||||
scope.AddressesFree = float64(subnetScopeInfo.NumAddressesFree)
|
||||
scope.PendingOffers = float64(subnetScopeInfo.NumPendingOffers)
|
||||
}
|
||||
|
||||
subnetReservationCount, err := dhcpV4EnumSubnetReservations(subnet.SubnetAddress)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get subnet reservation count: %w", err)
|
||||
} else {
|
||||
scope.ReservedAddress = float64(subnetReservationCount)
|
||||
}
|
||||
|
||||
var subnetStatistics *DHCP_FAILOVER_STATISTICS
|
||||
err = dhcpV4FailoverGetScopeStatistics(subnet.SubnetAddress, &subnetStatistics)
|
||||
|
||||
defer dhcpRpcFreeMemory(unsafe.Pointer(subnetStatistics))
|
||||
|
||||
if err == nil {
|
||||
scope.AddressesFree = float64(subnetStatistics.AddrFree)
|
||||
scope.AddressesInUse = float64(subnetStatistics.AddrInUse)
|
||||
scope.AddressesFreeOnPartnerServer = float64(subnetStatistics.PartnerAddrFree)
|
||||
scope.AddressesInUseOnPartnerServer = float64(subnetStatistics.PartnerAddrInUse)
|
||||
scope.AddressesFreeOnThisServer = float64(subnetStatistics.ThisAddrFree)
|
||||
scope.AddressesInUseOnThisServer = float64(subnetStatistics.ThisAddrInUse)
|
||||
} else if !errors.Is(err, ERROR_DHCP_FO_SCOPE_NOT_IN_RELATIONSHIP) {
|
||||
return fmt.Errorf("failed to get subnet statistics: %w", err)
|
||||
}
|
||||
|
||||
scopes = append(scopes, scope)
|
||||
|
||||
return nil
|
||||
})(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
return scopes, errors.Join(errs...)
|
||||
}
|
||||
|
||||
// dhcpGetSubnetInfo https://learn.microsoft.com/en-us/windows/win32/api/dhcpsapi/nf-dhcpsapi-dhcpgetsubnetinfo
|
||||
func dhcpGetSubnetInfo(subnetAddress DHCP_IP_ADDRESS, subnetInfo **DHCP_SUBNET_INFO) error {
|
||||
ret, _, _ := procDhcpGetSubnetInfo.Call(
|
||||
0,
|
||||
uintptr(subnetAddress),
|
||||
uintptr(unsafe.Pointer(subnetInfo)),
|
||||
)
|
||||
|
||||
if ret != 0 {
|
||||
return fmt.Errorf("dhcpGetSubnetInfo failed with code %w", windows.Errno(ret))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// dhcpV4FailoverGetScopeStatistics https://learn.microsoft.com/en-us/windows/win32/api/dhcpsapi/nf-dhcpsapi-dhcpv4failovergetscopestatistics
|
||||
func dhcpV4FailoverGetScopeStatistics(scopeId DHCP_IP_ADDRESS, stats **DHCP_FAILOVER_STATISTICS) error {
|
||||
ret, _, _ := procDhcpV4FailoverGetScopeStatistics.Call(
|
||||
0,
|
||||
uintptr(scopeId),
|
||||
uintptr(unsafe.Pointer(stats)),
|
||||
)
|
||||
|
||||
if ret != 0 {
|
||||
return fmt.Errorf("dhcpV4FailoverGetScopeStatistics failed with code %w", windows.Errno(ret))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// dhcpGetSuperScopeInfoV4 https://learn.microsoft.com/en-us/windows/win32/api/dhcpsapi/nf-dhcpsapi-dhcpgetsuperscopeinfov4
|
||||
func dhcpGetSuperScopeInfoV4(superScopeTable **DHCP_SUPER_SCOPE_TABLE) error {
|
||||
ret, _, _ := procDhcpGetSuperScopeInfoV4.Call(
|
||||
0,
|
||||
uintptr(unsafe.Pointer(superScopeTable)),
|
||||
)
|
||||
|
||||
if ret != 0 {
|
||||
return fmt.Errorf("dhcpGetSuperScopeInfoV4 failed with code %w", windows.Errno(ret))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// dhcpGetMibInfoV5 https://learn.microsoft.com/en-us/windows/win32/api/dhcpsapi/nf-dhcpsapi-dhcpgetmibinfov5
|
||||
func dhcpGetMibInfoV5(mibInfo **DHCP_MIB_INFO_V5) error {
|
||||
ret, _, _ := procDhcpGetMibInfoV5.Call(
|
||||
0,
|
||||
uintptr(unsafe.Pointer(mibInfo)),
|
||||
)
|
||||
|
||||
if ret != 0 {
|
||||
return fmt.Errorf("dhcpGetMibInfoV5 failed with code %w", windows.Errno(ret))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// dhcpV4EnumSubnetReservations https://learn.microsoft.com/en-us/windows/win32/api/dhcpsapi/nf-dhcpsapi-dhcpv4enumsubnetreservations
|
||||
func dhcpV4EnumSubnetReservations(subnetAddress DHCP_IP_ADDRESS) (uint32, error) {
|
||||
var (
|
||||
elementsRead uint32
|
||||
elementsTotal uint32
|
||||
elementsInfo uintptr
|
||||
resumeHandle *windows.Handle
|
||||
)
|
||||
|
||||
ret, _, _ := procDhcpV4EnumSubnetReservations.Call(
|
||||
0,
|
||||
uintptr(subnetAddress),
|
||||
uintptr(unsafe.Pointer(&resumeHandle)),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&elementsInfo)),
|
||||
uintptr(unsafe.Pointer(&elementsRead)),
|
||||
uintptr(unsafe.Pointer(&elementsTotal)),
|
||||
)
|
||||
|
||||
dhcpRpcFreeMemory(unsafe.Pointer(elementsInfo))
|
||||
|
||||
if !errors.Is(windows.Errno(ret), windows.ERROR_MORE_DATA) && !errors.Is(windows.Errno(ret), windows.ERROR_NO_MORE_ITEMS) {
|
||||
return 0, fmt.Errorf("dhcpV4EnumSubnetReservations failed with code %w", windows.Errno(ret))
|
||||
}
|
||||
|
||||
return elementsRead + elementsTotal, nil
|
||||
}
|
||||
|
||||
func dhcpRpcFreeMemory(pointer unsafe.Pointer) {
|
||||
if uintptr(pointer) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
//nolint:dogsled
|
||||
_, _, _ = procDhcpRpcFreeMemory.Call(uintptr(pointer))
|
||||
}
|
||||
24
internal/headers/dhcpsapi/dhcpsapi_test.go
Normal file
24
internal/headers/dhcpsapi/dhcpsapi_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package dhcpsapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func TestGetDHCPV4ScopeStatistics(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if procDhcpGetSuperScopeInfoV4.Find() != nil {
|
||||
t.Skip("DhcpGetSuperScopeInfoV4 is not available")
|
||||
}
|
||||
|
||||
_, err := GetDHCPV4ScopeStatistics()
|
||||
if errors.Is(err, windows.Errno(1753)) {
|
||||
t.Skip(err.Error())
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
}
|
||||
137
internal/headers/dhcpsapi/types.go
Normal file
137
internal/headers/dhcpsapi/types.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package dhcpsapi
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"net"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/headers/win32api"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var ERROR_DHCP_FO_SCOPE_NOT_IN_RELATIONSHIP = windows.Errno(20116)
|
||||
|
||||
type DHCPV4Scope struct {
|
||||
Name string
|
||||
State DHCP_SUBNET_STATE
|
||||
SuperScopeName string
|
||||
SuperScopeNumber uint32
|
||||
ScopeIPAddress net.IPNet
|
||||
|
||||
AddressesFree float64
|
||||
AddressesFreeOnPartnerServer float64
|
||||
AddressesFreeOnThisServer float64
|
||||
AddressesInUse float64
|
||||
AddressesInUseOnPartnerServer float64
|
||||
AddressesInUseOnThisServer float64
|
||||
PendingOffers float64
|
||||
ReservedAddress float64
|
||||
}
|
||||
|
||||
type (
|
||||
DHCP_IP_ADDRESS win32api.DWORD
|
||||
DHCP_IP_MASK win32api.DWORD
|
||||
)
|
||||
|
||||
func (ip DHCP_IP_ADDRESS) IPv4() net.IP {
|
||||
ipBytes := make([]byte, 4)
|
||||
|
||||
binary.BigEndian.PutUint32(ipBytes, uint32(ip))
|
||||
|
||||
return ipBytes
|
||||
}
|
||||
|
||||
func (ip DHCP_IP_MASK) IPv4Mask() net.IPMask {
|
||||
ipBytes := make([]byte, 4)
|
||||
|
||||
binary.BigEndian.PutUint32(ipBytes, uint32(ip))
|
||||
|
||||
return ipBytes
|
||||
}
|
||||
|
||||
type DHCP_SUPER_SCOPE_TABLE struct {
|
||||
Count win32api.DWORD
|
||||
Entries *DHCP_SUPER_SCOPE_TABLE_ENTRY
|
||||
}
|
||||
|
||||
type DHCP_SUPER_SCOPE_TABLE_ENTRY struct {
|
||||
SubnetAddress DHCP_IP_ADDRESS
|
||||
SuperScopeNumber win32api.DWORD
|
||||
NextInSuperScope win32api.DWORD
|
||||
SuperScopeName win32api.LPWSTR
|
||||
}
|
||||
|
||||
// DHCP_SUBNET_INFO https://learn.microsoft.com/de-de/windows/win32/api/dhcpsapi/ns-dhcpsapi-dhcp_subnet_info
|
||||
type DHCP_SUBNET_INFO struct {
|
||||
SubnetAddress DHCP_IP_ADDRESS
|
||||
SubnetMask DHCP_IP_MASK
|
||||
SubnetName win32api.LPWSTR
|
||||
SubnetComment win32api.LPWSTR
|
||||
PrimaryHost DHCP_HOST_INFO
|
||||
SubnetState DHCP_SUBNET_STATE
|
||||
}
|
||||
|
||||
type DHCP_HOST_INFO struct {
|
||||
IpAddress DHCP_IP_ADDRESS
|
||||
NetBiosName win32api.LPWSTR
|
||||
HostName win32api.LPWSTR
|
||||
}
|
||||
|
||||
// DHCP_SUBNET_STATE https://learn.microsoft.com/de-de/windows/win32/api/dhcpsapi/ne-dhcpsapi-dhcp_subnet_state
|
||||
type DHCP_SUBNET_STATE uint32
|
||||
|
||||
const (
|
||||
DhcpSubnetEnabled DHCP_SUBNET_STATE = 0
|
||||
DhcpSubnetDisabled DHCP_SUBNET_STATE = 1
|
||||
DhcpSubnetEnabledSwitched DHCP_SUBNET_STATE = 2
|
||||
DhcpSubnetDisabledSwitched DHCP_SUBNET_STATE = 3
|
||||
DhcpSubnetInvalidState DHCP_SUBNET_STATE = 4
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var DHCP_SUBNET_STATE_NAMES = map[DHCP_SUBNET_STATE]string{
|
||||
DhcpSubnetEnabled: "Enabled",
|
||||
DhcpSubnetDisabled: "Disabled",
|
||||
DhcpSubnetEnabledSwitched: "EnabledSwitched",
|
||||
DhcpSubnetDisabledSwitched: "DisabledSwitched",
|
||||
DhcpSubnetInvalidState: "InvalidState",
|
||||
}
|
||||
|
||||
type DHCP_FAILOVER_STATISTICS struct {
|
||||
NumAddr win32api.DWORD
|
||||
AddrFree win32api.DWORD
|
||||
AddrInUse win32api.DWORD
|
||||
PartnerAddrFree win32api.DWORD
|
||||
ThisAddrFree win32api.DWORD
|
||||
PartnerAddrInUse win32api.DWORD
|
||||
ThisAddrInUse win32api.DWORD
|
||||
}
|
||||
|
||||
type DHCP_MIB_INFO_V5 struct {
|
||||
Discovers win32api.DWORD
|
||||
Offers win32api.DWORD
|
||||
Requests win32api.DWORD
|
||||
Acks win32api.DWORD
|
||||
Naks win32api.DWORD
|
||||
Declines win32api.DWORD
|
||||
Releases win32api.DWORD
|
||||
ServerStartTime win32api.DATE_TIME
|
||||
QtnNumLeases win32api.DWORD
|
||||
QtnPctQtnLeases win32api.DWORD
|
||||
QtnProbationLeases win32api.DWORD
|
||||
QtnNonQtnLeases win32api.DWORD
|
||||
QtnExemptLeases win32api.DWORD
|
||||
QtnCapableClients win32api.DWORD
|
||||
QtnIASErrors win32api.DWORD
|
||||
DelayedOffers win32api.DWORD
|
||||
ScopesWithDelayedOffers win32api.DWORD
|
||||
Scopes win32api.DWORD
|
||||
ScopeInfo *DHCP_SUBNET_MIB_INFO_V5
|
||||
}
|
||||
|
||||
type DHCP_SUBNET_MIB_INFO_V5 struct {
|
||||
Subnet DHCP_IP_ADDRESS
|
||||
NumAddressesInUse win32api.DWORD
|
||||
NumAddressesFree win32api.DWORD
|
||||
NumPendingOffers win32api.DWORD
|
||||
}
|
||||
15
internal/headers/win32api/types.go
Normal file
15
internal/headers/win32api/types.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package win32api
|
||||
|
||||
import "golang.org/x/sys/windows"
|
||||
|
||||
type (
|
||||
DATE_TIME = windows.Filetime
|
||||
DWORD = uint32
|
||||
LPWSTR struct {
|
||||
*uint16
|
||||
}
|
||||
)
|
||||
|
||||
func (s LPWSTR) String() string {
|
||||
return windows.UTF16PtrToString(s.uint16)
|
||||
}
|
||||
@@ -34,6 +34,9 @@ const (
|
||||
// Interface guard.
|
||||
var _ io.Writer = (*Writer)(nil)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var EmptyStringUTF16 uint16
|
||||
|
||||
type Writer struct {
|
||||
handle windows.Handle
|
||||
}
|
||||
@@ -60,7 +63,7 @@ func (w *Writer) Write(p []byte) (int, error) {
|
||||
return 0, fmt.Errorf("error convert string to UTF-16: %w", err)
|
||||
}
|
||||
|
||||
ss := []*uint16{msg, nil, nil, nil, nil, nil, nil, nil, nil}
|
||||
ss := []*uint16{msg, &EmptyStringUTF16, &EmptyStringUTF16, &EmptyStringUTF16, &EmptyStringUTF16, &EmptyStringUTF16, &EmptyStringUTF16, &EmptyStringUTF16, &EmptyStringUTF16}
|
||||
|
||||
return len(p), windows.ReportEvent(w.handle, eType, 0, NeLogOemCode, 0, 9, 0, &ss[0], nil)
|
||||
}
|
||||
|
||||
@@ -390,7 +390,7 @@ func (c *Collector) collectWorkerRaw() {
|
||||
case PERF_ELAPSED_TIME:
|
||||
dv.Index(index).
|
||||
Field(counter.FieldIndexValue).
|
||||
SetFloat(float64((item.RawValue.FirstValue - WindowsEpoch) / counter.Frequency))
|
||||
SetFloat(float64((item.RawValue.SecondValue - item.RawValue.FirstValue) / counter.Frequency))
|
||||
case PERF_100NSEC_TIMER, PERF_PRECISION_100NS_TIMER:
|
||||
dv.Index(index).
|
||||
Field(counter.FieldIndexValue).
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
// Copyright 2024 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build windows
|
||||
|
||||
// Package windowsservice allows initiating time-sensitive components like registering the Windows service
|
||||
// as early as possible in the startup process.
|
||||
// init functions are called in the order they are declared, so this package should be imported first.
|
||||
// Declare imports on this package should be avoided where possible.
|
||||
//
|
||||
// Ref: https://github.com/prometheus-community/windows_exporter/issues/551#issuecomment-1220774835
|
||||
|
||||
package windowsservice
|
||||
@@ -1,61 +0,0 @@
|
||||
// Copyright 2024 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package windowsservice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/svc"
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
// IsService is true if the exporter is running as a Windows service.
|
||||
IsService bool
|
||||
// ExitCodeCh is a channel to send the exit code return from the [github.com/prometheus-community/windows_exporter/cmd/windows_exporter] function to the service manager.
|
||||
ExitCodeCh = make(chan int)
|
||||
|
||||
// StopCh is a channel to send a signal to the service manager that the service is stopping.
|
||||
StopCh = make(chan struct{})
|
||||
)
|
||||
|
||||
//nolint:gochecknoinits // An init function is required to communicate with the Windows service manager early in the program.
|
||||
func init() {
|
||||
var err error
|
||||
|
||||
IsService, err = svc.IsWindowsService()
|
||||
if err != nil {
|
||||
if err := logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("failed to detect service: %v", err)); err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !IsService {
|
||||
return
|
||||
}
|
||||
|
||||
if err := logToEventToLog(windows.EVENTLOG_INFORMATION_TYPE, "attempting to start exporter service"); err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := svc.Run(serviceName, &windowsExporterService{}); err != nil {
|
||||
_ = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("failed to start service: %v", err))
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
// Copyright 2024 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build windows
|
||||
|
||||
package windowsservice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
wineventlog "github.com/prometheus-community/windows_exporter/internal/log/eventlog"
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/svc/eventlog"
|
||||
)
|
||||
|
||||
// logToEventToLog logs a message to the Windows event log.
|
||||
func logToEventToLog(eType uint16, msg string) error {
|
||||
eventLog, err := eventlog.Open("windows_exporter")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open event log: %w", err)
|
||||
}
|
||||
defer func(eventLog *eventlog.Log) {
|
||||
_ = eventLog.Close()
|
||||
}(eventLog)
|
||||
|
||||
p, err := windows.UTF16PtrFromString(msg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error convert string to UTF-16: %w", err)
|
||||
}
|
||||
|
||||
ss := []*uint16{p, nil, nil, nil, nil, nil, nil, nil, nil}
|
||||
|
||||
return windows.ReportEvent(eventLog.Handle, eType, 0, wineventlog.NeLogOemCode, 0, 9, 0, &ss[0], nil)
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
// Copyright 2024 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build windows
|
||||
|
||||
package windowsservice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/svc"
|
||||
)
|
||||
|
||||
const (
|
||||
serviceName = "windows_exporter"
|
||||
)
|
||||
|
||||
type windowsExporterService struct{}
|
||||
|
||||
// Execute is the entry point for the Windows service manager.
|
||||
func (s *windowsExporterService) Execute(_ []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
|
||||
changes <- svc.Status{State: svc.StartPending}
|
||||
changes <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown}
|
||||
|
||||
for {
|
||||
select {
|
||||
case exitCodeCh := <-ExitCodeCh:
|
||||
// Stop the service if an exit code from the main function is received.
|
||||
changes <- svc.Status{State: svc.StopPending}
|
||||
|
||||
return true, uint32(exitCodeCh)
|
||||
case c := <-r:
|
||||
// Handle the service control request.
|
||||
switch c.Cmd {
|
||||
case svc.Interrogate:
|
||||
changes <- c.CurrentStatus
|
||||
case svc.Stop, svc.Shutdown:
|
||||
// Stop the service if a stop or shutdown request is received.
|
||||
_ = logToEventToLog(windows.EVENTLOG_INFORMATION_TYPE, "service stop received")
|
||||
|
||||
changes <- svc.Status{State: svc.StopPending}
|
||||
|
||||
// Send a signal to the main function to stop the service.
|
||||
StopCh <- struct{}{}
|
||||
|
||||
// Wait for the main function to stop the service.
|
||||
return false, uint32(<-ExitCodeCh)
|
||||
default:
|
||||
_ = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("unexpected control request #%d", c))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user