The script is for adding to the cron tab. The flow of the shell script should be like this. 1. Checks the CPU state. 2. Checks the RAM usage. 3. Checks the HDD usage. 4.Sends an alert mail to recipients if any of these are above the threshold value. (say 90%)
@matches=grep {$_ >90} (
df -H=~/(\S+%)/g);print "@matches\n";
This returns the output of 'df _H' if it is above 90%
for RAM,
free | grep Mem | awk '{print $3/$2 * 100.0}'
The above command returns the RAM usage in percentage
Now, this should be compared with a threshold of 80%. Then, if any of the above values [HDD/RAM] is on high side, the program should send a mail appending with the result of 'top' command in it, to show the cpu usage.
Please help.
This is to be added to the cron tab to run in equal intervals. Kindly update.
21 Answer
Let's start.
Create load.sh script
nano /path/load.sh
#!/bin/bash #we use output from "uptime" i "w" to have load on 5 min UPTIME=`uptime`; W=`w`; RAM=`free -m`; DATE=`date`; sleep 3 DF=`df -h` sleep 3 echo "######################################" >> /var/log/load-result/load_log echo "$DATE" >> /var/log/load-result/load_log echo "" >> /var/log/load-result/load_log echo "UPTIME:" >> /var/log/load-result/load_log echo "" >> /var/log/load-result/load_log echo "$UPTIME" >> /var/log/load-result/load_log echo "" >> /var/log/load-result/load_log echo "W COMMAND:" >> /var/log/load-result/load_log echo "" >> /var/log/load-result/load_log echo "$W" >> /var/log/load-result/load_log echo "" >> /var/log/load-result/load_log echo "FREE RAM:" >> /var/log/load-result/load_log echo "$RAM" >> /var/log/load-result/load_log echo "" >> /var/log/load-result/load_log echo "FREE SPACE:" >> /var/log/load-result/load_log echo "" >> /var/log/load-result/load_log echo "$DF" >> /var/log/load-result/load_log echo "" >> /var/log/load-result/load_log echo "#####################################" >> /var/log/load-result/load_log give script X permision
chmod 777 /path/load.sh Create dir for storing a log file
sudo mkdir /var/log/load-result Create script to send mails
nano /path/mail.sh
mail -s Load_From_PC < /var/log/load-result/load_log Give X mail.sh
chmod 777 /path/mail.sh Create cron jobs
sudo crontab -e
*/5 * * * * /path/load.sh 00 11 * * * /path/mail.sh load.sh will executed on every 5 min and result from script will be added on bottom in log file.
mail.sh will be triggered every day at 11:00 h
You can change time ...
1