Friday 10 August 2012

Load Email Alert Script

Load Email Alert Script
==========

 Load Email Alert Script

If you want to get alert mails when load in server spikes you can download this script. Alert mail will contain the results of w,pstree -apu,mysqladmin proc and netstat(for checking ddos attack)


cat > load-email.sh

#!/bin/bash
#script written by MANOJ
#This script will send mail alert if load is high in server with w,pstree,mysqladmin results


#Enter The Email address
email="youname@yourdomain.com"

#Enter the Load Average
critical_load=15



current_load=`cat /proc/loadavg |awk '{print $2 }' |cut -d"." -f1 `


if [ $current_load -ge $critical_load ]
then

cd /tmp
touch load.txt

#Enter the Text that you needs to display on mail

echo "Please check the Server and reduce the Load" >> /tmp/load.txt
echo

echo  "LOAD STATUS RUN ON" `date` >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt

echo " W Results  " >> /tmp/load.txt

w >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt

echo "PSTREE Results" >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt

pstree -apu >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt
echo "MYSQLADMIN Results" >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt

mysqladmin proc >> /tmp/load.txt

echo "*******************" >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt

echo "NETSTAT RESULTS (FOR CHKING DDOS ATTACK..10 HIGH HTTP CONNECTIONS ) " >>/tmp/load.txt

echo >>/load.txt
netstat -plan |grep :80 | awk '{print $5}' |cut -d: -f1 |sort |uniq -c |sort -n |tail -10 >>/tmp/load.txt

echo "*******************" >> /tmp/load.txt
echo "*******************" >> /tmp/load.txt



mail -s "!!!Urgent HIGH LOAD Avg=$current_load in  $HOSTNAME " $email < /tmp/load.txt

>/tmp/load.txt

fi


Set this as cronjob (*/10 * * * * sh /load-email.sh


17) Load Monitoring script
===========

Handy script for load monitoring
[edit]
=======================

We can monitor the server load effectively by making use of php and bash scripts,which prompts load as the output.

All we need to do is :

1] Create a php page say,tested.php , in the document root of the server: tested.php


++++++++++++++

<?php $output = shell_exec("w");
echo "

$output

";

?>

++++++++++++++

2] Execute the following bash script in your local machine to call the page recursively from the local machine

#!/bin/bash
while [ 1 -gt 0 ]
do
for i in server1 server2 server3 .... servern ;    <------------replace server1,server2  etc with the server hostname
do
echo $i;
curl http://$i/tested.php>load.out;
echo "output=$?";
error=`grep ERROR load.out`
if [ $? -ne 1 ]
then
kdialog --error "Please check the Httpd of server $i " --title "Warning" ;
else
load=`grep load load.out | awk -F : '{print $NF}'` ;
echo $load;
echo $load > load;
cur_load=`awk -F . '{print $1}' load`;
if [ $cur_load -ge 3 ] 
then
kdialog  --title "Warning" --passivepopup "Load of $i : $load"  5 ;

fi
fi
done
done


Here we go...........!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.