Application shell script "ten" view multiple system CPU, the specified process CPU, standby machine, memory usage

Demand: Check to see multiple machines plurality of system CPU, the specified process CPU, main machine standby state, memory usage; and displayed on a single machine;

First: to set free ssh password

ssh-keygen -t rsa P '' -f ~ / .ssh / id_rsa generated keys do not prompt direct
ssh-copy-id -i ~ / .ssh / id_rsa.pub root @ ip

Second: query scripts were copied to each machine

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")"

Third: The overall query and display script in a host: 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

Results of the

> [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]# 

Guess you like

Origin blog.51cto.com/14294148/2437921