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

psql: show unknown if database connection fails

parent 2636b1d4
No related branches found
No related tags found
1 merge request!90psql: show unknown if database connection fails
......@@ -14,6 +14,7 @@
# https://www.postgresql.org/docs/current/monitoring-stats.html
# ----------------------------------------------------------------------
# 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=" "
# 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
function _uninstall(){
echo UNINSTALL ...
su - postgres -c "psql -c \"DROP USER ${myuser};\""
echo "UNINSTALL ..."
su - postgres -c "psql -c \"DROP USER ${myuser};\"" 2>&1
unset PGHOST
unset PGUSER
unset PGPASSWORD
......@@ -47,7 +58,7 @@ function _uninstall(){
# (re)install database user for monitoring
function _install(){
echo INSTALLING ...
echo "INSTALLING ..."
local pwlength=64
echo "- check psql connection..."
su postgres -c "psql -V postgres"
......@@ -56,20 +67,18 @@ function _install(){
exit 1
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 )
SQL1="CREATE USER ${myuser} WITH PASSWORD '${mypw}' CONNECTION LIMIT 5";
SQL2="GRANT pg_monitor TO ${myuser};"
# SQL2="GRANT MONITOR_QUERIES TO ${myuser};"
su - postgres -c "psql -c \"${SQL1}\""
if [ $? -ne 0 ]; then
if ! su - postgres -c "psql -c \"${SQL1}\" 2>&1"; then
echo "ERROR: psql command to create user failed."
exit 1
fi
echo "- grant ..."
su - postgres -c "psql -c \"${SQL2}\""
if [ $? -ne 0 ]; then
if ! su - postgres -c "psql -c \"${SQL2}\" 2>&1" ; then
echo "ERROR: psql command to grant permissions failed."
# exit 1
fi
......@@ -86,8 +95,7 @@ export PGHOST=localhost
# set default database because a user db won't be created
export PGDATABASE=postgres
EOF
ls -l $cfgfile
if [ $? -ne 0 ]; then
if ! ls -l $cfgfile; then
echo "ERROR: creation of config file failed."
exit 1
fi
......@@ -116,8 +124,8 @@ OPTIONS:
PARAMETERS:
-m method; valid methods are:
activity running processes and queries
dbrows Count of database row actions
conflicts Detected conflicts from pg_stat_database_conflicts
dbrows Count of database row actions
diskblock Count of diskblocks physically read or coming from cache
problems Problems and troublemakers
replication Replication status (table output only)
......@@ -135,7 +143,7 @@ EOH
# param string database query
function renderCounters(){
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 _header; _header=$( echo "${_out}" | head -1 | tr -d ' ')
local _data; _data=$( echo "${_out}" | head -3 | tail -1 | tr -d ' ')
......@@ -200,8 +208,7 @@ fi
# --- check installation
grep $myuser $cfgfile >/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
if ! grep $myuser $cfgfile >/dev/null 2>&1; then
ph.abort "PSQL access not possible yet. You need to install the monitoring user first: as root execute `basename $0` -i"
fi
......@@ -213,7 +220,7 @@ sMode=$(ph.getValueWithParam '' m "$@")
case "${sMode}" in
"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 iQActive; iQActive=$( awk '{ print $5 }' <<< "$_out" | grep -c "active" )
......@@ -260,7 +267,7 @@ case "${sMode}" in
from pg_stat_database "
;;
"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
descr="No data in pg_stat_replication - this is no slave."
out=""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment