How does Linux execute scripts on multiple remote servers through a single server call, and the results are displayed locally?

Source: Internet
Author: User
Tags disk usage

Now all of the popular automation operations, may not be the current technology, many automation tools are not used, so this time only through SSH to achieve the function.

Description: A simple script of your own, just to achieve the basic functionality, still needs to be optimized.

Altogether three machines:

master:192.168.4.91

slave1:192.168.4.45

slave2:192.168.4.96

Scripts on master and Slave2 are executed remotely on slave1, and the results are displayed or placed locally. Of course, more than one system is OK.

First, the script function is mainly monitoring the system of some resources, such as CPU, disk, speed and so on. system_load.sh

[email protected] ~]# cat system_load.sh
#!/bin/bash
Echo-e "\033[31msystem time: ' Date" +%y-%m-%d%h:%m:%s "' \033[0m"
Count= ' ifconfig |grep Ethernet |grep ' ^e ' |awk ' {print $} ' |wc-l '
For i in ' ifconfig |grep Ethernet |grep ' ^e ' |awk ' {print $} '
Do
If [$count-gt 1];then
em1= $i
em2= $i
Else
em1= $i
Fi
Done
Hostname= ' Hostname '
Em1_ip= ' ifconfig $em 1 |grep "inet addr" |awk ' {print $} ' |awk-f: ' {print $} '
Echo-e "Hostname:" $Hostname "${EM1}_IP:" $em 1_ip


#system Load and process number
ECHO-E "\033[31M.............CPU load average and process number..................\033[0m"
load_average=$ (Uptime |gawk-f ': ' {print $NF} ')
running_process=$ (Top-b-D 1-n 1 |sed-n ' 2p ' |awk-f ', ' {print $} ' |awk ' {print $} ')
total_process=$ (Ps-ef |wc-l)
echo "Load_average: ${load_average}"
echo "Running_process: ${running_process}"
echo "Total_process: ${total_process}"

host_running_time=$ (Uptime |sed ' s/^.*up//' | awk-f "," ' {print $1,$2} ')
user_connection_number=$ (Uptime |cut-d,-f 3)
Echo-e "Host_running_time:\t${host_running_time}\t"
Echo-e "User_connection_number:${user_connection_number}"

#system Mem
Echo-e "\033[31m ............ ..... System Mem usage.............................\033[0m "
total_mem=$ (free-m |sed-n ' 2p ' |awk ' {print $} ')
usage_mem=$ (free-m |sed-n ' 2p ' |awk ' {print $} ')
free_mem=$ (free-m |sed-n ' 2p ' |awk ' {print $4} ')
Mem_usage_percent= ' free-m |sed-n ' 2p ' |awk ' {printf '%-1d ', $3/$2*100} '

swap_total_mem=$ (free-m |sed-n ' 4p ' |awk ' {print $} ')
swap_usage_mem=$ (free-m |sed-n ' 4p ' |awk ' {print $} ')
swap_free_mem=$ (free-m |sed-n ' 4p ' |awk ' {print $4} ')
swap_mem_usage_percent= ' free-m |sed-n ' 4p ' |awk ' {printf '%-1 D ", $3/$2*100} '
Echo-e" Total_mem: ${total_mem}m\t\t "" Usage_mem: ${usage_mem}m\t "
echo-e" Free_mem: ${Free_ mem}m\t\t "mem_usage_percent: ${mem_usage_percent}%
echo-e" Swap_total_mem: ${swap_total_mem}m\t\t "" Swap_Usage_ Mem: ${swap_usage_mem}m\t "
echo-e" Swap_free_mem: ${swap_free_mem}m\t\t "swap_mem_usage_percent: ${Swap_Mem_ usage_percent}%


#Disk Monitoring
Echo-e "\033[31m ................. Disk usager...........................\033[0m "
Sdb_usage= ' df-h |grep sdb |awk ' {print $} '
echo "SDB disk usage: $sdb _usage"
Sda_home_usage= ' df-h |grep home |awk ' {print $} '
Sda_root_usage= ' df-h |grep "/$" |awk ' {print $4} '
Sda_var_usage= ' df-h |grep "var" |awk ' {print $} '
echo "Root disk Usage: $SDA _root_usage"
echo "Home disk usage: $SDA _home_usage"
echo "var disk usage: $SDA _var_usage"

#网卡速率和流量监控
ECHO-E "\033[31m..................network card rate and traffic.......................\033[0m"
Em1_ip= ' ifconfig $em 1 |grep "inet addr" |awk ' {print $} ' |awk-f: ' {print $} '
Em2_ip= ' ifconfig $em 2 |grep "inet addr" |awk ' {print $} ' |awk-f: ' {print $} '
em1_rx= ' ifconfig $em 1 |sed-n ' 8p ' |awk-f ' [()] ' {print $} '
em1_tx= ' ifconfig $em 1 |sed-n ' 8p ' |awk-f ' [()] ' {print $4} '
Em2_rx= ' ifconfig $em 2 |sed-n ' 8p ' |awk-f ' [()] ' {print $} '
Em2_tx= ' ifconfig $em 2 |sed-n ' 8p ' |awk-f ' [()] ' {print $4} '
#em1_Speed = ' Ethtool $em 1 |grep-i speed |awk-f: ' {print $} '
#em2_Speed = ' Ethtool $em 2 |grep-i speed |awk-f: ' {print $} '
Flowa=/tmp/.eth1
Ifconfig $em 1 |grep "RX byte" |awk ' {print $ "" $6} ' |awk-fbytes: ' {print "INPUT" $ "OUTPUT" $ $ "\ > $FLOWA
Inputa= ' cat $FLOWA |awk ' {print $} '
Outputa= ' cat $FLOWA |awk ' {print $4} '
Sleep 1
Ifconfig $em 1 |grep "RX byte" |awk ' {print $ "" $6} ' |awk-fbytes: ' {print "INPUT" $ "OUTPUT" $ $ "\ > $FLOWA
Inputb= ' cat $FLOWA |awk ' {print $} '
Outputb= ' cat $FLOWA |awk ' {print $4} '
Inputc= ' echo ' $INPUTB-$INPUTA | BC '
Outputc= ' echo ' $OUTPUTB-$OUTPUTA | BC '
Inputmba= ' echo $INPUTC |awk ' {printf '%0.3f\n ', $1/1024} '
Outputmba= ' echo $OUTPUTC |awk ' {printf '%0.3f\n ', $1/1024} '
Input= ' echo $INPUTMBA |awk ' {printf '%0.3f\n ', $1/1024} '
Output= ' echo $OUTPUTMBA |awk ' {printf '%0.3f\n ', $1/1024} '
If [$INPUTC-le 1048576];then
If [$OUTPUTC-le 1048576];then
Echo-e "${EM1}_IP: ${em1_ip}\t" "${em1}_rx: ${em1_rx}\t" "${em1}_tx: ${em1_tx}" "${em1}_input_second:" ${INPUTMBA}K/S " "${em1}_output_second:" ${outputmba}k/s "
Else
Echo-e "${EM1}_IP: ${em1_ip}\t" "${em1}_rx: ${em1_rx}\t" "${em1}_tx: ${em1_tx}" "${em1}_input_second:" ${INPUTMBA}K/S " "${em1}_output_second:" ${output}m/s "
Fi
elif [$INPUTC-gt 1048576];then
If [$OUTPUTC-gt 1048576];then
Echo-e "${EM1}_IP: ${em1_ip}\t" "${em1}_rx: ${em1_rx}\t" "${em1}_tx: ${em1_tx}" "${em1}_input_second:" ${INPUT}M/S "" ${ Em1}_output_second: "${OUTPUTMBA}K/S"
Else
Echo-e "${EM1}_IP: ${em1_ip}\t" "${em1}_rx: ${em1_rx}\t" "${em1}_tx: ${em1_tx}" "${em1}_input_second:" ${INPUT}M/S "" ${ Em1}_output_second: "${output}m/s"
Fi
Fi
If [-Z $em 2];then
Echo
Else
Flowa=/tmp/.eth2
Ifconfig $em 2 |grep "RX byte" |awk ' {print $ "" $6} ' |awk-fbytes: ' {print "INPUT" $ "OUTPUT" $ $ "\ > $FLOWA
Inputa= ' cat $FLOWA |awk ' {print $} '
Outputa= ' cat $FLOWA |awk ' {print $4} '
Sleep 1
Ifconfig $em 2 |grep "RX byte" |awk ' {print $ "" $6} ' |awk-fbytes: ' {print "INPUT" $ "OUTPUT" $ $ "\ > $FLOWA
Inputb= ' cat $FLOWA |awk ' {print $} '
Outputb= ' cat $FLOWA |awk ' {print $4} '
Inputc= ' echo ' $INPUTB-$INPUTA | BC '
Outputc= ' echo ' $OUTPUTB-$OUTPUTA | BC '
Inputmba= ' echo $INPUTC |awk ' {printf '%0.3f\n ', $1/1024} '
Outputmba= ' echo $OUTPUTC |awk ' {printf '%0.3f\n ', $1/1024} '
Input= ' echo $INPUTMBA |awk ' {printf '%0.3f\n ', $1/1024} '
Output= ' echo $OUTPUTMBA |awk ' {printf '%0.3f\n ', $1/1024} '
If [$INPUTC-le 1048576];then
If [$OUTPUTC-le 1048576];then
Echo-e "${EM2}_IP: ${em2_ip}\t" "${em2}_rx: ${em2_rx}\t" "${em2}_tx: ${em2_tx}" "${em2}_input_second:" ${INPUTMBA}K/S " "${em2}_output_second:" ${outputmba}k/s "
Else
Echo-e "${EM2}_IP: ${em2_ip}\t" "${em2}_rx: ${em2_rx}\t" "${em2}_tx: ${em2_tx}" "${em2}_input_second:" ${INPUTMBA}K/S " "${em2}_output_second:" ${output}m/s "
Fi
elif [$INPUTC-gt 1048576];then
If ["$OUTPUTC"-gt 1048576];then
Echo-e "${EM2}_IP: ${em2_ip}\t" "${em2}_rx: ${em2_rx}\t" "${em2}_tx: ${em2_tx}" "${em2}_input_second:" ${INPUT}M/S "" ${ Em2}_output_second: "${OUTPUTMBA}K/S"
Else
Echo-e "${EM2}_IP: ${em2_ip}\t" "${em2}_rx: ${em2_rx}\t" "${em2}_tx: ${em2_tx}" "${em2}_input_second:" ${INPUT}M/S "" ${ Em2}_output_second: "${output}m/s"
Fi
Fi
Fi
Echo-e "\033[34m------------------------------END-----------------------------\033[0m"

Second, copy the script to other servers

[email protected] monitor]# cat remote_scp.sh
#!/bin/sh
While read User IP
Do
User= $user
ip= $ip
remote_cmd=/root/system_load.sh
SCP $remote _cmd [email protected] $ip:/home/hadoop
Done < User_ip.txt

Third, user and IP Correspondence list

[email protected] monitor]# cat User_ip.txt
Root 192.168.4.91
Root 192.168.4.96
Root 192.168.4.45

Iv. Remote Execution

[email protected] monitor]# cat remote_ssh.sh
#!/bin/sh
While read User IP
Do
User= $user
ip= $ip
remote_cmd=/root/system_load.sh
ssh-n [email protected] $ip $remote _cmd
Done < User_ip.txt

V. Results:

The red box shows a null value, which is because my test machine does not have these several disk partitions.

How does Linux execute scripts on multiple remote servers through a single server call, and the results are displayed locally?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.