#!/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 ""
linux 系统硬件系统服务的巡检脚本
最新推荐文章于 2025-04-30 16:06:13 发布