Linux 服务器巡检脚本
#!/bin/bash cat <<EOF +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++2022年某公司系统运行情况检查+++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ EOF LOGPATH="/tmp" [ -e $LOGPATH ] || mkdir -p $LOGPATH RESULTFILE="$LOGPATH/HostCheck-`hostname`-`date +%Y%m%d`.txt" libin1=`uptime` rhcsa=`top -b -d 1 -n 1 | head -n 10` rhce=`ps aux | awk '{print $1, $2, $3, $11}' | sort -k3rn | head -n 10` rhca=`ps aux | awk '{print $1, $2, $4, $5, $6, $11}' | sort -k3rn | head -n 5` hcia=`grep -Ei "error|false|faied" /var/log/messages | tail -n5` #libin2=`top -d 5 -b` function getSystemStatus(){ echo "++++++++2022某公司系统基本运行情况检查+++++++++" date=`date +"%Y-%m-%d-%H:%M:%S"` ip_info=`ip a s bond2 | awk "NR==3" |awk -F '[ /]' '{print $6}'` system_time=`uptime | awk -F " " '{print $3,$4}' | awk -F "," '{print $1}'` load_info=`uptime |awk '{print "Current Load: "$(NF-2)}'|sed 's/\,//g'` echo "" echo -e "\033[32m-------------------------------------------\033[1m" echo 当前时间: ${date} echo IP地址: ${ip_info} echo 运行时长: ${system_time} echo 系统当前负载: ${load_info} echo -e "\033[32m-------------------------------------------\033[0m" echo "" } function cpuSystemStatus(){ echo -e "\033[33m*************************************************CPU运行情况*****************************************************\033[0m" if [[ $libin < 5 ]];then #top -b -d 1 -n 1 | head -n 10 echo "$rhcsa" else echo "no" fi echo -e "\033[33m*************************************************CPU占用前10进程*****************************************************\033[0m" if [[ $libin < 5 ]];then #ps aux | awk '{print $1, $2, $3, $11}' | sort -k3rn | head -n 10 echo "$rhce" else echo "no" fi echo -e "\033[33m*************************************************系统负载情况*****************************************************\033[0m" if [[ 5 -eq 5 ]];then echo "$libin1" else echo "no" fi echo -e "\033[33m************************************************内存使用情况*****************************************************\033[0m" if [[ $libin < 5 ]];then free -h else echo "no" fi echo -e "\033[33m************************************************内存使用情况*****************************************************\033[0m" if [[ $libin < 5 ]];then #ps aux | awk '{print $1, $2, $4, $5, $6, $11}' | sort -k3rn | head -n 5 echo "$rhca" else echo "no" fi echo -e "\033[33m************************************************系统日志异常*****************************************************\033[0m" if [[ $libin < 5 ]];then echo "$hcia" else echo "no" fi } function check(){ getSystemStatus cpuSystemStatus } #执行检查并保存检查结果 check > $RESULTFILE echo -e "\033[44;37m 主机巡检结果存放在:$RESULTFILE \033[0m"