标题
课程价值
- 掌握Linux的性能统计相关命令
- 掌握基本的bash脚本编写与调试方法
- 可以应对大多数公司的linux技能面试题
大纲
- 性能统计
- 脚本编写
- 应用
ppt
作业
perf_avg
分析阿里云盾进程的cpu与mem,每秒统计一次,统计20s并给出一个cpu与mem的平均值
需要注意的几个事项
- ps与top在获取cpu数据上的区别, ps aux的cpu是累积平均cpu,不是实时cpu。实时cpu其实是top
- top -b 输出用于批处理的数据格式,并且使用了grep的时候要加上–line-buffered
- aliyundun有2个进程,需要使用aliyundun$去精准匹配
perf_avg(){
#todo:
}
perf_avg
cpu mem
10 5
8 5
...
avg
9 5
答案
[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作业: connections_summary
统计每个网络端口对应的不同状态的网络连接数
connections_summary
port status count
22 CLOSE_WAIT 3
22 ESTABLISHED 30
常用参数
-l, --listening
Show only listening sockets. (These are omitted by default.
[--tcp|-t]
-p, --program
Show the PID and name of the program to which each socket belongs.
--numeric , -n
Show numerical addresses instead of trying to determine symbolic host, port or user names.
常用命令
[00534760@shell.ceshiren.com ~]$ netstat -tlnp
(No info could be read for "-p": geteuid()=1929 but you should be root.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9101 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:9102 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:9009 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:8887 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:44728 0.0.0.0:* LISTEN -
tcp 6 0 0.0.0.0:8000 0.0.0.0:* LISTEN -
[00534760@shell.ceshiren.com ~]$ netstat -tnp |head -10
(No info could be read for "-p": geteuid()=1929 but you should be root.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.19.147.8:9102 39.106.113.77:38672 ESTABLISHED -
tcp 0 0 172.19.147.8:22 218.107.55.252:5109 ESTABLISHED -
tcp 0 0 172.19.147.8:22 117.10.232.185:55669 ESTABLISHED -
tcp 0 0 172.19.147.8:22 117.100.72.205:55133 ESTABLISHED -
tcp 0 0 127.0.0.1:9101 127.0.0.1:45570 TIME_WAIT -
tcp 0 0 172.19.147.8:22 101.206.13.175:53238 ESTABLISHED -
tcp 244 0 172.19.147.8:8000 3.236.77.22:58188 CLOSE_WAIT -
tcp 0 0 127.0.0.1:9101 127.0.0.1:45546 TIME_WAIT -
时长
ppt
见回帖
性能统计
[00534760@shell.ceshiren.com ~]$ ps aux | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 125952 3656 ? Ss 7月02 10:13 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root 2 0.0 0.0 0 0 ? S 7月02 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 7月02 0:22 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 7月02 0:00 [kworker/0:0H]
[00534760@shell.ceshiren.com ~]$ top -b -n 1 -d 1
top - 20:23:52 up 30 days, 2:13, 36 users, load average: 0.16, 0.06, 0.06
Tasks: 210 total, 1 running, 206 sleeping, 2 stopped, 1 zombie
%Cpu(s): 1.2 us, 0.8 sy, 0.0 ni, 97.9 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3881920 total, 1070900 free, 424112 used, 2386908 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 2943308 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
20102 root 20 0 0 0 0 S 6.2 0.0 0:00.66 kworker/0:1
23609 00534760 20 0 157700 2180 1516 R 6.2 0.1 0:00.01 top
1 root 20 0 125952 3656 1552 S 0.0 0.1 10:13.36 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.27 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:22.05 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:+
脚本编写
数组变量的用法
- 初始化:array=(1 3 4 6) array=(
ls
) - 基于下标赋值:array[2]=“hello world”; echo ${array[2]}
- 尾部追加:a2+=(d)
- 遍历:for i in “${array[@]}”; do echo $i; done
- 删除某个子元素:unset array[1]
- 取下标:echo ${!a1[@]}
- 数组长度:echo ${#array[@]}
课后作业 lucky()
要求
- 支持命令行参数传递,支持从文件、从管道读取种子清单,种子清单可以自己模拟一个多行数据文件
- echo $((RANDOM%6+1)) 模拟掷骰子 如果>3 晋级到下一轮
- 如果此轮无人晋级,自动复活上轮选手
lucky < name_list.txt
curl xxx | lucky
lucky a b c d e g h
第1轮: a e g h
第2轮: a g h
第3轮: a g h
第4轮:
第5轮: a g h
第6轮: h