Linux巡检脚本,兼容Redhat、Cantos、suse

本文介绍了一个全面的系统健康检查脚本,用于自动化收集Linux系统的各项指标,包括系统信息、CPU状态、内存状态、磁盘状态、网络状态、服务状态等,帮助管理员快速了解系统运行状况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PADDR=$(ip addr show|grep ‘inet’|awk -F ‘[ :]’ ‘{print $13}’)
#环境变量PATH没设好,在cron里执行时有很多命令会找不到
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
source /etc/profile
 
[ KaTeX parse error: Expected 'EOF', got '&' at position 17: …id -u) -gt 0 ] &̲& echo "请用root用…(awk ‘{print $(NF-1)}’ /etc/-release)
VERSION=“2017.08.22”
 
#日志相关
PROGPATH=echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'
[ -f KaTeX parse error: Expected 'EOF', got '&' at position 12: PROGPATH ] &̲& PROGPATH="." …PROGPATH/log"
[ -e $LOGPATH ] || mkdir LOGPATHRESULTFILE="LOGPATH RESULTFILE="LOGPATHRESULTFILE="LOGPATH/HostDailyCheck-$IPADDR-date +%Y%m%d.txt"
 
 
#定义报表的全局变量
report_DateTime=""    #日期 ok
report_Hostname=""    #主机名 ok
report_OSRelease=""    #发行版本 ok
report_Kernel=""    #内核 ok
report_Language=""    #语言/编码 ok
report_LastReboot=""    #最近启动时间 ok
report_Uptime=""    #运行时间(天) ok
report_CPUs=""    #CPU数量 ok
report_CPUType=""    #CPU类型 ok
report_Arch=""    #CPU架构 ok
report_MemTotal=""    #内存总容量(MB) ok
report_MemFree=""    #内存剩余(MB) ok
report_MemUsedPercent=""    #内存使用率% ok
report_DiskTotal=""    #硬盘总容量(GB) ok
report_DiskFree=""    #硬盘剩余(GB) ok
report_DiskUsedPercent=""    #硬盘使用率% ok
report_InodeTotal=""    #Inode总量 ok
report_InodeFree=""    #Inode剩余 ok
report_InodeUsedPercent=""    #Inode使用率 ok
report_IP=""    #IP地址 ok
report_MAC=""    #MAC地址 ok
report_Gateway=""    #默认网关 ok
report_DNS=""    #DNS ok
report_Listen=""    #监听 ok
report_Selinux=""    #Selinux ok
report_Firewall=""    #防火墙 ok
report_USERs=""    #用户 ok
report_USEREmptyPassword=""   #空密码用户 ok
report_USERTheSameUID=""      #相同ID的用户 ok
report_PasswordExpiry=""    #密码过期(天) ok
report_RootUser=""    #root用户 ok
report_Sudoers=""    #sudo授权  ok
report_SSHAuthorized=""    #SSH信任主机 ok
report_SSHDProtocolVersion=""    #SSH协议版本 ok
report_SSHDPermitRootLogin=""    #允许root远程登录 ok
report_DefunctProsess=""    #僵尸进程数量 ok
report_SelfInitiatedService=""    #自启动服务数量 ok
report_SelfInitiatedProgram=""    #自启动程序数量 ok
report_RuningService=""           #运行中服务数  ok
report_Crontab=""    #计划任务数 ok
report_Syslog=""    #日志服务 ok
report_SNMP=""    #SNMP  OK
report_NTP=""    #NTP ok
report_JDK=""    #JDK版本 ok
function version(){
    echo “”
    echo “”
    echo "系统巡检脚本:Version KaTeX parse error: Expected 'EOF', got '}' at position 10: VERSION" }̲   function get…(grep “physical id” /proc/cpuinfo| sort | uniq | wc -l)
    Virt_CPUs=(grep"processor"/proc/cpuinfo∣wc−l)   CPUKernels=(grep "processor" /proc/cpuinfo | wc -l)     CPU_Kernels=(grep"processor"/proc/cpuinfowcl)   CPUKernels=(grep “cores” /proc/cpuinfo|uniq| awk -F ': ’ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')     CPU_Type…(grep “model name” /proc/cpuinfo | awk -F ': ’ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲' | sort | uniq…(uname -m)
    echo “物理CPU个数:PhysicalCPUs"   echo"逻辑CPU个数:Physical_CPUs"     echo "逻辑CPU个数:PhysicalCPUs"   echo"CPU:Virt_CPUs”
    echo “每CPU核心数:CPUKernels"   echo"   CPU型号:CPU_Kernels"     echo "    CPU型号:CPUKernels"   echo"   CPU:CPU_Type”
    echo "    CPU架构:KaTeX parse error: Expected 'EOF', got '#' at position 15: CPU_Arch"     #̲报表信息     report…Virt_CPUs    #CPU数量
    report_CPUType=KaTeX parse error: Expected 'EOF', got '#' at position 11: CPU_Type  #̲CPU类型     repor…CPU_Arch     #CPU架构
}
 
function getMemStatus(){
    echo “”
    echo “”
    echo “############################ 内存检查 ############################”
    if [[ KaTeX parse error: Expected 'EOF', got '#' at position 80: … -h     fi     #̲报表信息     MemTot…(grep MemTotal /proc/meminfo| awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')  #KB     Mem…(grep MemFree /proc/meminfo| awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')    #KB     l…(awk "BEGIN {if(KaTeX parse error: Can't use function '\"' in math mode at position 37: …00}else{printf \̲"̲%.2f\",MemUsed
100/KaTeX parse error: Expected 'EOF', got '}' at position 9: MemTotal}̲}")     report_…((MemTotal/1024))"“MB”        #内存总容量(MB)
    report_MemFree="KaTeX parse error: Expected 'EOF', got '#' at position 32: …""MB"          #̲内存剩余(MB)     re…(awk “BEGIN {if(KaTeX parse error: Can't use function '\"' in math mode at position 37: …00}else{printf \̲"̲%.2f\",MemUsed100/$MemTotal}}")""%"   #内存使用率%
}
 
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,"|",KaTeX parse error: Expected 'EOF', got '}' at position 3: 12}̲'| column -t   …(df -TP | sed ‘1d’ | awk 'KaTeX parse error: Expected 'EOF', got '#' at position 21: …mpfs"{print}') #̲KB     disktota…(echo “$diskdata” | awk '{total+=KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲END{print total…(echo “$diskdata” | awk '{total+=KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲END{print total…((disktotal-diskused)) #KB
    diskusedpercent=$(echo $disktotal $diskused | awk '{if($1==0){printf 100}else{printf “%.2f”,$2
100/KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲}')     inoded…(df -iTP | sed ‘1d’ | awk '2!="tmpfs"print′)   inodetotal=2!="tmpfs"{print}')     inodetotal=2!="tmpfs"print)   inodetotal=(echo “$inodedata” | awk '{total+=KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲END{print total…(echo “$inodedata” | awk '{total+=KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲END{print total…((inodetotal-inodeused))
    inodeusedpercent=$(echo $inodetotal $inodeused | awk '{if($10){printf 100}else{printf “%.2f”,$2100/KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲}')     report_…((disktotal/1024/1024))“GB”   #硬盘总容量(GB)
    report_DiskFree=KaTeX parse error: Expected 'EOF', got '#' at position 32: …1024))"GB"     #̲硬盘剩余(GB)     re…diskusedpercent""%"    #硬盘使用率%
    report_InodeTotal=KaTeX parse error: Expected 'EOF', got '#' at position 30: …000))"K"       #̲Inode总量     rep…((inodefree/1000))“K”         #Inode剩余
    report_InodeUsedPercent="KaTeX parse error: Expected 'EOF', got '}' at position 36: …  #Inode使用率%   }̲   function get…(grep “LANG=” /etc/sysconfig/i18n | grep -v “^#” | awk -F ‘"’ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')"     else   …LANG
    fi
    export LANG=“en_US.UTF-8”
    Release=(cat/etc/redhat−release2>/dev/null)   Kernel=(cat /etc/redhat-release 2>/dev/null)     Kernel=(cat/etc/redhatrelease2>/dev/null)   Kernel=(uname -r)
    OS=(uname−o)   Hostname=(uname -o)     Hostname=(unameo)   Hostname=(uname -n)
    LastReboot=$(who -b | awk '{print $3,KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲')     uptime=(uptime | sed 's/.up ([^,]), .
/\1/’)
    echo "     系统:OS"   echo"发行版本:OS"     echo " 发行版本:OS"   echo"Release"
    echo "     内核:Kernel"   echo"  主机名:Kernel"     echo "   主机名:Kernel"   echo"  Hostname"
    echo "  SELinux:SELinux"   echo"语言/编码:SELinux"     echo "语言/编码:SELinux"   echo"/default_LANG"
    echo " 当前时间:(date+′   echo"最后启动:(date +'%F %T')"     echo " 最后启动:(date+   echo"LastReboot"
    echo " 运行时间:KaTeX parse error: Expected 'EOF', got '#' at position 13: uptime"     #̲报表信息     report…(date +"%F %T")  #日期
    report_Hostname=“KaTeX parse error: Expected 'EOF', got '#' at position 17: …ostname"       #̲主机名     report_…Release”       #发行版本
    report_Kernel=“KaTeX parse error: Expected 'EOF', got '#' at position 19: …nel"           #̲内核     report_L…default_LANG”   #语言/编码
    report_LastReboot=“KaTeX parse error: Expected 'EOF', got '#' at position 15: LastReboot"   #̲最近启动时间     repo…uptime”           #运行时间(天)
    report_Selinux=“SELinux"   exportLANG="SELinux"     export LANG="SELinux"   exportLANG="default_LANG”
 
}
 
function getServiceStatus(){
    echo “”
    echo “”
    echo “############################ 服务检查 ############################”
    echo “”
    if [[ centosVersion>7]];then       conf=centosVersion > 7 ]];then         conf=centosVersion>7]];then       conf=(systemctl list-unit-files --type=service --state=enabled --no-pager | grep “enabled”)
        process=KaTeX parse error: Expected 'EOF', got '#' at position 92: …vice")         #̲报表信息         re…(echo “KaTeX parse error: Expected 'EOF', got '#' at position 23: … wc -l)"       #̲自启动服务数量        …(echo “KaTeX parse error: Expected 'EOF', got '#' at position 30: …-l)"           #̲运行中服务数量     els…(/sbin/chkconfig | grep -E “:on|:启用”)
        process=KaTeX parse error: Expected 'EOF', got '#' at position 78: …正在运行")         #̲报表信息         re…(echo “KaTeX parse error: Expected 'EOF', got '#' at position 23: … wc -l)"       #̲自启动服务数量        …(echo “KaTeX parse error: Expected 'EOF', got '#' at position 30: …-l)"           #̲运行中服务数量     fi …conf”  | column -t
    echo “”
    echo “正在运行的服务”
    echo “--------------”
    echo “KaTeX parse error: Expected 'EOF', got '}' at position 12: process"   }̲     function g…(grep -v “^#” /etc/init.d/boot.local| sed '/^KaTeX parse error: Expected 'EOF', got '#' at position 6: /d') #̲   echo "conf”
    #报表信息
    report_SelfInitiatedProgram=”$(echo $conf | wc -l)”    #自启动程序数量
}
 
function getLoginStatus(){
    echo “”
    echo “”
    echo “############################ 登录检查 ############################”
    last | head
}
 
function getNetworkStatus(){
    echo “”
    echo “”
    echo “############################ 网络检查 ############################”
    if [[ $centosVersion < 7 ]];then
        /sbin/ifconfig -a | grep -v packets | grep -v collisions | grep -v inet6
    else
        #ip a
        for i in $(ip link | grep BROADCAST | awk -F: ‘{print $2}’);do ip add show $i | grep -E “BROADCAST|global”| awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲' | tr '\n' ' '…(ip route | grep default | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲')     DNS=(grep nameserver /etc/resolv.conf| grep -v “#” | awk ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲' | tr '\n' ','…//’)
    echo “”
    echo “网关:GATEWAY&quot;   echo&quot;DNS:GATEWAY &quot;     echo &quot; DNS:GATEWAY"   echo"DNSDNS”
    #报表信息
    IP=$(ip -f inet addr | grep -v 127.0.0.1 |  grep inet | awk ‘{print $NF,KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲' | tr '\n' ','…//’)
    MAC=$(ip link | grep -v “LOOPBACK|loopback” | awk ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲' | sed 'N;s/\n…//’)
    report_IP=“KaTeX parse error: Expected 'EOF', got '#' at position 16: IP"            #̲IP地址     report…MAC            #MAC地址
    report_Gateway=“KaTeX parse error: Expected 'EOF', got '#' at position 11: GATEWAY"  #̲默认网关     report…DNS”          #DNS
}
 
function getListenStatus(){
    echo “”
    echo “”
    echo “############################ 监听检查 ############################”
    TCPListen=(ss−ntul∣column−t)   echo&quot;(ss -ntul | column -t)     echo &quot;(ssntulcolumnt)   echo"TCPListen”
    #报表信息
    report_Listen=“(echo&quot;(echo &quot;(echo"TCPListen”| sed ‘1d’ | awk ‘/tcp/ {print $5}’ | awk -F: ‘{print $NF}’ | sort | uniq | wc -l)”
}
 
function getCronStatus(){
    echo “”
    echo “”
    echo “############################ 计划任务检查 ########################”
    Crontab=0
    for shell in $(grep -v “/sbin/nologin” /etc/shells);do
        for user in (grep&quot;(grep &quot;(grep"shell" /etc/passwd| awk -F: ‘{print $1}’);do
            crontab -l -u KaTeX parse error: Expected 'EOF', got '&' at position 19: …r >/dev/null 2>&̲1             s…?
            if [ status−eq0];then               echo&quot;status -eq 0 ];then                 echo &quot;statuseq0];then               echo"user"
                echo “--------”
                crontab -l -u user               letCrontab=Crontab+user                 let Crontab=Crontab+user               letCrontab=Crontab+(crontab -l -u KaTeX parse error: Expected 'EOF', got '#' at position 80: …e     done     #̲计划任务     find /…(find /etc/cron* -type f | wc -l)
    #报表信息
    report_Crontab="KaTeX parse error: Expected 'EOF', got '#' at position 13: Crontab"    #̲计划任务数 } functio…"
    [ -z “$datetime” ] && echo "错误的参数:getHowLongAgo() ∗&quot;   Timestamp=*&quot;     Timestamp="   Timestamp=(date +%s -d "KaTeX parse error: Expected 'EOF', got '#' at position 15: datetime")    #̲转化为时间戳     Now_…(date +%s)
    Difference_Timestamp=((((((Now_Timestamp-Timestamp))   days=0;hours=0;minutes=0;   secinday=Timestamp))     days=0;hours=0;minutes=0;     sec_in_day=Timestamp))   days=0;hours=0;minutes=0;   secinday=((60
6024));
    sec_in_hour=$((60
60));
    sec_in_minute=60
    while (( ((((((Difference_Timestamp-$sec_in_day)) > 1 ))
    do
        let Difference_Timestamp=Difference_Timestamp-sec_in_day
        let days++
    done
    while (( ((((((Difference_Timestamp-secinhour))&gt;1))   do       letDifferenceTimestamp=DifferenceTimestamp−secinhour       lethours++   done   echo&quot;sec_in_hour)) &gt; 1 ))     do         let Difference_Timestamp=Difference_Timestamp-sec_in_hour         let hours++     done     echo &quot;secinhour))>1))   do       letDifferenceTimestamp=DifferenceTimestampsecinhour       lethours++   done   echo"days 天 $hours 小时前"
}
 
function getUserLastLogin(){
    # 获取用户最近一次登录的时间,含年份
    # 很遗憾last命令不支持显示年份,只有"last -t YYYYMMDDHHMMSS"表示某个时间之间的登录,我
    # 们只能用最笨的方法了,对比今天之前和今年元旦之前(或者去年之前和前年之前……)某个用户
    # 登录次数,如果登录统计次数有变化,则说明最近一次登录是今年。
    username=$1
    : username:=&quot;‘whoami‘&quot;   thisYear={username:=&quot;`whoami`&quot;}     thisYear=username:="whoami"   thisYear=(date +%Y)
    oldesYear=$(last | tail -n1 | awk ‘{print $NF}’)
    while(( $thisYear >= oldesYear));do       loginBeforeToday=oldesYear));do         loginBeforeToday=oldesYear));do       loginBeforeToday=(last $username | grep username∣wc−l)       loginBeforeNewYearsDayOfThisYear=username | wc -l)         loginBeforeNewYearsDayOfThisYear=usernamewcl)       loginBeforeNewYearsDayOfThisYear=(last $username -t $thisYear"0101000000" | grep $username | wc -l)
        if [ $loginBeforeToday -eq 0 ];then
            echo “从未登录过”
            break
        elif [ $loginBeforeToday -gt loginBeforeNewYearsDayOfThisYear];then           lastDateTime=loginBeforeNewYearsDayOfThisYear ];then             lastDateTime=loginBeforeNewYearsDayOfThisYear];then           lastDateTime=(last -i KaTeX parse error: Expected '}', got 'EOF' at end of input: …++)printf"%s ",i}’)" KaTeX parse error: Expected 'EOF', got '#' at position 11: thisYear" #̲格式如: Sat Nov 2 …(date “+%Y-%m-%d %H:%M:%S” -d “lastDateTime&quot;)           echo&quot;lastDateTime&quot;)             echo &quot;lastDateTime")           echo"lastDateTime”
            break
        else
            thisYear=KaTeX parse error: Expected 'EOF', got '}' at position 38: … fi     done   }̲   function get…(cat /etc/passwd)"
    Modify=$(stat /etc/passwd | grep Modify | tr ‘.’ ’ ’ | awk '{print $2,KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲')       echo "…Modify ($(getHowLongAgo $Modify))"
    echo “”
    echo “特权用户”
    echo “--------”
    RootUser=""
    for user in (echo&quot;(echo &quot;(echo"pwdfile" | awk -F: ‘{print $1}’);do
        if [ $(id -u user)−eq0];then           echo&quot;user) -eq 0 ];then             echo &quot;user)eq0];then           echo"user"
            RootUser=“RootUser,RootUser,RootUser,user”
        fi
    done
    echo “”
    echo “用户列表”
    echo “--------”
    USERs=0
    echo “$(
    echo “用户名 UID GID HOME SHELL 最后一次登录”
    for shell in $(grep -v “/sbin/nologin” /etc/shells);do
        for username in (grep&quot;(grep &quot;(grep"shell” /etc/passwd| awk -F: '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲');do          …(getUserLastLogin username)&quot;           echo&quot;username)&quot;             echo &quot;username)"           echo"pwdfile" | grep -w “username&quot;∣grep−w&quot;username&quot; |grep -w &quot;username"grepw"shell”| awk -F: -v lastlogin=“(echo&quot;(echo &quot;(echo"userLastLogin” | tr ’ ’ ‘_’)" '{print $1,$3,$4,$6,KaTeX parse error: Expected 'EOF', got '}' at position 12: 7,lastlogin}̲'         done …(echo “pwdfile&quot;∣grep&quot;pwdfile&quot; | grep &quot;pwdfile"grep"shell”| wc -l)
    done
    )" | column -t
    echo “”
    echo “空密码用户”
    echo “----------”
    USEREmptyPassword=""
    for shell in $(grep -v “/sbin/nologin” /etc/shells);do
            for user in (echo&quot;(echo &quot;(echo"pwdfile" | grep "shell&quot;∣cut−d:−f1);do           r=shell&quot; | cut -d: -f1);do             r=shell"cutd:f1);do           r=(awk -F: '$2
”!!"{print $1}’ /etc/shadow | grep -w $user)
            if [ ! -z $r ];then
                echo r               USEREmptyPassword=&quot;r                 USEREmptyPassword=&quot;r               USEREmptyPassword="USEREmptyPassword,"r           fi       done      done   echo&quot;&quot;   echo&quot;相同ID的用户&quot;   echo&quot;−−−−−−−−−−−−&quot;   USERTheSameUID=&quot;&quot;   UIDs=r             fi         done        done     echo &quot;&quot;     echo &quot;相同ID的用户&quot;     echo &quot;------------&quot;     USERTheSameUID=&quot;&quot;     UIDs=r           fi       done      done   echo""   echo"ID"   echo""   USERTheSameUID=""   UIDs=(cut -d: -f3 /etc/passwd | sort | uniq -c | awk ‘$1>1{print $2}’)
    for uid in UIDs;do       echo−n&quot;UIDs;do         echo -n &quot;UIDs;do       echon"uid";
        USERTheSameUID="uid&quot;       r=uid&quot;         r=uid"       r=(awk -F: ‘ORS="";3==′&quot;3==&#x27;&quot;3=="uid"’{print “:”,KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲' /etc/passwd) …r"
        echo “”
        USERTheSameUID="$USERTheSameUID KaTeX parse error: Expected 'EOF', got '#' at position 18: …"     done     #̲报表信息     report…USERs"    #用户
    report_USEREmptyPassword=$(echo USEREmptyPassword∣sed′s/,//′)   reportUSERTheSameUID=USEREmptyPassword | sed &#x27;s/^,//&#x27;)     report_USERTheSameUID=USEREmptyPasswordseds/,//)   reportUSERTheSameUID=(echo USERTheSameUID∣sed′s/,USERTheSameUID | sed &#x27;s/,USERTheSameUIDseds/,//’)
    report_RootUser=$(echo KaTeX parse error: Expected 'EOF', got '#' at position 29: …d 's/^,//')    #̲特权用户 }     func…(cat /etc/passwd)"
    echo “”
    echo “密码过期检查”
    echo “------------”
    result=""
    for shell in $(grep -v “/sbin/nologin” /etc/shells);do
        for user in (echo&quot;(echo &quot;(echo"pwdfile" | grep “shell&quot;∣cut−d:−f1);do           getexpirydate=shell&quot; | cut -d: -f1);do             get_expiry_date=shell"cutd:f1);do           getexpirydate=(/usr/bin/chage -l $user | grep ‘Password expires’ | cut -d: -f2)
            if [[ $get_expiry_date = ’ never’ || $get_expiry_date = ‘never’ ]];then
                printf “%-15s 永不过期\n” user               result=&quot;user                 result=&quot;user               result="result,user:never&quot;           else               passwordexpirydate=user:never&quot;             else                 password_expiry_date=user:never"           else               passwordexpirydate=(date -d “getexpirydate&quot;&quot;+               currentdate=get_expiry_date&quot; &quot;+%s&quot;)                 current_date=getexpirydate""+               currentdate=(date “+%s”)
                diff=((((((password_expiry_date-currentdate))               letDAYS=current_date))                 let DAYS=currentdate))               letDAYS=(($diff/(606024)))
                printf “%-15s %s天后过期\n” $user DAYS               result=&quot;DAYS                 result=&quot;DAYS               result="result,user:user:user:DAYS days”
            fi
        done
    done
    report_PasswordExpiry=$(echo KaTeX parse error: Expected 'EOF', got '#' at position 93: …"     grep -v "#̲" /etc/login.de…(grep -v “^#” /etc/sudoers| grep -v “^Defaults” | sed '/^/d′)   echo&quot;/d&#x27;)     echo &quot;/d)   echo"conf”
    echo “”
    #报表信息
    report_Sudoers="$(echo $conf | wc -l)"
}
 
function getInstalledStatus(){
    echo “”
    echo “”
    echo “############################ 软件检查 ############################”
    rpm -qa --last | head | column -t
}
 
function getProcessStatus(){
    echo “”
    echo “”
    echo “############################ 进程检查 ############################”
    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, KaTeX parse error: Expected 'EOF', got '}' at position 3: 11}̲' | sort -k3rn …(ps -ef | grep defunct | grep -v grep|wc -l)”
}
 
function getJDKStatus(){
    echo “”
    echo “”
    echo “############################ JDK检查 #############################”
    java -version 2>/dev/null
    if [ KaTeX parse error: Expected 'EOF', got '&' at position 40: …ava -version 2>&̲1     fi     ec…JAVA_HOME""
    #报表信息
    report_JDK="$(java -version 2>&1 | grep version | awk ‘{print $1,KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲' | tr -d '"')"…(getState rsyslog)"
    echo “”
    echo “/etc/rsyslog.conf”
    echo “-----------------”
    cat /etc/rsyslog.conf 2>/dev/null | grep -v “^#” | grep -v "^\KaTeX parse error: Expected group after '^' at position 11: " | sed '/^̲/d’  | column -t
    #报表信息
    report_Syslog="$(getState rsyslog)"
}
function getFirewallStatus(){
    echo “”
    echo “”
    echo “############################ 防火墙检查 ##########################”
    #防火墙状态,策略等
    if [[ KaTeX parse error: Expected 'EOF', got '&' at position 77: … >/dev/null  2>&̲1         statu…?
        if [ $status -eq 0 ];then
                s=“active”
        elif [ $status -eq 3 ];then
                s=“inactive”
        elif [ status−eq4];then               s=&quot;permissiondenied&quot;       else               s=&quot;unknown&quot;       fi   else       s=&quot;status -eq 4 ];then                 s=&quot;permission denied&quot;         else                 s=&quot;unknown&quot;         fi     else         s=&quot;statuseq4];then               s="permissiondenied"       else               s="unknown"       fi   else       s="(getState iptables)"
    fi
    echo “iptables: KaTeX parse error: Expected 'EOF', got '#' at position 134: …>/dev/null     #̲报表信息     report…s”
}
 
function getSNMPStatus(){
    #SNMP服务状态,配置等
    echo “”
    echo “”
    echo “############################ SNMP检查 ############################”
    status=“(getStatesnmpd)&quot;   echo&quot;服务状态:(getState snmpd)&quot;     echo &quot;服务状态:(getStatesnmpd)"   echo"status”
    echo “”
    if [ -e /etc/snmp/snmpd.conf ];then
        echo “/etc/snmp/snmpd.conf”
        echo “--------------------”
        cat /etc/snmp/snmpd.conf 2>/dev/null | grep -v “^#” | sed '/^KaTeX parse error: Expected 'EOF', got '#' at position 16: /d'     fi     #̲报表信息     report…(getState snmpd)"
}
 
 
 
function getState(){
    if [[ $centosVersion < 7 ]];then
        if [ -e “/etc/init.d/$1” ];then
            if [ /etc/init.d/$1 status 2>/dev/null | grep -E "is running|正在运行" | wc -l -ge 1 ];then
                r=“active”
            else
                r=“inactive”
            fi
        else
            r=“unknown”
        fi
    else
        #CentOS 7+
        r="$(systemctl is-active KaTeX parse error: Expected 'EOF', got '&' at position 5: 1 2>&̲1)"     fi     …r"
}
 
function getSSHStatus(){
    #SSHD服务状态,配置,受信任主机等
    echo “”
    echo “”
    echo “############################ SSH检查 #############################”
    #检查受信任主机
    pwdfile="(cat/etc/passwd)&quot;   echo&quot;服务状态:(cat /etc/passwd)&quot;     echo &quot;服务状态:(cat/etc/passwd)"   echo"(getState sshd)"
    Protocol_Version=$(cat /etc/ssh/sshd_config | grep Protocol | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')     echo "SS…Protocol_Version"
    echo “”
    echo “信任主机”
    echo “--------”
    authorized=0
    for user in (echo&quot;(echo &quot;(echo"pwdfile" | grep /bin/bash | awk -F: '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲');do         a…(echo “$pwdfile” | grep -w $user | awk -F: '{printf KaTeX parse error: Expected 'EOF', got '}' at position 25: …uthorized_keys"}̲')         auth…(cat $authorize_file 2>/dev/null | awk ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲' | tr '\n' ','…//’)
        if [ ! -z authorizedhost];then           echo&quot;authorized_host ];then             echo &quot;authorizedhost];then           echo"user 授权 “KaTeX parse error: Can't use function '\"' in math mode at position 16: authorized_host\̲"̲ 无密码访问"        …(cat $authorize_file 2>/dev/null | awk ‘{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲'|wc -l)     do…(cat /etc/ssh/sshd_config | grep PermitRootLogin)
    firstChar=${config:0:1}
    if [ KaTeX parse error: Expected 'EOF', got '#' at position 15: firstChar == "#̲" ];then       …(echo $config | awk ‘{print $2}’)
    fi
    echo "PermitRootLogin KaTeX parse error: Expected group after '^' at position 136: …fig | grep -v "^̲#" | sed '/^/d’
 
    #报表信息
    report_SSHAuthorized=“KaTeX parse error: Expected 'EOF', got '#' at position 16: authorized"    #̲SSH信任主机     rep…Protocol_Version”    #SSH协议版本
    report_SSHDPermitRootLogin=”KaTeX parse error: Expected 'EOF', got '#' at position 21: …tRootLogin"    #̲允许root远程登录 } fu…(getState ntpd)"
        echo “”
        echo “/etc/ntp.conf”
        echo “-------------”
        cat /etc/ntp.conf 2>/dev/null | grep -v “^#” | sed '/^KaTeX parse error: Expected 'EOF', got '#' at position 16: /d'     fi     #̲报表信息     report…(getState ntpd)"
}
 
 
function uploadHostDailyCheckReport(){
    json="{
        “DateTime”:“KaTeX parse error: Can't use function '\"' in math mode at position 16: report_DateTime\̲"̲,         \"Hos…report_Hostname”,
        “OSRelease”:“KaTeX parse error: Can't use function '\"' in math mode at position 17: …eport_OSRelease\̲"̲,         \"Ker…report_Kernel”,
        “Language”:“KaTeX parse error: Can't use function '\"' in math mode at position 16: report_Language\̲"̲,         \"Las…report_LastReboot”,
        “Uptime”:“KaTeX parse error: Can't use function '\"' in math mode at position 14: report_Uptime\̲"̲,         \"CPU…report_CPUs”,
        “CPUType”:“KaTeX parse error: Can't use function '\"' in math mode at position 15: report_CPUType\̲"̲,         \"Arc…report_Arch”,
        “MemTotal”:“KaTeX parse error: Can't use function '\"' in math mode at position 16: report_MemTotal\̲"̲,         \"Mem…report_MemFree”,
        “MemUsedPercent”:“KaTeX parse error: Can't use function '\"' in math mode at position 22: …_MemUsedPercent\̲"̲,         \"Dis…report_DiskTotal”,
        “DiskFree”:“KaTeX parse error: Can't use function '\"' in math mode at position 16: report_DiskFree\̲"̲,         \"Dis…report_DiskUsedPercent”,
        “InodeTotal”:“KaTeX parse error: Can't use function '\"' in math mode at position 18: …port_InodeTotal\̲"̲,         \"Ino…report_InodeFree”,
        “InodeUsedPercent”:“KaTeX parse error: Can't use function '\"' in math mode at position 24: …nodeUsedPercent\̲"̲,         \"IP\…report_IP”,
        “MAC”:“KaTeX parse error: Can't use function '\"' in math mode at position 11: report_MAC\̲"̲,         \"Gat…report_Gateway”,
        “DNS”:“KaTeX parse error: Can't use function '\"' in math mode at position 11: report_DNS\̲"̲,         \"Lis…report_Listen”,
        “Selinux”:“KaTeX parse error: Can't use function '\"' in math mode at position 15: report_Selinux\̲"̲,         \"Fir…report_Firewall”,
        “USERs”:“KaTeX parse error: Can't use function '\"' in math mode at position 13: report_USERs\̲"̲,         \"USE…report_USEREmptyPassword”,
        “USERTheSameUID”:“KaTeX parse error: Can't use function '\"' in math mode at position 22: …_USERTheSameUID\̲"̲,         \"Pas…report_PasswordExpiry”,
        “RootUser”:“KaTeX parse error: Can't use function '\"' in math mode at position 16: report_RootUser\̲"̲,         \"Sud…report_Sudoers”,
        “SSHAuthorized”:“KaTeX parse error: Can't use function '\"' in math mode at position 21: …t_SSHAuthorized\̲"̲,         \"SSH…report_SSHDProtocolVersion”,
        “SSHDPermitRootLogin”:“KaTeX parse error: Can't use function '\"' in math mode at position 27: …PermitRootLogin\̲"̲,         \"Def…report_DefunctProsess”,
        “SelfInitiatedService”:“KaTeX parse error: Can't use function '\"' in math mode at position 28: …nitiatedService\̲"̲,         \"Sel…report_SelfInitiatedProgram”,
        “RuningService”:“KaTeX parse error: Can't use function '\"' in math mode at position 21: …t_RuningService\̲"̲,         \"Cro…report_Crontab”,
        “Syslog”:“KaTeX parse error: Can't use function '\"' in math mode at position 14: report_Syslog\̲"̲,         \"SNM…report_SNMP”,
        “NTP”:“KaTeX parse error: Can't use function '\"' in math mode at position 11: report_NTP\̲"̲,         \"JDK…report_JDK”
    }"
    #echo “json&quot;   curl−l−H&quot;Content−type:application/json&quot;−XPOST−d&quot;json&quot;     curl -l -H &quot;Content-type: application/json&quot; -X POST -d &quot;json"   curllH"Contenttype:application/json"XPOSTd"json” “$uploadHostDailyCheckReportApi” 2>/dev/null
}
 
function check(){
    version
    getSystemStatus
    getCpuStatus
    getMemStatus
    getDiskStatus
    getNetworkStatus
    getListenStatus
    getProcessStatus
    getServiceStatus
    getAutoStartStatus
    getLoginStatus
    getCronStatus
    getUserStatus
    getPasswordStatus
    getSudoersStatus
    getJDKStatus
    getFirewallStatus
    getSSHStatus
    getSyslogStatus
    getSNMPStatus
    getNTPStatus
    getInstalledStatus
}
 
 
#执行检查并保存检查结果
check > RESULTFILE echo&quot;检查结果:RESULTFILE   echo &quot;检查结果:RESULTFILE echo"RESULTFILE"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值