如果您出于某种原因要同时检查多个Linux系统性能,请用以下Bash脚本,使用此脚本为多个Linux系统生成CPU、内存和Swap(交换)使用情况。本文有三个脚本选择以生成报告,每个Bash脚本都具有独特的功能,另外,您可以根据需要修改此脚本。
脚本一:在多个Linux系统上检查CPU、内存和Swap使用情况 该脚本允许您从终端检查多个Linux系统上的CPU、内存和Swap使用情况。 # 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 "-------------------------------------------" 将可执行的Linux文件权限设置为“cpu-memory-swap.sh”文件: # chmod +x /opt/scripts/cpu-memory-swap.sh 运行脚本时,您将得到类似以下结果: # sh cpu-mem-black.sh
脚本二:监视多个Linux系统上的CPU、内存和Swap使用情况并把结果发送到指定的邮件中 该Bash脚本以文本文件的给定间隔收集并发送给定Linux系统的CPU、内存和Swap使用情况的邮件。 # 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 249562751@qq.com rm /tmp/cpu-mem-swap.txt 将可执行的Linux文件权限设置为“cpu-memory-swap-1.sh”文件: # chmod +x /opt/scripts/cpu-memory-swap-1.sh 最后添加一个cronjob以使其自动化,它每2小时运行一次: # crontab -e 0 */2 * * * /bin/bash /opt/scripts/cpu-memory-swap-1.sh 注意:您将每2小时收到一封电子邮件警报。
脚本三:在多个Linux系统上监视CPU,内存和Swap使用并把结果发送到指定的邮件中 该Bash脚本在excel文件中以给定的间隔收集并发送给定Linux系统的CPU、内存和Swap使用情况的邮件。 # 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 249562751@qq.com rm /tmp/cpu-mem-swap.csv 将可执行的Linux文件权限设置为“cpu-memory-swap-2.sh”文件: # chmod +x /opt/scripts/cpu-memory-swap-2.sh 最后添加一个cronjob以使其自动化,它将每天早上8点运行: # crontab -e 0 8 * * * /bin/bash /opt/scripts/cpu-memory-swap-2.sh 注意:您每天都会在8点收到电子邮件警报。 您将通过邮件收到类似以下的输出:
相关主题 |