0

I have used SAR in my below script and is trying to run on individual servers for my nagios monitoring. Can someone please suggest how i can get the swap warning and swap critical values in my check below ? I have tried to calculate it dynamically when running the below on each server but it is reporting for error.

(Swap warning/critical needs to be calculated like 20% of Total swap/40 % of total swap respectively)

Please suggest.

# 2 = CRITICAL (SWAP usage higher than CRITICAL)
# 3 = UNKNOWN (Wrong usage)

SWAP_WARN=
SWAP_CRIT=
SWAPOUT_ACTIVITY_TEST=

## GET SWAP Warning and Critical values from the Machine

# 2 = CRITICAL (SWAP usage higher than CRITICAL)
# 3 = UNKNOWN (Wrong usage)

SWAP_WARN=
SWAP_CRIT=
SWAPOUT_ACTIVITY_TEST=

**## GET SWAP Warning and Critical values from the Machine
temp=$(swapon -s | tail -n 1 | awk '{print $3}' )
SWAP_WARN=$(echo '$(temp)*.20' | bc)
SWAP_CRIT=$(echo '$(temp)*.40' | bc)**

## FETCH ARGUMENTS
##while getopts "h:" OPTION; do
#        case "${h}" in
#                 h)
#                        usage
#                        exit 3
#                        ;;
#                ?)
#                        usage
#                        exit 3
#                        ;;
#        esac
#done

## CHECK ARGUMENTS
if [ -z ${SWAP_WARN} ] || [ -z ${SWAP_CRIT} ] || [ ${SWAP_WARN} -gt ${SWAP_CRIT} ] ; then
        usage
        exit 3
fi

## GET SWAP INFO FROM MACHINE

cd /var/log/sa
FOR_VALUE=sa
FOR_DATE=$(date | awk '{print $3}')
SA=$FOR_VALUE$FOR_DATE
SWAPOUT_ACTIVITY_TEST=$(sar -S -f $SA | tail -n 2| head -n 1 | awk '{print $4}')


## CHECK SWAPPING ON MACHINE
if [ ${SWAPOUT_ACTIVITY_TEST} -lt ${SWAP_WARN} ]; then
        ## SWAP IS OK
        LINE="OK! Swapout size in last 10 minutes : ${SWAPOUT_ACTIVITY_TEST} | swapout_size=${SWAPOUT_ACTIVITY_TEST}B;${SWAP_WARN};${SWAP_CRIT};"
        echo $LINE
        exit 0
elif [ ${SWAPOUT_ACTIVITY_TEST} -gt ${SWAP_WARN} ] && [ ${SWAPOUT_ACTIVITY_TEST} -lt ${SWAP_CRIT} ] || [ ${SWAPOUT_ACTIVITY_TEST} -eq ${SWAP_WARN} ]; then
        ## SWAP IS IN WARNING STATE
        LINE="WARNING! Swapout size in last 10 minutes: ${SWAPOUT_ACTIVITY_TEST} | swapout_size=${SWAPOUT_ACTIVITY_TEST}B;${SWAP_WARN};${SWAP_CRIT};"
:

## FETCH ARGUMENTS
##while getopts "h:" OPTION; do
#        case "${h}" in
#                 h)
#                        usage
#                        exit 3
#                        ;;
#                ?)
#                        usage
#                        exit 3
#                        ;;
#        esac
#done

## CHECK ARGUMENTS
if [ -z ${SWAP_WARN} ] || [ -z ${SWAP_CRIT} ] || [ ${SWAP_WARN} -gt ${SWAP_CRIT} ] ; then
        usage
        exit 3
fi

## GET SWAP INFO FROM MACHINE

cd /var/log/sa
FOR_VALUE=sa
FOR_DATE=$(date | awk '{print $3}')
SA=$FOR_VALUE$FOR_DATE
SWAPOUT_ACTIVITY_TEST=$(sar -S -f $SA | tail -n 2| head -n 1 | awk '{print $4}')


## CHECK SWAPPING ON MACHINE
if [ ${SWAPOUT_ACTIVITY_TEST} -lt ${SWAP_WARN} ]; then
        ## SWAP IS OK
        LINE="OK! Swapout size in last 10 minutes : ${SWAPOUT_ACTIVITY_TEST} | swapout_size=${SWAPOUT_ACTIVITY_TEST}B;${SWAP_WARN};${SWAP_CRIT};"
        echo $LINE
        exit 0
elif [ ${SWAPOUT_ACTIVITY_TEST} -gt ${SWAP_WARN} ] && [ ${SWAPOUT_ACTIVITY_TEST} -lt ${SWAP_CRIT} ] || [ ${SWAPOUT_ACTIVITY_TEST} -eq ${SWAP_WARN} ]; then
        ## SWAP IS IN WARNING STATE
        LINE="WARNING! Swapout size in last 10 minutes: ${SWAPOUT_ACTIVITY_TEST} | swapout_size=${SWAPOUT_ACTIVITY_TEST}B;${SWAP_WARN};${SWAP_CRIT};"
0

Any specific reason you cannot use check_swap from nagios-plugins?

check_swap -w 80% -c 60%

If you insist on using sar, why calculate an absolute value for the threshold at all, can't you just use the value from the %swpused column to determine check state?

SWPUSED=$(sar -S -f $SA | tail -2 | head -1 | awk '{print $5}')
SWPUSED_ROUNDED=$(printf "%.0f" $SWPUSED)

if [ $SWPUSED_ROUNDED -gt 40 ]; then
    echo "CRITICAL: ${SWPUSED}% of swap space used"
    exit 2
elif [ $SWPUSED_ROUNDED -gt 20 ]; then
    echo "WARNING: ${SWPUSED}% of swap space used"
    exit 1
else
    echo "OK: ${SWPUSED}% of swap space used"
    exit 0
fi
  • I need to get Nagios output/alert as "Swap usage in last "x" minutes is " .... And due to the reason i am using SAR outputs for my alert...as they are generated every '10' minutes by default.... – Linux learner Jul 11 '15 at 3:06
  • Yes, but the sampling period depends on the crontab entry you added... How often are you calling sa1 from cron? Can you paste the relevant line here? – Kempniu Jul 11 '15 at 10:29
  • thanks....but the sampling in not my problem at all.............i am asking how could i calculate two values swap_war and swap_crit below (using the above mentioned script) ???????????? ## GET SWAP Warning and Critical values from the Machine temp=$(swapon -s | tail -n 1 | awk '{print $3}' ) SWAP_WARN=$(echo '$(temp)*.20' | bc) SWAP_CRIT=$(echo '$(temp)*.40' | bc) – Linux learner Jul 13 '15 at 12:46
  • Updated my answer, see if it works for you. – Kempniu Jul 16 '15 at 11:20
  • Many thanks kempniu. The numbers "40" and "20" are constant(fixed), As i don't know what is Swap warning/swap critical levels defined on each of my servers(that i will monitor through this script) . i need to calculate these values as 20 and 40 % respectively of the total swap on each server and hence the logic..... Can you please suggest now ? – Linux learner Jul 20 '15 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.