Archive for the ‘Shell Script’ Category


Script

#!/bin/bash
H=`hostname`
cpu=`nproc`
GB=1024
echo -e “—————————————————————————————————————————-”
echo -e “\t Hardware Resource and Model”
echo -e “—————————————————————————————————————————-”
echo -e “CPU Resources Allocated = $cpu core”
M=`dmidecode -t 17 | grep -w MB | awk ‘{ SUM += $2} END { print SUM }’`
res=$(echo “scale=3;$M/$GB” | bc)
echo -e “Memory Resoruces Allocated = $res GB”
P=`dmidecode -t processor | grep -i intel | grep -i version | head -1 | cut -d’:’ -f2`
echo -e “Processor Model = $P”
TH=`fdisk -l | grep -i sd | grep -i gb |wc -l`
echo -e “Total Number Disk connected = $TH”
TSH=`fdisk -l | grep -i sd | grep -i gb | awk ‘{ SUM += $3} END { print SUM }’`
dkres=$(echo “scale=3;$TSH/$GB” |bc)
echo -e “Total Size of all Disk = $dkres TB”
NN=`lspci | grep -i ethernet | cut -d ‘:’ -f3 | head -1`
echo -e “Network Interface card Model = $NN”
TN=`lspci | grep -i ethernet | wc -l`
echo -e “Total Number of NIC Connected = $TN”
NS=`ethtool eth0 | grep -i speed |awk ‘{print $2,$3}’`
echo -e “Network Interface Speed = $NS”
echo -e “—————————————————————————————————————————-”

Output:-
[root@VM ~]# ./test.sh
————————————————————————————-
Hardware Resource and Model
————————————————————————————-
CPU Resources Allocated = 16 core
Memory Resoruces Allocated = 32.000 GB
Processor Model = Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz
Total Number Disk connected = 8
Total Size of all Disk = 7.891 TB
Network Interface card Model = VMware VMXNET3 Ethernet Controller (rev 01)
Total Number of NIC Connected = 1
Network Interface Speed = 10000Mb/s
————————————————————————————-


Scenario

For my production Environment i want take backup of daily basis under /backup/DB using incremental backup method.

Example:

[root@server1 DB]# pwd
/backup/DB
[root@server1 DB]# ls -lt
total 20
drwxr-xr-x 2 root root 4096 Dec  5 05:29 backup-29-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:27 backup-28-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:25 backup-27-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:22 backup-26-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:21 backup-25-01-2016

From above output i am taking backup daily basis . But after few days my File system got full to avoid this we can delete except recent DB backup. But this i have to do daily or when ever disk full i have take action. Avoid this i am writing small script and scheduling.

Script: clean-old-backup.sh

#!/bin/bash
clear
P=’/backup/DB’
echo “script to Clean up Database”
cd $P
echo -e “\n\tBefore Clean up”
echo “————————————————————”
ls -lt
echo “————————————————————”
ls -t | tail -n +4 | xargs rm -rf
echo -e “\n\tAfter Clean up”
echo “———————————————————–”
ls -lt
echo “———————————————————–”

Run #./clean-old-backup.sh

Output

script to Clean up Database

Before Clean up
————————————————————————–
total 24
drwxr-xr-x 2 root root 4096 Dec  5 05:40 backup-30-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:29 backup-29-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:27 backup-28-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:25 backup-27-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:22 backup-26-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:21 backup-25-01-2016
—————————————————————————

After Clean up
————————————————————————-
total 12
drwxr-xr-x 2 root root 4096 Dec  5 05:40 backup-30-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:29 backup-29-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:27 backup-28-01-2016
————————————————————————

[root@server1 ~]# crontab -l
00      20      *       *       *       /bin/bash /root/clean-old-backup.sh


Scenario :

I want to create 10GB LVM partition….using fdisk -l get disk name “/dev/sdd”….. below script we have to pass 4 arguments like LUN name, Volume Group name,Logical Volume name and Mount Points…. first scan the disk and create LVM partition…

script :

[root@autel ~]# cat diskmanagr.sh
#!/bin/bash

echo -e ” \nScan and Detect newly connected SCSI LUN”
host=`ls -l /sys/class/scsi_host/ | grep -v total | awk ‘{print $9}’ | awk -F”host” ‘{print $2}’`
for i in $host
do
echo “Rescaning scsi host /sys/class/scsi_host/host$i”
echo “- – -” > /sys/class/scsi_host/host$i/scan
done
echo -e “\n All the SCSI LUN scanned Sucessfully…..”
lun=$1
vg=$2
lv=$3
mp=$4
mkdir -p $mp
echo -e “\n\n Partition creation is begin”
partx -av $lun
pvcreate $lun
echo -e “\nPhysical Volume $lun Created”
vgcreate $vg $lun
echo -e “\n Volume Group $vgCreated”
lvcreate -l 100%FREE -n $lv $vg
echo -e “\n Logical Volume $lv Created”
mkfs.ext4 /dev/$vg/$lv
echo “/dev/$vg/$lv      $mp     ext4    defaults        1 2 ” | cat >> /etc/fstab
mount -a
s=` df -h | grep /test | awk ‘{print $1}’`
echo -e “\nNewly created LUN $mp = $s ”
echo -e “Partition successfully created”

output :

[root@autel ~]# time ./diskmanagr.sh /dev/sdd new disk10 /autel

Scan and Detect newly connected SCSI LUN
Rescaning scsi host /sys/class/scsi_host/host0
Rescaning scsi host /sys/class/scsi_host/host1
Rescaning scsi host /sys/class/scsi_host/host2

All the SCSI LUN scanned Sucessfully…..

Partition creation is begin
device /dev/sdd: start 0 size 20971520
gpt: 0 slices
Physical volume “/dev/sdd” successfully created

Physical Volume /dev/sdd Created
Volume group “new” successfully created

Volume Group
Logical volume “disk10” created

Logical Voume disk10 Created
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2620416 blocks
131020 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Newly created LUN /autel = 5.0G
Partition successfully created

real    0m1.487s
user    0m0.034s
sys     0m0.873s
[root@autel ~]#


Script : scandisk.sh

#!/bin/bash

echo -e ” \nScan and Detect newly connected SCSI LUN\n”

host=`ls -l /sys/class/scsi_host/ | grep -v total | awk ‘{print $9}’ | awk -F “host” ‘{print $2}’`

for i in $host

do

echo “Rescaning scsi host /sys/class/scsi_host/host$i”

echo “- – -” > /sys/class/scsi_host/host$i/scan

done

echo -e “\n All the SCSI LUN scanned Successfully…..\n”

 

Output : 

[root@autel ~]# ./scandisk.sh

Scan and Detect newly connected SCSI LUN

Rescaning scsi host /sys/class/scsi_host/host0
Rescaning scsi host /sys/class/scsi_host/host1
Rescaning scsi host /sys/class/scsi_host/host2

All the SCSI LUN scanned Successfully…..

[root@autel ~]#


File 1 – Store List of server IP in /root/serverlist

Write a script to generate server performance report of CPU , Memory, Disk IO & Network at particular range of time using SAR command

script name : perfor-report1.sh

#!/bin/bash

for ip in `awk -F: ‘{print $1}’ /root/serverlist`
do
echo -e “\n CPU Performance of $ip \n”

ssh $ip ‘sar -f /var/log/sa/sa15 -s 18:33:02 -e 19:40:31’
echo -e “\n Memory Performance of $ip \n”
ssh $ip ‘sar -r -f /var/log/sa/sa15 -s 18:33:02 -e 19:40:31’
echo -e “\n Disk IO Performance of $ip \n”
ssh $ip ‘sar -b -f /var/log/sa/sa15 -s 18:33:02 -e 19:40:31’
echo -e “\n Network Performance of $ip \n”
ssh $ip ‘sar -n DEV -f /var/log/sa/sa15 -s 18:33:02 -e 19:40:31’
done

run as #./perfor-report1.sh

 

 

Want generate report your own input like time and date

script name : perfor-report2.sh

#!/bin/bash
day=$1

start =$2

end=$3

for ip in `awk -F: ‘{print $1}’ /root/serverlist`

do

echo -e “\n CPU Performance of $ip \n”

ssh $ip ‘sar -f $day -s $start -e $end’
echo -e “\n Memory Performance of $ip \n”
ssh $ip ‘sar -r -f $day -s $start -e $end’
echo -e “\n Disk IO Performance of $ip \n”
ssh $ip ‘sar -b -f $day -s $start -e $end’
echo -e “\n Network Performance of $ip \n”
ssh $ip ‘sar -n DEV -f $day -s $start -e $end’

done

run as #./perform-report2.sh /var/log/sa/sa21  12:05:00  19:00:58


Script : serverreboottime.sh

#!/bin/bash

#script to find out how much time tokk for previous reboot

clear

echo -e “\n\tScript to find How much time taken Linux Server for last Reboot\n”

#BT = Server Boot Time After Reboot
BT=`last -F reboot | head -n 1 | awk ‘{print $5,$6,$7,$8,$9}’`

#RT = Server Rebooted time
RT=`last -F reboot | head -n 2 | awk ‘{print $11,$12,$13,$14,$15}’| tail -1`

echo -e  “Server Boot Time after Reboot = $BT”;
echo -e  “Server Rebooted Time          = $RT”;

#Note below output is should print the value between different date also Example JAN 10 23:59:21 server rebooted again server is came up at JAN 11 00:04:54 it should calculate the difference

echo -e “\nAmount of Time Reboot (seconds)  = $(( $(date -ud “$BT” +’%s’) – $(date -ud “$RT” +’%s’) ))Sec” ;

echo $(( $(date -ud “$BT” +’%s’) – $(date -ud “$RT” +’%s’) )) > out.txt

seconds=`cat /root/out.txt`

echo “Amount of Time Reboot (MM:SS)    = $((seconds/60))Min $((seconds%60))Sec”

echo “Amount of Time Reboot (HH:MM:SS) = $(date -d @$(( $(date -d “$BT” +%s) – $(date -d “$RT” +%s) )) -u +’%H:%M:%S’)”

 

Run :[root@test ~]# ./serverreboottime.sh

 

Output :

Screenshot_1


Script

Run script in Local Machine save as “Server Performance.sh”

#!/bin/bash
clear

##variable Decalation Part
ip=$1
#SCRIPT STARTED

echo -e “\n\t\t\t $ip Server Performance\n\n ”
echo -e “\tNetwork Status\n”

# SERVER REACHABLE STATUS

COUNT=`ping -c2 $ip | grep received |awk ‘{print $4}’`

#check whether server is reachable Status
if [ $COUNT -eq 2 ]
then
echo -e “Reachable : Server is up”
else
echo -e “Reachable : Server is down”
fi

#Server Firwall Status

IT=`iptables -L | wc -l`

if [ $IT -eq 8 ]
then
echo -e “Firewall : Disabled \n”
else
echo -e “FIRWALL   : Enabled \n”
fi

echo -e “\tPhysical Status\n ”

#Server CPU Usage calculations

CU=`mpstat | awk ‘$12 ~ /[0-9.]+/ { print 100 – $12″%” }’`

echo -e “CPU Usage (%) : $CU”

# Server Memory Usage calculations
MU=`free -m | grep Mem | awk ‘{print $3/$2*100.0}’|cut -d”.” -f1`
U=`free -m | grep Mem | awk ‘{print $3}’`
F=`free -m | grep Mem | awk ‘{print $4}’`
C=`free -m | grep Mem | awk ‘{print $7}’`
echo -e “Memory Used (MB) : $U ”
R=`df -h | grep -w / | awk ‘{print $4}’|cut -d % -f1`
echo -e “Memory Free (MB) : $F ”
echo -e “Memory Clear (MB) : $C ”
echo -e “Disk Usage / (%) : $R ”

echo -e “\n\tProcess Status”
echo -e “\nTop 3 Memory Eating Process\n ”
ps axo rss,comm,pid | awk ‘{ proc_list[$2]++; proc_list[$2 “,”1] += $1; } END { for (proc in proc_list) { printf(“%d\t%s\n”, proc_list[proc “,” 1],proc); }}’ | sort -n | tail -n 3 | sort -rn | awk ‘{$1/=1024;printf “%.0fMB\t”,$1}{print $2}’

 

Output

Screenshot_4

Screenshot_8

Get Server Performance report from Remote Server run below script and save as “SPRM.sh”

#!/bin/bash
ip=$1
port=$2
user=$3
pass=$4

sshpass -p “$pass” scp -P $port /root/ServerPerformence.sh $user@$ip:/root/

clear

sshpass -p “$pass” ssh -p $port -o StrictHostKeyChecking=no $user@$ip “bash /root/ServerPerformence.sh $ip”

 

Output

Screenshot_7

Screenshot_9