linux 系统硬件系统服务的巡检脚本

#!/bin/bash

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 输出文件
OUTPUT_FILE="server_inspection_$(date +%Y%m%d_%H%M%S).log"

# 检查是否root用户
if [ "$EUID" -ne 0 ]; then
  echo -e "${RED}请使用root用户运行此脚本${NC}"
  exit 1
fi

# 1. 系统概览
function system_overview() {
  echo -e "${BLUE}===== 系统概览 =====${NC}" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}主机名:${NC} $(hostname)" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}操作系统:${NC} $(cat /etc/redhat-release)" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}内核版本:${NC} $(uname -r)" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}系统时间:${NC} $(date)" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}运行时间:${NC} $(uptime)" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}负载情况:${NC} $(cat /proc/loadavg)" | tee -a $OUTPUT_FILE
}

# 2. 硬件信息
function hardware_info() {
  echo -e "\n${BLUE}===== 硬件信息 =====${NC}" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}CPU信息:${NC}" | tee -a $OUTPUT_FILE
  lscpu | grep -E 'Model name|Socket|Core|Thread|MHz' | tee -a $OUTPUT_FILE
  echo -e "\n${GREEN}内存信息:${NC}" | tee -a $OUTPUT_FILE
  free -h | tee -a $OUTPUT_FILE
  echo -e "\n${GREEN}磁盘信息:${NC}" | tee -a $OUTPUT_FILE
  lsblk -o NAME,SIZE,TYPE,MOUNTPOINT | tee -a $OUTPUT_FILE
  echo -e "\n${GREEN}磁盘使用情况:${NC}" | tee -a $OUTPUT_FILE
  df -h | grep -v tmpfs | tee -a $OUTPUT_FILE
}

# 3. 性能指标
function performance_metrics() {
  echo -e "\n${BLUE}===== 性能指标 =====${NC}" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}CPU使用率:${NC}" | tee -a $OUTPUT_FILE
  top -bn1 | grep "Cpu(s)" | tee -a $OUTPUT_FILE
  echo -e "\n${GREEN}内存使用TOP5:${NC}" | tee -a $OUTPUT_FILE
  ps aux --sort=-%mem | head -n 6 | tee -a $OUTPUT_FILE
  echo -e "\n${GREEN}CPU使用TOP5:${NC}" | tee -a $OUTPUT_FILE
  ps aux --sort=-%cpu | head -n 6 | tee -a $OUTPUT_FILE
}

# 4. 网络信息
function network_info() {
  echo -e "\n${BLUE}===== 网络信息 =====${NC}" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}网络接口:${NC}" | tee -a $OUTPUT_FILE
  ip -br addr show | tee -a $OUTPUT_FILE
  echo -e "\n${GREEN}连接统计:${NC}" | tee -a $OUTPUT_FILE
  ss -s | tee -a $OUTPUT_FILE
  echo -e "\n${GREEN}网络性能:${NC}" | tee -a $OUTPUT_FILE
  ping -c 4 8.8.8.8 | tee -a $OUTPUT_FILE
}

# 5. 安全信息
function security_info() {
  echo -e "\n${BLUE}===== 安全信息 =====${NC}" | tee -a $OUTPUT_FILE
  
  # 新增用户登录统计
  echo -e "${GREEN}最近30天用户登录统计:${NC}" | tee -a $OUTPUT_FILE
  echo -e "${YELLOW}成功登录:${NC}" | tee -a $OUTPUT_FILE
  last -F | grep -v 'reboot' | grep -v 'wtmp' | awk '{print $1,$5,$6,$7,$8}' | sort | uniq -c | tee -a $OUTPUT_FILE
  echo -e "\n${YELLOW}失败登录:${NC}" | tee -a $OUTPUT_FILE
  grep "Failed password" /var/log/secure* | awk '{print $1,$2,$3,$9}' | sort | uniq -c | tee -a $OUTPUT_FILE
  
  echo -e "\n${GREEN}当前登录用户:${NC}" | tee -a $OUTPUT_FILE
  w | tee -a $OUTPUT_FILE
  echo -e "\n${GREEN}防火墙状态:${NC}" | tee -a $OUTPUT_FILE
  firewall-cmd --state 2>/dev/null | tee -a $OUTPUT_FILE
}

# 6. 服务检查
function service_check() {
  echo -e "\n${BLUE}===== 服务检查 =====${NC}" | tee -a $OUTPUT_FILE
  services=("httpd" "nginx" "mysqld" "mariadb" "postgresql" "redis" "docker")
  for service in "${services[@]}"; do
    if systemctl list-unit-files | grep -q "^$service.service"; then
      status=$(systemctl is-active $service)
      if [ "$status" == "active" ]; then
        echo -e "${GREEN}$service: ${status}${NC}" | tee -a $OUTPUT_FILE
      else
        echo -e "${RED}$service: ${status}${NC}" | tee -a $OUTPUT_FILE
      fi
    fi
  done
}

# 7. 定时任务检查
function cron_check() {
  echo -e "\n${BLUE}===== 定时任务检查 =====${NC}" | tee -a $OUTPUT_FILE
  echo -e "${GREEN}系统定时任务:${NC}" | tee -a $OUTPUT_FILE
  ls /etc/cron.* | xargs -n 1 cat 2>/dev/null | tee -a $OUTPUT_FILE
  echo -e "\n${GREEN}用户定时任务:${NC}" | tee -a $OUTPUT_FILE
  for user in $(cut -f1 -d: /etc/passwd); do
    crontab -u $user -l 2>/dev/null | tee -a $OUTPUT_FILE
  done
}

# 执行所有检查
system_overview
hardware_info
performance_metrics
network_info
security_info
service_check
cron_check

echo -e "\n${GREEN}巡检完成,结果已保存到 $OUTPUT_FILE${NC}"echo ""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值