Skip to content
Snippets Groups Projects
Commit f172f162 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch '6010-check-pqsql' into 'master'

psql: show unknown if database connection fails

See merge request !90
parents cba103f4 9e8afc51
No related branches found
No related tags found
1 merge request!90psql: show unknown if database connection fails
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
# https://www.postgresql.org/docs/current/monitoring-stats.html # https://www.postgresql.org/docs/current/monitoring-stats.html
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2023-06-07 v0.2 <axel.hahn@unibe.ch> # 2023-06-07 v0.2 <axel.hahn@unibe.ch>
# 2023-06-08 v0.3 <axel.hahn@unibe.ch> show unknown if database connection fails
# ====================================================================== # ======================================================================
...@@ -35,10 +36,20 @@ out=" " ...@@ -35,10 +36,20 @@ out=" "
# FUNCTIONS # FUNCTIONS
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# exit with status unknown if a database query failed
function _queryFailed(){
ph.setStatus "unknown"
echo "UNKNOWN: the database query failed"
echo "Quick troubleshooter:"
echo "* install a database user $myuser with command $(basename $0) -i"
echo "* check pg_hba.conf login fails if it set to ident (eg. change it to md5)"
ph.exit
}
# uninstall database user # uninstall database user
function _uninstall(){ function _uninstall(){
echo UNINSTALL ... echo "UNINSTALL ..."
su - postgres -c "psql -c \"DROP USER ${myuser};\"" su - postgres -c "psql -c \"DROP USER ${myuser};\"" 2>&1
unset PGHOST unset PGHOST
unset PGUSER unset PGUSER
unset PGPASSWORD unset PGPASSWORD
...@@ -47,7 +58,7 @@ function _uninstall(){ ...@@ -47,7 +58,7 @@ function _uninstall(){
# (re)install database user for monitoring # (re)install database user for monitoring
function _install(){ function _install(){
echo INSTALLING ... echo "INSTALLING ..."
local pwlength=64 local pwlength=64
echo "- check psql connection..." echo "- check psql connection..."
su postgres -c "psql -V postgres" su postgres -c "psql -V postgres"
...@@ -56,20 +67,18 @@ function _install(){ ...@@ -56,20 +67,18 @@ function _install(){
exit 1 exit 1
fi fi
echo "- creating mysql user $myuser@localhost with random password ($pwlength chars)..." echo "- creating database user $myuser with random password ($pwlength chars)..."
mypw=$( head /dev/urandom | tr -dc A-Za-z0-9 | head -c $pwlength ) mypw=$( head /dev/urandom | tr -dc A-Za-z0-9 | head -c $pwlength )
SQL1="CREATE USER ${myuser} WITH PASSWORD '${mypw}' CONNECTION LIMIT 5"; SQL1="CREATE USER ${myuser} WITH PASSWORD '${mypw}' CONNECTION LIMIT 5";
SQL2="GRANT pg_monitor TO ${myuser};" SQL2="GRANT pg_monitor TO ${myuser};"
# SQL2="GRANT MONITOR_QUERIES TO ${myuser};" # SQL2="GRANT MONITOR_QUERIES TO ${myuser};"
su - postgres -c "psql -c \"${SQL1}\"" if ! su - postgres -c "psql -c \"${SQL1}\" 2>&1"; then
if [ $? -ne 0 ]; then
echo "ERROR: psql command to create user failed." echo "ERROR: psql command to create user failed."
exit 1 exit 1
fi fi
echo "- grant ..." echo "- grant ..."
su - postgres -c "psql -c \"${SQL2}\"" if ! su - postgres -c "psql -c \"${SQL2}\" 2>&1" ; then
if [ $? -ne 0 ]; then
echo "ERROR: psql command to grant permissions failed." echo "ERROR: psql command to grant permissions failed."
# exit 1 # exit 1
fi fi
...@@ -86,8 +95,7 @@ export PGHOST=localhost ...@@ -86,8 +95,7 @@ export PGHOST=localhost
# set default database because a user db won't be created # set default database because a user db won't be created
export PGDATABASE=postgres export PGDATABASE=postgres
EOF EOF
ls -l $cfgfile if ! ls -l $cfgfile; then
if [ $? -ne 0 ]; then
echo "ERROR: creation of config file failed." echo "ERROR: creation of config file failed."
exit 1 exit 1
fi fi
...@@ -116,8 +124,8 @@ OPTIONS: ...@@ -116,8 +124,8 @@ OPTIONS:
PARAMETERS: PARAMETERS:
-m method; valid methods are: -m method; valid methods are:
activity running processes and queries activity running processes and queries
dbrows Count of database row actions
conflicts Detected conflicts from pg_stat_database_conflicts conflicts Detected conflicts from pg_stat_database_conflicts
dbrows Count of database row actions
diskblock Count of diskblocks physically read or coming from cache diskblock Count of diskblocks physically read or coming from cache
problems Problems and troublemakers problems Problems and troublemakers
replication Replication status (table output only) replication Replication status (table output only)
...@@ -135,7 +143,7 @@ EOH ...@@ -135,7 +143,7 @@ EOH
# param string database query # param string database query
function renderCounters(){ function renderCounters(){
local _query; _query="$1" local _query; _query="$1"
local _out; _out=$( psql -c "${_query}") local _out; _out=$( psql -c "${_query} 2>&1") || _queryFailed
local _iCounter; typeset -i _iCounter=0 local _iCounter; typeset -i _iCounter=0
local _header; _header=$( echo "${_out}" | head -1 | tr -d ' ') local _header; _header=$( echo "${_out}" | head -1 | tr -d ' ')
local _data; _data=$( echo "${_out}" | head -3 | tail -1 | tr -d ' ') local _data; _data=$( echo "${_out}" | head -3 | tail -1 | tr -d ' ')
...@@ -200,8 +208,7 @@ fi ...@@ -200,8 +208,7 @@ fi
# --- check installation # --- check installation
grep $myuser $cfgfile >/dev/null 2>/dev/null if ! grep $myuser $cfgfile >/dev/null 2>&1; then
if [ $? -ne 0 ]; then
ph.abort "PSQL access not possible yet. You need to install the monitoring user first: as root execute `basename $0` -i" ph.abort "PSQL access not possible yet. You need to install the monitoring user first: as root execute `basename $0` -i"
fi fi
...@@ -213,7 +220,7 @@ sMode=$(ph.getValueWithParam '' m "$@") ...@@ -213,7 +220,7 @@ sMode=$(ph.getValueWithParam '' m "$@")
case "${sMode}" in case "${sMode}" in
"activity") "activity")
_out=$( psql -c "select pid,usename,state,query,backend_type,backend_start from pg_stat_activity" ) _out=$( psql -c "select pid,usename,state,query,backend_type,backend_start from pg_stat_activity" ) || _queryFailed
typeset -i iQTotal; iQTotal=$( tail -1 <<< "$_out" | cut -f 1 -d ' ' | tr -d '(' ) typeset -i iQTotal; iQTotal=$( tail -1 <<< "$_out" | cut -f 1 -d ' ' | tr -d '(' )
typeset -i iQActive; iQActive=$( awk '{ print $5 }' <<< "$_out" | grep -c "active" ) typeset -i iQActive; iQActive=$( awk '{ print $5 }' <<< "$_out" | grep -c "active" )
...@@ -260,7 +267,7 @@ case "${sMode}" in ...@@ -260,7 +267,7 @@ case "${sMode}" in
from pg_stat_database " from pg_stat_database "
;; ;;
"replication") "replication")
_out=$( psql -c "select * from pg_stat_replication" ) _out=$( psql -c "select * from pg_stat_replication" ) || _queryFailed
if tail -1 <<< "$_out" | grep "(0 rows)" >/dev/null ; then if tail -1 <<< "$_out" | grep "(0 rows)" >/dev/null ; then
descr="No data in pg_stat_replication - this is no slave." descr="No data in pg_stat_replication - this is no slave."
out="" out=""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment