#!/bin/bash # Postfix queue depth textfile metric — runs every 5 min via root crontab. TEXTFILE="/var/lib/node_exporter/textfile/postfix_queue.prom" HOSTNAME=$(hostname -s) TMPFILE=$(mktemp /tmp/postfix_queue.XXXXXX.prom) trap 'rm -f "$TMPFILE"' EXIT QUEUE_SIZE=0 if command -v mailq >/dev/null 2>&1; then # grep -c exits 1 on no match even for count=0; handle separately QUEUE_SIZE=$(mailq 2>/dev/null | grep -c '^[0-9A-F]' 2>/dev/null) || QUEUE_SIZE=0 fi cat > "$TMPFILE" << EOF # HELP postfix_queue_size Number of messages in postfix mail queue # TYPE postfix_queue_size gauge postfix_queue_size{host="${HOSTNAME}"} ${QUEUE_SIZE} # HELP postfix_queue_check_last_run_seconds Unix timestamp of last queue check # TYPE postfix_queue_check_last_run_seconds gauge postfix_queue_check_last_run_seconds $(date +%s) EOF mkdir -p "$(dirname "$TEXTFILE")" mv "$TMPFILE" "$TEXTFILE" chmod 644 "$TEXTFILE" trap - EXIT exit 0