现在都流行自动化运维了,可能目前技术不够,很多自动化工具还不怎么会用,所以本次只是通过ssh来实现功能。
说明:自己写的一个简单脚本,只是实现了基础功能,还有待优化。
一共三台机器:
master:192.168.4.91
slave1:192.168.4.45
slave2:192.168.4.96
在slave1上远程执行master、slave2上的脚本,结果显示或者放到本地。当然多台系统也行。
一、脚本功能主要是监控系统的一些资源,像cpu,磁盘,网速等等。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 $1}‘|wc -l`
for i in `ifconfig |grep Ethernet |grep ‘^e‘ |awk ‘{print $1}‘`
do
if [ $count -gt 1 ];then
em1=$i
em2=$i
else
em1=$i
fi
done
Hostname=`hostname`
em1_ip=`ifconfig $em1 |grep "inet addr" |awk ‘{print $2}‘|awk -F: ‘{print $2}‘`
echo -e "Hostname:"$Hostname "${em1}_IP:" $em1_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 $2}‘ |awk ‘{print $1}‘)
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 $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}%
#Disk monitoring
echo -e "\033[31m..........................Disk usager...........................\033[0m"
sdb_usage=`df -h |grep sdb |awk ‘{print $5}‘`
echo "sdb disk usage: $sdb_usage"
sda_home_usage=`df -h |grep home |awk ‘{print $5}‘`
sda_root_usage=`df -h |grep "/$" |awk ‘{print $4}‘`
sda_var_usage=`df -h |grep "var" |awk ‘{print $5}‘`
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 $em1 |grep "inet addr" |awk ‘{print $2}‘|awk -F: ‘{print $2}‘`
em2_ip=`ifconfig $em2 |grep "inet addr" |awk ‘{print $2}‘|awk -F: ‘{print $2}‘`
em1_rx=`ifconfig $em1 |sed -n ‘8p‘ |awk -F ‘[()]‘ ‘{print $2}‘`
em1_tx=`ifconfig $em1 |sed -n ‘8p‘ |awk -F ‘[()]‘ ‘{print $4}‘`
em2_rx=`ifconfig $em2 |sed -n ‘8p‘ |awk -F ‘[()]‘ ‘{print $2}‘`
em2_tx=`ifconfig $em2 |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/.eth1
ifconfig $em1 |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 $em1 |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 "${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 $em2 ];then
echo
else
FLOWA=/tmp/.eth2
ifconfig $em2 |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 $em2 |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 "${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"
二、拷贝脚本到其他服务器
[[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
三、用户和IP对应列表
[[email protected] monitor]# cat user_ip.txt
root 192.168.4.91
root 192.168.4.96
root 192.168.4.45
四、远程执行
[[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
五、结果:
上图红框里面显示空值,这是因为我测试机没有这几个磁盘分区。
原文地址:https://www.cnblogs.com/yjt1993/p/9556131.html