• Linux 如何通过某一台服务器调用执行多台远程服务器上的脚本,结果显示在本地? 北漂


    现在都流行自动化运维了,可能目前技术不够,很多自动化工具还不怎么会用,所以本次只是通过ssh来实现功能。

    说明:自己写的一个简单脚本,只是实现了基础功能,还有待优化。

    一共三台机器:

    master:192.168.4.91

    slave1:192.168.4.45

    slave2:192.168.4.96

    在slave1上远程执行master、slave2上的脚本,结果显示或者放到本地。当然多台系统也行。

    一、脚本功能主要是监控系统的一些资源,像cpu,磁盘,网速等等。System_Load.sh

    [root@master ~]# cat System_Load.sh

    #!/bin/bash
    echo -e "\033[31m............................系统当前时间............................\033[0m"
    echo -e "System Time: `date "+%Y-%m-%d %H:%M:%S"`"
    echo -e "\033[31m...........................主机名以及IP地址.........................\033[0m"
    count=`ifconfig |grep Ethernet |grep '^e' |awk '{print $1}'|wc -l`
    ifconfig |grep Ethernet |grep '^e' |awk '{print $1}' > /tmp/.value
    i=1
    while read line
    do
    em[$i]=$line
    i=`expr $i + 1`
    done < /tmp/.value
    #echo $i
    #i=1
    for i in `seq $count`
    do
    echo "${em[$i]}" > /dev/null
    done
    Hostname=`hostname`
    if [ $count = 0 ];then
    echo "该服务器没有网卡"
    elif [ $count = 1 ];then
    em1_ip=`ifconfig ${em[1]} |grep "inet addr" |awk '{print $2}'|awk -F: '{print $2}'`
    echo -e "Hostname:"$Hostname "${em[1]}_IP:" $em1_ip
    else
    em1_ip=`ifconfig ${em[1]} |grep "inet addr" |awk '{print $2}'|awk -F: '{print $2}'`
    em2_ip=`ifconfig ${em[2]} |grep "inet addr" |awk '{print $2}'|awk -F: '{print $2}'`
    echo -e "Hostname:"$Hostname "${em[1]}_IP:" $em1_ip
    echo -e "Hostname:"$Hostname "${em[2]}_IP:" $em2_ip
    fi

    #CPU以及进程数量监控
    echo -e "\033[31m.............cpu load average and process number..................\033[0m"
    #us(user time):用户进程执行消耗cpu时间;sy(system time):系统进程执行消耗cpu时间;id:空闲时间(包括IO等待时间);wa:等待IO时间。
    CPU_us=$(vmstat | awk '{print $13}' | sed -n '$p')
    CPU_sy=$(vmstat | awk '{print $14}' | sed -n '$p')
    CPU_id=$(vmstat | awk '{print $15}' | sed -n '$p')
    CPU_wa=$(vmstat | awk '{print $16}' | sed -n '$p')
    CPU_st=$(vmstat | awk '{print $17}' | sed -n '$p')

    CPU1=`cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`
    sleep 1
    CPU2=`cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`
    IDLE1=`echo $CPU1 | awk '{print $4}'`
    IDLE2=`echo $CPU2 | awk '{print $4}'`
    CPU1_TOTAL=`echo $CPU1 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
    CPU2_TOTAL=`echo $CPU2 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
    IDLE=`echo "$IDLE2-$IDLE1" | bc`
    CPU_TOTAL=`echo "$CPU2_TOTAL-$CPU1_TOTAL" | bc`
    RATE=`echo "scale=4;($CPU_TOTAL-$IDLE)/$CPU_TOTAL*100" | bc | awk '{printf "%.2f",$1}'`

    echo -e "us=$CPU_us\tsy=$CPU_sy\tid=$CPU_id\twa=$CPU_wa\tst=$CPU_st"
    echo "CPU_RATE:${RATE}%"
    CPU_RATE=`echo $RATE | cut -d. -f1`
    if [ $CPU_RATE -ge 80 ]
    then echo "CPU Warn"
    ps aux | grep -v USER | sort -rn -k3 | head
    fi
    load_average=$(uptime |gawk -F':' '{print $NF}')
    running_process=$(top -b -d 1 -n 1 |sed -n '2p' |awk -F',' '{print $2}' |awk '{print $1}')
    total_process=$(ps -ef |wc -l)
    echo
    echo "CPU_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}"

    #系统内存监控
    echo -e "\033[31m.......................System Mem usage.............................\033[0m"
    Total_Mem=$(free -m |sed -n '2p' |awk '{print $2}')
    Usage_Mem=$(free -m |sed -n '2p' |awk '{print $3}')
    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 $2}')
    Swap_Usage_Mem=$(free -m |sed -n '4p' |awk '{print $3}')
    Swap_Free_Mem=$(free -m |sed -n '4p' |awk '{print $4}')
    Swap_Mem_Usage_Percent=`free -m |sed -n '4p'|awk '{printf "%-1d",$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}%


    #磁盘监控
    echo -e "\033[31m..........................Disk usager...........................\033[0m"
    #parted适用于Ubuntu
    #parted -l |grep -i 'disk' |grep '/dev/[a-z]d[a-z]' |awk -F: '{print $1}' |awk '{print $2}' > /tmp/.disknumber
    fdisk -l |grep -i 'disk' |grep '/dev/[a-z]d[a-z]' |awk -F: '{print $1}' |awk '{print $2}' > /tmp/.disknumber
    countdisk=`cat /tmp/.disknumber|wc -l`
    i=1
    while read line
    do
    disk[$i]=$line
    i=`expr $i + 1`
    done < /tmp/.disknumber
    for i in `set $countdisk`
    do
    echo "disk[$i]" > /dev/null
    done
    #disk1_usage=`df -h |grep "${disk[1]}" |awk '{print $5}'`
    disk1_home_usage=`df -h |grep home |awk '{print $5}'`
    disk1_root_usage=`df -h |grep "/$" |awk '{print $4}'`
    disk1_var_usage=`df -h |grep "var" |awk '{print $5}'`
    disk1_boot_usage=`df -h |grep "boot" |awk '{print $5}'`
    if [ $countdisk -eq 1 ];then
    echo -e "${disk[1]} disk usage---------> "
    if [ "${disk1_home_usage}" != '' ];then
    echo "home partion usage:$disk1_home_usage"
    fi
    if [ "${disk1_root_usage}" != '' ];then
    echo "root partion usage:$disk1_root_usage"
    fi
    if [ "${disk1_var_usage}" != '' ];then
    echo "var partion usage:$disk1_var_usage"
    fi
    if [ "${disk1_boot_usage}" != '' ];then
    echo "boot partion usage:$disk1_boot_usage"
    fi
    elif [ $countdisk -eq 2 ];then
    disk2_data1_usage=`df -h |grep "${disk[2]}" |awk '{print $5}'`
    echo -e "${disk[2]} disk usage: "
    echo "data1 partion usage:${disk2_data1_usage}"
    echo "${disk[1]} disk usage: "
    if [ "${disk1_home_usage}" != '' ];then
    echo "home partion usage:$disk1_home_usage"
    fi
    if [ "${disk1_root_usage}" != '' ];then
    echo "root partion usage:$disk1_root_usage"
    fi
    if [ "${disk1_var_usage}" != '' ];then
    echo "var partion usage:$disk1_var_usage"
    fi
    if [ "${disk1_boot_usage}" != '' ];then
    echo "boot partion usage:$disk1_boot_usage"
    fi
    fi
    #网卡速率和流量监控
    echo -e "\033[31m..................Network card rate and traffic.......................\033[0m"
    em1_ip=`ifconfig ${em[1]} |grep "inet addr" |awk '{print $2}'|awk -F: '{print $2}'`
    em2_ip=`ifconfig ${em[2]} |grep "inet addr" |awk '{print $2}'|awk -F: '{print $2}'`
    em1_rx=`ifconfig ${em[1]} |sed -n '8p' |awk -F '[()]' '{print $2}'`
    em1_tx=`ifconfig ${em[1]} |sed -n '8p' |awk -F '[()]' '{print $4}'`
    em2_rx=`ifconfig ${em[2]} |sed -n '8p' |awk -F '[()]' '{print $2}'`
    em2_tx=`ifconfig ${em[2]} |sed -n '8p' |awk -F '[()]' '{print $4}'`
    #em1_Speed=`ethtool $em1 |grep -i speed |awk -F : '{print $2}'`
    #em2_Speed=`ethtool $em2 |grep -i speed |awk -F : '{print $2}'`
    FLOWA=/tmp/.${em[1]}
    ifconfig ${em[1]} |grep "RX byte" |awk '{print $2" "$6}' |awk -Fbytes: '{print "INPUT "$2"OUTPUT "$3}'\ > $FLOWA
    INPUTA=`cat $FLOWA |awk '{print $2}'`
    OUTPUTA=`cat $FLOWA |awk '{print $4}'`
    sleep 1
    ifconfig ${em[1]} |grep "RX byte" |awk '{print $2" "$6}' |awk -Fbytes: '{print "INPUT "$2"OUTPUT "$3}'\ > $FLOWA
    INPUTB=`cat $FLOWA |awk '{print $2}'`
    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 "${em[1]}_ip: ${em1_ip}\t" "${em[1]}_RX: ${em1_rx}\t" "${em[1]}_TX: ${em1_tx} " "${em[1]}_Input_Second:"${INPUTMBA}K/S" "${em[1]}_Output_Second:"${OUTPUTMBA}K/S"
    else
    echo -e "${em[1]}_ip: ${em1_ip}\t" "${em[1]}_RX: ${em1_rx}\t" "${em[1]}_TX: ${em1_tx} " "${em[1]}_Input_Second:"${INPUTMBA}K/S" "${em[1]}_Output_Second:"${OUTPUT}M/S"
    fi
    elif [ $INPUTC -gt 1048576 ];then
    if [ $OUTPUTC -gt 1048576 ];then
    echo -e "${em[1]}_ip: ${em1_ip}\t" "${em[1]}_RX: ${em1_rx}\t" "${em[1]}_TX: ${em1_tx} " "${em[1]}_Input_Second:"${INPUT}M/S" "${em[1]}_Output_Second:"${OUTPUTMBA}K/S"
    else
    echo -e "${em[1]}_ip: ${em1_ip}\t" "${em[1]}_RX: ${em1_rx}\t" "${em[1]}_TX: ${em1_tx} " "${em[1]}_Input_Second:"${INPUT}M/S" "${em[1]}_Output_Second:"${OUTPUT}M/S"
    fi
    fi
    if [ -z ${em[2]} ];then
    echo
    else
    FLOWA=/tmp/.${em[2]}
    ifconfig ${em[2]} |grep "RX byte" |awk '{print $2" "$6}' |awk -Fbytes: '{print "INPUT "$2"OUTPUT "$3}'\ > $FLOWA
    INPUTA=`cat $FLOWA |awk '{print $2}'`
    OUTPUTA=`cat $FLOWA |awk '{print $4}'`
    sleep 1
    ifconfig ${em[2]} |grep "RX byte" |awk '{print $2" "$6}' |awk -Fbytes: '{print "INPUT "$2"OUTPUT "$3}'\ > $FLOWA
    INPUTB=`cat $FLOWA |awk '{print $2}'`
    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 "${em[2]}_ip: ${em2_ip}\t" "${em[2]}_RX: ${em2_rx}\t" "${em[2]}_TX: ${em2_tx} " "${em[2]}_Input_Second:"${INPUTMBA}K/S" "${em[2]}_Output_Second:"${OUTPUTMBA}K/S"
    else
    echo -e "${em[2]}_ip: ${em2_ip}\t" "${em[2]}_RX: ${em2_rx}\t" "${em[2]}_TX: ${em2_tx} " "${em[2]}_Input_Second:"${INPUTMBA}K/S" "${em[2]}_Output_Second:"${OUTPUT}M/S"
    fi
    elif [ $INPUTC -gt 1048576 ];then
    if [ "$OUTPUTC" -gt 1048576 ];then
    echo -e "${em[2]}_ip: ${em2_ip}\t" "${em[2]}_RX: ${em2_rx}\t" "${em[2]}_TX: ${em2_tx} " "${em[2]}_Input_Second:"${INPUT}M/S" "${em[2]}_Output_Second:"${OUTPUTMBA}K/S"
    else
    echo -e "${em[2]}_ip: ${em2_ip}\t" "${em[2]}_RX: ${em2_rx}\t" "${em[2]}_TX: ${em2_tx} " "${em[2]}_Input_Second:"${INPUT}M/S" "${em[2]}_Output_Second:"${OUTPUT}M/S"
    fi
    fi
    fi
    echo -e "\033[34m------------------------------END-----------------------------\033[0m"

    二、拷贝脚本到其他服务器

    [root@slave1 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 $user@$ip:/home/hadoop
    done < user_ip.txt

    三、用户和IP对应列表

    [root@slave1 monitor]# cat user_ip.txt
    root 192.168.4.91
    root 192.168.4.96
    root 192.168.4.45

    四、远程执行

    [root@slave1 monitor]# cat remote_ssh.sh
    #!/bin/sh
    while read user ip
    do
    user=$user
    ip=$ip
    remote_cmd=/root/System_Load.sh
    ssh -n $user@$ip $remote_cmd
    done < user_ip.txt

    五、结果

  • 相关阅读:
    [C++设计模式]observer 观察者模式
    Codeforces 425A Sereja and Swaps(暴力枚举)
    linux中设置TAB键的宽度
    iOS 常见面试图总结2
    网络爬虫初步:从訪问网页到数据解析
    大数据时代之hadoop(二):hadoop脚本解析
    数据格式,訪问信息以及操作数指示符
    javaScript实现日历控件
    每一个程序猿都须要了解的一个SQL技巧
    OpenGL编程逐步深入(九)插值处理
  • 原文地址:https://www.cnblogs.com/yjt1993/p/9556131.html
Copyright © 2020-2023  润新知