Pre & Post Check script for Centos7 Server.

The duty of system administration is really tough as he/she has to monitor servers, users, logs create backup and perform OS Patching. For the most repetitive tasks most of the administrator write a scripts to automate their day to day repetitive task. Here we have written a shell script that will help you to take the pre-check and post check of the system before doing the OS Patching.
We need to execute the script before OS Patching, so that we can collect the system information and compare it with post checks and identify if there are any missing parameter (like missing routes, nfs partition, configuration over write. )before handover the server to application team.

It will collected the below information.

  • OS Version
  • Fstab
  • Grub
  • Mail configuration.
  • Bonding
  • Sysctl
  • Ethernet
  • Route
  • Kernel Release
  • Mount points
  • Services running.

[root@node03 ~]# vi pre-post-check.sh
#!/bin/bash
# Purpose: Pre and Post Check before performing any change on system
# Version: 1.0
# Created Date: 
# Modified Date: 
# Author : 
###############START OF SCRIPT#######################
echo
echo "1. Pre-checks"
echo "2. Post-checks"
echo "3. Quit"
echo "Please select the option:"
read OPT
ROOT_DIR=/root/Patch_`date +"%B-%Y"`
PRE_DIR=$ROOT_DIR/Pre/
POST_DIR=$ROOT_DIR//Post/
BOND_FILE=/proc/net/bonding/bond0
prechecks()
{
if [ -d "$ROOT_DIR" ]; then
echo "$PRE_DIR already exists!!, Please rename it before executing the script"
exit
fi
mkdir $ROOT_DIR
mkdir $PRE_DIR
#Files to be backed up
cp -p /etc/redhat-release $PRE_DIR
cp -p /etc/fstab $PRE_DIR
cp -p /etc/grub2.cfg $PRE_DIR
cp -p /etc/postfix/* $PRE_DIR
if [ -f "$BOND_FILE" ]; then
cp -p /proc/net/bonding/bond0 $PRE_DIR/bond0.txt
fi
#Outputs of Important commands
for i in `ip a | grep mtu | cut -d " " -f2`;
do
ethtool $i > $PRE_DIR/ethtool_$i.txt;
done
uname -a > $PRE_DIR/uname.txt
ip a > $PRE_DIR/ifconfig_all.txt
netstat -rn > $PRE_DIR/routing_table.txt
mount | wc -l> $PRE_DIR/mount.txt
df -PTh | egrep 'xfs|ext4|ext3|nfs/cifs' > $PRE_DIR/df.txt
sysctl -a | sort  > $PRE_DIR/sysctl
cat /proc/mounts | egrep 'nfs|cifs'  | grep -v sunrpc > $PRE_DIR/nfs.txt
netstat -nuatp | grep LIST | awk -F "/" '{ print $2 }' > $PRE_DIR/services.txt
rpm -qa | sort > $PRE_DIR/rpm.txt
echo "Required files and command outputs copied to $PRE_DIR.."
}
postchecks()
{
if [ -d "$POST_DIR" ]; then
echo "$POST_DIR already exists!!, Please rename it before executing the script"
exit
fi
if [ ! -d "$PRE_DIR" ]; then
echo "Precheck Directory $PRE_DIR does not exist, please perform Prechecks first"
exit
fi
mkdir $POST_DIR
#Files to be backed up
cp -p /etc/redhat-release $POST_DIR
cp -p /etc/fstab $POST_DIR
cp -p /etc/grub2.cfg $PRE_DIR
cp -r /etc/postfix/ $POST_DIR
if [ -f "$BOND_FILE" ]; then
cp /proc/net/bonding/bond0 $POST_DIR/bond0.txt
fi
#Outputs of Important commands
for i in `ip a | grep mtu | cut -d " " -f2`;
do
ethtool $i > $POST_DIR/ethtool_$i.txt;
done
uname -a > $POST_DIR/uname.txt
ip a > $POST_DIR/ifconfig_all.txt
netstat -rn > $POST_DIR/routing_table.txt
mount | wc -l> $POST_DIR/mount.txt
df -PTh | egrep 'xfs|ext4|ext3|nfs/cifs' > $POST_DIR/df.txt
sysctl -a | sort  > $POST_DIR/sysctl
cat /proc/mounts | egrep 'nfs|cifs'  | grep -v sunrpc > $POST_DIR/nfs.txt
netstat -nuatp | grep LIST | awk -F "/" '{ print $2 }' > $POST_DIR/services.txt
rpm -qa | sort > $POST_DIR/rpm.txt
for i in `ls -l $POST_DIR | grep ^- | awk '{print $9}'`
do
echo >> $POST_DIR/DIFFERENCES.TXT
echo $i >> $POST_DIR/DIFFERENCES.TXT
echo "--------" >> $POST_DIR/DIFFERENCES.TXT
diff -y --suppress-common-lines $PRE_DIR/$i $POST_DIR/$i >> $POST_DIR/DIFFERENCES.TXT
echo >> $POST_DIR/DIFFERENCES.TXT
echo >> $POST_DIR/DIFFERENCES.TXT
done
echo
echo "Post Patching Checks done, please check $POST_DIR/DIFFERENCES.TXT for important differences"
HOSTNAME=`hostname`
# echo "$HOSTNAME" | mailx -s "Post-Checks of $RHOSTNAME" -a "$POST_DIR/DIFFERENCES.TXT"
}
case $OPT in
"1") prechecks
;;
"2") postchecks
;;
"3") exit
;;
*) echo "$OPT is not a valid option"
;;
esac
###############END OF SCRIPT#######################

Change the ownership of file.
[root@node03 ~]# chmod +x pre-post-check.sh
It will ask, do you want to perform pre-check before performing the activity or post-check after performing the activity.
In precheck it will take the output of critical parameters. And in post check it will take the output of critical parameter and compare with pre-check and share the difference.
[root@node03 ~]# sh pre-post-check.sh
1. Pre-checks
2. Post-checks
3. Quit
Please select the option:

Thanks for reading.

  


Pre & Post Check script for RHEL 8 Server.


# vi pre-post-check.sh

#!/bin/bash
# Purpose: Pre and Post Check before performing any change on system
# Version: 1.0
# Created Date: 
# Modified Date: 
# Author : 
###############START OF SCRIPT#######################
echo
echo "1. Pre-checks"
echo "2. Post-checks"
echo "3. Quit"
echo "Please select the option:"
read OPT
ROOT_DIR=/root/Patch_`date +"%B-%Y"`
PRE_DIR=$ROOT_DIR/Pre/
POST_DIR=$ROOT_DIR//Post/
BOND_FILE=/proc/net/bonding/bond0
prechecks()
{
if [ -d "$ROOT_DIR" ]; then
echo "$PRE_DIR already exists!!, Please rename it before executing the script"
exit
fi
mkdir $ROOT_DIR
mkdir $PRE_DIR
#Files to be backed up
cp -p /etc/redhat-release $PRE_DIR
cp -p /etc/fstab $PRE_DIR
cp -p /etc/grub2-efi.cfg $PRE_DIR
if [ -f "$BOND_FILE" ]; then
cp -p /proc/net/bonding/bond0 $PRE_DIR/bond0.txt
fi
#Outputs of Important commands
for i in `ip r | grep kernel | cut -d " " -f3 | sort -u`;
do
ethtool $i > $PRE_DIR/ethtool_$i.txt;
done
uname -a > $PRE_DIR/uname.txt
ip a > $PRE_DIR/ifconfig_all.txt
netstat -rn > $PRE_DIR/routing_table.txt
mount | wc -l> $PRE_DIR/mount.txt
df -PTh | egrep 'xfs|ext4|ext3|nfs/cifs' > $PRE_DIR/df.txt
sysctl -a | sort  > $PRE_DIR/sysctl
cat /proc/mounts | egrep 'nfs|cifs'  | grep -v sunrpc > $PRE_DIR/nfs.txt
netstat -nuatp | grep LIST | awk -F "/" '{ print $2 }' | sort -u > $PRE_DIR/services.txt
rpm -qa | sort > $PRE_DIR/rpm.txt
echo "Required files and command outputs copied to $PRE_DIR.."
}
postchecks()
{
if [ -d "$POST_DIR" ]; then
echo "$POST_DIR already exists!!, Please rename it before executing the script"
exit
fi
if [ ! -d "$PRE_DIR" ]; then
echo "Precheck Directory $PRE_DIR does not exist, please perform Prechecks first"
exit
fi
mkdir $POST_DIR
#Files to be backed up
cp -p /etc/redhat-release $POST_DIR
cp -p /etc/fstab $POST_DIR
cp -p /etc/grub2-efi.cfg $PRE_DIR
if [ -f "$BOND_FILE" ]; then
cp /proc/net/bonding/bond0 $POST_DIR/bond0.txt
fi
#Outputs of Important commands
for i in `ip r | grep kernel | cut -d " " -f3 | sort -u`;
do
ethtool $i > $POST_DIR/ethtool_$i.txt;
done
uname -a > $POST_DIR/uname.txt
ip a > $POST_DIR/ifconfig_all.txt
netstat -rn > $POST_DIR/routing_table.txt
mount | wc -l> $POST_DIR/mount.txt
df -PTh | egrep 'xfs|ext4|ext3|nfs/cifs' > $POST_DIR/df.txt
sysctl -a | sort  > $POST_DIR/sysctl
cat /proc/mounts | egrep 'nfs|cifs'  | grep -v sunrpc > $POST_DIR/nfs.txt
netstat -nuatp | grep LIST | awk -F "/" '{ print $2 }' | sort -u > $POST_DIR/services.txt
rpm -qa | sort > $POST_DIR/rpm.txt
for i in `ls -l $POST_DIR | grep ^- | awk '{print $9}'`
do
echo >> $POST_DIR/DIFFERENCES.TXT
echo $i >> $POST_DIR/DIFFERENCES.TXT
echo "--------" >> $POST_DIR/DIFFERENCES.TXT
diff -y --suppress-common-lines $PRE_DIR/$i $POST_DIR/$i >> $POST_DIR/DIFFERENCES.TXT
echo >> $POST_DIR/DIFFERENCES.TXT
echo >> $POST_DIR/DIFFERENCES.TXT
done
echo
echo "Post Patching Checks done, please check $POST_DIR/DIFFERENCES.TXT for important differences"
HOSTNAME=`hostname`
# echo "$HOSTNAME" | mailx -s "Post-Checks of $RHOSTNAME" -a "$POST_DIR/DIFFERENCES.TXT"
}
case $OPT in
"1") prechecks
;;
"2") postchecks
;;
"3") exit
;;
*) echo "$OPT is not a valid option"
;;
esac
###############END OF SCRIPT#######################

 

1 comment:

  1. Hi My dev, this script how to implement ansibe .Please help

    ReplyDelete