插件窝 干货文章 linux monit监控阈值怎么设置

linux monit监控阈值怎么设置

阈值 then cycles alert 856    来源:    2025-05-14

Linux Monit 监控阈值设置指南

Monit 是一个功能强大的进程、文件、目录和系统资源监控工具。合理设置监控阈值是确保系统稳定运行的关键。以下是关于如何设置Monit监控阈值的详细说明。

基本阈值设置语法

Monit的阈值设置通常遵循以下格式:

if <condition> then <action>

常见监控项阈值设置

1. CPU使用率监控

check system $HOST
    if cpu usage > 90% for 5 cycles then alert
    if cpu usage > 95% for 5 cycles then alert
    if cpu usage > 98% for 5 cycles then restart

2. 内存使用监控

check system $HOST
    if memory usage > 85% for 5 cycles then alert
    if memory usage > 90% for 5 cycles then alert
    if memory usage > 95% for 5 cycles then restart

3. 交换空间监控

check system $HOST
    if swap usage > 50% for 5 cycles then alert
    if swap usage > 70% for 5 cycles then alert
    if swap usage > 90% for 5 cycles then restart

4. 磁盘空间监控

check filesystem rootfs with path /
    if space usage > 80% for 5 cycles then alert
    if space usage > 90% for 5 cycles then alert
    if space usage > 95% for 5 cycles then exec "/usr/local/bin/cleanup_script.sh"

5. 进程监控

check process nginx with pidfile /var/run/nginx.pid
    start program = "/etc/init.d/nginx start"
    stop program = "/etc/init.d/nginx stop"
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if totalmem > 500 MB for 5 cycles then restart
    if children > 200 then alert
    if failed host 127.0.0.1 port 80 protocol http then restart

6. 网络连接监控

check host example.com with address example.com
    if failed ping then alert
    if failed port 80 protocol http then alert
    if failed port 443 protocol https with timeout 30 seconds then alert

高级阈值设置技巧

  1. 设置检查间隔

    set daemon 60  # 每60秒检查一次
    
  2. 使用多个条件

    if cpu > 80% and memory > 80% for 3 cycles then alert
    
  3. 自定义执行脚本

    if space usage > 95% then exec "/usr/local/bin/emergency_cleanup.sh"
    
  4. 设置延迟触发

    if loadavg (1min) > 10 for 10 cycles then alert
    

阈值设置建议

  1. 根据系统特性调整

    • 生产环境:设置更严格的阈值
    • 开发环境:可以适当放宽
  2. 逐步调整

    • 先设置宽松阈值,根据实际运行情况逐步收紧
  3. 考虑业务特性

    • 高负载系统:CPU阈值可以设置更高
    • 关键业务系统:内存阈值应设置更严格
  4. 合理设置触发周期

    • 避免因瞬时峰值触发误报
    • 关键指标:3-5个周期
    • 非关键指标:5-10个周期

配置示例

check system $HOST
    if loadavg (1min) > 4 for 3 cycles then alert
    if loadavg (5min) > 2 for 3 cycles then alert
    if memory usage > 75% for 3 cycles then alert
    if swap usage > 25% for 3 cycles then alert
    if cpu usage (user) > 70% for 3 cycles then alert
    if cpu usage (system) > 30% for 3 cycles then alert

配置完成后,记得使用 monit -t 测试配置是否正确,然后重启Monit服务使配置生效。

希望这些信息对您设置Monit监控阈值有所帮助!如需针对特定场景的更详细配置,请提供更多信息。