Linux脚本监视多个电脑状态,shell脚本通过电子邮件警报监视多个Linux系统上的CPU,内存和交换使用...

shell脚本通过电子邮件警报监视多个Linux系统上的CPU,内存和交换使用

您的基础架构很小,出于某种原因要同时检查多个系统性能。

该脚本将帮助您为您创建报告。

您可以使用此脚本为多个Linux系统生成CPU,内存和交换使用情况。

本教程中添加了三个Shell脚本以生成报告。

每个shell脚本都具有独特的功能,并选择您需要的功能。

另外,您可以根据需要修改此脚本。

方法1:在多个Linux系统上检查CPU,内存和交换使用情况的Shell脚本

该脚本使您可以从终端检查多个Linux系统上的CPU,内存和交换使用情况。

# 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

---------------------------------------------------

Server_Name             CPU(%) Memory(%)   Swap(%)

---------------------------------------------------

CentOS7.2daygeek.com    0.04%  12.77%      0.00%

Ubuntu18.2daygeek.com   0.29%  28.43%      0.00%

---------------------------------------------------

方法2:使用电子邮件警报在多个Linux系统上监视CPU,内存和交换使用的Shell脚本

该shell脚本以文本文件的给定间隔收集并发送给定Linux系统的CPU,内存和交换使用情况的邮件。

# 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 daygeek@gmail.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小时收到一封电子邮件警报。

方法3:使用电子邮件警报在多个Linux系统上监视CPU,内存和交换使用的Shell脚本

此Shell脚本以excel文件的给定间隔收集并发送给定Linux系统的CPU,内存和交换使用情况的邮件。

# 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  daygeek@gmail.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点收到电子邮件警报。

您将通过邮件收到类似下面的输出。

linux-shell-script-to-monitor-cpu-memory-swap-usage-send-email-2a.png

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值