網絡監控
一. ss
display socket statistics.
參數簡單易用,還提供了很多 TCP 協議棧的參數,而且在 socket 數量巨大時,比 netstat 的輸出快多了,大愛啊!
-a: all,顯示所有的 socket,默認只顯示非 listening 狀態的
-t: tcp,顯示 tcp socket
-u: udp,顯示 udp socket
-w: raw,顯示 raw sockt
-x: unix,顯示 unix domain socket。可以看到 virtual box 使用了 domain socket。
-p: processes,顯示開啓此 socket 的進程,不過很多時候無法顯示,不知道爲什麼
-n: not,不解析服務名稱,默認會顯示知名協議名而非端口號,但很多知名協議,其實並不熟悉其端口號
-l: listening,顯示處於 listening 狀態的 socket
-i: internal,顯示 tcp 的內部信息
-s: summary,顯示彙總信息
一種常用的場景,顯示本機開放的 tcp/udp 端口,現在可以用 ss 來查看:
看了才知道,原來 Fedora 默認開啓了 25 等端口。接下來又涉及到另外一個問題,在 -p 參數無法顯示進程名稱的情況下,如何知道某個端口是哪個進程開的呢?這就要用到接下來的 lsof!
二. lsof
對於一切皆文件的 Linux,list open files 無疑非常管用,常用參數 mark 下,以備後續快速查詢。
1. -i
internet,即跟網絡有關,用法:
lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
46 --> IPv4 or IPv6
protocol --> TCP or UDP
hostname --> Internet host name
hostaddr --> IPv4地址
service --> /etc/service中的 service name (可以不止一個)
port --> 端口號 (可以不止一個)
例:
lsof -i 顯示所有的連接
該命令也可查看本機開放的端口,還能顯示對應的進程,但其缺點是慢,在連接數巨大的情況下基本不可用。
lsof [email protected]:513-515 顯示到 10.143.0.10 的 513-515 端口的 tcp 連接
注意,udp 是無連接的,所以無法通過該命令顯示到其它服務器的 UDP 連接。
比如前面提到的問題,想知道本機的 29754 端口是哪個進程開啓的:
2. 不帶參數,直接接文件名
查看該文件正在被哪些進程打開
lsof abc.txt
3. -c
command,命令,對應進程名稱,列出該進程所打開的文件,用法:
lsof -c nginx 顯示 nginx 進程現在打開的文件
4. -a
and,lsof 的參數之間是 OR 的關係,該參數將 OR 修改成 AND。
5. -s [p:s]
直接 -s 也可使用,用於顯示 file 的 size,但最常用的還是接 [p:s] 選項,p 表示 protocol,s 表示 state,此時一定要同時帶上 -i 選項:“If only TCP and UDP files are to be listed, as controlled by the specified exclusions and inclusions, the -i option must be specified, too”。
lsof -s tcp:established -i tcp
三. nmap
network mapper,掃描神器!
3 種需求:活着?端口?版本?
-sP: Ping Scan - go no further than determining if host is online
-A: Enables OS detection and Version detection
不帶選項運行時,輸出是否 up 及開放的端口。
1. 主機發現
其實把主機發現定義爲是否活着不妥,在 IP 確定的情況下是 OK 的,但如果不知道有哪些 IP,就需要主動來發現了。
-sL: List Scan - simply list targets to scan
說是簡單的列舉參數中的所有主機,在大部分情況下好像是的,但其實會爲每個主機做一次反向域名解析,在全都解析不出來的時候,就變成了 simply list。其作用體現在,如果解析出域名來了,那麼也許能從中發現一些信息。
-sP: Ping Scan - go no further than determining if host is online
man 裏稱其爲地毯式 ping,我比較喜歡這個名字。
關於其行爲,實測後發現有 2 點需要注意:
a. 不僅僅發送 ICMP Echo 報文,還會有 SYN 報文、ACK 報文以及 ICMP TimeStamp 報文,而如果掃描的是局域網,還會發送 ARP 請求
b. root 用戶和非 root 用戶的行爲不同,這個也好理解。對於 root 用戶,有權限使用 rawsocket 僅僅發送 SYN 報文,且在收到 SYN+ACK 後直接 RST 掉,而非 root 用戶,則只能調用 connect,一旦目的端口是開放的,那麼協議棧會回覆 ACK,這樣就會在目標服務器上留下記錄。看來還是 root 用戶比較安全。
-P0: Treat all hosts as online -- skip host discovery
p: probe,0 意味着關閉。
忽略上一條主機發現,對列表中的每一個 IP 地址進行掃描。默認情況下,如果 nmap 需要進行高強度掃描,會先用主機發現確定正在運行的機器,進而只對運行的機器進行高強度掃描。
-PS/PA/PU [portlist]: TCP SYN/ACK or UDP discovery probes to given ports
如果非 root 用戶,對於 SYN Probe 還能使用 connect 完成,但 ACK Probe 就無法做到了。
ACK Probe 存在的意義,在於比 SYN Probe 更有可能繞開防火牆。
-PE/PP/PM: ICMP echo, timestamp, and netmask request discovery probes
ICMP Echo 很有可能被防火牆封鎖,但是 timestamp 和 netmask request 往往就不會了。
2. 端口掃描
這是最常用的功能,也是 nmap 設計的初衷。
-sS/sT/sA/sW/sM: TCP SYN/Connect()/ACK/Window/Maimon scans
--scanflags <flags>: Customize TCP scan flags
這個選項是大殺器啊,可以構造 TCP 標誌位的任意組合。
-p <port ranges>: Only scan specified ports
指定掃描端口。
3. 應用版本及操作系統版本探測
記住 -A 就行了。
4. 時間和性能
-T0-5
數字越高掃描越快,對帶寬的要求也越高。加速掃描是以縮短超時時間爲代價的,因此準確性會降低。
默認爲 T3,未做任何優化。
5. 防火牆/IDS躲避
-f; --mtu <val>: fragment packets (optionally w/given MTU)
可用於發送分片報文。正常情況下使用場景不多,可用於測試。
--data-length <number>
發送報文時,附加指定長度的隨機數據,但無法發送指定的內容。
6. 輸出
-v verbose
輸出詳細信息,類似 tcpdump 的 -v,可最多附加 2 個 v 以輸出更詳細的內容,而 tcpdump 最多可附加 3 個。
四. traceroute
這個命令倒沒什麼好說的,通過 IP 頭中的 TTL 字段遞增的方法,來探測路徑中的各個 Router。
默認的探測協議是 UDP,目的端口大於 30000,因爲基本不會有應用把端口開在這個範圍內。
-I: 使用 ICMP 協議
-T: 使用 TCP 協議
-p: 指定目的端口
-m: 指定 TTL 上限,默認爲 30
-N: 指定探測包的總併發數量
-q: 指定針對一個 route 的探測包的併發數量
manual 宣稱可以利用其來探測路徑 MTU,但實際只會顯示第一條的 MTU。
五. tracepath
該命令同 traceroute 的區別在於,能顯示完整的路徑 MTU 及對稱/非對稱信息。
六. mtr
相比而言,該命令比較小衆,在 Fedora 17 中默認沒有安裝。
其主要作用等同上面的 2 個命令,但又集成了 ping 的部分功能,可以輸出網絡質量狀況,平均值表示網絡質量,方差表示網絡抖動。
-r:report 模式
七. ip
ip addr 替代 ifconfig
ip route 替代 netstat -r
ip -s link 替代 netstat -i
八. iwconfig
性能監控
一. vmstat
虛擬內存統計工具,但能提供包括 CPU、內存、IO、swap 在內的各種監控數據。
Procs
r: The number of runnable processes (running or waiting for run time). 運行或等待運行
b: The number of processes in uninterruptible sleep. block
Memory
swpd: the amount of virtual memory used.
free: the amount of idle memory.
buff: the amount of memory used as buffers.
cache: the amount of memory used as cache.
Swap
si: Amount of memory swapped in from disk (/s).
so: Amount of memory swapped to disk (/s).
IO
bi: Blocks received from a block device (blocks/s).
bo: Blocks sent to a block device (blocks/s).
System
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.
CPU
These are percentages of total CPU time.
us: Time spent running non-kernel code. (user time, including nice time)
sy: Time spent running kernel code. (system time)
id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.
-f: the number of forks since boot
-m: displays slabinfo
-s: displays a table of various event counters and memory statistics
-d: Report disk statistics
-D: Report some summary statistics about disk activity
二. mpstat
mpstat -P ALL interval count
監控 CPU 的運行狀況,與 top 的輸出差別不大,但支持查看 CPU 收到的中斷數。
其 -A 的輸出比較複雜。
三. iostat
iostat interval count
同時輸出 cpu 和磁盤 IO,分析 IO 瓶頸時有用。
-k Display statistics in kilobytes per second.
-m Display statistics in megabytes per second.
tps Indicate the number of transfers per second that were issued to the device. A transfer is an I/O request to the device. Multiple logical requests can be combined into a single I/O request to the device. A transfer is of indeterminate size.
四. ifstat
interface statistics
查看網卡流量,相對於 iptraf 等,還是方便很多的,一是支持同時顯示多張網卡的流量,二是延時低。
-l Enables monitoring of loopback interfaces for which statistics are available. By default, ifstat monitors all non-loopback interfaces that are up.
-a Enables monitoring of all interfaces found for which statistics are available.
-t Adds a timestamp at the beginning of each line.
-S Keep stats updated on the same line if possible (no scrolling nor wrapping).
-b Reports bandwith in kbits/sec instead of kbytes/sec.
五. dstat
號稱是以上 xxstat 的集大成者,但實際上,其特點僅僅在於集成和彙總,例如,同時輸出包括 CPU、IO、內存、網絡的實時狀況,而如果想觀察每一個 CPU、每一塊網卡的情況,就必須在命令行中一一指定,這顯然談不上用戶友好。
-c, --cpu enable cpu stats (system, user, idle, wait, hardware interrupt, software interrupt)
-d, --disk enable disk stats (read, write)
-g, --page enable page stats (page in, page out)
-i, --int enable interrupt stats
-l, --load enable load average stats (1 min, 5 mins, 15mins)
-m, --mem enable memory stats (used, buffers, cache, free)
-n, --net enable network stats (receive, send)
-N eth1,total include eth1 and total
-p, --proc enable process stats (runnable, uninterruptible, new)
-r, --io enable I/O request stats (read, write requests)
-s, --swap enable swap stats (used, free)
-y, --sys enable system stats (interrupts, context switches)
--aio enable aio stats (asynchronous I/O)
--fs enable filesystem stats (open files, inodes)
--ipc enable ipc stats (message queue, semaphores, shared memory)
--lock enable file lock stats (posix, flock, read, write)
--raw enable raw stats (raw sockets)
--socket enable socket stats (total, tcp, udp, raw, ip-fragments)
--tcp enable tcp stats (listen, established, syn, time_wait, close)
--udp enable udp stats (listen, active)
--unix enable unix stats (datagram, stream, listen, active)
--vm enable vm stats (hard pagefaults, soft pagefaults, allocated, free)
六. nstat
七. sar
System Activity Reporter。不同於 xxstat,sar 是一個比較全面的性能分析工具,可以查看 CPU、文件、IO、網卡、內存、IPC 等狀況。
-u: CPU
%user:顯示在用戶級別(application)運行使用 CPU 總時間的百分比。
%nice:顯示在用戶級別,用於nice操作,所佔用 CPU 總時間的百分比。
%system:在覈心級別(kernel)運行所使用 CPU 總時間的百分比。
%iowait:顯示用於等待I/O操作佔用 CPU 總時間的百分比。
%steal:管理程序(hypervisor)爲另一個虛擬進程提供服務而等待虛擬 CPU 的百分比。
%idle:顯示 CPU 空閒時間佔用 CPU 總時間的百分比。
相比於 top,多了 iowait,少了中斷。
八. perf
九. stap
十. pidstat
十一. blktrace
九. strace
十. ltrace
十一. df
df displays the amount of disk space available on the file system containing each file name argument.
df -h:human-readable
df -i:顯示 inode 的使用情況,小文件衆多的場景下,有可能磁盤未滿但 inode 耗光
df --total:顯示總的使用情況
十二. du
du summarize disk usage of each FILE, recursively for directories.
du -s:顯示當前目錄總的空間佔用
du -d --max-depth=:分目錄統計空間佔用