Ubuntu系统级资源监控工具
在Ubuntu上监控系统资源(CPU、内存、磁盘、网络等)是保障Apache服务稳定运行的基础,以下是常用的命令行与图形化工具:
top是实时显示系统进程资源占用的基础工具,可按q退出;htop是其增强版,提供更友好的界面(支持鼠标操作、颜色区分),安装命令为sudo apt install htop。sysstat包后使用(sudo apt install sysstat),命令格式为vmstat [interval](如vmstat 1表示每秒更新一次)。sysstat包,命令格式为iostat -c -d [interval](-c显示CPU统计,-d显示磁盘统计,-x显示扩展信息)。sysstat后使用,命令格式为sar -u [interval](CPU使用率)、sar -r [interval](内存使用情况)。sudo apt install dstat,基本用法为dstat -ta 6(每6秒更新一次,显示所有指标)。sudo apt install glances,运行后可通过http://localhost:61208访问(默认端口)。http://localhost:19999),安装方式为Docker(docker run -d --name netdata -p 19999:19999 --network host netdata/netdata),适合远程监控。sudo apt install conky,配置文件位于~/.conkyrc。Apache特定资源监控工具
除系统级工具外,针对Apache服务的资源使用,可使用以下专用工具:
mod_status是Apache的内置模块,可提供服务器状态页面(包括活动连接数、请求处理速度、CPU/内存使用率等)。启用步骤:
/etc/apache2/mods-enabled/status.conf),确保<Location "/server-status">段未被注释,且设置为允许访问(如Require local或指定IP);sudo systemctl restart apache2);http://your_server_ip/server-status查看状态(需输入密码,若配置了认证)。对于高级监控需求(如长期数据存储、可视化、告警),可使用Prometheus(时间序列数据库)+Grafana(可视化平台):
prometheus.yml配置文件,添加Apache抓取目标(scrape_configs段);sudo apt install grafana安装,启动后登录(http://localhost:3000,默认账号admin/admin);通过分析Apache的访问日志(/var/log/apache2/access.log)和错误日志(/var/log/apache2/error.log),可了解服务器负载情况(如高访问量IP、慢请求、错误类型)。常用命令:
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -n 10;cat access.log | grep "03/Aug/2023:08:" | awk '{print $1}' | sort -nr | uniq -c;%T或%D字段):awk '{print $10, $7}' access.log | sort -k2,2rn | head -20。