Linux Programmer | RHCE | RHCSA

Search This Blog

Notify by mail if network bandwidth is High

Packages needs to be installed for sending mail :
1. libemail-valid-perl
2. libmail-sendmail-perl
3. libmailtools-perl
4. libmailutils4
5. lockfile-progs
6. mailutils
7. mailutils-common
8.mime-support
9. postfix 



#### script  Started here
## you can run this script as daemon

#!/bin/bash
mail=`dpkg -l | grep -i mailutils`
if [ ! -n "$mail" ];
then
    apt-get -y install mailutils
fi
while :
do
sleep 1
awk '{if(l1){print ($2-l1)/1024,($10-l2)/1024} else{l1=$2; l2=$10;}}' <(grep eth0 /proc/net/dev) <(sleep 1; grep eth0 /proc/net/dev) >/tmp/.netspeed >/tmp/.netkbyte

dload_kbyte=`cat /tmp/.netkbyte | awk '{print$1}'`
uload_kbyte=`cat /tmp/.netkbyte | awk '{print$2}'`

incoming=${dload_kbyte%.*}
outgoing=${uload_kbyte%.*}

if [ $incoming -gt 5000 ];
then
    IP=`/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
    SUBJECT="Bandwidth Limit Exceed"
    if [ ! -f /tmp/.dmail ];
    then
        echo "1" >/tmp/.dmail
        echo "$SUBJECT - Incoming bandwidth usageis high in :$HOSTNAME-$IP" | mail -s "Warning-Data Usage $IP" john@gmail.com,purval@gmail.com,martin@hotmail.com -aFrom:zuba@gmail.com
    fi
    echo "Downloading Speed is greater then 50MBit/s"
    echo "Sending Mail to Enjay Support Team"
else
    if [ -f /tmp/.dmail ];
    then
        rm -rf /tmp/.dmail
    fi
    echo "Downloading Speed is $dload_kbyte Kbytes/s"
fi

if [ $outgoing -gt 5000 ];
then
    echo "Uploading Speed is greater then 50MBit/s"
    echo "Sending Mail to Enjay Support Team..."
    IP=`/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
    SUBJECT="Bandwidth Limit Exceed"
    if [ ! -f /tmp/.umail ];
    then
        echo "1" >/tmp/.umail
        echo "$SUBJECT - Outgoing bandwidth usage is high in :$HOSTNAME-$IP" | mail -s "Warning-Data Usage $IP" john@gmail.com -aFrom:zuba@gmail.com
    fi
else
    if [ -f /tmp/.umail ];
    then
        rm -rf /tmp/.umail
    fi
    echo "Uploading speed is $uload_kbyte Kbyte/s"
fi
done



Notify by Email if Network Usage Limit is Exceed
 (usage limit of day)
#!/bin/bash

#stat=`vnstat | tail -1 | awk '{print $8}'`
stat=`vnstat | grep -A 2 eth0 | tail -1 | awk '{print$11}'`

IP=`/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`

TO_ADDRESS="purval.patel@enjayworld.com"

SUBJECT="Bandwidth AVG Used"



#set bandwith limit in GiB size,

band=300



#get bandwith reports

if [ $(echo "$stat >  $band" | bc) -ne 0 ];
then
    echo "$stat >  $band" | bc
    echo $stat

    echo > /tmp/bandwidth.txt

    vnstat > /tmp/bandwidth.txt
    echo "sending mail....."
#    mail -s "$SUBJECT - $stat GiB in :$HOSTNAME-$IP" $TO_ADDRESS  < /tmp/bandwidth.txt

    echo "$SUBJECT - $stat MiB in :$HOSTNAME-$IP" | mail -s "Warning-Data Usage Limit" john@gmail.com -aFrom:purval.patel@gmail.com

else

       echo "None of the condition met"

fi
 

No comments:

Post a Comment

SSH not working with password after upgrade ubuntu 22.04 or above

In recent upgrade of ubuntu 22.04 and above we are not able to login server with SSH password. but when we try to login with key then it all...