Linux服务器巡检脚本
⚙️系统基本信息: 自动获取操作系统、内核版本、主机名、运行时间等信息,为后续诊断提供基础数据。
🖥️硬件检测: 检测CPU型号、核心数、内存总量、硬盘使用情况等,帮助你全面了解服务器硬件状态。
📈性能监控: 实时监控内存使用率、负载情况、磁盘利用率等关键指标,预防系统过载风险。
🔒配置检查: 自动检查SSH配置、防火墙状态以及其他安全参数,确保系统安全稳固。
🌐网络信息: 收集IP地址、活动网卡等信息,方便进行网络故障排查
Linux
#!/bin/bash # 定义输出文件 REPORT_FILE="system_inspection_$(date +%Y%m%d%H%M).html" # 开始生成HTML报告 cat > $REPORT_FILE <<EOF <html> <head> <title>Linux系统巡检报告</title> <meta charset="UTF-8"> <style> body { font-family:"Microsoft YaHei", sans-serif; margin:20px; } h1 { color:#2E8B57; } h2 { color:#2c3e50; border-bottom: 2px solid #2c3e50; padding-bottom: 5px; } /*统一表格列宽*/ table { border-collapse:collapse; width:100%; margin-bottom:20px; table-layout:fixed; /*强制列宽均等*/ } /*统一单元格格式*/ th, td { border:1px solid #dddddd; padding:12px; text-align:center; /*默认居中对齐*/ vertical-align:middle; /*上下居中对齐*/ } /*第一列左对齐*/ /*设置列宽,确保列宽均等*/ th:first-child, td:first-child { text-align:left; width:20%; /*让第一列占据相同的宽度*/ } th:nth-child(2), td:nth-child(2) { width:40%; /*第二列*/ } th:nth-child(3), td:nth-child(3) { width:40%; /*第三列*/ } th { background-color:#f8f9fa; } .warning { background-color:#fff3cd; } .critical { background-color:#f8d7da; } .ok { background-color:#d4edda; } /*响应式表格*/ @media (max-width:768px) { table, th, td { font-size:12px; } } </style> </head> <body> <h1>Linux系统巡检报告</h1> <p>生成时间: $(date "+%Y-%m-%d %H:%M:%S")</p> EOF # 函数:添加报告段落 add_section() { echo "<h2>$1</h2>" >> $REPORT_FILE echo "<table>" >> $REPORT_FILE } # 函数:结束段落 end_section() { echo "</table>" >> $REPORT_FILE } # 函数:添加表格行 add_row() { echo "<tr><th>$1</th><td>$2</td><td>$3</td></tr>" >> $REPORT_FILE } ############################## # 1. 主机基本信息 ############################## add_section "主机基本信息" { echo "<tr><th>检查项</th><th>值</th><th>状态</th></tr>" os_name=$(grep PRETTY_NAME /etc/os-release | cut -d= -f2 | tr -d '"') add_row "操作系统" "$os_name" "正常" kernel_version=$(uname -r) add_row "内核版本" "$kernel_version" "正常" uptime_info=$(uptime -p) add_row "运行时间" "$uptime_info" "正常" hostname=$(hostname -f) add_row "主机名" "$hostname" "正常" } >> $REPORT_FILE end_section ############################## # 2. 硬件信息 ############################## add_section "硬件信息" { echo "<tr><th>检查项</th><th>值</th><th>状态</th></tr>" cpu_model=$(grep "model name" /proc/cpuinfo | uniq | cut -d: -f2 | sed 's/^ //') cpu_cores=$(grep -c "processor" /proc/cpuinfo) add_row "CPU型号" "$cpu_model" "正常" add_row "CPU核心数" "$cpu_cores" "正常" mem_total=$(free -h | awk '/Mem/{print $2}') add_row "总内存" "$mem_total" "正常" swap_total=$(free -h | awk '/Swap/{print $2}') add_row "交换内存" "$swap_total" "正常" disk_info=$(df -hT / | awk 'NR==2{print $2, $3"/"$4}') add_row "根分区使用" "$disk_info" "正常" } >> $REPORT_FILE end_section ############################## # 3. 性能指标 ############################## add_section "性能指标" { echo "<tr><th>检查项</th><th>值</th><th>状态</th></tr>" mem_usage=$(free | awk '/Mem/{printf "%.1f%%", $3/$2*100}') add_row "内存使用率" "$mem_usage" "正常" load_avg=$(uptime | awk -F 'load average:' '{print $2}') add_row "15分钟负载" "$load_avg" "正常" disk_usage=$(df -h / | awk 'NR==2{print $5}') usage_percent=${disk_usage%\%} if [ $usage_percent -gt 90 ]; then status="<div class='critical'>critical</div>" elif [ $usage_percent -gt 80 ]; then status="<div class='warning'>warning</div>" else status="<div class='ok'>ok</div>" fi add_row "根分区使用率" "$disk_usage" "$status" } >> $REPORT_FILE end_section ############################## # 4. 安全配置检查 ############################## add_section "安全配置检查" { echo "<tr><th>检查项</th><th>值</th><th>状态</th></tr>" ssh_root_login=$(grep "^PermitRootLogin" /etc/ssh/sshd_config | awk '{print $2}') if [ "$ssh_root_login" == "yes" ]; then status="<div class='warning'>warning</div>" else status="<div class='ok'>ok</div>" fi add_row "SSH Root登录" "$ssh_root_login" "$status" ssh_password_auth=$(grep "^PasswordAuthentication" /etc/ssh/sshd_config | awk '{print $2}') if [ "$ssh_password_auth" == "yes" ]; then status="<div class='warning'>warning</div>" else status="<div class='ok'>ok</div>" fi add_row "SSH密码认证" "$ssh_password_auth" "$status" firewall_status=$(systemctl is-active firewalld 2>/dev/null || echo "inactive") if [ "$firewall_status" == "active" ]; then status="<div class='ok'>ok</div>" else status="<div class='warning'>warning</div>" fi add_row "防火墙状态" "$firewall_status" "$status" } >> $REPORT_FILE end_section ############################## # 5. 网络信息 ############################## add_section "网络信息" { echo "<tr><th>检查项</th><th>值</th><th>状态</th></tr>" ip_info=$(hostname -I) add_row "IP地址" "$ip_info" "正常" net_status=$(ip link show | grep "state UP" | wc -l) add_row "活动网卡数量" "$net_status" "正常" } >> $REPORT_FILE end_section # 结束HTML报告 cat >> $REPORT_FILE <<EOF </body> </html> EOF echo "巡检报告已生成: $REPORT_FILE"
放Gitee了,自取把
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...