Skip to content

Commit

Permalink
Merge pull request #14 from natrontech/feat/6-support-multiple-endpoints
Browse files Browse the repository at this point in the history
feat: add possibility for multiple targets with one exporter
  • Loading branch information
janfuhrer authored Apr 11, 2024
2 parents 00ab739 + 617f6bd commit 30386fa
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 20 deletions.
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,26 @@ $ ./pbs-exporter -help

You can use the following flags to configure the exporter. All flags can also be set using environment variables. Environment variables take precedence over flags.

| Flag | Environment Variable | Description | Default |
| ------------------------ | -------------------- | ---------------------------------------------------- | ----------------------- |
| `pbs.loglevl` | `PBS_LOGLEVEL` | Log level (debug, info) | `info` |
| `pbs.api.token` | `PBS_API_TOKEN` | API token to use for authentication | |
| `pbs.api.token.name` | `PBS_API_TOKEN_NAME` | Name of the API token to use for authentication | `pbs-exporter` |
| `pbs.endpoint` | `PBS_ENDPOINT` | Address of the Proxmox Backup Server | `http://localhost:8007` |
| `pbs.username` | `PBS_USERNAME` | Username to use for authentication | `root@pam` |
| `pbs.timeout` | `PBS_TIMEOUT` | Timeout for requests to Proxmox Backup Server | `5s` |
| `pbs.insecure` | `PBS_INSECURE` | Disable TLS certificate verification | `false` |
| `pbs.metrics-path` | `PBS_METRICS_PATH` | Path under which to expose metrics | `/metrics` |
| `pbs.web.listen-address` | `PBS_LISTEN_ADDRESS` | Address to listen on for web interface and telemetry | `:9101` |
| Flag | Environment Variable | Description | Default |
| ------------------------ | -------------------- | ---------------------------------------------------- | ------------------------------------------------------ |
| `pbs.loglevl` | `PBS_LOGLEVEL` | Log level (debug, info) | `info` |
| `pbs.api.token` | `PBS_API_TOKEN` | API token to use for authentication | |
| `pbs.api.token.name` | `PBS_API_TOKEN_NAME` | Name of the API token to use for authentication | `pbs-exporter` |
| `pbs.endpoint` | `PBS_ENDPOINT` | Address of the Proxmox Backup Server | `http://localhost:8007` (if no parameter `target` set) |
| `pbs.username` | `PBS_USERNAME` | Username to use for authentication | `root@pam` |
| `pbs.timeout` | `PBS_TIMEOUT` | Timeout for requests to Proxmox Backup Server | `5s` |
| `pbs.insecure` | `PBS_INSECURE` | Disable TLS certificate verification | `false` |
| `pbs.metrics-path` | `PBS_METRICS_PATH` | Path under which to expose metrics | `/metrics` |
| `pbs.web.listen-address` | `PBS_LISTEN_ADDRESS` | Address to listen on for web interface and telemetry | `:9101` |

## Multiple Proxmox Backup Servers

If you want to monitor multiple Proxmox Backup Servers, you can use the `targets` parameter in the query string. Instead of setting the `pbs.endpoint` flag (or `PBS_ENDPOINT` env), you can use the `target` parameter in the query string to specify the Proxmox Backup Server to monitor. You would then use following URL to scrape metrics: `http://localhost:9101/metrics?target=http://10.10.10.10:8007`.

This is useful if you are using Prometheus and want to monitor multiple Proxmox Backup Servers with one "pbs-exporter" instance.
You find examples for Prometheus static configuration in the [prometheus/static-config](prometheus/static-config) directory.

:warning: **Important**: if `pbs.endpoint` or `PBS_ENDPOINT` is set, the `target` parameter is ignored.

## Node metrics

Expand Down
36 changes: 30 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
}

// Flags
endpoint = flag.String("pbs.endpoint", "http://localhost:8007",
endpoint = flag.String("pbs.endpoint", "",
"Proxmox Backup Server endpoint")
username = flag.String("pbs.username", "root@pam",
"Proxmox Backup Server username")
Expand Down Expand Up @@ -710,15 +710,39 @@ func main() {
log.Printf("DEBUG: Using listen address: %s", *listenAddress)
}

// register exporter
exporter := NewExporter(*endpoint, *username, *apitoken, *apitokenname)
prometheus.MustRegister(exporter)
log.Printf("INFO: Using connection endpoint: %s", *endpoint)
if *endpoint != "" {
log.Printf("INFO: Using fix connection endpoint: %s", *endpoint)
}
log.Printf("INFO: Listening on: %s", *listenAddress)
log.Printf("INFO: Metrics path: %s", *metricsPath)

// start http server
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc(*metricsPath, func(w http.ResponseWriter, r *http.Request) {
target := ""

// if endpoint was not set as flag or env variable, we try to get it from "target" query parameter
if *endpoint != "" {
target = *endpoint
} else {
target = r.URL.Query().Get("target")
if target == "" {
// if target is not set, we use the default
target = "http://localhost:8007"
}
}

// debug
if *loglevel == "debug" {
log.Printf("DEBUG: ----Using connection endpoint %s", target)
}

exporter := NewExporter(target, *username, *apitoken, *apitokenname)
prometheus.MustRegister(exporter)
promhttp.Handler().ServeHTTP(w, r) // Serve the metrics
prometheus.Unregister(exporter) // Clean up after serving

})

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>PBS Exporter</title></head>
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions prometheus/static-config/multiple-exporter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Use Case: multiple Proxmox Backup Server, they have different authentication user/token
# configure multiple authentication for each exporter instance
# important: set env PBS_ENDPOINT per exporter instance!
#
# Setup:
# - first exporter instance: pbs-exporter-1
# - second exporter instance: pbs-exporter-2

- job_name: 'pbs-exporter'
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
static_configs:
- targets:
- 'pbs-exporter-1:9101' # PBS_ENDPOINT set to target 1
- 'pbs-exporter-2:9101' # PBS_ENDPOINT set to target 2
22 changes: 22 additions & 0 deletions prometheus/static-config/multiple-targets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use Case: multiple Proxmox Backup Server, all have the same authentication user/token
#
# important: env PBS_ENDPOINT NOT SET!
#
scrape_configs:
- job_name: 'pbs-exporter'
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
static_configs:
- targets:
- 'https://10.10.10.10:8007' # Proxmox Backup Server 1
- 'https://10.10.10.11:8007' # Proxmox Backup Server 2
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: pbs-exporter:9101 # pbs-exporter address
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Use Case: single Proxmox Backup Server
#
# Setup:
# - exporter instance: pbs-exporter:9101
# - PBS target set in env PBS_ENDPOINT

scrape_configs:
- job_name: 'pbs-exporter'
honor_timestamps: true
Expand All @@ -7,9 +13,10 @@ scrape_configs:
scheme: http
static_configs:
- targets:
- 'pbs-exporter:9101'
- 'pbs-exporter:9101' # PBS_ENDPOINT set to target


# example with relablings to have the host name (e.g. host-001) as a metric label
# example with relablings to have the host name (e.g. host-001) as metric label "instance"
- job_name: 'pbs-exporter-relabel'
honor_timestamps: true
scrape_interval: 15s
Expand All @@ -18,7 +25,7 @@ scrape_configs:
scheme: http
static_configs:
- targets:
- 'pbs-exporter@host-001'
- 'pbs-exporter@host-001' # PBS_ENDPOINT set to target
relabel_configs:
- source_labels: [ __address__ ]
regex: '.*@(.*)'
Expand Down

0 comments on commit 30386fa

Please sign in to comment.