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了,自取把

下载权限
查看
  • 免费下载
    评论并刷新后下载
    登录后下载
  • {{attr.name}}:
您当前的等级为
登录后免费下载登录 小黑屋反思中,不准下载! 评论后刷新页面下载评论 支付以后下载 请先登录 您今天的下载次数(次)用完了,请明天再来 支付积分以后下载立即支付 支付以后下载立即支付 您当前的用户组不允许下载升级会员
您已获得下载权限 您可以每天下载资源次,今日剩余
免责声明:
1.本站所有内容只做学习和交流使用。 版权归原作者所有。
2.保证站内提供的所有可下载源码资源(软件等)都是按“原样”提供,本站未做过任何改动;但本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。
3.本站部分内容均收集于网络!如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。请联系站长邮箱:admin#ibian.online(#换成@)处理!

给TA打赏
共{{data.count}}人
人已打赏
服务器

 Linux下Tomcat端口、进程以及防火墙设置

2025-3-15 16:29:58

服务器

Windows 服务器巡检脚本

2025-3-22 14:03:36

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索