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

Merge branch 'add-check-requirement' into 'master'

check_requirements v1.1 add label

See merge request !57
parents af6f8d85 a55aed6e
No related branches found
No related tags found
1 merge request!57check_requirements v1.1 add label
...@@ -9,14 +9,15 @@ ...@@ -9,14 +9,15 @@
# #
# ah=axel.hahn@unibe.ch # ah=axel.hahn@unibe.ch
# #
# 2023-02-13 v1.0 ah # 2023-02-13 v1.0 ah initial version with check for processes, tcp/ udp
# 2023-02-15 v1.1 ah add label
# ====================================================================== # ======================================================================
. "$( dirname $0 )/inc_pluginfunctions" . "$( dirname $0 )/inc_pluginfunctions"
self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] ) self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=1.0 self_APPVERSION=1.1
self=$( basename $0 ) self=$( basename $0 )
...@@ -24,6 +25,7 @@ typeset -i VERBOSE=0 ...@@ -24,6 +25,7 @@ typeset -i VERBOSE=0
OUT= OUT=
typeset -i ADDHEADER=1
typeset -i COUNT=0 typeset -i COUNT=0
typeset -i COUNTOK=0 typeset -i COUNTOK=0
typeset -i COUNTERR=0 typeset -i COUNTERR=0
...@@ -52,12 +54,12 @@ connections do exist. ...@@ -52,12 +54,12 @@ connections do exist.
The check returns OK if all given requirements match. The check returns OK if all given requirements match.
SYNTAX: SYNTAX:
$(basename $0) [-h] [PARAMETERS] $self [-h] [PARAMETERS]
OPTIONS: OPTIONS:
-h or --help show this help. -h|--help show this help.
-v or --verbose show more data: processes and port numbers from -v|--verbose show more data: processes and port numbers from
/etc/services; add it as first param /etc/services; add it as first param
PARAMETERS: PARAMETERS:
...@@ -68,23 +70,60 @@ PARAMETERS: ...@@ -68,23 +70,60 @@ PARAMETERS:
TYPE string one of tcp|udp TYPE string one of tcp|udp
TARGET string target host TARGET string target host
PORT int port number PORT int port number
-p|--process STRING check if a process with given regex exists -l|--label STRING add label to divide the output in multiple sections
-p|--process STRING check if a process with given regex exists in the
output of 'ps aux'
-t|--tcp PORT check if connect on local tcp port is reachable; -t|--tcp PORT check if connect on local tcp port is reachable;
This is a shortcut for -c tcp localhost [port] This is a shortcut for '-c tcp localhost PORT'
The parameters can be repeated multiple times. Checks will be executed in The parameters can be repeated multiple times. Checks will be executed in
the given order. the given order.
EXAMPLE EXAMPLES
$(basename $0) -p httpd -p mysqld -t 22 -t 80 -t 443 -t 3306 $self -p httpd -p mysqld -t 22 -t 80 -t 443 -t 3306
Check if Check if
- a process httpd and a process mysqld exist - a process httpd and a process mysqld exist
- localhost listens to ports 22, 80, 443 and 3306 - localhost listens to ports 22, 80, 443 and 3306
$self -l "webservice" -p httpd -t 80 -t 443 -l "database" -p mysqld -t 3306
Use output with multiple sections
EOF EOF
} }
# show a label to divide the output in multiple sections
function showLabel(){
ADDHEADER=1
newline
OUT+="========== $1"
newline;
}
# show a line with a result
# param string status, eg. OK|ERROR
# param string type, eg. process|connection
# param string result
function addOutput(){
if [ $ADDHEADER -ne 0 ]; then
ADDHEADER=0
newline
addOutput STATUS TYPE RESULT
newline
# addOutput ------ ---- ------
# newline
fi
OUT+=$(printf "%-7s %-11s %s\n" "$1" "$2" "$3")
}
function addVerbose(){
if [ $VERBOSE -gt 0 ]; then
newline
OUT+="$(echo """$*""" | sed 's#^# > #g')"
newline
fi
}
# helper function: add new line in $OUT # helper function: add new line in $OUT
function newline(){ function newline(){
...@@ -96,19 +135,19 @@ function newline(){ ...@@ -96,19 +135,19 @@ function newline(){
# param string regex to search for in output of "ps aux" # param string regex to search for in output of "ps aux"
function chkProcess(){ function chkProcess(){
local label="$1" local label="$1"
local found;
test -z "$PROCESSES" && PROCESSES=$( ps aux ) test -z "$PROCESSES" && PROCESSES=$( ps aux )
COUNT+=1 COUNT+=1
local found=$( echo "$PROCESSES" | grep -v "grep" | grep -v "$self" | grep -E "$label" ) found=$( echo "$PROCESSES" | grep -v "grep" | grep -v "$self" | grep -E "$label" )
if [ -z "$found" ]; then if [ -z "$found" ]; then
COUNTERR+=1 COUNTERR+=1
ph.setStatus critical ph.setStatus critical
OUT+="ERROR process $label was not found" addOutput "ERROR" "process" "$label was not found"
else else
COUNTOK+=1 COUNTOK+=1
OUT+="OK process $label ($( echo """$found""" | wc -l ) x)" addOutput "OK" "process" "$label ($( echo """$found""" | wc -l ) x)"
test $VERBOSE -gt 0 && newline addVerbose "$found"
test $VERBOSE -gt 0 && OUT+="$( echo """$found""" | sed 's#^# #g')"
test $VERBOSE -gt 0 && newline
fi fi
newline newline
} }
...@@ -129,15 +168,13 @@ function chkConnection(){ ...@@ -129,15 +168,13 @@ function chkConnection(){
else else
if (>/dev/$type/$target/$port) 2>/dev/null; then if (>/dev/$type/$target/$port) 2>/dev/null; then
COUNTOK+=1 COUNTOK+=1
OUT+="OK connection $type to $target on port $port" addOutput "OK" "connection" "$type to $target on port $port"
else else
COUNTERR+=1 COUNTERR+=1
ph.setStatus critical ph.setStatus critical
OUT+="ERROR connection $type to $target on port $port FAILED" addOutput "ERROR" "connection" "$type to $target on port $port FAILED"
fi fi
test $VERBOSE -gt 0 && newline addVerbose "$( grep """ $port/$type""" /etc/services )"
test $VERBOSE -gt 0 && OUT+="$( grep """ $port/$type""" /etc/services | sed 's#^# #g')"
test $VERBOSE -gt 0 && newline
fi fi
newline newline
} }
...@@ -154,6 +191,7 @@ fi ...@@ -154,6 +191,7 @@ fi
# parse params # parse params
while [[ "$#" -gt 0 ]]; do case $1 in while [[ "$#" -gt 0 ]]; do case $1 in
-c|--connect) chkConnection "$2" "$3" "$4"; shift;shift;shift;shift ;; -c|--connect) chkConnection "$2" "$3" "$4"; shift;shift;shift;shift ;;
-l|--label) showLabel "$2"; shift;shift ;;
-p|--process) chkProcess "$2";shift 2;; -p|--process) chkProcess "$2";shift 2;;
-h|--help) showHelp; exit 0;; -h|--help) showHelp; exit 0;;
-t|--tcp) chkConnection tcp localhost "$2"; shift;shift ;; -t|--tcp) chkConnection tcp localhost "$2"; shift;shift ;;
...@@ -162,8 +200,6 @@ while [[ "$#" -gt 0 ]]; do case $1 in ...@@ -162,8 +200,6 @@ while [[ "$#" -gt 0 ]]; do case $1 in
esac; done esac; done
ph.status "$COUNT Requirement checks - errors: $COUNTERR" ph.status "$COUNT Requirement checks - errors: $COUNTERR"
echo
echo "STATUS TYPE RESULT"
echo "$OUT" echo "$OUT"
ph.exit ph.exit
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment