linux查看io代码,监控IO性能| free命令 |ps命令 |查看网络状态 |linux下抓包(示例代码)...

10.6 监控IO性能

[[email protected] ~]# iostat -x

Linux 3.10.0-514.el7.x86_64 (aminglinux-001) 2018年01月22日 _x86_64_ (2 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle

4.87 0.00 8.42 14.51 0.00 72.20

Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util

sda 0.01 0.74 32.95 10.74 901.28 80.73 44.95 1.19 27.17 22.32 42.04 6.94 30.31

sdb 0.00 0.00 2.39 0.00 14.79 0.00 12.35 0.01 4.12 4.12 0.00 2.40 0.57

dm-0 0.00 0.00 0.48 0.00 3.66 0.00 15.20 0.00 2.62 2.62 0.00 2.53 0.12

关注%util 磁盘运行情况

查看哪个经常在读写,使用iotop [[email protected] ~]# yum install -y iotop

10.7free命令

[[email protected] ~]# free

total used free shared buff/cache available

Mem: 8002828 183304 7458728 8784 360796 7536660

Swap: 4194300 0 4194300

[[email protected] ~]# free -m

total used free shared buff/cache available

Mem: 7815 139 7531 8 144 7454

Swap: 4095 0 4095

[[email protected] ~]# free -h

total used free shared buff/cache available

Mem: 7.6G 139M 7.4G 8.5M 144M 7.3G

Swap: 4.0G 0B 4.0G

只需要敲一个 free 然后回车就可以当前系统的总内存大小以及使用内存的情况。从上例中可看到当前系统内存总大小为8002828(单位是k)已经使用183304, 剩余7458728. 其实真正剩余并不是这个7458728, 而是第二行的4194300, 真正使用的也是第二行的0, 这是因为系统初始化时,就已经分配出很大一部分内存给缓存,这部分缓存用来随时提供给程序使用,如果程序不用,那这部分内存就空闲。所以,查看内存使用多少,剩余多少请看第二行的数据。另外我们还可以加-m 或者-g选项分别以M或G为单位打印内存使用状况 公式:total=used+free+buff/cache avaliab包含free和buffer/cache剩余部分

10.8ps命令

把当前的所有进程列出来

[[email protected] ~]# ps aux

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1 0.4 0.0 190660 3584 ? Ss 22:12 0:06 /usr/lib/systemd/systemd --swit

root 2 0.0 0.0 0 0 ? S 22:12 0:00 [kthreadd]

root 3 0.0 0.0 0 0 ? S 22:12 0:00 [ksoftirqd/0]

root 6 0.0 0.0 0 0 ? S 22:12 0:00 [kworker/u128:0]

root 7 0.0 0.0 0 0 ? S 22:12 0:00 [migration/0]

root 8 0.0 0.0 0 0 ? S 22:12 0:00 [rcu_bh]

root 9 0.0 0.0 0 0 ? S 22:12 0:00 [rcu_sched]

root 10 0.0 0.0 0 0 ? S 22:12 0:00 [watchdog/0]

查看进程中有没有某个进程在运行

[[email protected] ~]# ps aux | grep mysql

root 2151 0.0 0.0 112664 972 pts/0 S+ 22:39 0:00 grep --color=auto mysql

把进程杀死,就要用到PID

[[email protected] ~]# kill 5648

[[email protected] ~]# ps aux |grep pickup

root 5828 0.0 0.0 112664 972 pts/2 S+ 22:44 0:00 grep --color=auto pickup

查看某个进程是在哪里起来的

[[email protected] ~]# ls -l /proc/1663

ps -elf 大同小异,显示的信息基本上和ps aux是一样的。下面介绍几个参数的意义。

PID :进程的id,这个id很有用,在linux中内核管理进程就得靠pid来识别和管理某一个程,比如我想终止某一个进程,则用 ‘kill 进程的pid 有时并不能杀掉,则需要加一个-9选项了 kill -9 进程pid

STAT :表示进程的状态,进程状态分为以下几种(不要求记住,但要了解)

D 不能中断的进程(通常为IO)

R 正在运行中的进程

S 已经中断的进程,通常情况下,系统中大部分进程都是这个状态

T 已经停止或者暂停的进程,如果我们正在运行一个命令,比如说 sleep 10 如果我们按一下ctrl -z 让他暂停,那么我们用ps查看就会显示T这个状态

W 这个好像是说,从内核2.6xx 以后,表示为没有足够的内存页分配

X 已经死掉的进程(这个好像从来不会出现)

Z 僵尸进程,杀不掉,打不死的垃圾进程,占系统一小点资源,不过没有关系。如果太多,就有问题了。一般不会出现。

< 高优先级进程

N 低优先级进程

L 在内存中被锁了内存分页

s 主进程

l 多线程进程

代表在前台运行的进程

这个ps命令是工作中用的非常多的命令之一,所以要记住它。关于ps命令的使用,经常会连同管道符一起使用,用来查看某个进程或者它的数量。

[[email protected] ~]# vmstat 1

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----

r b swpd free buff cache si so bi bo in cs us sy id wa st

0 0 0 7458604 876 360148 0 0 2 1 19 25 0 0 100 0 0

0 0 0 7458612 876 360148 0 0 0 0 66 68 0 0 100 0 0

0 0 0 7458612 876 360148 0 0 0 0 36 44 0 0 100 0 0

^Z

[1]+ 已停止 vmstat 1

[[email protected] ~]# ps aux |grep vmstat

root 5862 0.0 0.0 148308 1352 pts/2 T 15:53 0:00 vmstat 1

root 5864 0.0 0.0 112664 972 pts/2 R+ 15:54 0:00 grep --color=auto vmstat

[[email protected] ~]# fg

10.9、查看网络状态

[[email protected] ~]# netstat -lnp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 952/sshd

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1595/master

tcp6 0 0 :::22 :::* LISTEN 952/sshd

tcp6 0 0 ::1:25 :::* LISTEN 1595/master

udp 0 0 0.0.0.0:68 0.0.0.0:* 4422/dhclient

udp 0 0 127.0.0.1:323 0.0.0.0:* 546/chronyd

udp 0 0 0.0.0.0:38180 0.0.0.0:* 4422/dhclient

udp6 0 0 ::1:323 :::* 546/chronyd

udp6 0 0 :::41347 :::* 4422/dhclient

raw6 0 0 :::58

netstat命令用来打印网络连接状况、系统所开放端口、路由表等信息。最常用的关于netstat的命令就是这个 netstat -lnp (打印当前系统启动哪些端口)以及 netstat -an (打印网络连接状况)这两个命令非常有用,一定要记住。

[[email protected] ~]# netstat -an

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN

tcp 0 52 192.168.187.130:22 192.168.187.1:51328 ESTABLISHED

tcp6 0 0 :::22 :::* LISTEN

tcp6 0 0 ::1:25 :::* LISTEN

udp 0 0 192.168.187.130:39457 108.59.2.24:123 ESTABLISHED

udp 0 0 0.0.0.0:68 0.0.0.0:*

udp 0 0 127.0.0.1:323 0.0.0.0:*

udp 0 0 0.0.0.0:38180 0.0.0.0:*

udp 0 0 192.168.187.130:43156 85.199.214.101:123 ESTABLISHED

udp6 0 0 ::1:323 :::*

udp6 0 0 :::41347 :::*

raw6 0 0 :::58 :::* 7

查看所有状态的数字

[[email protected] ~]# netstat -an | awk ‘/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}‘

LISTEN 4

ESTABLISHED 1

10.10 linux下抓包

[[email protected] ~]# tcpdump -nn -i ens33 -c 3

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes

16:59:04.360034 IP 192.168.187.130.22 > 192.168.187.1.51328: Flags [P.], seq 1241162132:1241162344, ack 1047084528, win 273, length 212

16:59:04.361318 IP 192.168.187.1.51328 > 192.168.187.130.22: Flags [.], ack 212, win 16201, length 0

16:59:04.361596 IP 192.168.187.130.22 > 192.168.187.1.51328: Flags [P.], seq 212:504, ack 1, win 273, length 292

3 packets captured

3 packets received by filter

0 packets dropped by kernel

指定端口22

[[email protected] ~]# tcpdump -nn -i ens33 port 22

指定host的包

[[email protected] ~]# tcpdump -nn -i ens33 not port 22 and host 192.168.187.130

把抓包内容存放在文件中

[[email protected] ~]# tcpdump -nn -i ens33 -c 100 -w /tmp/1.cap

读这个文件

[[email protected] ~]# tcpdump -r /tmp/1.cap

可以抓WEB80端口的包。比较实用。

[[email protected] ~]# tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值