CentOS系统主机每日巡检脚本

1、由于运维工程师的工作需要,要求每日对服务器进行线上巡检,巡检的指标有系统状态信息、CPU、内存、硬盘、网络这几个大项,

所以编写了一个shell脚本,用于主机每日巡检

这个巡检脚本需要保证SSH服务正常访问,并且需要登录root用户,进行脚本运行

#!/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、该脚本适用于CentOS6.X CentOS7.X系统,打开终端连接到需要巡检的主机

 vim host_check.sh

CentOS系统主机每日巡检脚本_第1张图片

3、按i键,然后粘贴上述代码

CentOS系统主机每日巡检脚本_第2张图片4、代码太长,并没有截全部,只截取一部分示例

保存退出,然后运行

sh host_check.sh

CentOS系统主机每日巡检脚本_第3张图片

5、查看巡检日志

vim /root/check_log/check-20221027.txt

 CentOS系统主机每日巡检脚本_第4张图片

6、smartctl工具未安装,无法进行硬盘健康检测

出现这个提示,只需要安装一下就行

yum install smartmontools -y

 

 然后重新运行巡检脚本,输出巡检日志

你可能感兴趣的:(CentOS,centos,linux,服务器,bash,运维)