针对Linux的检查,编写了shell脚本,如下:
#!/bin/sh
###############################################################################################################
#Script name: linuxHealthCheck.sh
#Script description: for Linux Health Check
#Current Release Version: 1.0.0
#Script Owner: hbhe0316
#Latest editor: hbhe0316
#Support platform: Linux OS for redhat and centos.
#Date: 2021/10/15---first Version for linuxHealthCheck.sh.
#Change log:
#Descript:sh linuxHealthCheck.sh
#
###############################################################################################################
export LANG=en_US
osbox=`uname`
RHversion=$(cat /proc/version | sed 's/[^0-9]//g' | cut -b -3)
###打印日志函数
log_info(){
DATE_N=`date "+%Y-%m-%d %H:%M:%S"`
USER_N=`whoami`
echo "${DATE_N} ${USER_N} execute $0 [INFO] $@"
}
log_error(){
DATE_N=`date "+%Y-%m-%d %H:%M:%S"`
USER_N=`whoami`
echo -e "/033[41;37m ${DATE_N} ${USER_N} execute $0 [ERROR] $@ /033[0m"
}
fn_log(){
if [[ $? -eq 0 ]];then
log_info "$@ sucessed."
echo -e "/033[32m $@ sucessed. /033[0m"
else
log_error "$@ failed."
echo -e "/033[41;37m $@ failed. /033[0m"
fi
}
checkExecuteUser(){
uid=`id -u`
if [[ $uid == 0 ]];then
continue
else
fn_log "Current excute user is not root ,shell will exist."
exit 1
fi
}
###Create /tmp/log if not exist.
mkdirLogPath(){
if [[ ! -d /tmp/log ]];then
mkdir -p /tmp/log
chmod 767 /tmp/log
fi
CheckLinuxLogPath="/tmp/log"
CheckLinuxLog=`hostname`_$(date +%y%m%d).txt
}
###Get OS Arch Linux or not
getOsArch(){
if [[ "$osbox" == "Linux" ]];then
continue
else
fn_log "Current OS is $osbox,shell is exit now."
echo 0
exit 0
fi
}
###Get redhat or centos
getOsCentosOrRedhat(){
cat /proc/version | grep -iE "redhat|centos" > /dev/null
if [[ $? == 0 ]];then
continue
else
echo "Current OS is not centos or redhat."
echo 1
exit 1
fi
}
networkinfo(){
#先过滤网卡名称,存到数组DEVNAME中
DEVNAME=(`ifconfig | grep ^[a-z] | awk -F: '{print $1}'`)
#在拿到IP地址,存到数组IPADDR中
IPADDR=(`ifconfig | grep 'inet' | sed 's/^.*inet //g' | sed 's/ *netmask.*$//g' | grep -v "inet6"`)
##get RX
RX=(`ifconfig | grep 'RX' | awk '{print $3/1024/1024}'`)
TX=(`ifconfig | grep 'TX' | awk '{print $3/1024/1024}'`)
RX_ERROR=(`ifconfig | grep "RX errors" | awk -F ' ' {'print $3'}`)
TX_ERROR=(`ifconfig | grep "TX errors" | awk -F ' ' {'print $3'}`)
for ((i=0;i<${#DEVNAME[@]};i++))
do
echo ${DEVNAME[$i]} ${IPADDR[$i]} ${RX[$i]} ${TX[$i]} ${RX_ERROR[$i]} ${TX_ERROR[$i]}
done
}
osSystem(){
os_type=`uname`
echo "Current System is:$os_type"
os_banben=`cat /etc/redhat-release`
echo "Current System Version is:$os_banben"
os_neihe=`uname -r`
echo "Current System Kernel is:$os_neihe"
os_time=`date +%F_%T`
echo "Current System Time is:$os_time"
os_last=`uptime |awk '{print $3}'`
echo "Current System Last Reboot Time is:$os_last Days."
os_hostname=`hostname`
echo "Current System Hostname is:$os_hostname"
}
#硬件信息
cpuInfo(){
#cpu的物理个数
#cpu_phy=`cat /proc/cpuinfo | grep "physical id" | wc -l`
#echo "当前系统物理cpu为:$cpu_phy"
#cpu的核心数
#cpu_core=`cat /proc/cpuinfo | grep "core" | wc -l`
#echo "当前系统cpu核心数为:$cpu_core"
#cpu的型号
#cpu_model=`cat /proc/cpuinfo | grep "model" | sed -n '2p' | awk -F : '{print $2}'`
#echo "当前系统cpu型号是:$cpu_model"
cpu_idle=$(top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d ".")
if [[ "$cpu_idle" -gt 80 ]]; then
echo "Check System CPU used is geater than 80%,warning."
else
echo "Check System CPU used is $cpu_idle%,success."
fi
}
##内存信息
memInfo(){
mem_total=`free -m | grep Mem | awk '{print $2}'`
# echo "内存总大小为:$mem_total"
mem_used=`free -m | grep Mem | awk '{print $3}'`
# echo "已使用内存为:$mem_used"
mem_free=`free -m | grep Mem | awk '{print $4}'`
# echo "剩余内存为:$mem_free"
#百分比
mem_used_percent=`echo "scale=2;$mem_used/$mem_total*100"|bc`
mem_used_percent=`echo $mem_used_percent | awk -F '.' {'print $1'}`
# echo "已使用内存百分比为:$mem_used_percent"
if [[ $mem_used_percent -gt 80 ]];then
echo "Check System have memory used percent geater than 80%,warning."
else
echo "Check System have memory used percent is $mem_used_percent%,success."
fi
}
swapInfo(){
swap_total=`free -m | grep Swap | awk '{print $2}'`
swap_used=`free -m | grep Swap | awk '{print $3}'`
swap_used_percent=`echo "scale=2;$swap_used/$swap_total*100"|bc`
swap_used_percent=`echo $swap_used_percent | awk -F '.' {'print $1'}`
if [[ $swap_used_percent -gt 80 ]];then
echo "Check System have swap used percent geater than 80%,warning."
else
echo "Check Swap Used is $swap_used_percent%,success."
fi
}
##get df -h infomation
dfInfo(){
percent=`df -h | awk '{print int($5)}'`
for each_one in $percent
do
if [[ $each_one -gt 80 ]];then
echo "Check System have filedisk used geater than 80%,warning."
else
echo "Check File System success."
fi
done
}
userLogin(){
userNum=`uptime |awk '{print $6}'`
if [[ $userNum -gt 3 ]];then
echo "Check System User Login Number is greater than 3,warning."
else
echo "Check Current User Login is $userNum,success."
fi
}
checkDefunct(){
DefunctNum=`ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' | wc -l`
if [[ $DefunctNum == 0 ]];then
echo "Check Defunct process,success."
else
echo "Check System have Defunct process,warning."
fi
}
checkIOInfo(){
IOPercent=`iostat | head -4 | awk 'END {print}' | awk -F ' ' {'print $6'} | sed 's/[ /t]//g'`
IOPercent=`echo $IOPercent | awk -F '.' {'print $1'}`
if [[ $IOPercent -lt 70 ]];then
echo "Check System is letter than 70%,warning."
else
echo "Check iostat is $IOPercent%,success."
fi
}
checkNtp(){
ntpstat > /dev/null 2>&1
if [[ $? == 0 ]];then
echo "Check ntp status success."
else
echo "Check ntp status warning."
fi
}
main(){
checkExecuteUser
getOsArch
getOsCentosOrRedhat
mkdirLogPath
osSystem
cpuInfo
memInfo
swapInfo
dfInfo
checkIOInfo
# networkinfo
userLogin
checkDefunct
checkNtp
}
main
版权声明:本文为博主原创文章,未经博主允许不得转载。
oracle,linux