算法:
- 从所有服务器获取文件/目录的 md5sums [从文件中选择服务器列表和目录列表]
- 将所有服务器的文件 md5sums 与活动节点进行比较 [活动节点可以是服务器列表中的任意 1 个节点]
- 报告文件的 md5sum 不正确/不匹配
- 报告所有 md5sum 不正确的文件的上次更新时间以及它们不同步的时间[逻辑:将该文件的上次修改时间与活动节点上的同一文件进行比较]
- 报告文件是否不存在于任何服务器上
预期日志格式:
[Warn] Node Name: 192.168.1.23 | Out-ofSync-Filename: /home/user2/nginx/Templates/file34 | Last-Modified-Time: 2021-07-19 19:25:37 | Out-Of-sync-Since: 15 minutes
[CRITICAL] Node Name: 192.168.1.23 | Out-ofSync-Filename: /home/user2/nginx/Templates/file34 | Last-Modified-Time: 2021-07-19 19:25:37 | Out-Of-sync-Since: 20 minutes
我的脚本进度:
### Defining Server INI files ###
serverList="/home/user1/testdir/server-list"
filesList="/home/user1/testdir/directory-list"
### Defining output files for extracting md5sums ###
md5sumofDIR="/home/user1/testdir/directory-md5sum"
filemd5sum="/home/user1/testdir/file-md5sum"
### Read Server IP from File ###
for i in $(cat $serverList | awk '{print $3}'); do
echo $i
done
### Read File-List from File ###
for j in $(cat $filesList | awk '{print $3}'); do
if [[ -d $j ]]; then
echo "$j is a directory"
for i in $(cat $serverList | awk '{print $3}'); do
ssh root@$i "find "$j" -type f -exec md5sum {} \;" >> "$i"
done
elif [[ -f $j ]]; then
echo "$j is a file"
for i in $(cat $serverList | awk '{print $3}'); do
ssh root@$i "md5sum $j" >> "$i"
done
fi
done
## Finding Active Node with a condition ##
for i in $(cat $serverList | awk '{print $3}'); do
s1=`ssh root@$i /etc/sysconfig/ha/getactive.sh`
if [[ $s1 == "NodeType: Active" ]]; then
t1=$i
fi
done
## Compare md5sum of Active Node with All Nodes ##
for j in $(cat $serverList | awk '{print $3}'); do
f1=`grep -xvFf $t1 $j | awk '{print $2}'`
if [[ $f1 == *"/"* ]]; then
filestat=$(echo "Node Name: " $j "Out-ofSync-Filename: " $f1)
echo $filestat >> final-md5sum
fi
done
#### Calculating Last modifiction time of Inconsistent file ####
var=`cat final-md5sum`
if [ -n "$var" ]; then
awk '{print $3, $5}' final-md5sum |
while read -r ip path; do
(ssh root@"$ip" "stat -c%y $path | cut -d'.' -f1" < /dev/null) >> file-stat
done
fi
我陷入了进一步的困境,无法在日志中获取所需的日志和文件修改详细信息。如果我的方法很长或者我应该遵循任何其他方法,请随时发表评论