栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ansible收集日志归类脚本

ansible收集日志归类脚本

#!/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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731051.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号