diff --git a/check_requirements b/check_requirements index 9008da192e34972e2b8683b6cb12f11043b1604a..ab209325aa75455213227c07345d423d9cc25ec9 100755 --- a/check_requirements +++ b/check_requirements @@ -12,12 +12,13 @@ # 2023-02-13 v1.0 ah initial version with check for processes, tcp/ udp # 2023-02-15 v1.1 ah add label # 2024-06-07 v1.2 ah use iml help and add link to docs +# 2025-02-11 v1.3 ah add ping check; add timeout in connection check # ====================================================================== . "$( dirname $0 )/inc_pluginfunctions" -self_APPVERSION=1.2 +self_APPVERSION=1.3 self=$( basename $0 ) @@ -63,6 +64,7 @@ PARAMETERS: TYPE string one of tcp|udp TARGET string target host PORT int port number + -i|--ping TARGET ping a target system -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' @@ -124,6 +126,10 @@ function newline(){ " } +# ---------------------------------------------------------------------- +# check functions +# ---------------------------------------------------------------------- + # check in process list # param string regex to search for in output of "ps aux" function chkProcess(){ @@ -145,7 +151,7 @@ function chkProcess(){ newline } -# check connection +# Check connection # param string type: one of tcp|udp # param string target: a servername eg. localhost or www.example.com # param integer port number to connect @@ -155,11 +161,11 @@ function chkConnection(){ local port="$3" # port number COUNT+=1 - if ! echo "$type/$target/$port" | grep -E "^(tcp|udp)/[a-z][a-z0-9\.\-]*/[1-9][0-9]*$" >/dev/null 2>&1; then + if ! echo "$type/$target/$port" | grep -Eq "^(tcp|udp)/[a-z][a-z0-9\.\-]*/[1-9][0-9]*$"; then COUNTERR+=1 OUT+="ERROR syntax error detected - verify params for port check; type=$type (one of tcp|udp) target=$target (servername to connect) port=$port" else - if (>/dev/$type/$target/$port) 2>/dev/null; then + if timeout 1 bash -c ">/dev/$type/$target/$port" 2>/dev/null; then COUNTOK+=1 addOutput "OK" "connection" "$type to $target on port $port" else @@ -172,6 +178,25 @@ function chkConnection(){ newline } +# Check ping to a target +# param string target: a servername eg. localhost or www.example.com +function chkPing(){ + local myHost="$1" + local cmdout + + COUNT+=1 + if cmdout="$( ping -c 1 $myHost 2>&1 )"; then + COUNTOK+=1 + addOutput "OK" "ping" "ping to $myHost $( echo "$cmdout" | head -2 | tail -1 | rev | cut -f -1,2 -d ' ' | rev)" + else + COUNTERR+=1 + ph.setStatus critical + addOutput "ERROR" "ping" "ping to host $myHost FAILED" + fi + addVerbose "$cmdout" + newline +} + # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- @@ -184,6 +209,7 @@ fi # parse params while [[ "$#" -gt 0 ]]; do case $1 in -c|--connect) chkConnection "$2" "$3" "$4"; shift;shift;shift;shift ;; + -i|--ping) chkPing "$2"; shift;shift ;; -l|--label) showLabel "$2"; shift;shift ;; -p|--process) chkProcess "$2";shift 2;; -h|--help) showHelp; exit 0;; diff --git a/docs/20_Checks/check_requirements.md b/docs/20_Checks/check_requirements.md index ac3e580b158ccb2cb3292d8fc1c9ddcf10087fcf..0ea9b6ca2fd8982481c6faca014ceb703a1acd4e 100644 --- a/docs/20_Checks/check_requirements.md +++ b/docs/20_Checks/check_requirements.md @@ -6,13 +6,16 @@ Script: `check_requirements` * Check if a process exists * Check an open udp or tcp port (local or on given host/ ip) +* Ping a target system (new in v1.3) You can insert a label to create multiple blocks. ## Requirements -* `ps` -* `top` +* Linux GNU tools + * `ps` + * `top` + * `ping` ## Standalone installation @@ -26,7 +29,7 @@ From this repository ypu need next to this script: ______________________________________________________________________ CHECK_REQUIREMENTS -v1.2 +v1.3 (c) Institute for Medical Education - University of Bern Licence: GNU GPL 3 @@ -40,7 +43,7 @@ connections do exist. The check returns OK if all given requirements match. SYNTAX: -check_requirements [-h] [-v] [PARAMETERS] +check_requirements [-h] [PARAMETERS] OPTIONS: @@ -56,6 +59,7 @@ PARAMETERS: TYPE string one of tcp|udp TARGET string target host PORT int port number + -i|--ping TARGET ping a target system -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' @@ -83,7 +87,9 @@ check_requirements -l "webservice" -p httpd -t 80 -t 443 -l "database" -p mysqld ## Examples -Check a webservice as process and 2 ports: ``check_requirements -p nginx -t 443 -t 80`` +### 3 checks + +Check a locally rtunning webservice as process and 2 ports: ``check_requirements -p nginx -t 443 -t 80`` ```txt OK: 3 Requirement checks - errors: 0 @@ -93,3 +99,48 @@ OK process nginx (2 x) OK connection tcp to localhost on port 443 OK connection tcp to localhost on port 80 ``` + +### Verbose mode + +The verbose mode can be activated with `-v`. The checks are execeuted parameter by parameter. The debug begins where you add `-v` - so the best option is to add it at the beginning. + +This is the same list of checks again that you can compare both outputs: ``check_requirements -v -p nginx -t 443 -t 80`` + +```txt +OK: 3 Requirement checks - errors: 0 + +STATUS TYPE RESULT +OK process nginx (2 x) + > root 1211 0.0 0.0 16468 3436 ? Ss 13:34 0:00 nginx: master process /usr/bin/nginx + > http 1212 0.0 0.0 16476 5188 ? S 13:34 0:00 nginx: worker process + +OK connection tcp to localhost on port 443 + > https 443/tcp + +OK connection tcp to localhost on port 80 + > http 80/tcp + > www 80/tcp + > www-http 80/tcp +``` + +### Labels + +The `-l <TEXT>` parameter adds a label. With it you can visually group some checks. + +When executing this: `/check_requirements -l "webservice" -p nginx -t 443 -t 80 -l "network" -c tcp www.example.com 443` then we get 2 sections with a table of executed checks inside: + +```txt +OK: 4 Requirement checks - errors: 0 + +========== webservice + +STATUS TYPE RESULT +OK process nginx (2 x) +OK connection tcp to localhost on port 443 +OK connection tcp to localhost on port 80 + +========== network + +STATUS TYPE RESULT +OK connection tcp to www.example.com on port 443 +```