现在都流行自动化运维了,可能目前技术不够,很多自动化工具还不怎么会用,所以本次只是通过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

五、结果

Linux 如何通过某一台服务器调用执行多台远程服务器上的脚本,结果显示在本地?的更多相关文章

  1. Windows如何快速远程到另一台Windows并管理多个远程服务器

    Windows如何远程到另一台 Windows管理多个远程服务器 Windows第三方远程管理工具 准备远程机器 开启远程机器的远程桌面功能 首先在此电脑(我的电脑)图标上点击鼠标右键,选择" ...

  2. (C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误

    因为工作需要调用WebService接口,查了下资料,发现添加服务引用可以直接调用websevice 参考地址:https://www.cnblogs.com/peterpc/p/4628441.ht ...

  3. python的subprocess:子程序调用(调用执行其他命令);获取子程序脚本当前路径问题

    python当前进程可以调用子进程,子进程可以执行其他命令,如shell,python,java,c... 而调用子进程方法有 os模块 参见:http://blog.csdn.net/longshe ...

  4. linux下连接到远程主机,用图像界面(想在远程服务器上用cmake)

    1. 需要通过SSH -X username@ip登陆服务器后,再用图形界面,比如用cmake 2.直接用 SSH username@ip命令登陆服务器后,不能用cmake

  5. [转]SQLSERVER存储过程调用不同数据库的数据_存储过程中通过链接服务器访问远程服务器

    本文转自:http://blog.csdn.net/nnaabbcc/article/details/7967761 存储过程调用不同数据库的数据 在存储过程调用不同数据库的数据该如何做,比如在存储过 ...

  6. 『学了就忘』Linux基础 — 17、远程服务器关机及重启时的注意事项

    目录 1.为什么远程服务器不能关机 2.远程服务器重启时需要注意两点 3.不要在服务器访问高峰运行高负载命令 4.远程配置防火墙时不要把自己踢出服务器 5.指定合理的密码规范并定期更新 6.合理分配权 ...

  7. wcf远程服务器返回错误404

    最近根据quartz.net 和wcf做资讯内容定时推送,wcf调用的时候出现远程服务器返回错误404,一直找不到原因是什么,客户端和服务器地址和配置都没啥问题,最后发现wcf请求数据,有传输大小限制 ...

  8. Git学习-->如何通过Shell脚本自动定时将Gitlab备份文件复制到远程服务器?

    一.背景 在我之前的博客 git学习--> Gitlab如何进行备份恢复与迁移? (地址:http://blog.csdn.net/ouyang_peng/article/details/770 ...

  9. SQL使用链接服务器执行远程数据库上的存储过程

    原文:SQL使用链接服务器执行远程数据库上的存储过程 --创建链接服务器 exec sp_addlinkedserver'server_tmp','','SQLOLEDB','远程服务器名或ip地址' ...

随机推荐

  1. Authorization Bypass in RSA NetWitness

    https://www.cnblogs.com/iAmSoScArEd/ SEC Consult Vulnerability Lab Security Advisory < 20190515-0 ...

  2. html流程实现

    https://blog.csdn.net/github_39335046/article/details/73930886 https://www.cnblogs.com/xcj26/p/98707 ...

  3. javascript_05-操作符

    一元运算符 a++和++a //5 2 3 var a =1; var b = ++a + ++a; console.log(b) //4 1 3 var a =1; var b = a++ + ++ ...

  4. Zabbix MySQL percona 模板部署

    Zabbix MySQL percona服务端执行以下操作https://www.zabbix.com/download?zabbix=4.0&os_distribution=centos&a ...

  5. JAVA 多线程(一)

    进程和线程 进程:是一个正在执行中的程序.每一个进程执行都有一个执行顺序,该执行顺序是一个执行路径,或者叫一个控制单元. 线程:就是进程中的一个独立的控制单元. 线程在控制着进程的执行. 在计算机中多 ...

  6. Linux之redis的安装,主从配置

    一,redis安装 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  7. 云计算(7)---the scheduler of Hadoop

    The scheduler of Hadoop Programming MapReduce 在有些情况下,reducer也可以先开始于Map.但为了便于理解,在这儿我们都是使reduce不会早于map ...

  8. PL/SQL复合类型

    一.PL/SQL记录:一条记录. 可简化单行多列的数据的处理.当使用pl/sql记录时,应用开发人员即可以自定义记录类型和记录变量,也可以使用%rowtype属性直接定义记录变量. 1.当使用自定义的 ...

  9. 多任务案例--文件夹copy.py

    import os import multiprocessing def copy_file(q,file_name,new_folder_name,old_folder_name): with op ...

  10. Python 使用装饰器装饰类

    1.装饰器装饰函数 了解过或学过装饰器的同学都知道,Python 中的装饰器是可以装饰函数的,如: # 定义一个装饰器 def decorator(func): def inner(*args,**k ...