CentOS system host daily inspection script

1. Due to the work needs of operation and maintenance engineers, it is required to conduct daily online inspections on the server. The inspection indicators include system status information, CPU, memory, hard disk, and network.

So I wrote a shell script for the daily inspection of the host

This inspection script needs to ensure normal access to the SSH service, and needs to log in as the root user to run the script

#!/bin/bash
# @Author: danqing
# @Description: Host Daily Check Script
# beseem CentOS6.X CentOS7.X
# 
echo "Host Daily Check Script"
[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1


if [ ! -d /root/check_log  ];then
  mkdir /root/check_log
  echo "/root/check_log检查日志存放目录创建成功"
else
  echo "/root/check_log检查日志存放目录已存在"
fi


function getSystem(){
echo ""
echo ""
echo "############################ 系统信息检查 ############################"
Default_LANG=${LANG}
OS=$(uname -o)
Release=$(cat /etc/redhat-release 2>/dev/null)
Kernel=$(uname -r)
Hostname=$(uname -n)
Nowdate=$(date +'%F %T')
LastReboot=$(who -b | awk '{print $3,$4}')
uptime=$(uptime | sed 's/.*up \([^,]*\), .*/\1/')
echo " 语言环境: $Default_LANG"
echo " 系统: $OS"
echo " 发行版本: $Release"
echo " 内核: $Kernel"
echo " 主机名: $Hostname"
echo " 当前时间: $Nowdate"
echo " 最后启动: $LastReboot"
echo " 运行时间: $uptime"
}


function getCpu(){
echo ""
echo ""
echo "############################ CPU检查 ############################"
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_Hz=$(cat /proc/cpuinfo | grep "cpu MHz" | uniq | awk -F':' '{sub(/ /,"",$2);printf "%s MHz\n",$2}')
CPU_Arch=$(uname -m)
CPU_Usage=$(cat /proc/loadavg | awk '{print $1}')
echo "物理CPU个数: $Physical_CPUs"
echo "逻辑CPU个数: $Virt_CPUs"
echo "每CPU核心数: $CPU_Kernels"
echo "CPU型号: $CPU_Type"
echo "CPU频率: $CPU_Hz"
echo "CPU架构: $CPU_Arch"
echo "CPU使用率: ${CPU_Usage}%"
}


function getMemory(){
echo ""
echo ""
echo "############################ 内存检查 ############################"
Memory_Used=$(awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024/1024}'  /proc/meminfo)
Memory_Total=$(awk '/MemTotal/{total=$2}END{print (total)/1024/1024}' /proc/meminfo)
# kb的换算是1000 kB的换算是1024
Memory_Usage=$(awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/total*100}'  /proc/meminfo)
echo "已使用内存/全部内存: ${Memory_Used}GB/${Memory_Total}GB"
echo "内存使用率: ${Memory_Usage}%"
}


function getDisk(){
echo ""
echo ""
echo "############################ 硬盘检查 ############################"
Disk_Count=$(lsblk |awk '/disk/{print $1}'|wc -l)
echo "硬盘数量: ${Disk_Count}个"
echo "硬盘分区情况: "
echo "`df -hTP | sort |grep -E "/sd|/mapper" |awk '{print ($1 "\t\n" "  文件系统"$2 "  合计"$3 "  已用"$4 "  剩余"$5 "  使用率"$6 "  挂载点"$7)}'`"
# -P, --portability 使用 POSIX 输出格式,方便shell过滤处理
smartctl -V >&/dev/null
if [ $? -eq 0 ]; then
    echo "smartctl工具已安装,可以进行硬盘健康检测: "
    for i in $(lsblk |awk '/disk/{print $1}')
    do
      echo "硬盘"$i   `smartctl -H /dev/$i |grep -Ei "OK|PASSED|FAILED|Failure|Failed"`
    done
else
    echo "smartctl工具未安装,无法进行硬盘健康检测"
fi
# "\n磁盘IO信息:$(iotop -bon 1 &>/dev/null || echo 'iotop 未安装信息获取失败')"
}


function getNetwork(){
echo ""
echo ""
echo "############################ 网络检查 ############################"
Network_Device=$(cat /proc/net/dev | awk 'NR>2 && $1 !~/lo/ {sub(/:/,"");print $1}')
for i in $Network_Device
do
  echo "网卡:$i  状态: $(ip link show $Network_Device | awk 'NR==1{print $9}') RX: $(ethtool -g $Network_Device | grep "RX:" | tail -1 | awk '{print $2}') TX: $(ethtool -g $Network_Device | grep "TX:" | tail -1 | awk '{print $2}')"
  # rx是接收(receive),tx是发送(transport)
  Mac_Info=$(ip link | egrep -v "lo" | grep link | awk '{print $2}')
  echo "MAC地址: $Mac_Info"
  Private_Ip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
  echo "IP地址: $Private_Ip"
  # Public_Ip=$(curl ifconfig.me -s)
  # echo "公网IP地址: $Public_Ip"
  Gateway=$(ip route | grep default | awk '{print $3}')
  # echo "网关地址: $Gateway"
  # Dns_Config=$(grep nameserver /etc/resolv.conf| grep -v "#" | awk '{print $2}' | tr '\n' ',' | sed 's/,$//')
  # echo "DNS地址: $Dns_Config"
  echo "网关连接情况: $(ping -c 4 -i 0.5 -W 3 $Gateway &>/dev/null && echo '正常通信' || echo '无法通信')"
  echo "外网连接情况: $(ping -c 4 -i 0.5 -W 3 baidu.com &>/dev/null && echo '正常通信' || echo '无法通信')"
  # 发送4次请求包,每次间隔0.5秒,最长等待时间为3秒
done
Listen_Port=$(ss -tuln | grep LISTEN | awk '{print $5}' | awk -F: '{print $2$4}' | sort |uniq -d | tr '\n' ',' | sed 's/,$//')
echo "系统运行的端口: $Listen_Port"
}


function check(){
echo "Host Daily Check Script"
getSystem
getCpu
getMemory
getDisk
getNetwork
}


RESULTFILE="/root/check_log/check-`date +%Y%m%d`.txt"
check > $RESULTFILE
echo "检查结果:$RESULTFILE"

2. This script is suitable for CentOS6.X CentOS7.X system, open the terminal and connect to the host that needs to be inspected

 vim host_check.sh

3. Press the i key, and then paste the above code

4. The code is too long, not all of it is cut, only a part of the example is cut

Save and exit, then run

sh host_check.sh

5. View inspection logs

vim /root/check_log/check-20221027.txt

 

6. The smartctl tool is not installed, and the hard disk health check cannot be performed

This prompt appears, just install it

yum install smartmontools -y

 

 Then re-run the inspection script to output the inspection log

Guess you like

Origin blog.csdn.net/wxqndm/article/details/127535619