From 617f6bde5bad920a9ebdc8425f74c579c3c24736 Mon Sep 17 00:00:00 2001 From: Jan Fuhrer Date: Thu, 11 Apr 2024 20:08:25 +0200 Subject: [PATCH] feat: add possibility for multiple targets with one exporter --- README.md | 31 ++++++++++------ main.go | 36 +++++++++++++++---- prometheus/{ => rules}/example-rules.yaml | 0 .../static-config/multiple-exporter.yaml | 18 ++++++++++ .../static-config/multiple-targets.yaml | 22 ++++++++++++ .../single-target.yaml} | 13 +++++-- 6 files changed, 100 insertions(+), 20 deletions(-) rename prometheus/{ => rules}/example-rules.yaml (100%) create mode 100644 prometheus/static-config/multiple-exporter.yaml create mode 100644 prometheus/static-config/multiple-targets.yaml rename prometheus/{example-target.yaml => static-config/single-target.yaml} (70%) diff --git a/README.md b/README.md index 5677127..8ff8462 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index 1e6686d..5534764 100644 --- a/main.go +++ b/main.go @@ -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") @@ -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(` PBS Exporter diff --git a/prometheus/example-rules.yaml b/prometheus/rules/example-rules.yaml similarity index 100% rename from prometheus/example-rules.yaml rename to prometheus/rules/example-rules.yaml diff --git a/prometheus/static-config/multiple-exporter.yaml b/prometheus/static-config/multiple-exporter.yaml new file mode 100644 index 0000000..9f23f7c --- /dev/null +++ b/prometheus/static-config/multiple-exporter.yaml @@ -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 diff --git a/prometheus/static-config/multiple-targets.yaml b/prometheus/static-config/multiple-targets.yaml new file mode 100644 index 0000000..e01406a --- /dev/null +++ b/prometheus/static-config/multiple-targets.yaml @@ -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 diff --git a/prometheus/example-target.yaml b/prometheus/static-config/single-target.yaml similarity index 70% rename from prometheus/example-target.yaml rename to prometheus/static-config/single-target.yaml index e3b5634..10eac62 100644 --- a/prometheus/example-target.yaml +++ b/prometheus/static-config/single-target.yaml @@ -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 @@ -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 @@ -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: '.*@(.*)'