Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
icinga-checks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
icinga-checks
Commits
9e8afc51
Commit
9e8afc51
authored
2 years ago
by
Hahn Axel (hahn)
Browse files
Options
Downloads
Patches
Plain Diff
psql: show unknown if database connection fails
parent
2636b1d4
No related branches found
No related tags found
1 merge request
!90
psql: show unknown if database connection fails
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
check_psqlserver
+23
-16
23 additions, 16 deletions
check_psqlserver
with
23 additions
and
16 deletions
check_psqlserver
+
23
−
16
View file @
9e8afc51
...
...
@@ -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
=
""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment