#-------------------监视主机是否存活-------------------------
for (( i = 0; i < 4; i++ )); do
if ping -c1 $1 &>/dev/null;then
export ping_count"$i"=1
else
export ping_count"$i"=0
fi
sleep 0.3
done
if [ $ping_count1 -eq $ping_count2 ] && [ $ping_count2 -eq $ping_count3 ] && [ $ping_count1 -eq 1 ];then
echo "$1 is down"
else
echo "$1 is up"
fi
#-----------监视端口是否存在---------
#1.推荐测试端口是否存活
telnet IP地址 端口号
#创建一个临时文件
mktemp port_status.xxx
#创建一个临时文件夹
mktemp -d port_status.xxx
#1.判断依赖命令是否存在
port_status (){
temp_file='mktemp port_status.XXX'
#1.判断依赖命令telnet是否存在
[ ! -x /usr/bin/telnet ]&&echo "telnet: not found command "&& exit 1
#2.测试端口 $1 IP $2 port
( telnet $1 $2 <<EOF
quit
EOF
) &>$temp_file
if egrep "\^]" $temp_file &>/dev/null;then
echo "$1 $2 is open"
else
echo "$1 $2 is down"
fi
rm -f $temp_file
}
port_status $1 $2
#---------------------统计内存使用率---------------
head -2 /proc/meminfo |awk 'NR==1{t=$2}NR==2{f=$2;print(t-f)*100/t}'
#内存的申请顺序 free-cache-buffer-swap
memory_user (){
memory_used=$(head -2 /proc/meminfo |awk 'NR==1{t=$2}NR==2{f=$2;print(t-f)*100/t"%"}')
memory_cache=$(head -5 /proc/meminfo |awk 'NR==1{t=$2}NR==2{c=$2;print c*100/t"%"}')
memory_buffer=$(head -5 /proc/meminfo |awk 'NR==1{t=$2}NR==2{b=$2;print b*100/t"%"}')
echo -e "memory_used:$memory_used\tbuffer:$memory_buffer\t cache:$memory_cache"
}
memory_use
#-------------------------------统计使用内存或cpu前10的名的进程---------------------
memory() {
temp_file=`mktemp memory.XXX`
top -b -n 1 > $temp_file
tail -n +8 $temp_file |awk '{array[NF]+=($6)}END {for (i in array) print array[i],i}'|sort -k 1 -n -r |head -10
rm -f $temp_file
}
cpu() {
temp_file=`mktemp cup.XXX`
top -b -n 1 > $temp_file
tail -n +8 $temp_file |awk '{array[NF]+=$9}END {for (i in array) print array[i],i}' |sort -k 1 -n -r |head -10
rm -f $temp_file
}
echo memory
memory
echo cpu
cpu