60 lines
No EOL
1.9 KiB
Django/Jinja
60 lines
No EOL
1.9 KiB
Django/Jinja
#!/bin/bash
|
|
# PostgreSQL health check script
|
|
|
|
set -euo pipefail
|
|
|
|
# Check if PostgreSQL is running
|
|
if ! systemctl is-active --quiet postgresql; then
|
|
echo "PostgreSQL service is not running"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if we can connect to PostgreSQL
|
|
if ! pg_isready -h localhost -p 5432 -U postgres -q; then
|
|
echo "Cannot connect to PostgreSQL"
|
|
exit 1
|
|
fi
|
|
|
|
# Check database connection
|
|
if ! psql -h localhost -p 5432 -U postgres -d aprsme_prod -c "SELECT 1" > /dev/null 2>&1; then
|
|
echo "Cannot connect to aprsme_prod database"
|
|
exit 1
|
|
fi
|
|
|
|
# Check PgBouncer
|
|
if ! pg_isready -h localhost -p 6432 -U postgres -q; then
|
|
echo "PgBouncer is not responding"
|
|
exit 1
|
|
fi
|
|
|
|
# Check replication lag (if applicable)
|
|
LAG=$(psql -h localhost -p 5432 -U postgres -t -c "SELECT EXTRACT(EPOCH FROM (NOW() - pg_last_xact_replay_timestamp()))::INT" 2>/dev/null || echo "0")
|
|
if [ "${LAG}" -gt "60" ]; then
|
|
echo "Replication lag is too high: ${LAG} seconds"
|
|
exit 1
|
|
fi
|
|
|
|
# Check disk space
|
|
DISK_USAGE=$(df -h /var/lib/postgresql | tail -1 | awk '{print $5}' | sed 's/%//')
|
|
if [ "${DISK_USAGE}" -gt "90" ]; then
|
|
echo "Disk usage is critical: ${DISK_USAGE}%"
|
|
exit 1
|
|
fi
|
|
|
|
# Check connection count
|
|
CONNECTIONS=$(psql -h localhost -p 5432 -U postgres -t -c "SELECT COUNT(*) FROM pg_stat_activity" 2>/dev/null || echo "0")
|
|
MAX_CONNECTIONS=$(psql -h localhost -p 5432 -U postgres -t -c "SHOW max_connections" 2>/dev/null || echo "200")
|
|
if [ "${CONNECTIONS}" -gt "$(( ${MAX_CONNECTIONS} * 90 / 100 ))" ]; then
|
|
echo "Connection count is high: ${CONNECTIONS}/${MAX_CONNECTIONS}"
|
|
exit 1
|
|
fi
|
|
|
|
# Check for long running queries
|
|
LONG_QUERIES=$(psql -h localhost -p 5432 -U postgres -t -c "SELECT COUNT(*) FROM pg_stat_activity WHERE state != 'idle' AND query_start < NOW() - INTERVAL '5 minutes'" 2>/dev/null || echo "0")
|
|
if [ "${LONG_QUERIES}" -gt "5" ]; then
|
|
echo "Too many long running queries: ${LONG_QUERIES}"
|
|
exit 1
|
|
fi
|
|
|
|
echo "PostgreSQL health check passed"
|
|
exit 0 |