Shell实战Linux性能统计

ps

ps aux
ps -ef

top

top -n 1 -b
top -d 1 -n 3 -b | grep -i aliyundun --line-buffered | awk '{print $9}'

netstat

作业题

  • 文件检索
    • 在特定目录下找到包含特定debug的数据或者代码
  • 网络统计
    • 压测时统计当前机器的连接数
    • 查看当前开放的端口和进程
  • 性能统计
    • 统计某个进程的cpu和mem的增长情况
  • 任务处理
    • 使用简易的工具对第三方服务做加压并统计性能

作业1

统计某个进程的cpu和mem的增长情况

#不区分大小写
#perf_get aliyundun
#perf_get  12780
perf_get(){
#todo
}

perf_get yundun
cpu mem
1 2
3 4

avg: 2 3 

答案

#perf_get yundun$
#perf_get 12789
perf_get() {
    #使用变量替换位置参数
    local proc="$1"
    local timeout="$2"

    #基本使用给予基本检查
    [ -z "$proc" ] && { echo please give a proc name or pid; return 1; }
    [ -z "$timeout" ] && timeout=10

    #top的批处理输出
    top -b -d 1 -n $timeout |
    #范围限定,为了精准处理
        grep '^[ 0-9][1-9]' |
        #提取精准数据
        awk '{print $1,$9,$10,$12}' |
        #取出感兴趣数据,同时读一行就给后面的管道进程,方便实时显示
        grep --line-buffered -i "$proc" |
        #分组统计并打印,使用tab作为OFS进行输出,fflush()可以实时输出给后面的进程
        awk '
    BEGIN{OFS="\t";print "CPU","MEM"}
    {cpu+=$2; mem+=$3;print $1,$2,$3,$4;}
    END{print "";print "avg: ",cpu/NR,mem/NR}'
}

[root@shell.testing-studio.com ~]$ perf_get yundun$ 5
CPU	MEM
7957	0.0	0.7	AliYunDun
7957	2.9	0.7	AliYunDun
7957	3.9	0.7	AliYunDun
7957	3.9	0.7	AliYunDun
7957	3.9	0.7	AliYunDun

avg: 	2.92	0.7

课后作业1

查下当前机器有多少到22端口的tcp连接

tcp_connections(){
}
2 Likes

1楼占楼

function perf_get(){
  local k=$1
  if [ -z "${k}" ]; then
    echo 'Please input a pid or program name!'
    exit 0
  fi
  if echo "${k}" | grep -E "^[0-9]+$" > /dev/null; then
    # 这里应该还需要先判断进程号是否存在
    top -d 1 -n 20 -b -p ${k} | grep -E "^ [0-9]+" | grep ${k} --line-buffered | awk 'BEGIN{printf("%5s\t%5s\n", "cpu", "mem")} {cpu+=$9; mem+=$10; printf("%5s\t%5s\n", $9, $10);} END{printf("\navg: %5s %5s\n", cpu/NR, mem/NR)}'
  else
    # 这里应该还需要先判断进程是否存在
    top -d 1 -n 20 -b | grep -E "^ [0-9]+" | grep -iw "${k}" --line-buffered | awk 'BEGIN{printf("%5s\t%5s\n", "cpu", "mem")} {cpu+=$9; mem+=$10; printf("%5s\t%5s\n", $9, $10);} END{printf("\navg: %5s %5s\n", cpu/NR, mem/NR)}'
  fi
}

# 访问当前机器22端口的tcp连接数
function tcp_connections(){
  netstat -tn 2>/dev/null | 
  grep "^tcp" | 
  grep -v '127.0.0.1' | 
  awk '$4~/:22$/ {print $5}' | 
  wc -l
}

# 访问当前机器22端口的IP数
function ip_tcp_connections(){
  netstat -tn 2>/dev/null | 
  grep "^tcp" | 
  grep -v '127.0.0.1' | 
  awk '$4~/:22$/ {print $5}' | 
  awk -F: '{print $1}' | 
  sort | uniq -c | wc -l
}
1 Like

作业一

perf_get(){
    top -b -n 1 |
    grep --line-buffered -i "$1" |
    awk 'BEGIN{OFS="\t";print "cpu","mem"}{c+=$9;m+=$10}{print $9,$10,fflush();}END{print "";print "avg: ", c/NR,m/NR}'
}

作业1

perf_get(){
	# 取到对应的pid
	pid=`ps -ef|grep $1|grep -v 'grep' | awk '{print $2}'|head -1`
	# 过滤打印
	top -b -p ${pid} -d 1 -n 10 |
	grep ${pid} --line-buffered |
	awk 'BEGIN{print "cpu\tmem"} {print $9,OFS,$10} {cpu+=$9;mem+=$10} END{print "avg: " cpu/10,OFS,mem/10}'
}

image

课后作业一

tcp_connections(){
    netstat -ant|awk '{print $4}'|grep ":22" |wc -l
}

作业1
perf_get() {
  top -d 1 -n 5 -b | grep -i --line-buffered -E "^ *$1 | $1$" |
    awk 'BEGIN{print "cpu mem"} {print $9" "$10;cpu+=$9;mem+=$10;count+=1} END{print "\navg: "cpu/count" "mem/count}'
}
课后作业
tcp_connections() {
  netstat -nt | grep ESTABLISHED | awk '{print $4}' | grep ":22" | wc -l
}
perf_get(){
    top -b -p 27493 -d 1 -n 3 | 
    grep -i Aliyundun --line-buffered | 
    awk '{print $10"\t"$11}{cpu+=$10;mem+=$11}END{print "avg";print cpu/NR,mem/NR}'
}

课后作业1

查下当前机器有多少到22端口的tcp连接

tcp_connections(){
	netstat -tn | 
	grep -v '127.0.0.1' |
	awk '$4 ~ /:22/ {print $4}' | 
	uniq -c
}

perf_get(){
top -d 1 -n 20 -b | grep -i aliyundun | awk ‘{print $9,$10;fflush();}’ | awk '{a+=$1;b+=$2;c+=1}END{print a/c,b/c}'a,b
}

pref_get(){
    top -n 2 -d 1 -b|grep -i $1|
    awk '{print $9,$10}'|
    awk 'BEGIN {print "cpu\tmem"}{x+=$1;y+=$2;printf "%.1f\t%.1f\n",$1,$2} END {print "avg:\t"x/NR"\t"y/NR}'
}
pref_get(){
    top -d 1 -n 2 -b | grep -i "$1" --line-buffered | awk 'BEGIN{print"%CPU,%MEN"}{print $9,$10;sum_cpu+=$9;sum_mem+=$10}END{print"avg: " sum_cpu/NR" "sum_mem/NR}'
}

课后作业

tcp_connections() {
    netstat -tanp | awk '$4~/:22$/{print $5}' | awk -F: '{print $1}' | uniq -c | wc -l
}

image

课后作业1

查下当前机器有多少到22端口的tcp连接

tcp_connections(){
   netstat -tnp | awk '{if($1=="tcp"||$1=="tcp6") {print $4}}' | awk -F: '{if($2==22){print$1}}' | sort | uniq -c | wc -l
}

作业1

课堂作业:
perf_get(){ 
    top -d 1 -n $1 -b |
    grep '^[0-9][0-9]' |
    awk '{print $1, $9, $10, $12}' |
    grep -i --line-buffered "$2" |
    awk 'BEGIN{OFS="\t"; print "PPID","CPU","MEM","COMMAND"}
    {cpu+=$2; mem+=$3; print $0}
    END{print "\n"; print "avg: ", cpu/NR, mem/NR}';
    }
课后作业一:
tcp_connections(){
    netstat -tn |
    awk '{print $4}' |
    grep '.*\:22$' |
    wc -l
}

课后作业
查下当前机器又多少道22端口的tcp连接
tcp_connections(){
netstat -nat|grep -i “22”|awk ‘{print $NF}’|sort|uniq -c
}

统计一定时间内,某个进程的cpu和mem的增长情况和平均值

perf_get(){
    
    # 使用变量来代替位置参数
    local proc="$1"  # 进程名称或进程pid
    locat timeout="$2"  # 查询多少秒内的情况
    
    # 检查参数变量,设置默认提示或默认值
    [ -z "$proc"] && { echo "请输入一个进程名称或进程pid"; return 1; }
    [ -z "$timeout"] && timeout=10
    
    # 使用top进行批处理输出
    top -b -d 1 -n $timeout |
    # 限定某个范围,为了后续的精准处理
    grep '^[ 0-9][0-9]' |
    # 在限定的范围中,提取指定列的数据
    awk '{print $1,$9,$10,$NF}' |
    # 根据已提取的列数据,用grep查找变量proc的关键字数据,每读一行就给后面的管道进程,便于实时显示
    grep --line-buffered -i $proc |
    # 给每列数据进行分组统计并打印,使用\t(即tab键)作为OFS字段的分隔符进行输出排版
    awk '
    BEGIN {OFS="\t"; print "[PID]\t[CPU]\t[MEM]\t[COMMAND]"}
    {cpu+=$2; mem+=$3; print $1,$2,$3,$4;}
    END {print ""; print "平均值:", cpu/NR, mem/NR}'
}

课后作业:查看当前22端口的tcp连接

tcp_connections(){
    netstat -tn | grep "^tcp" | grep -v '127.0.0.1' | awk '$4~/:22$/ {print $5}' | wc -l
}

课后作业

tcp_connections(){
        netstat -tn|
        awk '$4~/:22$/ {print $5}'|
        awk -F: '{print $1}'|
        sort|uniq -c| wc -l
}

课后作业
netstat -tn|grep ":22"|awk '{print $5}'|sort|uniq -c|wc -l

第一题

perf_get() {
    local proc="$1"
    local timeout="$2"
    local timecount="$3"
    [ -z "$proc" ] && { echo please give a proc name or pid; return 1; }
    [ -z "timeout" ] && timeout=1
    [ -z "timecount" ] && timecount=10
    top -b -d $timeout -n $timecount |
        grep '^[ 0-9][0-9]' |
        awk '{print $1,$9,$10,$12}' |
        grep --line-buffered -i "$proc" |
        awk '
     BEGIN{OFS="\t";print "PID","CPU","MEM","NAME"}
     {cpu+=$2;mem+=$3;print $1,$2,$3,$4;}
     END{print "";print "avg: ",cpu/NR,mem/NR}'
}

第二题

tcp_connections(){
    netstat -ant | awk '{print $4}' | grep ':22' | wc -l
}
关闭