A lightweight Prometheus exporter that runs shell commands at fixed intervals and exposes their numeric output as metrics.
- Executes arbitrary shell commands
- Parses
stdoutfor float values - Exposes metrics with
command,exit_code,stdout_summary,stderr_summarylabels - Logs full execution details
- Configurable interval and output summary truncation
Create a config.yaml file:
commands:
- name: process_count
command: ["bash", "-c", "ps aux | wc -l"]
interval: "10s"
stdout_limit: 50
- name: load_avg
command: ["bash", "-c", "cat /proc/loadavg | awk '{print $1}'"]
interval: "5s"
stdout_limit: 40go build -o command-exporter ./cmd/command-exporter
./command-exporterExporter listens by default on :9860.
Add this to your prometheus.yml:
scrape_configs:
- job_name: 'command-exporter'
static_configs:
- targets: ['localhost:9860']Sample:
# HELP command_result Result of command execution
# TYPE command_result gauge
command_result{command="load_avg", exit_code="0", stdout_summary="0.13", stderr_summary=""} 0.13
command_result{command="process_count", exit_code="0", stdout_summary="142", stderr_summary=""} 142
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o command-exporter ./cmd/command-exporter
FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/command-exporter .
COPY config.yaml .
EXPOSE 9860
ENTRYPOINT ["./command-exporter"]- Only the first float token from stdout is used as the metric value.
- If stdout cannot be parsed to float,
command_resultwill return1and log a warning.