ServerDiff.sh
Jump to navigation
Jump to search
#!/bin/bash
# diff entre serveurs
# "serverCheck" [srv1 srv2 ... srvn]
# APA 20180325
# APA 20190419 refactory
# APA 20191107 clean
# APA 20211020 improve file and link check
# initialisation des variables
serverCheck="$1"
serverRef="$2"
if [ -z "${serverRef}" ] ;then
$0 "$1" "$(hostList.sh all |grep -vEx "$(hostList.sh "$1")" |shuf |head -7 |tr '\n' ' ' )"
exit 0
fi
baseDir="$(realpath "$(dirname $0)"/..)"
cacheFile=/tmp/"$(basename $0)"$$
logFile="${baseDir}"/log/"$(echo "${serverCheck}" | tr -c '[:alnum:]' '_' | cut -c 1-68 )"VS"$(echo "${serverRef}" | tr -c '[:alnum:]' '_' | cut -c 1-68 )".$(date -u '+%Y-%m-%dT%H_%M_%SZ')
(
echo '"'${serverCheck}'" VS "'${serverRef}'"'
echo
echo ${logFile} >&2
echo >&2
echo "= HW diff ="
# diff.sh "${serverCheck}" "${serverRef}" 'dmidecode'
echo "== CPU power audit =="
diff.sh "${serverCheck}" "${serverRef}" 'lscpu |grep -E "^CPU\(s\):|^BogoMIPS:" |awk "BEGIN{val=1};{val = val*\$2};END{print int(val/1000)}"'
echo "== memory =="
diff.sh "${serverCheck}" "${serverRef}" 'free -h |grep -iE "^Mem:|^Swap:" |sed -rn "s#^((Mem|Swap):[[:space:]]+[[:graph:]]+).*#\1#p"'
echo
echo "= Kernel diff ="
diff.sh "${serverCheck}" "${serverRef}" 'uname -r'
echo
echo "= partitions diff ="
# diff.sh "${serverCheck}" "${serverRef}" 'cat /proc/partitions |sed -rn "s#.* ([[:graph:]]+) ([[:graph:]]+)\$#\1 \2#p"'
# diff.sh "${serverCheck}" "${serverRef}" 'mount |grep ^/dev/ |cut -d' ' -f3 |grep -vwE '^/(mnt|media)' |while read lineMy ;do df -h "${lineMy}" ;done |sed -rn "s#^[[:graph:]]+[[:space:]]+([[:graph:]]+) [^/]+(/.*)\$#\1 \2#p"'
# diff.sh "${serverCheck}" "${serverRef}" "mount |grep -E 'vg_root|rhel_' |grep ^/dev/ |cut -d' ' -f3 |grep -vwE '^/(mnt|media)' |while read lineMy ;do df -h \${lineMy} ;done |sed -rn 's#^[[:graph:]]+[[:space:]]+([[:graph:]]+) [^/]+(/.*)\$#\1 \2#p'"
diff.sh "${serverCheck}" "${serverRef}" "mount |grep ^/dev/ |cut -d' ' -f3 |grep -vwE '^/(mnt|media)' |while read lineMy ;do df -h \${lineMy} ;done |sed -rn 's#^[[:graph:]]+[[:space:]]+([[:graph:]]+) [^/]+(/.*)\$#\1 \2#p'"
#echo
#echo "= FS diff ="
# diff.sh "${serverCheck}" "${serverRef}" 'xfs_info /'
echo
echo "= RMP diff ="
diff.sh "${serverCheck}" "${serverRef}" 'rpm -qa --qf "%{n}.%{arch}\n" |sed -r "s#\.(noarch|x86_64)\$##" |sort -u'
echo
echo "= services ="
diff.sh "${serverCheck}" "${serverRef}" '(systemctl list-unit-files 2>/dev/null |sort -u ;chkconfig --list 2>/dev/null |sort -u ) |grep -Ev "^session-|^user-" |grep -Ew "enabled|on" |sed -rn "s#^[[:space:]]*([[:graph:]]+)[[:space:]].*\$#\1#p" |sed -r "s#(\.service|\.target|\.socket)##g"' 2>/dev/null
>&2 echo
>&2 echo "# listing files diff step"
>&2 echo "## listing cfg files"
echo
echo "= files ="
# missing recent after boot
cat <<EOT >"${cacheFile}".sh
uptimeDays=\$(((\$(date +%s) - \$(date -d"\$(who -b |sed 's# *system boot *##')" +%s))/86400+1))
uptimeDays0=\$(((\$(date +%s) - \$(date -d"\$(who -b |sed 's# *system boot *##')" +%s))/86400-1))
sync
(
#find /boot -type f -size -9k -mmin +60 2>/dev/null
find /etc ! -path '/etc/lvm/backup/*' ! -path '/etc/lvm/archive/*' -type f -atime -\$uptimeDays -size -9k -mmin +6 -exec grep -Iq . {} \\; -and -print 2>/dev/null |head -4096
#find /etc ! -path '/etc/lvm/backup/*' ! -path '/etc/lvm/archive/*' -type f -atime -1 -size -9k -mmin +6 2>/dev/null
timeout --signal=9 16 find \$(mount |grep ^/dev/ |grep -E "vg_root|rhel_" |cut -d' ' -f3 |grep -vwE '^/(mnt|media)' ) \
-xdev ! -path '*/log/*' ! -path '/boot/*' ! -path '/etc/*' ! -path '/var/lib/*' -type f -atime -\$uptimeDays -size -9k -mmin +6 -exec grep -Iq . {} \\; -and -print 2>/dev/null \
|grep -vwEi '(log|logs|deb|dpkg|rpm|yum|cache|tmp|bash_history|old|bck|back)' |head -8192
#timeout --signal=9 12 find \$(mount |grep ^/dev/ |grep -E "vg_root|rhel_" |cut -d' ' -f3 |grep -vwE '^/(mnt|media)' ) \
-xdev ! -path '*/log/*' ! -path '/boot/*' ! -path '/etc/*' ! -path '/var/lib/*' -type f -atime -1 -size -9k -mmin +6 2>/dev/null \
|grep -vwEi '(log|logs|deb|dpkg|rpm|yum|cache|tmp|bash_history|old|bck|back)' |head -2048
timeout --signal=9 16 find \$(mount |grep ^/dev/ |grep -vE "vg_root|rhel_" |cut -d' ' -f3 |grep -vwE '^/(mnt|media)' ) \
-xdev ! -path '*/log/*' ! -path '/boot/*' ! -path '/etc/*' ! -path '/var/lib/*' -type f -atime -\$uptimeDays -size -9k -mmin +6 -exec grep -Iq . {} \\; -and -print 2>/dev/null \
|grep -vwEi '(log|logs|deb|dpkg|rpm|yum|cache|tmp|bash_history|old|bck|back|trace)' |head -8192
#timeout --signal=9 12 find \$(mount |grep ^/dev/ |grep -vE "vg_root|rhel_" |cut -d' ' -f3 |grep -vwE '^/(mnt|media)' ) \
-xdev ! -path '*/log/*' ! -path '/boot/*' ! -path '/etc/*' ! -path '/var/lib/*' -type f -atime -1 -size -9k -mmin +6 2>/dev/null \
|grep -vwEi '(log|logs|deb|dpkg|rpm|yum|cache|tmp|bash_history|old|bck|back|trace)' |head -2048
)|grep -vE '(\\.rpmnew|\\.rpmsave|-|~)\$' |sort -u |tee /tmp/cfgList
EOT
.scriptDiff.sh "${serverCheck}" "${serverRef}" "${cacheFile}".sh |grep -v "^==" |awk '!seen[$0]++' >"${cacheFile}".missing
# cp missing recent after boot
( echo "cat <<EOT >/tmp/cfgList-missing"
cat "${cacheFile}".missing
echo "EOT") >"${cacheFile}".missing2
mv -f "${cacheFile}".missing2 "${cacheFile}".missing
script.sh "${serverCheck} ${serverRef}" "${cacheFile}".missing
# missing infra
diff.sh "${serverCheck}" "${serverRef}" 'cat /tmp/cfgList-missing |while read fileName ;do [ -f "${fileName}" ] &&[ ! -L "${fileName}" ] &&(echo "${fileName}" ) ;done ;rm -f /tmp/cfgList-missing'
# \
# |grep -vxf "${baseDir}"/system_restore/rsync.list
>&2 echo
>&2 echo "# calculing md5sum"
ssh.sh "${serverCheck} ${serverRef}" 'cat /tmp/cfgList |while read fileName ;do md5sum "${fileName}" 2>/dev/null ||sudo -n md5sum "${fileName}" 2>/dev/null ;done ' >"${cacheFile}".md5sum
# cfg diff details if >=2 cfg
>&2 echo "# formating"
cat "${cacheFile}".md5sum \
|grep -v "^==.*==$" \
|awk '!seen[$0]++' \
|sed -r 's/^[0-9a-zA-Z]+ +//' \
>"${cacheFile}".files
>&2 echo "# removing uniq files"
awk 'NR == FNR {count[$0]++; next}; count[$0] >= 2' "${cacheFile}".files "${cacheFile}".files \
|sort -ru >"${cacheFile}".diff
# |grep -vxf "${baseDir}"/system_restore/rsync.list >"${cacheFile}".diff
echo
echo "= diff cfg ="
while [ -s "${cacheFile}".diff ] ;do
fileName="$(tail -1 "${cacheFile}".diff)"
sed -i '$ d' "${cacheFile}".diff
timeout --signal=9 45 diff.sh "${serverCheck}" "${serverRef}" 'grep -x "'"${fileName}"'" /tmp/cfgList 2>/dev/null >/dev/null &&( ls -dZ "'"${fileName}"'" ;cat "'"${fileName}"'" ||sudo -n cat "'"${fileName}"'" )|grep -vE "^[[:space:]]*#"' |grep -v ' no result ==$' |\
awk -v titleStart="===" '{if ($1 == titleStart)
title=$0 ;
else
{if (title != "")
print title ;
title="" ;
print $0 ;}}' >"${cacheFile}".result
if [[ $(cat "${cacheFile}".result |wc -l) -ge 2 ]] ;then
echo
echo "= ${fileName} ="
cat "${cacheFile}".result
fi
done
>&2 echo
>&2 echo "# listing links diff step"
>&2 echo "## listing links"
echo
echo "= links ="
# missing recent after boot
cat <<EOT >"${cacheFile}".sh
uptimeDays=\$(((\$(date +%s) - \$(date -d"\$(who -b |sed 's# *system boot *##')" +%s))/86400+1))
(
find /etc ! -path '/etc/lvm/backup/*' ! -path '/etc/lvm/archive/*' -type l -atime -\$uptimeDays 2>/dev/null
)|grep -vE '(-|~)\$' |sort -u |tee /tmp/linksList
EOT
.scriptDiff.sh "${serverCheck}" "${serverRef}" "${cacheFile}".sh |grep -v "^==" |awk '!seen[$0]++' >"${cacheFile}".missing
# cp missing recent after boot
( echo "cat <<EOT >/tmp/linksList-missing"
cat "${cacheFile}".missing
echo "EOT") >"${cacheFile}".missing2
mv "${cacheFile}".missing2 "${cacheFile}".missing
script.sh "${serverCheck} ${serverRef}" "${cacheFile}".missing
# missing infra
diff.sh "${serverCheck}" "${serverRef}" 'cat /tmp/linksList-missing |while read fileName ;do [ -L "${fileName}" ] &&(stat -c%N "${fileName}" ) ;done ;rm -f /tmp/linksList-missing'
# clean
rm -f /tmp/"$(basename $0)"$$* 2>/dev/null
) |tee "${logFile}"
echo >&2
echo ${logFile} >&2