10.6 监控 IO性能

iostat -x   磁盘使用
iotop  磁盘使用

free   查看内存使用情况
free -m / -g / -h
buffer/cache
公式:total=used+free+buff/cache
avaliable 包含free和buffer/cache

iostat -x

	[root@aming-01 ~]# iostat -x
	Linux 3.10.0-693.17.1.el7.x86_64 (aming-01) 	2018年03月05日 	_x86_64_	(1 CPU)

	avg-cpu:  %user   %nice %system %iowait  %steal   %idle
	           0.06    0.00    0.13    0.00    0.00   99.80

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


%util  io等待处理数据百分比,数字大,说明磁盘忙


iotop

	主要看IO选项

10.7 free

	[root@aming-01 ~]# free -h
	              total        used        free      shared  buff/cache   available
	Mem:           1.8G        142M        1.3G        8.6M        372M        1.5G
	Swap:          4.0G          0B        4.0G

预分配一定容量给 buff/cache 磁盘 --> 内存cache --> CPU CPU --> 内存buffer --> 磁盘

10.8 ps

ps   查看系统进程
	用法: ps aux、ps -elf

stat 部分说明
	D   不能中断的进程
	R   run状态的进程
	S   sleep状态进程
	T   暂停的进程
	Z   僵尸进程
	<   高优先级进程
	N   低优先级进程
	L   内存中被锁了内存分页
	s   主进程
	l   多线程进程
	+   前台进程


	[root@aming-01 ~]# ps aux |grep apache
	root       2391  0.0  0.0 112676   980 pts/0    R+   06:42   0:00 grep --color=auto apache


杀掉某个进程,先找到这个进程的PID,及进程目录

	[root@aming-01 ~]# ls -l /proc/566
	总用量 0
	............................
	lrwxrwxrwx. 1 root root 0 3月   5 06:47 exe -> /usr/bin/vmtoolsd

10.9 查看网络状态

netstat   查看网络状态
netstat -lnp   查看监听端口
netstat -an    查看系统的网络连接状况
netstat -lntp  只看出tcp的,不包含socket
ss -an 和 netstat 异曲同工
分享一个小技巧
netstat -an | awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'

-a  :将目前系统上所有的连线、监听、Socket 数据都列出来
-t  :列出 tcp 网络封包的数据
-u  :列出 udp 网络封包的数据
-n  :不以程序的服务名称,以端口号 (port number) 来显示;
-l  :列出目前正在网络监听 (listen) 的服务;
-p  :列出该网络服务的程序 PID


	[root@aming-01 ~]# netstat -nltup
	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      898/sshd            
	tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1135/master         
	tcp6       0      0 :::22                   :::*                    LISTEN      898/sshd            
	tcp6       0      0 ::1:25                  :::*                    LISTEN      1135/master


	[root@aming-01 ~]# netstat -an | awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'
	LISTEN 	 4
	ESTABLISHED 	 1

	ESTABLISHED   并发连接数,有多少个与服务器连接通信,1000以内服务器通常可以接受

10.10 Linux下抓包

监控系统状态

	抓包工具 tcpdump   yum install -y tcpdump
	用法:tcpdump -nn
	tcpdump -nn -i ens33
	tcpdump -nn port 80
	tcpdump -nn not port 22 and host 192.168.0.100
	tcpdump -nn -c 100 -w 1.cap

	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"
	yum install -y wireshark

	tcpdump 看数据的流向
	udp 包是攻击的可能性比较高


		[root@aming-01 ~]# tcpdump -nn -i ens33 port 22

		[root@aming-01 ~]# tcpdump -nn not port 22
		tcpdump: Bluetooth link-layer type filtering not implemented

		[root@aming-01 ~]# tcpdump -nn not port 22 and host 192.168.104.1
		tcpdump: Bluetooth link-layer type filtering not implemented

		[root@aming-01 ~]# tcpdump -nn -i ens33 -c 1
		tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
		listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
		06:34:33.980191 IP 192.168.104.160.22 > 192.168.104.1.49499: Flags [P.], seq 2353381568:2353381756, ack 1768454766, win 313, options [nop,nop,TS val 13350665 ecr 888596759], length 188
		1 packet captured
		2 packets received by filter
		0 packets dropped by kernel


		[root@aming-01 ~]# tcpdump -nn -i ens33 -c 10 -w /tmp/33.cap
		tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
		10 packets captured
		10 packets received by filter
		0 packets dropped by kernel
		[root@aming-01 ~]# file /tmp/33.cap 
		/tmp/33.cap: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 262144)

		[root@aming-01 ~]# tcpdump -r /tmp/33.cap 
		reading from file /tmp/33.cap, link-type EN10MB (Ethernet)
		06:41:05.765404 IP aming-01.ssh > 192.168.104.1.49499: Flags [P.], seq 2353411216:2353411340, ack 1768460730, win 313, options [nop,nop,TS val 13742450 ecr 888989950], length 124