You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Monit工具监控其他服务器的CPU负载平均值?现有示例仅针对本地主机

Monitoring Remote Server CPU Load Average with Monit

Got it, let's break down how to set up Monit to track CPU load average on remote servers—since the default examples only cover localhost, this is a super common use case that's easy to implement once you know the steps.

Prerequisites First

First, your Monit host needs passwordless SSH access to the remote server. This is non-negotiable because Monit needs to run commands on the remote machine without manual input. Here's how to set that up:

  1. Generate an SSH key pair on your Monit host (skip this if you already have one for Monit):

    ssh-keygen -t ed25519 -N "" -f /var/lib/monit/.ssh/monit_remote_key
    

    We're putting the key in the monit user's home directory because that's usually the user Monit runs as.

  2. Copy the public key to the remote server (replace remote_user and remote_host with your actual values):

    ssh-copy-id -i /var/lib/monit/.ssh/monit_remote_key.pub remote_user@remote_host
    
  3. Set the correct permissions for the key (Monit is picky about this):

    chown -R monit:monit /var/lib/monit/.ssh
    chmod 700 /var/lib/monit/.ssh
    chmod 600 /var/lib/monit/.ssh/monit_remote_key
    

Option 1: Use a Custom Script (Most Flexible)

Create a simple shell script to fetch and check the remote CPU load average. This gives you full control over thresholds and parsing.

  1. Create the script (e.g., /usr/local/bin/check_remote_cpu.sh):

    #!/bin/bash
    # Configuration
    REMOTE_USER="your_remote_username"
    REMOTE_HOST="192.168.1.100" # Or domain name
    THRESHOLD=2.0 # Adjust this to your preferred load average limit
    
    # Fetch 1-minute load average from remote server
    REMOTE_LOAD=$(ssh -i /var/lib/monit/.ssh/monit_remote_key $REMOTE_USER@$REMOTE_HOST \
        "uptime | awk -F 'load average:' '{print \$2}' | awk '{print \$1}' | tr -d ','")
    
    # Compare load to threshold (using bc for floating point math)
    if (( $(echo "$REMOTE_LOAD > $THRESHOLD" | bc -l) )); then
        exit 1 # Exit with non-zero status to trigger Monit alert
    else
        exit 0 # All good
    fi
    
  2. Make the script executable:

    chmod +x /usr/local/bin/check_remote_cpu.sh
    
  3. Add a Monit configuration file (e.g., /etc/monit/conf.d/remote_cpu.conf):

    check program remote-server-cpu with path "/usr/local/bin/check_remote_cpu.sh"
        every 2 minutes # Check every 2 minutes
        if status = 1 then alert # Send alert if script returns 1
        if status = 1 for 3 cycles then exec "/usr/local/bin/your_custom_alert_script.sh" # Optional: Run a script after 3 consecutive alerts
    

Option 2: Direct Command in Monit Config (Simpler)

If you don't want a separate script, you can put the remote command directly in your Monit config:

check program remote-server-cpu with path "/usr/bin/ssh -i /var/lib/monit/.ssh/monit_remote_key remote_user@remote_host 'uptime | awk -F \"load average:\" \"{print \\$2}\" | awk \"{print \\$1}\" | tr -d \",\" | xargs -I {} sh -c \"if (( \\$(echo \\\"{} > 2.0\\\" | bc -l) )); then exit 1; else exit 0; fi\"'"
       every 2 minutes
       if status = 1 then alert

This does the same thing as the script, but it's all in one line—just adjust the threshold, remote user, and host as needed.

Test and Verify

  1. Test the script/command manually first to make sure it works:

    /usr/local/bin/check_remote_cpu.sh
    echo $? # Should return 0 if load is under threshold, 1 if over
    
  2. Reload Monit to apply the new config:

    monit reload
    
  3. Check the status to confirm the monitor is active:

    monit status remote-server-cpu
    

Quick Notes

  • The uptime output format can vary slightly between Linux distros—if your parsing isn't working, run uptime on the remote server and adjust the awk commands to match.
  • For more detailed CPU metrics (like per-core usage), replace uptime with mpstat 1 1 and parse that output instead.
  • Make sure the monit user has permission to execute the SSH command and access the key file—this is the most common issue people run into.

Hope this gets you up and running with remote CPU load monitoring using Monit. Let me know if you hit any snags with SSH permissions or output parsing!

内容的提问来源于stack exchange,提问作者pyramid13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:32:09