Current File : //proc/thread-self/root/proc/self/root/proc/self/root/lib64/nagios/plugins/nccustom/check_cwaf.sh
#!/bin/bash

#########################################################
#							#
# Check updates for CWAF client and rules		#
# created by Max.N v 0.0.3				#
#							#
#########################################################

CWAFDIR="/var/cpanel/cwaf/scripts"
CWAFRULES="updater.pl"
CWAFCLIENT="update-client.pl"
TMPFILE="/tmp/cwaf.lock"
ALERTTIME="24"
UNIXTIME=$(date +%s)
re='^[0-9.]+$'
#warn="\b\b"

function SCHEDULER() {
#LASTCHANGES=$(grep -m1 "[0-9]*" ${TMPFILE})
#echo $((${UNIXTIME} - ${LASTCHANGES})) $((${ALERTTIME} * 60))
if [ -f ${TMPFILE} ]; then
    LASTCHANGES=$(grep -m1 "[0-9]*" ${TMPFILE})
    if [ $((${UNIXTIME} - ${LASTCHANGES})) -le $((${ALERTTIME} * 60 * 60)) ]
    then
	#echo "lock file exist"
        warn="Alert scheduled. ${warn}"
        exitcode=0
    fi
else 
    echo ${UNIXTIME} > ${TMPFILE}
    warn="Alert scheduled. ${warn}"
    exitcode=0
fi
}

#ARR1=($(sudo ${CWAFDIR}/${CWAFRULES} --version | grep -owP "(?<=rules version=)(\d+\.?\d+)"))
#ARR2=($(sudo ${CWAFDIR}/${CWAFCLIENT} --version | grep -owP "(?<= version=)(\d+\.?\d+)"))


ARR1=($(sudo ${CWAFDIR}/${CWAFRULES} --version | awk -F 'rules version=' '{print $2}'))
ARR2=($(sudo ${CWAFDIR}/${CWAFCLIENT} --version | awk -F 'version=' '{print $2}'))


#ARR ("current ver" "avalible ver")
#ARR1=(1 2) #catch to array vars from updater.pl --version
#ARR2=(1 2) #catch to array vars from update-client.pl --version

#clietn ver https://waf.comodo.com/doc/version.dat and local /etc/cwaf/version.dat

if [ -z "${ARR1[1]}" ] ||  [ -z "${ARR2[1]}" ]
then 
    warn="Can't check latest version."
fi 



if ! [[ "${ARR1[1]}" =~ $re ]]
then
    warn="${warn} ${ARR1[@]:1}."

elif ! [[ "${ARR2[1]}" =~ $re ]]
then
    warn="${warn} ${ARR2[@]:1}."

elif ! [[ "${ARR2[1]}" =~ $re ]] && [[ "${ARR2[1]}" =~ $re ]]
then
    warn="${warn} ${ARR1[@]:1} and ${ARR2[@]:1}."
fi



if [ "${ARR1[0]}" == "${ARR1[1]}" ] && [ "${ARR2[0]}" == "${ARR2[1]}" ]
then
    echo "[OK]"
    rm -f ${TMPFILE}
    exit 0
elif [ "${ARR1[0]}" != "${ARR1[1]}" ] && [ "${ARR2[0]}" != "${ARR2[1]}" ]
then
    out="client and rules"
    exitcode="2"
elif [ "${ARR1[0]}" != "${ARR1[1]}" ]
then 
    out="CWAF rules"
    exitcode="2"
elif [ "${ARR2[0]}" != "${ARR2[1]}" ]
then
    out="CWAF client"
    exitcode="2"
else
    warn="check license can't stat current version"
    out="CWAF"
    exitcode="2"
fi

SCHEDULER

echo -e "[CRITICAL]: ${warn} Please check updates for ${out} manually."| sed -e 's% [[:space:]]*% %g'


exit "${exitcode}"