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

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

Add check requirement

See merge request !55
parents 03ed3937 8f09470d
No related branches found
No related tags found
1 merge request!55Add check requirement
......@@ -21,13 +21,14 @@
# 2022-04-14 v1.10 <axel.hahn@iml.unibe.ch> show consuming cpu processes with top and ps
# 2022-08-29 v1.11 <axel.hahn@iml.unibe.ch> replace pipe to prevent start of metrics section
# 2022-08-29 v1.12 <axel.hahn@iml.unibe.ch> fix: replace pipe
# 2023-02-13 v1.13 <axel.hahn@iml.unibe.ch> small shell fixes
# ======================================================================
. $(dirname $0)/inc_pluginfunctions
self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=1.12
self_APPVERSION=1.13
# ----------------------------------------------------------------------
# functions
......@@ -114,7 +115,7 @@ typeset -i iMinCpuUsageToShowProcesses=$( ph.getValueWithParam 50 p "$@")
# top -b -n 1 | head -5 | grep "^\%Cpu" >$tmpfile
# FIX read cpu from 2nd output of top
data=$( top -b -n 2 -d 0.1 | grep -i "^\%Cpu" | tail -1 | cut -f 2- -d ':' | tr ',' "\n" )
data=$( top -b -n 2 -d 0.1 | grep -i "^%Cpu" | tail -1 | cut -f 2- -d ':' | tr ',' "\n" )
cpuUser=$( echo "$data" | grep "us" | awk '{ print $1 }' )
cpuSystem=$( echo "$data" | grep "sy" | awk '{ print $1 }' )
......@@ -160,14 +161,17 @@ echo "$plist" | grep "[0-9]" >/dev/null \
&& echo "$plist" | tr '|' ':'
# v1.7: show most consuming processes if usage is > nn %
typeset -i iUsed=$( echo $cpuNonIdle | cut -f 1 -d '.' )
typeset -i iUsed
iUsed=$( echo $cpuNonIdle | cut -f 1 -d '.' )
if [ $iUsed -gt $iMinCpuUsageToShowProcesses ]; then
echo
echo "CPU usage is higher $iMinCpuUsageToShowProcesses percent ... showing most consuming processes"
echo "output of top :"
topout=$( top -b -n 1 -d 0.1 )
typeset -i iStart=$( echo "$topout" | grep -n "PID.*USER" | cut -f 1 -d ':' )
typeset -i iEnd=$iStart+5
typeset -i iStart
iStart=$( echo "$topout" | grep -n "PID.*USER" | cut -f 1 -d ':' )
typeset -i iEnd
iEnd=$iStart+5
echo "$topout" | sed -n "${iStart},${iEnd}p" | tr '|' ':'
echo
echo "output of ps:"
......
......@@ -24,13 +24,14 @@
# 2021-03-24 v1.5 <axel.hahn@iml.unibe.ch> increase Swap critical limit 5 --> 50
# 2022-03-09 v1.6 <axel.hahn@iml.unibe.ch> show most ram intensive processes
# 2022-03-10 v1.7 <axel.hahn@iml.unibe.ch> add cli param -p; update help
# 2023-02-13 v1.8 <axel.hahn@unibe.ch> shell fixes
# ======================================================================
. $(dirname $0)/inc_pluginfunctions
. "$(dirname $0)/inc_pluginfunctions"
self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=1.7
self_APPVERSION=1.8
# ----------------------------------------------------------------------
# FUNCTIONS
......@@ -97,30 +98,30 @@ ph.require bc
# ----------------------------------------------------------------------
# set default / override from command line params
typeset -i iWarnLimit=$( ph.getValueWithParam 75 w "$@")
typeset -i iCriticalLimit=$( ph.getValueWithParam 90 c "$@")
typeset -i iCriticalSwap=$( ph.getValueWithParam 50 s "$@")
typeset -i iMinRamUsageToShowProcesses=$( ph.getValueWithParam 50 p "$@")
typeset -i iWarnLimit; iWarnLimit=$( ph.getValueWithParam 75 w "$@")
typeset -i iCriticalLimit; iCriticalLimit=$( ph.getValueWithParam 90 c "$@")
typeset -i iCriticalSwap; iCriticalSwap=$( ph.getValueWithParam 50 s "$@")
typeset -i iMinRamUsageToShowProcesses; iMinRamUsageToShowProcesses=$( ph.getValueWithParam 50 p "$@")
# ----------------------------------------------------------------------
# read values
# --- RAM
typeset -i ramTotal=$( getMemvalue MemTotal)
typeset -i ramAvail=$( getMemvalue MemAvailable)
typeset -i ramUsed=$ramTotal-$ramAvail
typeset -i ramTotal; ramTotal=$( getMemvalue MemTotal)
typeset -i ramAvail; ramAvail=$( getMemvalue MemAvailable)
typeset -i ramUsed; ramUsed=$ramTotal-$ramAvail
typeset -i ramFree=$( getMemvalue MemFree)
typeset -i ramBuffers=$( getMemvalue Buffers)
typeset -i ramCached=$( getMemvalue Cached)
typeset -i ramSReclaim=$( getMemvalue SReclaimable)
typeset -i ramFree; ramFree=$( getMemvalue MemFree)
typeset -i ramBuffers; ramBuffers=$( getMemvalue Buffers)
typeset -i ramCached; ramCached=$( getMemvalue Cached)
typeset -i ramSReclaim; ramSReclaim=$( getMemvalue SReclaimable)
typeset -i ramUsage=$(echo "($ramUsed) *100 / $ramTotal " | bc)
typeset -i ramUsage; ramUsage=$(echo "($ramUsed) *100 / $ramTotal " | bc)
# --- Swap
typeset -i swapTotal=$( getMemvalue SwapTotal)
typeset -i swapFree=$( getMemvalue SwapFree)
typeset -i swapCached=$( getMemvalue SwapCached)
typeset -i swapTotal; swapTotal=$( getMemvalue SwapTotal)
typeset -i swapFree; swapFree=$( getMemvalue SwapFree)
typeset -i swapCached; swapCached=$( getMemvalue SwapCached)
typeset -i swapUsage=0
if [ $swapTotal -gt 0 ]; then
......@@ -147,7 +148,7 @@ fi
# --- show details
echo
cat /proc/meminfo | egrep "^(Mem|Cache|Buffers|Swap|Slab|SReclaimable)"
grep -E "^(Mem|Cache|Buffers|Swap|Slab|SReclaimable)" "/proc/meminfo"
# v1.6: show most consuming processes if usage is > nn %
......
......@@ -12,6 +12,7 @@
# ----------------------------------------------------------------------
# 2020-03-02 v1.0 initial version
# 2020-03-05 v1.1 <axel.hahn@iml.unibe.ch> switch to ph.* helper functions
# 2023-02-13 v1.2 <axel.hahn@unibe.ch> some shell fixes
# ======================================================================
# --- tmp files for internal usage
......@@ -30,19 +31,19 @@ typeset -i iCountCritical=0
rm -f $tmpfile $tmpfile2 $outCritical $outWarning 2>/dev/null
. `dirname $0`/inc_pluginfunctions
. $(dirname $0)/inc_pluginfunctions
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
# set default / override from command line params
typeset -i iWarnLimit=` ph.getValueWithParam 100 w "$@"`
typeset -i iCriticalLimit=` ph.getValueWithParam 500 c "$@"`
typeset -i iWarnLimit=$( ph.getValueWithParam 100 w "$@")
typeset -i iCriticalLimit=$( ph.getValueWithParam 500 c "$@")
# --- read processlist and create helper table
ps -yle >$tmpfile
for processname in `cat $tmpfile | awk {'print $13'} | sort -u | fgrep -v "/"`
for processname in $(cat $tmpfile | awk {'print $13'} | sort -u | grep -Fv "/")
do
#echo -n "$processname; ">>$tmpfile2
ps -ylC $processname | awk '
......@@ -54,10 +55,10 @@ done
# --- check limits
while read line
do
typeset -i iSizeMB=`echo $line | awk '{ print $2 }' | sed "s#\..*##"`
typeset -i iSizeMB=$(echo $line | awk '{ print $2 }' | sed "s#\..*##")
if [ $iSizeMB -ge $iWarnLimit ]; then
processname=`echo $line | awk '{ print $1 }'`
processcount=`echo $line | awk '{ print $3 }'`
processname=$(echo $line | awk '{ print $1 }')
processcount=$(echo $line | awk '{ print $3 }')
if [ $iSizeMB -ge $iCriticalLimit ]; then
iCountCritical=$iCountCritical+1
echo "Critical: $iSizeMB MB - $processname ($processcount)" >>$outCritical
......
#!/bin/bash
# ======================================================================
#
# NAGIOS CLIENT CHECK :: check requirements
#
# Check if a list requirements regarding processes and network
# connections do exist
# ----------------------------------------------------------------------
#
# ah=axel.hahn@unibe.ch
#
# 2023-02-13 v1.0 ah
# ======================================================================
. "$( dirname $0 )/inc_pluginfunctions"
self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=1.0
self=$( basename $0 )
typeset -i VERBOSE=0
OUT=
typeset -i COUNT=0
typeset -i COUNTOK=0
typeset -i COUNTERR=0
PROCESSES=
# ----------------------------------------------------------------------
# functions
# ----------------------------------------------------------------------
# show help
function showHelp(){
cat <<EOF
______________________________________________________________________
$self_APPNAME
v$self_APPVERSION
(c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3
______________________________________________________________________
Check if a list requirements regarding processes and network
connections do exist.
The check returns OK if all given requirements match.
SYNTAX:
$(basename $0) [-h] [PARAMETERS]
OPTIONS:
-h or --help show this help.
-v or --verbose show more data: processes and port numbers from
/etc/services; add it as first param
PARAMETERS:
-c|--connect TYPE TARGET PORT
check if connect to a server to a given port
is reachable
TYPE string one of tcp|udp
TARGET string target host
PORT int port number
-p|--process STRING check if a process with given regex exists
-t|--tcp PORT check if connect on local tcp port is reachable;
This is a shortcut for -c tcp localhost [port]
The parameters can be repeated multiple times. Checks will be executed in
the given order.
EXAMPLE
$(basename $0) -p httpd -p mysqld -t 22 -t 80 -t 443 -t 3306
Check if
- a process httpd and a process mysqld exist
- localhost listens to ports 22, 80, 443 and 3306
EOF
}
# helper function: add new line in $OUT
function newline(){
OUT+="
"
}
# check in process list
# param string regex to search for in output of "ps x"
function chkProcess(){
local label="$1"
test -z "$PROCESSES" && PROCESSES=$( ps x )
COUNT+=1
local found=$( echo "$PROCESSES" | grep -v "grep" | grep -v "$self" | grep -E "$label" )
if [ -z "$found" ]; then
COUNTERR+=1
ph.setStatus critical
OUT+="ERROR process $label was not found"
else
COUNTOK+=1
OUT+="OK process $label ($( echo """$found""" | wc -l ) x)"
test $VERBOSE -gt 0 && newline
test $VERBOSE -gt 0 && OUT+="$( echo """$found""" | sed 's#^# #g')"
test $VERBOSE -gt 0 && newline
fi
newline
}
# 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
function chkConnection(){
local type="$1" # tcp | udp
local target="$2" # servername
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
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
COUNTOK+=1
OUT+="OK connection $type to $target on port $port"
else
COUNTERR+=1
ph.setStatus critical
OUT+="ERROR connection $type to $target on port $port FAILED"
fi
test $VERBOSE -gt 0 && newline
test $VERBOSE -gt 0 && OUT+="$( grep """ $port/$type""" /etc/services | sed 's#^# #g')"
test $VERBOSE -gt 0 && newline
fi
newline
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
if [ "$#" -eq 0 ]; then
showHelp;
exit 1
fi
# parse params
while [[ "$#" -gt 0 ]]; do case $1 in
-c|--connect) chkConnection "$2" "$3" "$4"; shift;shift;shift;shift ;;
-p|--process) chkProcess "$2";shift 2;;
-h|--help) showHelp; exit 0;;
-t|--tcp) chkConnection tcp localhost "$2"; shift;shift ;;
-v|--verbose) VERBOSE=1; shift;;
*) echo "ERROR: Unknown parameter: $1"; showHelp; exit 1;
esac; done
ph.status "$COUNT Requirement checks - errors: $COUNTERR"
echo
echo "STATUS TYPE RESULT"
echo "$OUT"
ph.exit
# ----------------------------------------------------------------------
......@@ -16,10 +16,11 @@
#
# 2017-03-03 v1.0 ah,ds
# 2020-03-05 v1.1 <axel.hahn@iml.unibe.ch> switch to ph.* helper functions
# 2023-02-13 v1.2 <axel.hahn@unibe.ch> some shell fixes
# ======================================================================
. `dirname $0`/inc_pluginfunctions
. $(dirname $0)/inc_pluginfunctions
sDomain=
iPort=443
......@@ -41,7 +42,7 @@ function showHelp(){
echo
echo ----- SSL Check v1.0
echo
echo "SYNTAX: `basename $0` [domain] [[port]]"
echo "SYNTAX: $(basename $0) [domain] [[port]]"
echo " domain - domain to verify the ssl vertificate from (required)"
echo " port - port number to connect (default: 443)"
echo
......@@ -81,7 +82,7 @@ function showHelp(){
ph.exit
fi
echo | openssl s_client -connect ${sDomain}:${iPort} 2>/dev/null | openssl x509 -noout -subject | fgrep ${sDomain} >/dev/null
echo | openssl s_client -connect ${sDomain}:${iPort} 2>/dev/null | openssl x509 -noout -subject | grep -F ${sDomain} >/dev/null
if [ $? -ne 0 ]; then
ph.setStatus "unknown"
echo SORRY, openssl was unable to fetch the right certificate - this happens on multiple ssl webs - it finds
......@@ -91,13 +92,13 @@ function showHelp(){
# --- unix timestamps valid from .. to
dateFrom=`echo | openssl s_client -connect ${sDomain}:${iPort} 2>/dev/null | openssl x509 -noout -startdate | cut -f 2 -d "="`
dateTo=`echo | openssl s_client -connect ${sDomain}:${iPort} 2>/dev/null | openssl x509 -noout -enddate | cut -f 2 -d "="`
dateFrom=$(echo | openssl s_client -connect ${sDomain}:${iPort} 2>/dev/null | openssl x509 -noout -startdate | cut -f 2 -d "=")
dateTo=$(echo | openssl s_client -connect ${sDomain}:${iPort} 2>/dev/null | openssl x509 -noout -enddate | cut -f 2 -d "=")
tsFrom=`date -d "${dateFrom}" +%s`
tsTo=`date -d "${dateTo}" +%s`
tsFrom=$(date -d "${dateFrom}" +%s)
tsTo=$(date -d "${dateTo}" +%s)
tsNow=`date +%s`
tsNow=$(date +%s)
typeset -i iDaysLeft=($tsTo-$tsNow)/60/60/24
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment