You have a small infrastructure and want to check multiple system performance simultaneously for some reason.
This script will help you to create a report for you.
You can use this script to generate a CPU, memory and swap usage for a multiple Linux system.
Three shell scripts are added in this tutorial to generate a report.
Each bash script has written with unique features and choose the one you need.
Also, you can modify this script based on your needs.
Method-1 : Shell Script to Check CPU, Memory and Swap usage on Multiple Linux System
This script allows you to check CPU, memory and swap usage on multiple Linux system from the terminal.
# vi /opt/scripts/cpu-memory-swap.sh #!/bin/bash echo "-------------------------------------------" echo "Server_Name CPU(%) Memory(%) Swap(%)" echo "-------------------------------------------" for server in `more /opt/scripts/server-list.txt` do scpu=$(ssh $server cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1) smem=$(ssh $server free | awk '/Mem/{printf("%.2f%"), $3/$2*100}') sswap=$(ssh $server free | awk '/Swap/{printf("%.2f%"), $3/$2*100}') echo "$server $scpu $smem $sswap" done | column -t echo "-------------------------------------------"
Set an executable Linux file permission to “cpu-memory-swap.sh” file.
# chmod +x /opt/scripts/cpu-memory-swap.sh
When you run the script you will get a result like the one below.
# sh cpu-mem-black.sh --------------------------------------------------- Server_Name CPU(%) Memory(%) Swap(%) --------------------------------------------------- CentOS7.2daygeek.com 0.04% 12.77% 0.00% Ubuntu18.2daygeek.com 0.29% 28.43% 0.00% ---------------------------------------------------
Method-2 : Shell Script to Monitor CPU, Memory and Swap usage on Multiple Linux System with eMail alert
This Bash script collects and sends a mail with a CPU, Memory and Swap usage for a given Linux system at a given interval in a text file.
# vi /opt/scripts/cpu-memory-swap-1.sh #!/bin/bash if which mailx > /dev/null then echo "mailx package is exist" elif (( $(cat /etc/*-release | grep "Red Hat" | wc -l) > 0 )) then yum install mailx -y > /dev/null else apt install mailutils -y > /dev/null fi echo "-------------------------------------------" >> /tmp/cpu-mem-swap.txt echo "Server_Name CPU(%) Memory(%) Swap(%)" >> /tmp/cpu-mem-swap.txt echo "-------------------------------------------" >> /tmp/cpu-mem-swap.txt for server in `more /opt/scripts/server-list.txt` do scpu=$(ssh $server cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1) smem=$(ssh $server free | awk '/Mem/{printf("%.2f%"), $3/$2*100}') sswap=$(ssh $server free | awk '/Swap/{printf("%.2f%"), $3/$2*100}') echo "$server $scpu $smem $sswap" >> /tmp/cpu-mem-swap.txt done | column -t echo "-------------------------------------------" >> /tmp/cpu-mem-swap.txt echo "CPU and Memory Report for `date +"%B %Y"`" | mailx -s "CPU and Memory Report on `date`" -a /tmp/cpu-mem-swap.txt [email protected] rm /tmp/cpu-mem-swap.txt
Set an executable Linux file permission to “cpu-memory-swap-1.sh” file.
# chmod +x /opt/scripts/cpu-memory-swap-1.sh
Finally add a cronjob to automate this. It runs every 2 hours.
# crontab -e 0 */2 * * * /bin/bash /opt/scripts/cpu-memory-swap-1.sh
Note: You will receive an email alert every 2 hours.
Method-3 : Shell Script to Monitor CPU, Memory and Swap usage on Multiple Linux System with eMail alert
This Bash script collects and sends a mail with a CPU, Memory and Swap usage for a given Linux system at a given interval in a excel file.
# vi /opt/scripts/cpu-memory-swap-2.sh #!/bin/bash if which mailx > /dev/null then echo "mailx package is exist" elif (( $(cat /etc/*-release | grep "Red Hat" | wc -l) > 0 )) then yum install mailx -y > /dev/null else apt install mailutils -y > /dev/null fi echo "Server_Name, CPU, Memory, Swap" > /tmp/cpu-mem-swap.csv for server in `more /opt/scripts/server-list.txt` do scpu=$(ssh $server cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1) smem=$(ssh $server free | awk '/Mem/{printf("%.2f%"), $3/$2*100}') sswap=$(ssh $server free | awk '/Swap/{printf("%.2f%"), $3/$2*100}') echo "$server, $scpu, $smem, $sswap" >> /tmp/cpu-mem-swap.csv done echo "CPU and Memory Report for `date +"%B %Y"`" | mailx -s "CPU and Memory Report on `date`" -a /tmp/cpu-mem-swap.csv [email protected] rm /tmp/cpu-mem-swap.csv
Set an executable Linux file permission to “cpu-memory-swap-2.sh” file.
# chmod +x /opt/scripts/cpu-memory-swap-2.sh
Finally add a cronjob to automate this. It will run everyday at 8’o clock.
# crontab -e 0 8 * * * /bin/bash /opt/scripts/cpu-memory-swap-2.sh
Note: You will be getting an email alert everyday at 8 o’clock.
You will receive a output like the one below by mail.
Thanks Magesh. I wrote a single system profiling script with inspiration from yours above. I included an option to output to screen as well as a file via argument “p”:
#!/bin/bash
OUT_FILE=”/tmp/system_profile.log”
echo “——————————————-” >> $OUT_FILE
echo -e “Timestamp\tCPU(%)\tRAM(%)\tSwap(%)” >> $OUT_FILE
while true; do
stimestamp=$(date –rfc-3339=ns)
scpu=$(cat /proc/stat | awk ‘/cpu/{printf(“%.2f\n”), ($2+$4)*100/($2+$4+$5)}’ | awk ‘{print $0}’ | head -1)
smem=$(free | awk ‘/Mem/{printf(“%.2f”), $3/$2*100}’)
sswap=$(free | awk ‘/Swap/{printf(“%.2f”), $3/$2*100}’)
echo -e “$stimestamp\t$scpu\t$smem\t$sswap” >> $OUT_FILE
if [[ ! -z $1 && $1 = “p” ]]
then
echo -e “$stimestamp\t$scpu\t$smem\t$sswap”
fi
sleep 0.2
done
You are welcome.