该脚本主要将巡检服务器的CPU、内存、磁盘、服务进程情况打印并生成到txt文档中
#/bin/bash
#Linux Check Script
#v20220222
#
dat=`date +%Y-%m-%d`
mem=`free -m |grep "Mem" |awk -F ' ' '{print $2}'`
vers=`cat /etc/redhat-release`
ipp=`ifconfig |grep 'inet addr' |grep -v '127.0.0.1' |awk -F ' ' '{print $2}' |awk -F ':' '{print $2}' |sed -n 1p`
hn=`hostname`
pycpu=`cat /proc/cpuinfo | grep "physical id"|sort | uniq | wc -l`
locpu=`cat /proc/cpuinfo | grep "processor"| wc -l`
centosversion=`awk '{print $(NF-1)}' /etc/redhat-release`
resultfile="/check/${hn}_${dat}_${ipp}.txt"
Physical_CPUs=`grep "physical id" /proc/cpuinfo| sort | uniq | wc -l`
Virt_CPUs=`grep "processor" /proc/cpuinfo | wc -l`
CPU_Kernels=`grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}'`
CPU_Type=`grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq`
CPU_Arch=`uname -m`
SELinux=`/usr/sbin/sestatus | grep "SELinux status: " | awk '{print $3}'`
uptime=`uptime | sed 's/.*up \([^,]*\), .*/\1/'`
mysql=`ps -ef |grep mysql |grep -v 'grep' |wc -l`
nginx=`ps -ef |grep nginx |grep -v 'grep' |wc -l`
redis=`ps -ef |grep redis |grep -v 'grep' |wc -l`
java=`ps -ef |grep java |grep -v 'grep' |wc -l`
function getjiemian()
{
clear
echo " "
echo " "
echo " ******************************服务器日常巡检*****************************"
echo " "
echo "-- 服务器巡检完毕 !......"
echo "-- 服务器当前日期:${dat}"
echo "-- 主机名:${hn}"
echo " "
echo "========================================================================="  
echo "1.内存(总):${mem} MB"
echo "2.操作系统版本:${vers}"  
echo "3.IP地址: ${ipp}"
echo "4.物理CPU个数:${pycpu}"
echo "5.逻辑CPU个数:${locpu}"
echo "6.CPU型号:${CPU_Type}"
echo "7.CPU架构:${CPU_Arch}"
echo "8.SELINUX:${SELinux}"
echo "9.系统运行时间:${uptime}"
echo "=========================================================================" 
sleep 5
}
function getmemstatus()
{
    echo ""
    echo ""
    echo "************************** 内存检查 **************************"
    if [[ $centosVersion < 7 ]];then
        free -m
    else
        free -h
    fi
}
function getdiskstatus()
{
    echo ""
    echo ""
    echo "************************** 磁盘检查 **************************"
    df -hiP | sed 's/Mounted on/Mounted/'> /tmp/inode
    df -hTP | sed 's/Mounted on/Mounted/'> /tmp/disk 
    join /tmp/disk /tmp/inode | awk '{print $1,$2,"|",$3,$4,$5,$6,"|",$8,$9,$10,$11,"|",$12}'| column -t
}
function getprocessstatus()
{
    echo ""
    echo ""
    echo "************************** PID进程检查 **************************"
    if [ $(ps -ef | grep defunct | grep -v grep | wc -l) -ge 1 ];then
        echo ""
        echo "僵尸进程";
        echo "--------"
        ps -ef | head -n1
        ps -ef | grep defunct | grep -v grep
    fi
    echo ""
    echo "内存占用TOP10"
    echo "-------------"
    echo -e "PID %MEM RSS COMMAND
    $(ps aux | awk '{print $2, $4, $6, $11}' | sort -k3rn | head -n 10 )"| column -t 
    echo ""
    echo "CPU占用TOP10"
    echo "------------"
    top b -n1 | head -17 | tail -11
}
function getservicestatus()
{
echo ""
    echo ""
    echo "************************** 服务进程检查 **************************"
    if [ ${mysql} -gt 0 ];then
        echo ""
        echo "-------------------"
        echo "MYSQL数据库进程正常运行!"
    else
        echo ""
        echo "-------------------"
        echo "MYSQL数据库进程已挂掉,请重启!"
     fi
   
    if [ ${nginx} -gt 0 ];then
        echo "" 
        echo "-------------------"
        echo "nginx进程正常运行!"
    else
        echo ""
        echo "-------------------"
        echo "nginx进程已挂掉,请重启!"
     fi
     if [ ${redis} -gt 0 ];then
        echo "" 
        echo "-------------------"
        echo "redis进程正常运行!"
    else
        echo ""
        echo "-------------------"
        echo "redis进程已挂掉,请重启!"
     fi
     if [ ${java} -gt 0 ];then
        echo "" 
        echo "-------------------"
        echo "管理系统进程正常运行!"
    else
        echo ""
        echo "-------------------"
        echo "管理系统数据库进程已挂掉,请重启!"
     fi
}
getjiemian
getmemstatus
getdiskstatus
getprocessstatus
getservicestatus
function check()
{
getjiemian
getmemstatus
getdiskstatus
getprocessstatus
getservicestatus
}
#执行检查并保存检查结果
check > ${resultfile} 
echo " " 
echo " " 
echo "检查结果:"
echo "---------"
echo "${resultfile}"
echo " " 
echo " " 
#sed '/^SQL/d' -i /check/${resultfile}









