nut_exporter

repository·master·Indexed 43 days ago

https://github.com/druggeri/nut_exporter

A Prometheus exporter for Network UPS Tools (NUT) that allows administrators to monitor UPS devices and their variables as Prometheus metrics. It supports scraping multiple UPS devices and NUT servers via query string parameters and provides specialized handling for the ups.status variable to convert status flags into individual Prometheus labels.

Tokens
4.3K
Snippets
8
Records
13
Agent score
6%

What's inside nut_exporter

  1. How `ups.status` handling works

    master

    The ups.status variable is a special case. NUT returns this as a string containing multiple status flags (e.g., OL TRIM CHRG). The exporter converts these flags into individual labels on a single metric: network_ups_tools_ups_status.

    Each active flag is exported with a value of 1. Flags that are not present in the current status are exported with a value of 0 (based on the flags enabled via the --nut.statuses flag).

    Example Transformation: Input: OL TRIM CHRG Output:

    network_ups_tools_ups_status{flag="OL"} 1
    network_ups_tools_ups_status{flag="TRIM"} 1
    network_ups_tools_ups_status{flag="CHRG"} 1
    network_ups_tools_ups_status{flag="OB"} 0
    ... (other flags)

    Alerting Examples:

    • Alert if the UPS is no longer online: network_ups_tools_ups_status{flag="OL"} == 0
    • Alert if the UPS is on battery: network_ups_tools_ups_status{flag="OB"} == 1
    • Alert on any status change in 5 minutes: changes(network_ups_tools_ups_status[5m]) > 0
    network_ups_tools_ups_status{flag="OL"} 1
  2. How the NUT Prometheus Exporter works

    master

    The nut_exporter is a Prometheus exporter for the Network UPS Tools (NUT) server. It uses the go.nut library to act as a network client for the NUT platform.

    Key behaviors:

    • Scraping Scope: A single instance can scrape one or many UPS devices and one or many NUT servers.
    • Metric Naming: NUT variables are exported as Prometheus metrics with the ups name added as a label. For example, the NUT variable ups.load becomes the metric network_ups_tools_ups_load.
    • Single UPS Constraint: The exporter scrapes exactly one UPS per scrape invocation. If your NUT server manages multiple UPS devices, you must specify the target UPS in the query string (e.g., ?ups=foo). If the exporter detects multiple UPS devices without a specific ups parameter being set, the scrape will fail.
    • Data Types:
      • Numeric values are exported as their corresponding Go types.
      • Boolean values are coerced to 0 (false) or 1 (true).
      • String values from NUT are not natively supported by Prometheus; you can use the on_regex and off_regex parameters to convert specific strings to 0 or 1.
  3. Understand the exported NUT metrics

    master

    The exporter provides two primary types of metrics for the NUT device:

    1. Device Information: network_ups_tools_device_info provides metadata about the UPS device.
    2. UPS Variables: network_ups_tools_<VARIABLE_NAME> exports specific variables requested via --nut.vars_enable. These metrics include ups and variable labels.

    Example metric format: network_ups_tools_ups_variable{ups="myups", variable="battery.charge"} 100

  4. Install the nut-exporter Helm chart

    master

    To install the nut-exporter into a Kubernetes cluster using Helm, add the repository and run the install command.

    helm repo add nut-exporter https://DRuggeri.github.io/nut_exporter
    helm install nut-exporter/nut-exporter nut-exporter
  5. Install the NUT Prometheus Exporter

    master

    Using Binaries

    Download the binaries for your platform from the GitHub releases page and run:

    ./nut_exporter <flags>

    Using Go (from source)

    If you have Go installed, you can install directly using go install:

    go install github.com/DRuggeri/nut_exporter/v3@latest
    nut_exporter <flags>

    Using Docker

    Official scratch-based images are available on DockerHub and GHCR:

    • druggeri/nut_exporter
    • ghcr.io/DRuggeri/nut_exporter
    go install github.com/DRuggeri/nut_exporter/v3@latest
  6. Scrape specific UPS instances via query parameters

    master

    The nut_exporter supports dynamic target selection via HTTP query parameters on the UPS metrics path (default: /ups_metrics). This allows a single exporter instance to serve multiple UPS devices by specifying connection details in the request URL.

    Supported query parameters:

    • ups: The name of the UPS device.
    • server: Override the default NUT server hostname.
    • serverport: Override the default NUT server port.
    • username: Override the username.
    • password: Override the password.
    • variables: A comma-separated list of variables to export.
    • statuses: A comma-separated list of statuses to include.

    Example Scrape URL: http://<exporter-host>:<exporter-port>/ups_metrics?ups=myups&server=192.168.1.50

  7. Configure Prometheus to scrape multiple NUT servers

    master

    You can use query string parameters to point a single exporter instance at different NUT servers.

    Example: Scrape different servers via one exporter

      - job_name: nut-primary
        metrics_path: /ups_metrics
        static_configs:
          - targets: ['exporterserver:9199']
            labels:
              ups:  "primary"
        params:
          ups: [ "primary" ]
          server: [ "nutserver1" ]
      - job_name: nut-secondary
        metrics_path: /ups_metrics
        static_configs:
          - targets: ['exporterserver:9199']
            labels:
              ups:  "secondary"
        params:
          ups: [ "secondary" ]
          server: [ "nutserver2" ]
    - job_name: nut-primary
        metrics_path: /ups_metrics
        static_configs:
          - targets: ['exporterserver:9199']
            labels:
              ups:  "primary"
        params:
          ups: [ "primary" ]
          server: [ "nutserver1" ]
      - job_name: nut-secondary
        metrics_path: /ups_metrics
        static_configs:
          - targets: ['exporterserver:9199']
            labels:
              ups:  "secondary"
        params:
          ups: [ "secondary" ]
          server: [ "nutserver2" ]
  8. Configure Prometheus scrape jobs for multiple UPS devices

    master

    Because the exporter only scrapes one UPS per request, you must define separate scrape jobs (or use params) for each UPS device in your prometheus.yml.

    Example: Scaping multiple UPS devices on the same NUT server

      - job_name: nut-primary
        metrics_path: /ups_metrics
        static_configs:
          - targets: ['myserver:9199']
            labels:
              ups:  "primary"
        params:
          ups: [ "primary" ]
      - job_name: nut-secondary
        metrics_path: /ups_metrics
        static_configs:
          - targets: ['myserver:9199']
            labels:
              ups:  "secondary"
        params:
          ups: [ "secondary" ]
    - job_name: nut-primary
        metrics_path: /ups_metrics
        static_configs:
          - targets: ['myserver:9199']
            labels:
              ups:  "primary"
        params:
          ups: [ "primary" ]
      - job_name: nut-secondary
        metrics_path: /ups_metrics
        static_configs:
          - targets: ['myserver:9199']
            labels:
              ups:  "secondary"
        params:
          ups: [ "secondary" ]
  9. Query String Parameters for `/ups_metrics`

    master

    You can override command-line flags and environment variables on a per-scrape basis by passing parameters to the /ups_metrics endpoint. This is essential for scraping multiple UPS devices or multiple NUT servers from a single exporter instance.

    ParameterDescription
    upsRequired if more than one UPS is present in NUT. Specifies the UPS to scrape.
    serverOverrides the --nut.server flag.
    usernameOverrides the --nut.username flag.
    passwordOverrides the NUT_EXPORTER_PASSWORD environment variable. Warning: Avoid using this over unencrypted HTTP.
    variablesOverrides the --nut.vars_enable flag. Setting to an empty string exports all numeric variables.
    statusesOverrides the --nut.statuses flag.
  10. Configure nut_exporter via CLI flags

    master

    The nut_exporter can be configured using several command-line flags. Many of these flags can also be set via environment variables.

    Connection Settings

    • --nut.server: Hostname or IP address of the NUT server. (Env: $NUT_EXPORTER_SERVER)
    • --nut.serverport: Port on the NUT server. (Env: $NUT_EXPORTER_SERVERPORT)
    • --nut.username: Username for NUT authentication. (Env: $NUT_EXPORTER_USERNAME). Note: The password must be provided via the $NUT_EXPORTER_PASSWORD environment variable.

    Metric Customization

    • --nut.vars_enable: A comma-separated list of NUT variable names to monitor. (Env: $NUT_EXPORTER_VARIABLES)
    • --nut.disable_device_info: Flag to disable the device_info meta metric. (Env: $NUT_EXPORTER_DISABLE_DEVICE_INFO)
    • --nut.on_regex: Regex to determine if a string value should be coerced to 1. (Env: $NUT_EXPORTER_ON_REGEX)
    • --nut.off_regex: Regex to determine if a string value should be coerced to 0. (Env: $NUT_EXPORTER_OFF_REGEX)
    • --nut.statuses: Comma-separated list of status labels to always include. If NUT doesn't set a flag, the exporter forces the corresponding network_ups_tools_ups_status{flag="NAME"} to 0. (Env: $NUT_EXPORTER_STATUSES)
    • --metrics.namespace: The Prometheus metrics namespace. (Env: $NUT_EXPORTER_METRICS_NAMESPACE)

    Web and Logging

    • --web.listen-address: Address to expose metrics (e.g., :9199). Repeatable for multiple addresses.
    • --web.telemetry-path: Path for UPS metrics (default: /ups_metrics). (Env: $NUT_EXPORTER_WEB_TELEMETRY_PATH)
    • --web.exporter-telemetry-path: Path for exporter process metrics (default: /metrics). (Env: $NUT_EXPORTER_WEB_EXPORTER_TELEMETRY_PATH)
    • --log.level: Minimum log level (error, warn, info, debug). (Env: $NUT_EXPORTER_LOG_LEVEL)
    • --log.json: Format log lines as JSON. (Env: $NUT_EXPORTER_LOG_JSON)
    • --printMetrics: Print metrics to stdout and exit. (Env: $NUT_EXPORTER_PRINT_METRICS)
    usage: nut_exporter [<flags>]
    
    Flags:
      -h, --[no-]help                Show context-sensitive help
          --nut.server="127.0.0.1"   Hostname or IP address of the server to connect to. ($NUT_EXPORTER_SERVER)
          --nut.serverport=3493      Port on the NUT server to connect to. ($NUT_EXPORTER_SERVERPORT)
          --nut.username=NUT.USERNAME  If set, will authenticate with this username to the server. Password must be set in NUT_EXPORTER_PASSWORD environment variable. ($NUT_EXPORTER_USERNAME)
          --[no-]nut.disable_device_info  A flag to disable the generation of the device_info meta metric. ($NUT_EXPORTER_DISABLE_DEVICE_INFO)
          --nut.vars_enable="battery.charge,battery.voltage,battery.voltage.nominal,input.voltage,input.voltage.nominal,ups.load,ups.status"  A comma-separated list of variable names to monitor. ($NUT_EXPORTER_VARIABLES)
          --nut.on_regex="^(enable|enabled|on|true|active|activated)$"  Regex to determine if the var's value should be coaxed to 1 if it is a string. ($NUT_EXPORTER_ON_REGEX)
          --nut.off_regex="^(disable|disabled|off|false|inactive|deactivated)$"  Regex to determine if the var's value should be coaxed to 0 if it is a string. ($NUT_EXPORTER_OFF_REGEX)
          --nut.statuses="OL,OB,LB,HB,RB,CHRG,DISCHRG,BYPASS,CAL,OFF,OVER,TRIM,BOOST,FSD,SD"  A comma-separated list of statuses labels that will always be set by the exporter. ($NUT_EXPORTER_STATUSES)
          --metrics.namespace="network_ups_tools"  Metrics Namespace ($NUT_EXPORTER_METRICS_NAMESPACE)
          --[no-]web.systemd-socket  Use systemd socket activation listeners (Linux only).
          --web.listen-address=:9199 ...  Addresses on which to expose metrics. Repeatable.
          --web.config.file=""       Path to configuration file for TLS/auth.
          --web.telemetry-path="/ups_metrics"  Path under which to expose the UPS Prometheus metrics ($NUT_EXPORTER_WEB_TELEMETRY_PATH)
          --web.exporter-telemetry-path="/metrics"  Path under which to expose process metrics ($NUT_EXPORTER_WEB_EXPORTER_TELEMETRY_PATH)
          --[no-]printMetrics        Print the metrics this exporter exposes and exits. ($NUT_EXPORTER_PRINT_METRICS)
          --log.level="info"         Minimum log level. (error, warn, info, debug) ($NUT_EXPORTER_LOG_LEVEL)
          --[no-]log.json            Format log lines as JSON. ($NUT_EXPORTER_LOG_JSON)
          --[no-]version             Show application version.
  11. Print exported metrics to stdout

    master

    To inspect the metrics that the exporter is capable of producing without running the full HTTP server, use the --printMetrics flag. This will output the metric names and their help text to the console and then exit.

    ./nut_exporter --printMetrics