Search

Tuesday, August 19, 2008

shell script to check Server Status

#!/bin/bash
clear
#Global variable
#Please enter the free memory you want to keep in server else mail will send
FMEM=10
#Please Enter free disk Space in MB
FDSK=1000
#Your Default Gateway
DGATE=20.12.31.101
#Please enter the email address
EMAIL=mailtodaman@gmail.com
#Enter the name of the http server you want to monitor.
SNAME=http://linuxguruji.blogspot.com
#Your SMTP Service
SMTP=mailtodaman@gmail.com
#Default Time for SMTP
SEL=5

TMEM=`free -m | awk '{ print $2}' | head -n 2 | tail -1`
echo "Total Memory(Mb) in your system is -->" $TMEM

MEM=`free -m | awk '{ print $4}' | head -n 2 | tail -1`
echo "Free Memory(Mb) in your system is -->" $MEM

FDSK=`df -h | awk '{ print $4}' | head -n 2 | tail -1`
echo "Free HardDisk(Kb) in your System is -->" $FDSK
FDSKK=`df | awk '{ print $4}' | head -n 2 | tail -1`
last | awk '{ print $3}' > a.bat
w | grep pts | awk '{ print $3}' > current.bat


if [ "$MEM" > "$FMEM" ]; then
mail -s " Critical --> Your server memory " $EMAIL <<> "$FDSKK" ]; then
mail -s " Critical --> Your server HDD " $EMAIL <<> Your server Has Hacked " $EMAIL <<>> last.bat

fi
done

for uname1 in $(cat current.bat);
do if [ "$DGATE" != "$uname1" ]; then
mail -s " Critical --> Your server Has Hacked " $EMAIL <<>> last1.bat


fi
done
if ! $(lynx -head -dump "$SNAME" 2> /dev/null | head -1 \
| grep ' 200 ' > /dev/null )
then
mail -s " Critical --> Your site is down " $EMAIL <<> mtest.bat



for uname2 in $(cat mtest.bat);
do if [ "$SMTP" = "$uname2" ]; then
mail -s " Critical --> Your SMTP Service " $EMAIL << EOF
Hello,
Your SMTP Service is not working fine.
Regards,
System Admin
EOF


fi
done

Arguements check

#!/bin/bash
if [ -z "$1" ]
then
echo "Oops! Sorry no arguement $0"
exit
fi

While Example

#!/bin/bash
c=0
while [ $c -lt 100 ]
do
echo "counter is $c"
let c=c+1
done

list of users and count total number of users

#!/bin/bash
for i in $(cat /etc/passwd | cut -d: -f 1) ; do
echo username:$i

done
echo "Total number of user is $(cat /etc/passwd | wc -l)"

File description

if [ $# -ne 1 ]
then
echo "Argument not given"
exit 0
else

Filename=$1
if [ -f $1 ]
then
echo "File Name is $1"
echo "Owner is $(ls -la $Filename | awk '{print $3}')"
echo "Group is $(ls -la $Filename | awk '{print $4}')"
echo "Size is $(ls -hls $Filename | awk '{print $1}')"
echo "Inode number is $(ls -ils $Filename | awk '{print $1}')"
echo "File type is $(file $1)"
echo "File is mounted in $(df -h $Filename | grep -v Mou | awk '{ print
$6}')"

else
echo "Oops! $1 doesn't exist"
fi
fi

check if I am xyz user or not

if [ "$(whoami)" != 'xyz' ]; then
echo "You are not xyz user."
exit 0
fi

Check if file exists

#!/bin/bash
if [ -e xyz ]
then
echo "file xyz is there"
else
echo "Oops "
fi

if condition

#!/bin/bash
if [ "fioo" = "foo" ]
then
echo "it equal"
else
echo "Oops! something is wrong"
fi

take backup

#!/bin/bash
OF=/home/backup$(date +%Y%m%d).tgz
tar -cvf $OF /home/User_Directory

Scan the file "xyz.tar" and remove from system

rm -rf $(find / -name xyz.tar) &> /dev/null

Send mail if site is down

Space check and send the mail if space greater than 90%

Below script is used to check the space on server, If space is greater than 90%, it sends the mail to admin.
==============Start script================
#!/bin/tcsh

set a=`df -h /PATH | head -13 | tail -1 | awk '{print $4}' | cut -c 1`
set b=`df -h /PATH | head -13 | tail -1 | awk '{print $4}' | cut -c 2`
set c=`echo $a$b`

if ( "$c" > 90 ) then

mail -s "Space problem in server" myemail@yahoo.com << EOF
Space in server is now $c%.
EOF
else
echo Everything looks fine.
endif

=================================

tcl expect example rsync

Below script can be used for backup the user data from remote server to local directory (LOCAL_DIRECTORY) without prompting the password.


==========Start Script===========
#!/usr/local/bin/tclsh8.4
package require Expect
spawn rsync -avz -e ssh username@serverIP.net:/home/username/data/ LOCAL_DIRECTORY
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password password
send "Password123\n"
interact
puts "Enjoy Expect"
=======================End Script===============