#!/bin/sh
###############################################################################################################
#script name: collectLog.sh
#script description: for Ansible Server collectLog
#Current Release Version: 1.0.0
#script Owner: He ,Haibo
#Latest editor: He, Haibo
#Support platform: Linux OS for redhat and centos.
#Date: 2021/10/27---first Version for collectLog.sh.
#Change log:
#Descript:sh collectLog.sh
#
###############################################################################################################
mkdirLogPath(){
if [[ ! -d /home/hbhe/logs ]];then
mkdir -p /home/hbhe/logs
chmod 767 /home/hbhe/logs
fi
ansibleLinuxLogPath="/home/hbhe/logs"
ansibleLinuxLog=ansible.log
}
copyLinuxShell(){
ansible -i hosts all -m ping >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
if [[ $? == 0 ]];then
ansible webservers -i hosts -m copy -a "src=/home/hbhe/linuxHealthCheck.sh dest=/home/hbhe/linuxHealthCheck.sh mode=0755" >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
else
echo "ansible -i hosts all -m ping failed." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
exit 1
fi
}
excuteLinuxShell(){
ansible webservers -i hosts -m shell -a "/home/hbhe/linuxHealthCheck.sh" >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
if [[ $? == 0 ]];then
echo "ansible webservers -i hosts -m shell -a /home/hbhe/linuxHealthCheck.sh excute successfully." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
else
echo "ansible webservers -i hosts -m shell -a /home/hbhe/linuxHealthCheck.sh excute failed." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
fi
}
copyLogFileToLocal(){
ansible webservers -i hosts -m synchronize -a "mode=pull src=/tmp/log/*_linuxHealthCheck_$(date +%y%m%d).log dest=$ansibleLinuxLogPath" >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
if [[ $? == 0 ]];then
echo "ansible webservers -i hosts -m synchronize -a 'mode=pull src=/tmp/log/*_linuxHealthCheck_$(date +%y%m%d).log dest=$ansibleLinuxLogPath' successfully." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
else
echo "ansible webservers -i hosts -m synchronize -a 'mode=pull src=/tmp/log/*_linuxHealthCheck_$(date +%y%m%d).log dest=$ansibleLinuxLogPath' failed." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
fi
}
moveLogs(){
if [[ ! -d "$ansibleLinuxLogPath"/$(date +%y%m%d) ]];then
mkdir "$ansibleLinuxLogPath"/$(date +%y%m%d) && cd "$ansibleLinuxLogPath" && mv *_linuxHealthCheck_$(date +%y%m%d).log "$ansibleLinuxLogPath"/$(date +%y%m%d)
else
echo "Cann't create "$ansibleLinuxLogPath"/$(date +%y%m%d),failed." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
exit 2
fi
}
main(){
mkdirLogPath
copyLinuxShell
excuteLinuxShell
copyLogFileToLocal
moveLogs
}
main
https://www.cndba.cn/hbhe0316/article/5001
https://www.cndba.cn/hbhe0316/article/5001
https://www.cndba.cn/hbhe0316/article/5001
https://www.cndba.cn/hbhe0316/article/5001
https://www.cndba.cn/hbhe0316/article/5001
https://www.cndba.cn/hbhe0316/article/5001
https://www.cndba.cn/hbhe0316/article/5001
https://www.cndba.cn/hbhe0316/article/5001
https://www.cndba.cn/hbhe0316/article/5001
https://www.cndba.cn/hbhe0316/article/5001
版权声明:本文为博主原创文章,未经博主允许不得转载。
shell



