Skip to content

JannesStroehlein/PrometheusPeriodicCommands

Repository files navigation

Prometheus Periodic Commands

Create and publish a Docker image Release

This tiny Rust program periodically runs commands that are specified in a config file and parses the output of the commands using a RegEx. The output will be exposed as a Prometheus metric.

Example Use-Case

I use a storage box as a backup storage, but my backup system cannot tell me how much storage is available. My solution is to use this tool to periodically connect to the storage box using SSH to run the du command to find out how much storage is available.

With this tool, I can create AlertManager rules to warn me if the storage box comes close to being out of storage.

Features

  • 🧷 Multithreaded
  • 🔒 Thread Safe
  • 🎨 Colored Console Log
  • 💾 YAML Config
  • 🖥️ CLI Arguments

Installation

This tool can be installed manually or using Docker.

Manual Installation

  1. Clone the Git repository and cd into the cloned directory
git clone https://github.com/JannesStroehlein/PrometheusPeriodicCommands.git
cd PrometheusPeriodicCommands
  1. Build and install using cargo
cargo install
  1. Run the tool

Note

If the tool does not find a config file, it will exit.

./PrometheusPeriodicCommands

Docker Installation

Important

You most likely need to create your own Dockerfile to use this tool. The included Dockerfile is only for building the tool with minimal dependencies. That means the shell commands that are executed inside the container are very limited by the installed software.

You can use the Dockerfile as a starting point to create your own image with the necessary dependencies.

  1. Pull the image
docker pull ghcr.io/jannesstroehlein/prometheusperiodiccommands:main
  1. Create and run a container with the image
docker run -d \
  -p 8080:8080 \
  -v ${PWD}/config.yaml:/app/config.yaml:ro \
  --restart always \
  ghcr.io/jannesstroehlein/prometheusperiodiccommands:main \
  --host 127.0.0.1 --port 8080

Tip

Use the pre-made docker compose file as your starting point.

The Prometheus metrics are now exposed under: host:port/metrics

Configuration

A YAML file is used to configure the tool. If no config file path is specified using the --config-file <path> parameter, some paths are checked if they contain a config.yaml file.

OS specific config paths
OS Paths
Linux ~/.config/prometheus_periodic_commands/config.yaml, /etc/prometheus_periodic_commands/config.yaml
Windows %LocalAppData%/prometheus_periodic_commands/config.yaml

Config file format

# The host to bind to
host: 0.0.0.0
# The port of the webserver
port: 8080

# A list of all commands to execute and parse
targets:
  # The name of the target. This will be part of the metric name
  - name: echo
    # A list of shell commands and additional labels
    commands:
      # The shell command to execute
      - exec: echo '42'
        # Additional labels for the metric
        labels:
          label1: value1
          # You can use named groups from the RegEx to template the label value
          label2: "Wrapped result {result}"

    # Specify the RegEx to parse the standard output of the command
    # The regex must include at least one named group (which needs to be specified below)
    # which is used to extract a numeric value from the command output
    regex: '(?<result>.*)'
    # The name of the group that contains the numeric result (the result can also be a float)
    regex_named_group: result
    # A list of exit codes that mark the execution as successful.
    # If the child process exits with an exit code not in this list,
    # the execution will be marked as faulted.
    success_exit_codes: [ 0 ]
    # Set the interval in which the command should be executed
    # You can use any format supported by https://docs.rs/duration-string/latest/duration_string/
    # Examples: 2h, 5h10m3s etc.
    run_every: 5s

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

This tool uses these crates and other dependencies.

About

Run shell commands periodically and expose their output for Prometheus

Resources

License

Stars

Watchers

Forks

Packages