Linux三剑客实战2-性能分析与脚本编写

[00534760@shell.ceshiren.com ~]$ top -b -n 5 -d 1 | grep --line-buffered -i aliyundun$ | awk 'BEGIN{OFS="\t";print "cpu", "mem"}{print $9, $10;cpu+=$9;mem+=$10}END{print "\navg";printf "%.2f\t%.2f\n",cpu/NR, mem/NR}'
cpu	mem
0.0	1.1
1.0	1.1
0.0	1.1
1.0	1.1
1.0	1.1

avg
0.60	1.10


perf_avg ()
{
    top -b -n 5 -d 1 | grep --color=auto --line-buffered -i aliyundun$ | awk 'BEGIN{OFS="\t";print "cpu", "mem"}{print $9, $10;cpu+=$9;mem+=$10}END{print "\navg";printf "%.2f\t%.2f\n",cpu/NR, mem/NR}'
}

vs code格式化

perf_avg() {
    top -b -n 5 -d 1 |
        grep --color=auto --line-buffered -i aliyundun$ |
        awk '
    BEGIN{OFS="\t";print "cpu", "mem"}
    {print $9, $10;cpu+=$9;mem+=$10}
    END{print "\navg";printf "%.2f\t%.2f\n",cpu/NR, mem/NR}
    '
}

netstat -tnp | awk '{print $4, $6}' | sed -r 's#172.19.147.8:##g' | sort | uniq -c | sort -nr
 netstat -tnp |awk '/[0-9]/{print $4,$6}'|awk 'BEGIN{FS="[: ]"}{print $2,$3}'|sort | uniq -c |sort -rn|awk '{print $2,$3,$1}'
1 个赞
connections_summary(){
netstat -tnp |awk '{print $4,$6}' | awk -F ":" '{print $2}' |sort | uniq -c
}

[quote=“seveniruby, post:1, topic:4332”]
分析阿里云盾进程的cpu与mem,每秒统计一次,统计20

netstat -tnp| awk 'BEGIN{OFS="\t";print "num", "port", "state"}{print $4,$6}'|sed 's/[0-9.]*://g' |sort |uniq -c
netstat -tnp | awk '{print $4,$6}' | sort | sed -e 's/:/ /g' | awk '{print $2 "\t" $3}'|uniq -c
function lucky(){
if [ $# -eq  0 ]
then
	read  currentName
else
	currentName=$@
fi
count=0
while true
do
	let count+=1
	result=""
	for name in $currentName
	do
		result+=$name":"$((RANDOM%6+1))" "
	done
	winName=`echo $result |awk 'BEGIN{FS=":";RS=" "}$2>3{print $1}'`
	winNum=`echo $result |grep -io [456] |wc -l`
	
	echo "第$count轮:"$winName
	if [ $winNum -eq 0 ]
	then
		continue
	elif [ $winNum -eq 1 ]
	then
		break
	else
		currentName=$winName
	fi
done
}
lucky() {
	rm -f round*
	seeds=$(cat name_list.txt)
	count=0
	num=0
	while (($count != 1)); do
		((num += 1))
		touch round$num.txt
		for line in $seeds; do
			if ((($RANDOM % 6 + 1) > 3)); then
				echo $line >>round$num.txt
			fi
		done
		if [ ! -s round$num.txt ]; then
			continue
		else
			seeds=$(cat round$num.txt)
		fi
		count=$(echo "$seeds" | wc -l)
	done
}
#!/bin/bash
luck(){
name=()
name_del=()
num=0
i=0
          
# 从文件中获取用户存入到数组里
for item in $(cat name_list.txt)
do       
        name[num]="$item"
        let num+=1 
done         
while [ ${#name[*]} -gt 1 ]
do
        for num in ${!name[*]}
        do
                luck_num=$((RANDOM%6+1))
                if [ $luck_num -le 3 ]
                then 
                        name_del[i]="${name[$num]}"
                        let i+=1
                        unset name[$num]
                fi
        done
        let round++
        echo "第"$round"轮:"${name[*]}
        if [ ${#name[*]} -eq 0 ]
        then
                name=(${name[*]} ${name_del[*]})
                let round++
                echo "第"$round"轮:"${name[*]}
        elif [ ${#name[*]} -eq 1 ]
        then break
        elif [ ${#name[*]} -gt 1 ]
        then
                unset name_del
                i=0
        fi
done

}
luck

抽奖脚本
抽奖名单:lucky.txt

lucky(){
#!/bin/bash
num=0
for line in `awk '{print $1}' lucky.txt `
 do
  a+=$line" "
done
echo $a
while true
do
c=0
b=""
((num++))
 for ln in  $a
 do
  if [ $((RANDOM%6+1)) -gt 3 ];then
   ((c++))
   b+=$ln" "
  fi
 done
 if [ "$b" =  "" ] ;then
  echo "第 $num 轮无人中奖";
 else
  echo "第 $num 轮中奖名单 : $b"
 fi
 if [ $c == 0 ] ;then
 continue
 elif [ $c == 1 ] ; then
 break
 else
 a=$b
 fi
done
}
lucky(){
#从name_list中读取名单到seeds
seeds=()
for name in $(cat name_list.txt)
do       
   seeds+=($name)
done
echo '种子选手名单:'${seeds[@]}

#计算出一名幸运中奖者
count=1
while true
do
   winner=()
   for i in ${seeds[@]}
   do
      if [ $((RANDOM%6+1)) -gt 3 ]
      then winner+=($i)
      fi
   done
   echo "第“$count”轮胜出的人员是:"${winner[@]}
   ((count++))	
   if [ ${#winner[@]} -eq 0 ]
   then winner=$seeds
   else seeds=$winner
   fi
   if [ ${#winner[@]} -eq 1 ]
   then break
   fi
done
echo "幸运中奖者是:"${winner[@]}
}

命令行参数传递:lucky a b c d e g h
文件获取清单: lucky XXX.txt, lucky >xxx.txt 均可
管道获取清单: echo a b c d | lucky

lucky(){
if [ $# -gt 1 ] ; then
    promotion_name=($@)
elif [ $# == 1 ] && [ -f $1 ] ; then
    num=0
    for name in $(cat $1)
    do
        promotion_name[num]="$name"
        let num+=1
    done
elif [ $# == 0 ] ; then
    num=0
    for name in $(cat)
    do
        promotion_name[num]="$name"
        let num+=1
    done
fi
eliminate_lname=()
round=0
while [ ${#promotion_name[@]} -ge 1 ]
do  
    for i in "${!promotion_name[@]}";
    do  
        if [ $((RANDOM%6+1)) -le 3 ] ; then
            eliminate_name+=(${promotion_name[$i]})
            unset promotion_name[$i]
        fi
    done    
    if [ ${#promotion_name[@]} -eq 0 ] ; then
        let round++
        echo "第"$round"轮:"${promotion_name[@]}
        promotion_name=(${eliminate_name[@]})
        unset eliminate_name
        let round++
        echo "第"$round"轮:"${promotion_name[@]}
    elif [ ${#promotion_name[@]} -eq 1 ] ; then
        let round++  
        echo "第"$round"轮获胜:" ${promotion_name[@]}
        break
    elif [ ${#promotion_name[@]} -gt 1 ] ; then
        let round++  
        echo "第"$round"轮:" ${promotion_name[@]}
        unset eliminate_name 
    fi
done
}
lucky(){
	list_name=0
	list_No=0
	list=()
	time_list=()
	time=1
	for name in $(cat name_list.txt) ; 
		do num=$((RANDOM%6+1)) ; 
			if (($num>3)); 
				then list[$list_No]=$name ;
				let list_No=list_No+1 ;
			fi ; 
		done ;
	per_round_list=${list[*]}
	per_round_list_len=${#list[*]}
	
	while [ ${#list[*]} -ge 1 ] ; 
	do
		time_list[$time]=$per_round_list ; 
		if (($per_round_list_len>0)) ; 
			then echo '第'$time'轮:'${time_list[$time]} ; 
		elif (($per_round_list_len=0})) ; 
			then echo '第'$time'轮:'${time_list[$time]} ;
			time_list[$time]=${time_list[`expr $time - 1`]}
		elif (($per_round_list_len=1})) ; 
			then echo '第'$time'轮:'${time_list[$time]} ;
	  fi ; 
	
		list_No=0
		list=()
		for name in ${time_list[$time]} ; 
		do num=$((RANDOM%6+1)) ; 
			if (($num>3)); 
				then list[$list_No]=$name ;
				let list_No=list_No+1 ;
			fi ; 
		done ;
		per_round_list=${list[*]}
		per_round_list_len=${#list[*]}
	  let time+=1
	done
}
connections_summary()
{
netstat -tnp 2>/dev/null | awk 'NR>2{print $4,$6}' | awk -F: '{print $2,$3}' | sort | uniq -c | sort -rn|awk 'BEGIN{OFS="\t";print "port", "status","count"}{print $2,$3,$1}';
}

connections_summary

port status count
22 CLOSE_WAIT 3
22 ESTABLISHED 30

 netstat -tnp 2>/dev/null | awk 'NR>2' | awk -F '[: ]+' '{print $5,$8}' | sort | uniq -c | sort -nr | awk '{print $2, $3, $1}'
  • 实战shell脚本小游戏,支持命令行参数和文件读入参数 :cactus:
    预格式化文本将缩进 4 格

–shell游戏脚本
#!/bin/bash
#掷色子游戏
#支持命令行参数传递,支持从文件、从管道读取种子清单,种子清单可以自己模拟一个多行数据文件
#游戏规则:RANDOM%6+1 模拟掷骰子 如果>3 晋级到下一轮
#如果此轮无人晋级,自动复活上轮选手
function lucky(){
#玩家
players=$@;
if [ $# -eq 0 ];then
#从文件或管道符读入
unset players;
while read line;
do
players+=( “$line” );
done
if [ ${#players[@]} -eq 0 ];then
printf “没有参赛选手,游戏自动结束!\n”;
fi
fi
#开始游戏
printf “开始游戏,现在玩家入场”;
n=1;#游戏轮次
state=0;#0-继续 、1-重玩、2-结束
while [ $state -ne 2 ]
do
printf “\n第%d轮:%s” $n “${players[]}";
temp=();#存放进入下一轮的选手
for p in ${players[@]}
do
point=$((RANDOM%6+1));
#printf “%s掷出了%d点\t” $p $point;
if [ $point -gt 3 ];then
temp+=( “$p” );#进入下一轮的选手
fi
done
#printf “\n第%d轮结束!\n” $n;
#判断游戏是否结束
if [ ${#temp[@]} -eq 0 ];then
if [ ${#players[@]} -eq 1 ];then state=2;#当前只有一位玩家,且未进入下一轮,游戏结束
else state=1;
fi
else #下一轮选手数量大于0,游戏继续
players=("${temp[@]}");
state=0;
fi
#printf “%s进入下一轮\n” "${temp[
]}”;
n=expr $n + 1;
done
printf “\n****游戏结束\n”;
}
lucky a b c d e f g #调用函数并传递参数
lucky < name_list.txt

命令行参数传递:lucky a b c d e g h
文件获取清单: lucky < xxx.txt
管道获取清单: echo a b c d | lucky

lucky(){
let count=0
if [ $# -lt 1 ]
then
    pro_name=($(cat $1))
else
    pro_name=$@
fi
while true
do
    echo ${pro_name[@]}
    winner=()
    for i in ${pro_name[@]}
    do
         if [ $((RANDOM%6+1)) -ge 3 ]
         then  winner+=($i)
         fi
    done
    let count++
    if [[  ${#winner[@]} -gt 1 ]]
    then 
        pro_name=(${winner[@]})
        echo "第$count轮胜出人员:"${winner[@]}
    elif  [ ${#winner[@]} -eq 1 ]
    then
        echo "第$count轮胜出人员:"${winner[@]}
        echo "最终获胜者:"${winner[@]}
        break;
     else
         echo "第$count轮无获胜者,重新比赛!"
    fi
done
}

perf_avg () 
{ 
    top -b -n 20 -d 1 | 
            grep --color=auto --line-buffered -i aliyundun$ | 
            awk '
    BEGIN{OFS="\t";print "cpu","mem"}{print $9,$10; cpu+=$9; mem+=$10}END{print 
    "\navg\n";printf "%.2f\t%.2f\n", cpu/NR,mem/NR}
    '
}

##printf "%.2lf" 和 printf "%.2f" 都可以


connections_summary () 
{ 
    netstat -ntp 2> /dev/null | 
        awk 'NR>2{print $4,$6}' | 
        awk 'BEGIN{FS=":"}{ print $2}' | 
        sort | uniq -c | sort -nr | 
    awk 'BEGIN{OFS="\t";print "port","status\t","count"}{print $2,$3,$1}'
}



lucky()
{
	seeds=$(while read line ; do echo ${line// /..} ; done < name_list.txt)
	count=0;
	index=0;
	while((count!=1)); do
		((index+=1));
		seeds=$(for line in $seeds ; do (((RANDOM%6 + 1)>3)) && echo $line ; done);
		echo;
		echo 第 $index 轮 $seeds;
		count=$(echo "$seeds" | wc -l);
	done;
}
lucky(){
Namelist=$(cat name_list.txt)
round=0
while true
do
        let round+=1
        echo "round:$round" "member:${Namelist[*]}"
        i=0
        pass_list=()
        for member in ${Namelist[*]}
        do
                result=$((RANDOM%6+1))
                if [ $result -gt 3 ]
                then
                        pass_list[$i]=$member
                        let i+=1
                fi
        done
        if [ $i == 0 ]
        then
                continue
        elif [ $i == 1 ]
        then
                echo "winner:${pass_list[0]}"
                break
        else
                Namelist=${pass_list[*]}
                continue
        fi
done
}
lucky