Thursday, 30 July 2026

Filled under:

 #!/bin/bash


# Patroni/etcd fragmentation alert

# Reads only the latest 250 lines from the existing Patroni log.

# No state file or temporary report file is created.


PATRONI_LOG="/var/log/patroni/patroni.log"

MAIL_TO="postgres-support-dl@example.com"

MAIL_SUBJECT="WARNING: Patroni/etcd space alert on $(hostname -s)"


export ETCDCTL_API=3

export ETCDCTL_ENDPOINTS="https://etcd1:2379,https://etcd2:2379,https://etcd3:2379"

export ETCDCTL_CACERT="/path/to/ca.crt"

export ETCDCTL_CERT="/path/to/client.crt"

export ETCDCTL_KEY="/path/to/client.key"


# Match only etcd space/quota/fragmentation-related messages.

ERROR_PATTERN='NOSPACE|database space exceeded|mvcc: database space exceeded|etcd.*(space|quota|fragment|alarm)'


if [ ! -r "$PATRONI_LOG" ]; then

    echo "Cannot read Patroni log: $PATRONI_LOG" >&2

    exit 1

fi


# Read only the latest 250 lines from the existing Patroni log.

NEW_ERRORS=$(tail -n 250 "$PATRONI_LOG" | grep -Ei "$ERROR_PATTERN" || true)


# No matching message found.

if [ -z "$NEW_ERRORS" ]; then

    exit 0

fi


send_report() {

    {

        echo "Patroni/etcd space-related warning detected"

        echo "Host: $(hostname -f)"

        echo "Time: $(date)"

        echo

        echo "===== Matching entries from last 250 Patroni log lines ====="

        printf '%s\n' "$NEW_ERRORS"

        echo

        echo "===== etcd endpoint status ====="

        echo "Compare DB SIZE with SIZE IN USE to identify fragmentation."

        etcdctl endpoint status --cluster -w table 2>&1

        echo

        echo "===== etcd active alarms ====="

        etcdctl alarm list 2>&1

    }

}


if command -v mailx >/dev/null 2>&1; then

    send_report | mailx -s "$MAIL_SUBJECT" "$MAIL_TO"

elif command -v mail >/dev/null 2>&1; then

    send_report | mail -s "$MAIL_SUBJECT" "$MAIL_TO"

else

    echo "Neither mailx nor mail is installed." >&2

    send_report

    exit 1

fi


0 comments:

Post a Comment