监控远程服务器上的 RAM 和 CPU 使用情况?

监控远程服务器上的 RAM 和 CPU 使用情况?

我想监控远程服务器上的 CPU 和 RAM 使用情况,并设置一个通知系统,当 RAM 使用率过高时通知我并自动终止该进程。

我尝试了 Nagios 插件,但出现了一些错误。

还有其他替代方案吗?

编辑:请不要建议htop、、freetop类的,因为我不会继续通过 ssh 进入远程服务器并反复检查其健康状况!

答案1

您熟悉吗top?显示每个进程的 CPU 和 RAM 使用情况(以及更多)以及总量。

您还可以运行top -u USER以仅查看特定用户的进程。只需top从不带参数的运行开始。q按以退出。

编辑

并在脚本中使用:尝试top -b -n1在非交互模式下运行。然后您可以使用来解析数据headsed无论如何grep您都可以。

答案2

我创建了一些自定义脚本。虽然对你来说不是完整的解决方案,但也许有帮助。

#!/bin/bash

#we use output from "uptime" and "w" on every 5 min

UPTIME=`uptime`;
W=`w`;
RAM=`free -m`;
PS=`ps auxf | sort -nr -k 4 | head -15`
DATUM=`date`;
sleep 3
MYSQL=`mysqladmin -u root -pmz567sQld processlist;`
sleep 3
SYN=`netstat -natp | grep SYN`
sleep 3
HTTPD=`pgrep httpd | wc -l`
sleep 3

echo "######################################" >> /var/log/load-result/load_log
echo "$DATE" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "UPTIME:" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "$UPTIME" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "W COMMAND:" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "$W" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "FREE RAM:" >> /var/log/load-result/load_log
echo "$RAM" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "TOP 10 processes:" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "$PS" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "MYSQL PROCESSLIST:" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "$MYSQL" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "POTENTIAL ATTACKERS:" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "$SYN" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "NUMBER OF HTTPD PROCESS (currently maxclients are 64):" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "$HTTPD" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "CURRENTLY OPENED HTTPD CONNECTIONS:" >> /var/log/load-result/load_log
echo "" >> /var/log/load-result/load_log
echo "#####################################" >> /var/log/load-result/load_log

之后还添加 crontab 规则,每 5 分钟将日志与日志文件一起发送到某些邮件。每 5 分钟运行一次脚本

sudo nano /etc/crontab

*/5 * * * * sh /path_to_script/name_of_script     
*/5 * * * * mail -s "insert_subject" some_adress@domain < /var/log/load-result/load_log

相关内容