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

Advertisements
Previous Post
Leave a comment

Leave your opinion

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: