配置文件(/etc/monit/conf.d/vps.conf)
check system vps
if loadavg (1min) 4 then alert
if memory usage 85% then alert
if cpu usage 80% then alert
check process nginx with pidfile /run/nginx.pid
start program = /etc/init.d/nginx start
stop program = /etc/init.d/nginx stop
if failed port 80 protocol http then restart
2、自定义脚本输出
python
示例:每分钟记录CPU和内存到CSV
import psutil, time
with open('monitor.log', 'a') as f:
while True:
cpu = psutil.cpu_percent()
mem = psutil.virtual_memory().percent
f.write(f{time.time()},{cpu},{mem}\n)
time.sleep(60)
六、优化建议
1、冷热数据分离:将日志/临时文件存放在SSD,数据库存放在HDD
2、动态扩展:根据监控数据自动调整实例规格(如AWS Auto Scaling)
3、基线分析:对比正常时段与异常时段的指标差异
4、容量规划:通过历史数据预测未来资源需求