File: //proc/self/root/lib64/nagios/plugins/a2_postgresql-monitor.shared
#!/bin/bash
# Script to monitor PostGresSQL service and verify that postgres user can authenticate and connect to PostgreSQL
# SYSENG-25206
#Checking if PostgreSQL is running
if systemctl is-active --quiet postgresql; then
pgpass_file="/var/lib/pgsql/.pgpass"
root_pgpass_file="/root/.pgpass"
#Fix .pgpass
_fix_pgpass_file() {
if [[ -f "$root_pgpass_file" ]]; then
/usr/bin/cp -f ${root_pgpass_file} ${pgpass_file}
/usr/bin/chmod 600 ${pgpass_file}
/usr/bin/chown postgres.postgres ${pgpass_file}
else
echo "PostgreSQL is missing /root/.pgpass file. Please check."
exit 2
fi
}
#Function to check if postgres user can authenticate
_check_auth_postgresql() {
pg_voutput=$(su - postgres -c 'psql -wc "SELECT version();"' 2>&1)
if echo "${pg_voutput}" | grep -q PostgreSQL; then
pg_Ver=$(echo "${pg_voutput}"| awk '/PostgreSQL/ {print $2}')
echo "PostgreSQL ${pg_Ver} is up and postgres user can authenticate."
exit 0
else
echo "postgres user cannot authenticate to PostgreSQL."
exit 0
fi
}
if ! cmp -s "$root_pgpass_file" "$pgpass_file"; then
_fix_pgpass_file
fi
_check_auth_postgresql
else
echo "PostgreSQL is not running. Please check."
exit 2
fi