Linux 服务器巡检脚本

2022/9/5 5:52:50

本文主要是介绍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"

  



这篇关于Linux 服务器巡检脚本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程