欢迎光临,赤鹿小组
记录过程,分享经验

Linux 服务器巡检脚本

本脚本主要是用于检测服务器基本配置信息(CPU,内存,磁盘),磁盘 I/O ,以及个别威胁服务器的安全巡检项。后续会继续优化加入其余巡检项。

#!/bin/bash
#Author Seichung

set -u


# Color Tips
yellow_echo () { local content=$*; echo -e "\e[1;33m${content}\e[0m";}
red_echo () { local content=$*; echo -e "\e[1;31m${content}\e[0m";}
green_echo () { local content=$*; echo -e "\e[1;32m${content}\e[0m";}
blue_echo () { local content=$*; echo -e "\e[1;34m${content}\e[0m";}



check_user () {
    if [ $(id -u) != 0 ];then
        red_echo " $0 Should run as root" 
        exit 1
    fi
}

cpu_info () {
    green_echo "CPU INFO:"
    {
        grep -m1 -E "(^model name)" /proc/cpuinfo | sed -r "s/://" | sed 's/^/\t/g'
        grep -m1 -E "(^cpu MHz)" /proc/cpuinfo | sed -r "s/://" | sed 's/^/\t/g'
        grep -m1 -E "(^cache size)" /proc/cpuinfo | sed -r "s/://" | sed 's/^/\t/g'
        grep -m1 -E "(^physical id)" /proc/cpuinfo | sed -r "s/://" | sed 's/^/\t/g'

        local cores=$(grep -cE "^processor" /proc/cpuinfo)
        echo -e "\tCore(s):\t"$cores""

    }
    echo
}


disk_info (){
    local disk_total=$(df -h | grep -E '^/dev/' | awk '{print "Total:" $2}' | sed -e 's/:/\t/g')
    local disk_used=$(df -h | grep -E '^/dev/' | awk '{print "Used:" $3}' | sed -e 's/:/\t/g')
    green_echo "Disk INFO:"
    {
        echo -e "\t$disk_total"
        echo -e   "\t$disk_used"
    }
    echo
}

disk_io (){

    # iostat 每秒执行一次,共两次
    local read_kb=$(iostat -x 1 2 | tail -3 | grep -E '^vda' | awk '{print $6}')
    local wirte_kb=$(iostat -x 1 2 | tail -3 | grep -E '^vda' | awk '{print $7}')
    local iowait=$(iostat -x 1 2 | tail -6 | head -2 | awk '{print $5}'| tail -1)
    local cpu_idle=$(iostat -x 1 2 | tail -6 | head -2 | awk '{print $NF}'| tail -1)
    green_echo "Disk I/O:"
    {
        echo -e "\t磁盘读数据: $read_kb kB/s"
        echo -e "\t磁盘写数据: $wirte_kb kB/s"
        echo -e "\t磁盘 I/O: $iowait ms"
        echo -e "\tCPU空闲百分比: $cpu_idle \n"
        yellow_echo "\tTips:"
        {
          echo -e "\t1.若 %iowait 的值过高,表示硬盘存在I/O瓶颈"
          echo -e "\t2.若 %idle 的值高但系统响应慢时,有可能是CPU等待分配内存,此时应加大内存容量"
          echo -e "\t3.若 %idle 的值持续低于1,则系统的CPU处理能力相对较低,表明系统中最需要解决的资源是 CPU"
        }
    }
    echo
}

mem_info (){
    local mem_total=$(free -h | grep -E "^Mem" | awk '{print "Total:" $2}'| sed 's/:/\t/g')
    local mem_used=$(free -h | grep -E "^Mem" | awk '{print "Used:" $3}' | sed 's/:/\t/g')
    local swap_total=$(free -h | grep -E "^Swap" | awk '{print "Total:" $2}' | sed 's/:/\t/g')
    local swap_used=$(free -h | grep -E "^Swap" | awk '{print "Used:" $3}' | sed 's/:/\t/g')
    green_echo "Mem INFO:"
    {
        blue_echo "    Mem:"
        {
           echo -e  "\t"$mem_total
           echo -e "\t"$mem_used
        }
        blue_echo "    Swap:"
        {
           echo -e "\t"$swap_total
           echo -e "\t"$swap_used
        }
    }
    echo
}

system_running (){
    green_echo "System Running Time:"
    {
        uptime | awk '{print "Running Day:" $3}'|sed -e 's/:/\t/g'| sed -e 's/^/\t/g'
    }
    echo
}

sshd_port () {
    local ssh_port=$(netstat -ntulp | grep sshd | awk '{print $4}' | awk -F ':' '{print $2}')
    green_echo "SSH INFO:"
    {
    if [ $ssh_port == 22 ];then
        red_echo "\t[WARNING] 远程登陆不建议开启22端口,请及时修改!"
    else
       echo -e "\t[INFO] 远程登陆非22端口"
    fi
    }
   echo     
}
system_user () {
    user_list=(adm lp sync shutdown halt mail uucp operator games gopher dbus rpc vcsa abrt saslauth haldaemon)
    local  user_dir="/etc/passwd"
    green_echo "System Default User:" 
    {
    cat $user_dir |awk -F : '{print $1}'  | while read user
    do

        for system_user in ${user_list[@]}
        do
           if [ $user == $system_user ];then
               nologin=$(grep -E $system_user $user_dir | awk -F : '{print $NF}')
                if [ "$nologin" != "/sbin/nologin" ];then
                       echo -e "\t$system_user 为系统默认用户未禁用,为预防被利用,请将其禁用"
                fi
           fi
        done
    done
    }
    echo
}

check_user
cpu_info
mem_info
disk_info
disk_io
system_running
sshd_port
system_user
赞(1)

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址