Monitor RAM and CPU usage on Remote Server?
Solution 1
I create some custom script. Is not complete solution for you but maybe help.
#!/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
after that also add crontab rule to send log to some mail on every 5 min with log file. Also run script on every 5 min
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
Solution 2
Are you familiar with top
?
Shows you CPU and RAM usage (and more) for each process and also the total sum.
you can also run top -u USER
to only see a specific user's processes.
just start with running top
with no arguments. press q
in order to quit.
EDIT
and for using within a script: try top -b -n1
for running in non interactive mode. you can then parse the data with head
, sed
and grep
however you like.
Related videos on Youtube
Tasdik Rahman
Updated on September 18, 2022Comments
-
Tasdik Rahman over 1 year
I want to monitor the CPU and RAM usage on my remote server and set up a notification system which notifies me when the RAM usage is high and automatically kill that process.
I tried the Nagios plugin but there were some errors.
Any alternatives to that?
Edit: please don't suggest
htop
,free
,top
and the like as I am not going to keep ssh'ing into the remote server and check its health repeatedly!-
muru about 9 yearsWhat errors did you get with the Nagios plugin? And it looks like you don't want to just monitor, but take action as well.
-
-
Tasdik Rahman about 9 yearsI added some more details to the question a little bit
-
talz about 9 yearsobviously you don't want the interactive mode, you want to run a cron that parses the output of
top -b -n1
and kills the over consuming process. sorry if that doesn't help you, mate.