Tuesday, February 28, 2012

How to send alert message for SWAP Usage


here is a shell script which will Alert by send mail if the system started to use more than 20% of swap area and 80% of hard disk space.
$ vim swap-use.sh
#!/bin/sh
free -m | grep Swap | while read output;
do
  swap=$(echo $output | awk '{print $2}' )
  used=$(echo $output | awk '{ print $3 }' )
  freed=$(echo $output | awk '{ print $4 }' )
  echo "Swap : $swap"
  echo "Used : $used"
  echo "Free : $freed"
  usep=`expr $used \* 100 / $swap`
  echo $usep
  if [ $usep -ge 20 ]; then
    echo "Swap Usage Alert Total Swap: \"$swap\" Used: \"$used ($usep%)\" Free:
\"$freed\" on $(hostname) as on $(date)" |
     mail -s "Alert: Swap Usage space $usep%" mail1@example.com,mail2@example.com
  fi
done
Then schedule it in cron
-- 

No comments:

Post a Comment