shell脚本应用《十》查看多个系统CPU,指定的进程CPU,主备机,内存使用情况

需求:查看多台机器看多个系统CPU,指定的进程CPU,主备机状态,内存使用情况;并在一台机器上显示;

第一:先设置ssh免密码登录

ssh-keygen -t rsa P'' -f ~/.ssh/id_rsa 不提示直接生成秘钥
ssh-copy-id -i ~/.ssh/id_rsa.pub root@ip

第二:查询脚本,分别拷贝到各个机器上

scp /usr/local/src/cpu_men.sh root@ip: /usr/local/src/
脚本内容
#!/bin/bash
cpu=top -bn1|awk -F "[,:%]" '/Cpu/{print $2}'
men_total=free -m | grep Mem|awk '{print $2}'
men_used=free -m | grep Mem|awk '{print $3}'
men_av=awk 'BEGIN{printf"%.0f\n",('${men_used}'/'${men_total}')*100}'
num=ifconfig|awk '/eth.:./{print $1}'|wc -l
syseth="主"
[ ${num} -eq 0 ] && syseth="备"
IP=ifconfig|awk -F"[: ]+" 'NR==2{print $4}'
pronum=ps aux|grep tdci|grep -Ev "$$|grep"|awk '{print $3}'|wc -l
procpu=ps aux|grep tdci|grep -Ev "$$|grep"|awk '{print $3}'|xargs
#awk 'BEGIN{printf"%-20s%-10s%-10s%-10s%-20s\n","IP------------------","status---------","CPU%------","men%------","procpu%-----"}'
awk 'BEGIN{printf"%-20s%-13s%-12s%-10s","'${IP}'","'$syseth'","'"${cpu}"'",'"${men_av}"'}'
echo "$procpu (${pronum} "tdic")"

第三:总体查询并在一台主机显示脚本:ssh_exec.sh

#!/bin/bash
awk 'BEGIN{printf"%-20s%-10s%-10s%-10s%-20s\n","IP------------------","status---------","CPU%------","men%------","procpu%-----"}'br/>arry=(
192.168.50.5
192.168.50.6
192.168.50.9
)
for((i=0;i<${#arry[@]};i++))
do
ssh root@${arry[i]} "/bin/bash /usr/local/src/cpu_men.sh"
done
/bin/bash /usr/local/src/cpu_men.sh

执行结果

> [root@INTSVR-B src]# sh ssh_exec.sh 
> IP------------------status---------CPU%------men%------procpu%-----        
> 192.168.50.5         备             3.6             10                  1.3 0.5 (2 tdic)
> 192.168.50.6         主             8.4             12                  24.8 2.4 (2 tdic)
> 192.168.50.9        主             4.5             30                  0.7 (1 tdic)
> 192.168.5010       备             1.4             10                  (0 tdic)
> [root@INTSVR-B src]# 
上一篇:SP1480口胡


下一篇:Hello,Word!