如何通过Monit工具监控其他服务器的CPU负载平均值?现有示例仅针对本地主机
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:
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_keyWe're putting the key in the
monituser's home directory because that's usually the user Monit runs as.Copy the public key to the remote server (replace
remote_userandremote_hostwith your actual values):ssh-copy-id -i /var/lib/monit/.ssh/monit_remote_key.pub remote_user@remote_hostSet 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.
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 fiMake the script executable:
chmod +x /usr/local/bin/check_remote_cpu.shAdd 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
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 overReload Monit to apply the new config:
monit reloadCheck the status to confirm the monitor is active:
monit status remote-server-cpu
Quick Notes
- The
uptimeoutput format can vary slightly between Linux distros—if your parsing isn't working, runuptimeon the remote server and adjust the awk commands to match. - For more detailed CPU metrics (like per-core usage), replace
uptimewithmpstat 1 1and parse that output instead. - Make sure the
monituser 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

