HEX
Server: LiteSpeed
System: Linux s3604.bom1.stableserver.net 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64
User: dmstechonline (1480)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //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