题目5:系统服务状态监控
描述:分析 systemctl 输出,识别启动失败的服务并提供详细信息。
测试数据(systemctl list-units --type=service --all输出保存为 systemctl_status.txt):
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
sys-devices-pci0000:00-0000:00:1f.2-ata1-host0-target0:0:0-0:0:0:0-block-sr0.device loaded active plugged QEMU DVD-ROM
sys-devices-pci0000:00-0000:00:1f.2-ata2-host1-target1:0:0-1:0:0:0-block-sda-sda1.device loaded active plugged VBOX_HARDDISK Partition 1
sys-devices-pci0000:00-0000:00:1f.2-ata2-host1-target1:0:0-1:0:0:0-block-sda-sda2.device loaded active plugged VBOX_HARDDISK Partition 2
sys-devices-pci0000:00-0000:00:1f.2-ata2-host1-target1:0:0-1:0:0:0-block-sda.device loaded active plugged VBOX_HARDDISK
dev-disk-by\x2duuid-12345678\x2dabcd\x2defgh\x2dijklmnop.device loaded active plugged /dev/disk/by-uuid/12345678-abcd-efgh-ijklmnop
dev-sda1.device loaded active plugged VBOX_HARDDISK Partition 1
dev-sda2.device loaded active plugged VBOX_HARDDISK Partition 2
dev-sda.device loaded active plugged VBOX_HARDDISK
sys-subsystem-net-devices-eth0.device loaded active plugged Virtio network device
-.mount loaded active mounted Root Mount
boot-efi.mount loaded active mounted /boot/efi
dev-hugepages.mount loaded active mounted Huge Pages File System
dev-mqueue.mount loaded active mounted POSIX Message Queue File System
proc-sys-fs-binfmt_misc.mount loaded active mounted Arbitrary Executable File Formats File System
run-rpc_pipefs.mount loaded active mounted RPC Pipe File System
sys-fs-fuse-connections.mount loaded active mounted FUSE Control File System
sys-kernel-config.mount loaded active mounted Kernel Configuration File System
sys-kernel-debug.mount loaded active mounted Kernel Debug File System
tmp.mount loaded active mounted /tmp
var-lib-docker-containers.mount loaded active mounted /var/lib/docker/containers
var-lib-docker-overlay.mount loaded active mounted /var/lib/docker/overlay
var-lib-docker-overlay2-mount loaded active mounted /var/lib/docker/overlay2
apache2.service loaded failed failed The Apache HTTP Server
mysql.service loaded active running MySQL Community Server
nginx.service loaded active running A high performance web server and a reverse proxy server
redis-server.service loaded active running Advanced key-value store
docker.service loaded active running Docker Application Container Engine
ssh.service loaded active running OpenBSD Secure Shell server
cron.service loaded active running Regular background program processing daemon
networking.service loaded active exited Raise network interfaces
rsyslog.service loaded active running System Logging Service
systemd-timesyncd.service loaded active running Network Time Synchronization
fail2ban.service loaded failed failed Advanced intrusion prevention framework
postfix.service loaded active running Postfix Mail Transport Agent
bash
#!/bin/bash
#系统服务状态监控
echo "===系统服务状态监控报告==="
#提取失败的服务
echo "失败的服务:"
grep "loaded failed" systemctl_status.txt | awk '{print " -" $1}' | sed 's/\.service//'
#提取运行中的关键服务
echo -e "\n 运行中的关键服务"
key_service="apache2 mysql nginx redis-server docker ssh cron postfix"
for service in $key_service; do
if grep -q "$service.service.* loaded active running" systemctl_status.txt; then
echo "$service 成功"
elif grep -q "$service.service.* loaded failed" systemctl_status.txt; then
echo "$service 失败"
else
echo "$service 未知"
fi
done
#统计信息
echo -e "\n服务统计"
total_service=$(grep -c "\.service" systemctl_status.txt)
active_service=$(grep -c "loaded active running" systemctl_status.txt)
failed_service=$(grep -c "loaded failed" systemctl_status.txt)
echo "总服务数: $total_service"
echo "成功服务数: $active_service"
echo "失败服务数: $failed_service"
echo "其他状态: $((total_service-active_service-failed_service))"
题目6:系统性能瓶颈分析
描述:分析 top 命令输出,识别 CPU 和内存使用率最高的进程。
测试数据(保存为 top_output.txt):
top - 14:30:15 up 5 days, 3:22, 2 users, load average: 1.25, 0.98, 0.85
Tasks: 185 total, 1 running, 184 sleeping, 0 stopped, 0 zombie
%Cpu(s): 25.3 us, 5.2 sy, 0.0 ni, 68.1 id, 1.2 wa, 0.0 hi, 0.2 si, 0.0 st
MiB Mem : 7976.5 total, 1234.8 free, 4567.2 used, 2174.5 buff/cache
MiB Swap: 2048.0 total, 1845.3 free, 202.7 used. 3123.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 mysql 20 0 2345676 567892 12345 S 45.2 7.1 1234:56 mysqld
2345 tomcat 20 0 1234567 345678 23456 S 23.4 4.3 456:78 java
3456 apache 20 0 456789 123456 5678 S 12.8 1.5 234:56 httpd
4567 redis 20 0 234567 67890 12345 S 8.9 0.8 89:12 redis-server
5678 docker 20 0 789012 234567 34567 S 5.6 2.9 156:34 dockerd
6789 kibana 20 0 567890 156789 23456 S 3.4 1.9 67:89 node
7890 elasticsearch 20 0 1567890 456789 45678 S 2.1 5.7 345:67 java
8901 postgres 20 0 678901 189012 34567 S 1.8 2.4 98:76 postgres
9012 nginx 20 0 123456 45678 1234 S 0.9 0.6 23:45 nginx
bash
#!/bin/bash
# 系统性能瓶颈分析
echo "===系统性能分析报告==="
#提取性能负载信息
echo "系统负载信息:"
head -1 top_output.txt | sed 's/^/ /'
#提取cpu和内存使用情况
echo -e "\n资源使用情况:"
grep "MiB Mem" top_output.txt | sed 's/^/ /'
grep "MiB Swap" top_output.txt | sed 's/^/ /'
#分析高cpu使用进程
echo -e "\n===高cpu使用进程==="
awk 'NR>7 && NR<12 {
if ($9+0>1){
printf " %-8s %-15s %6.1f%% %6.1f%% %s\n",$1,$12,$9,$10,$13
}
}' top_output.txt | sort -k3 -nr | head -5 | cat -n
#分析内存使用率高的进程
echo -e "\n===高内存使用进程==="
awk 'NR>7 && NR<=12{
if ($10+0>1){
printf " %-8s %-15s %6.1f%% %6.1f%% %s\n",$1,$12,$9,$10,$13
}
}' top_output.txt | sort -k4 -nr | head -5 | cat -n
#性能建议
echo -e "\n===性能建议==="
cpu_usage=$(grep "^Cpu" top_output.txt |awk '{print $2}' | sed 's/us,//')
mem_used=$(grep "Mib Mem" top_output.txt | awk '{print $6}')
mem_total$(grep "Mib Mem" top_output.txt | awk '{print $4}')
#处理浮点数运算
cpu_persent=${cpu_usage%.*}
mem_percent=$(echo "scale=0;$mem_used*100/$mem_total" | bc 2>/dev/null)
#如果bc不可用,用awk计算
if [[-z "$mem_percent" || "$mem_percent" == "."]]; then
mem_percent=$(awk -v used="$mem_used" -v total="$mem_total" 'BEGIN{printf "%.0f",used*100/total}')
fi
echo "调试信息:CPU=${cpu_percent}%,MEM=${mem_percent}%"
if [[ -z "$cpu_int"]]; then cpu_int=0; fi
if [[ -z "$mem_int"]]; then mem_int=0; fi
cpu_int=$(echo "$cpu_percent" | cut -d'.' -f1)
mem_int=$(echo "$mem_percent" | cut -d'.' -f1)
echo "调试信息:CPU整数部分=$cpu_int,MEM整数部分=$mem_int"
if [[ $cpu_int -gt 80]]; then
echo " ⚠️ CPU使用率较高($cpu_int%),建议检查高CPU进程"
fi
if [[ $mem_int -gt 80]]; then
echo " ⚠️ MEM使用率较高($mem_int%),建议检查高MEM进程"
fi
if [[$cpu_int -lt 20 && $mem_int -lt 30 ]]; then
echo " ✅ 系统资源充足,运行正常"
fi
#添加更多具体的建议
if [[ $cpu_int -gt 70 ]]; then
echo " 🔧 建议使用'top'或'htop'命令检查具体高CPU使用进程"
echo " 🔧 建议优化数据库查询或增加CPU资源"
fi
if [[ $mem_int -gt 70]]; then
echo " 🔧 建议使用'ps aux --sort=-%mem'查看高内存进程"
echo " 🔧 建议优化应用内存使用或增加物理内存"
fi
#检查具体进程给出优化建议
echo " 🔍 具体进程建议"
high_cpu_pids=$(awk 'NR>7 && NR<12 && $9+0>30 {print $1":"$12":"$9}' top_output.txt)
if [[-n "$high_cpu_pids"]]; then
echo "$high_cpu_pids" | while IFS=':' read -r pid command cpu; do
case $command in
mysqld)
echo " -MYSQL进程(PID:$pid) CPU使用率较高($cpu%),建议检查慢查询"
;;
java)
echo " -java进程(PID:$pid) CPU使用率较高($cpu%),建议检查线程池配置"
;;
httpd|nginx)
echo " -Web服务器进程(PID:$pid) CPU使用率较高($cpu%),建议检查并发连接数"
;;
*)
echo " -$command进程(PID:$pid) CPU使用率较高($cpu%)"
;;
esac
done
fi