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

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

6010 check pqsql

See merge request !85
parents 96c67987 77d0e434
No related branches found
No related tags found
1 merge request!856010 check pqsql
#!/bin/bash
# ======================================================================
#
# !!! WORK IN PROGRESS !!! DO NOT USE YET !!!
#
# Check MYSQL / MARIADB SERVER
#
# requirements:
......@@ -20,19 +18,17 @@
# 2020-08-xx v0.0 <axel.hahn@iml.unibe.ch>
# 2021-06-08 v0.1 <axel.hahn@iml.unibe.ch> update HOME .. better implement a param and ENV var
# 2021-06-24 v0.2 <axel.hahn@iml.unibe.ch> check if query cache is enabled as a shared function
# 2023-06-06 v0.3 <axel.hahn@unibe.ch> some shell fixes
# ======================================================================
. `dirname $0`/inc_pluginfunctions
_version="0.2"
. $( dirname $0 )/inc_pluginfunctions
self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=0.3
# --- set HOME
# HOME=/etc/icinga2-passive-client
HOME=/etc/icingaclient
# cd $( dirname $0)
# cd ..
# HOME=$( pwd ); export HOME
# cd - >/dev/null
# --- other vars...
cfgfile=$HOME/.my.cnf
......@@ -89,7 +85,7 @@ function _install(){
echo "- creating config file $cfgfile ... "
cat >$cfgfile <<EOF
#
# generated on `date`
# generated on $(date)
#
[client]
user=$myuser
......@@ -106,17 +102,18 @@ EOF
# show usage
function _usage(){
local _self=$( basename $0 )
cat <<EOH
______________________________________________________________________
CHECK MYSQL SERVER :: v${_version}
${self_APPNAME} :: v${self_APPVERSION}
(c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3
______________________________________________________________________
USAGE:
`basename $0` [OPTIONS] -m METHOD
$_self [OPTIONS] -m METHOD
OPTIONS:
-h this help
......@@ -133,8 +130,8 @@ PARAMETERS:
qcache-hits query cache efficiency: cached vs non cached and inserted
EXAMPLES:
`basename $0` -i
`basename $0` -m commands
$_self -i
$_self -m commands
EOH
}
......@@ -146,8 +143,8 @@ function _mysqlreadvars(){
function _mysqlgetvar() {
local sVarname=$1
lastvalue=`grep "^$sVarname[^_a-z]" ${datafile} | awk '{ print $2 }'`
echo $lastvalue
lastvalue=$(grep "^${sVarname}[^_a-z]" ${datafile} | awk '{ print $2 }')
echo "$lastvalue"
}
# get a value from mysql status output
# param string variable name
......@@ -160,20 +157,20 @@ function _mysqlrendervar() {
local sFloat=$4
# local iValue=`grep "^$sVarname[^_a-z]" ${datafile} | awk '{ print $2 }'`
local iValue=`_mysqlgetvar "$sVarname"`
local iValue=$(_mysqlgetvar "$sVarname")
if [ "$iValue" = "" ]; then
ph.abort "no value for ${sVarname}"
fi
# get label for perfdata
local sLabel=`echo ${sVarname} | sed "s#^${sRemove}##g"`
local sLabel=$(echo ${sVarname} | sed "s#^${sRemove}##g")
if [ ! -z $sDeltaUnit ]; then
local iSpeed=` ph.perfdeltaspeed "mysql-${sVarname}" ${iValue} $sDeltaUnit $sFloat`
out=$out`printf "%25s: %10s %s \n" "${sLabel}" "${iValue}" "... delta = ${iSpeed} per $sDeltaUnit${NL}"`
local iSpeed=$( ph.perfdeltaspeed "mysql-${sVarname}" ${iValue} $sDeltaUnit $sFloat)
out=$out$(printf "%25s: %10s %s \n" "${sLabel}" "${iValue}" "... delta = ${iSpeed} per $sDeltaUnit${NL}")
ph.perfadd "${sLabel}" "${iSpeed}"
lastvalue=$iSpeed
else
out=${out}`printf "%25s: %10s \n" "${sLabel}" "${iValue}${NL}"`
out=${out}$(printf "%25s: %10s \n" "${sLabel}" "${iValue}${NL}")
ph.perfadd "${sLabel}" "${iValue}"
lastvalue=$iValue
fi
......@@ -182,8 +179,8 @@ function _mysqlrendervar() {
function _mysqlrenderdelta() {
local deltaUnit=$3
test -z $deltaUnit && deltaUnit="sec"
_mysqlrendervar "$1" "$2" $deltaUnit $4
test -z "$deltaUnit" && deltaUnit="sec"
_mysqlrendervar "$1" "$2" "$deltaUnit" "$4"
}
# helper function for caching checks
......@@ -204,9 +201,9 @@ function _verify_cache(){
# MAIN
# ----------------------------------------------------------------------
bOptInstall=` ph.hasParamoption "i" "$@"`
bOptUninstall=` ph.hasParamoption "u" "$@"`
bOptHelp=` ph.hasParamoption "h" "$@"`
bOptInstall=$( ph.hasParamoption "i" "$@")
bOptUninstall=$( ph.hasParamoption "u" "$@")
bOptHelp=$( ph.hasParamoption "h" "$@")
if [ $bOptHelp -eq 1 -o $# -lt 1 ]; then
_usage
......@@ -247,7 +244,7 @@ fi
# --- check installation
grep $myuser $cfgfile >/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
ph.abort "MYSQL access not possible yet. You need to install the monitoring user first: as root execute `basename $0` -i"
ph.abort "MYSQL access not possible yet. You need to install the monitoring user first: as root execute $(basename $0) -i"
fi
# ----------------------------------------------------------------------
......@@ -260,10 +257,10 @@ if [ $? -ne 0 ]; then
fi
# --- set optional limits
typeset -i iWarnLimit=` ph.getValueWithParam 0 w "$@"`
typeset -i iCriticalLimit=` ph.getValueWithParam 0 c "$@"`
typeset -i iWarnLimit=$( ph.getValueWithParam 0 w "$@")
typeset -i iCriticalLimit=$( ph.getValueWithParam 0 c "$@")
sMode=`ph.getValueWithParam '' m "$@"`
sMode=$(ph.getValueWithParam '' m "$@")
case "${sMode}" in
......
#!/bin/bash
# ======================================================================
#
# !!! WORK IN PROGRESS !!! DO NOT USE YET !!!
#
# Check PSQL SERVER
#
# requirements:
# - psql client
#
# installation:
# - execute check_psqlserver -i
#
# ----------------------------------------------------------------------
# 2023-06-xx v0.0 <axel.hahn@unibe.ch>
# ======================================================================
. $(dirname $0)/inc_pluginfunctions
self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=0.1
# --- set HOME
HOME=/etc/icinga2-passive-client
# HOME=/etc/icingaclient
# --- other vars...
cfgfile=$HOME/.psql.conf
myuser=icingamonitor
# new line
NL="
"
lastvalue=
out=" "
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
# uninstall database user
function _uninstall(){
echo UNINSTALL ...
su - postgres -c "psql -c \"DROP USER ${myuser};\""
unset PGHOST
unset PGUSER
unset PGPASSWORD
}
# (re)install database user for monitoring
function _install(){
echo INSTALLING ...
local pwlength=64
echo "- check psql connection..."
su postgres -c "psql -V postgres"
if [ $? -ne 0 ]; then
echo "ERROR: psql connect without password failed."
exit 1
fi
echo "- creating mysql user $myuser@localhost 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
echo "ERROR: psql command to create user failed."
exit 1
fi
echo "- grant ..."
su - postgres -c "psql -c \"${SQL2}\""
if [ $? -ne 0 ]; then
echo "ERROR: psql command to grant permissions failed."
# exit 1
fi
echo "- creating config file $cfgfile ... "
cat >$cfgfile <<EOF
#
# generated on $(date)
#
export PGUSER=${myuser}
export PGPASSWORD=${mypw}
export PGHOST=localhost
export PGDATABASE=postgres
EOF
ls -l $cfgfile
if [ $? -ne 0 ]; then
echo "ERROR: creation of config file failed."
exit 1
fi
}
function _usage(){
local _self=$( basename $0 )
cat <<EOH
______________________________________________________________________
${self_APPNAME} :: v${self_APPVERSION}
THIS IS AN PRE ALPHA VERSION - DO NOT USE THIS
(c) Institute for Medical Education - Univerity of Bern
Licence: GNU GPL 3
______________________________________________________________________
USAGE:
$_self [OPTIONS] -m METHOD
OPTIONS:
-h this help
-i install monitoring user (must be executed as root)
-u uninstall monitoring user (must be executed as root)
PARAMETERS:
-m method; valid methods are:
dbstat first implmenetation test
EXAMPLES:
$_self -i
$_self -m commands
EOH
}
function renderCounts(){
local _query="$1"
local _out=$( psql -c "${_query}")
# out+=$( echo "DEBUG: _query=${_query}${NL}" )
# echo "DEBUG: _out="; echo "${_out}"
local _iCounter=0
typeset -i _iCounter
local _header
_header=$( echo "${_out}" | head -1 | tr -d ' ')
local _data
_data=$( echo "${_out}" | head -3 | tail -1 | tr -d ' ')
# echo "DEBUG: _header=${_header}"
IFS="|"
read -ra aCols <<< "$_header"
read -ra aVals <<< "$_data"
for sColumn in ${aCols[*]}
do
local value=${aVals[$_iCounter]}
# echo "DEBUG: $sColumn = $value"
out+=$( printf "%25s: %10s \n" "${sColumn}" "${value}${NL}")
ph.perfadd "${sColumn}" "${value}"
_iCounter+=1
done
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
bOptInstall=$( ph.hasParamoption "i" "$@")
bOptUninstall=$( ph.hasParamoption "u" "$@")
bOptHelp=$( ph.hasParamoption "h" "$@")
if [ $bOptHelp -eq 1 -o $# -lt 1 ]; then
_usage
exit 0
fi
# --- check required tools
# ph.require mysql
# --- install
if [ $bOptInstall -eq 1 -a "$( whoami )" = "root" ]; then
if [ -f $cfgfile ]; then
ph.status "SKIP installation. config file already exists: $cfgfile."
ph.exit
fi
HOME=/root
export HOME
_uninstall
_install
ph.status "Installation was done"
ph.exit
fi
# --- uninstall
if [ $bOptUninstall -eq 1 -a "$( whoami )" = "root" ]; then
HOME=/root
_uninstall
rm -f $cfgfile
ph.status "Uninstalled."
ph.exit
fi
# --- check installation
grep $myuser $cfgfile >/dev/null 2>/dev/null
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"
fi
# ----------------------------------------------------------------------
sMode=$(ph.getValueWithParam '' m "$@")
case "${sMode}" in
"dbstat")
descr="statistics over all databases"
renderCounts "select \
sum(xact_commit) as commit, \
sum(xact_rollback) as rollback, \
sum(blks_read) as blkread, \
sum(blks_hit) as blkhit \
from pg_stat_database "
;;
"queries")
# psql -c "select * from pg_stat_activity "
# psql -c "select * from pg_stat_statements " -> av psql v11
;;
*)
echo ERRROR: [${sMode}] is an INVALID mode
_usage
ph.abort
esac
ph.status "Pgsql $sMode :: $descr"
echo "$out"
ph.exit
# ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment