现在很多项目对过程文档要求愈加严格,在很多环境使用了各种监控运行手段的基础上一般还需要提交一份每日的人工巡检报告,因很多项目涉及服务器数量较多,为了减少报告产出工作量,针对要求形成一份shell脚本,并通过crontab每日定时执行,在执行完后的的时间点由人工汇总执行日志文档进行填报。以下是针对Linux服务器,包括Centos、Ubuntu、UOS、麒麟下都能兼容的巡检脚本,已进行分解说明,可按需取用
#!/bin/bash
# 定义输出文件
current_date=$(date "+%Y%m%d") # Gets the current date in YYYYMMDD format
output_file="server_bas_inspection_report_${current_date}.txt"
# 获取巡检时间
inspection_time=$(date "+%Y-%m-%d %H:%M:%S")
# 获取主机名称
hostname=$(hostname)
# 获取主机IP地址
host_ip=$(hostname -I | awk '{print $1}')
# 检查sysstat是否安装,没有的根据系统版本自动安装
if ! command -v mpstat &> /dev/null; then
echo "sysstat 未安装. 尝试安装中..."
# (Debian, Ubuntu, UOS and derivatives)
if command -v apt &> /dev/null; then
echo "Using apt to install sysstat..."
sudo apt update && sudo apt install -y sysstat
# (RedHat, CentOS, Fedora, 麒麟 and derivatives)
elif command -v yum &> /dev/null; then
echo "Using yum to install sysstat..."
sudo yum install -y sysstat
else
echo "yum 或 apt 包管理安装工具未找到无法自动安装,请手动安装."
fi
fi
# 3分钟cpu平均使用率
# 需要注意,不同版本mpstat的数据列数可能为11或12列,如果输出为11列需要修改$12为$11
cpu_usage=$(mpstat 1 190 | awk '$0 ~ /Average|平均时间/ {print 100 - $12}')
# 获取内存使用情况,计算规则(total-available)/total
mem_total=$(free -m | grep -E "Mem|内存" | awk '{print $2}')
mem_used=$(free -m | grep -E "Mem|内存" | awk '{print $3}')
mem_available=$(free -m | grep -E "Mem|内存" | awk '{print $7}')
mem_actual_used=$(($mem_total - $mem_available))
mem_usage=$(printf "%.2f" $((10000 * mem_actual_used / mem_total))e-2)
# 初始化报告文件
echo "===============================================" > $output_file
echo "-- 巡检时间:$inspection_time" >> $output_file
echo "-- 主机IP地址:$host_ip" >> $output_file
# 磁盘使用情况检查
disk_warning=0
echo "1. 磁盘使用情况:" >> $output_file
while read -r line; do
filesystem=$(echo $line | awk '{print $1}')
disk_total=$(echo $line | awk '{print $2}')
disk_used=$(echo $line | awk '{print $3}')
disk_usage=$(echo $line | awk '{print $5}' | sed 's/%//')
mount_point=$(echo $line | awk '{print $6}')
if [ "$filesystem" != "/dev/loop0" ]; then
echo " 挂载点:$mount_point" >> $output_file
echo " 文件系统:$filesystem" >> $output_file
echo " 总磁盘空间:$disk_total" >> $output_file
echo " 已使用磁盘空间:$disk_used" >> $output_file
echo " 磁盘使用率:$disk_usage%" >> $output_file
if [ $disk_usage -gt 80 ]; then
echo " 【警告】此挂载点磁盘使用率超过正常值 (80%)" >> $output_file
disk_warning=1
fi
fi
done < <(df -h | grep "^/dev")
echo "2. 内存使用率:$mem_usage%" >> $output_file
echo "3. CPU 3分钟内平均使用率:$cpu_usage%" >> $output_file
echo "" >> $output_file
# 磁盘空间利用率检查结果
if [ $disk_warning -eq 1 ]; then
echo "-- 【警告】存在磁盘使用率超过正常值 (80%)" >> $output_file
else
echo "-- 【正常】磁盘空间利用率" >> $output_file
fi
# 内存使用率检查
if awk -v usage="$mem_usage" 'BEGIN {exit !(usage > 70)}'; then
echo "-- 【警告】内存使用率超过正常值 (70%)" >> $output_file
else
echo "-- 【正常】内存使用率" >> $output_file
fi
# CPU使用率检查
if awk -v usage="$cpu_usage" 'BEGIN {exit !(usage > 85)}'; then
echo "-- 【警告】CPU使用率高 (>85%)" >> $output_file
else
echo "-- 【正常】CPU使用率" >> $output_file
fi
echo "===============================================" >> $output_file
# 显示报告
cat $output_file
脚本执行输出样例: