下例为查看本机监听TCP端口的进程列表的命令输出
[root@MIO-2 ~]# netstat -plnt 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:80 0.0.0.0:* LISTEN 1320/nginx tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 26922/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1291/master tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1303/php-fpm tcp 0 0 :::22 :::* LISTEN 26922/sshd tcp 0 0 ::1:25 :::* LISTEN 1291/master
netstat查看到的state是当前统计和排障中最被关心的数据,通常我们使用如下shell命令来计算当前服务器的80端口网络连接数
[root@MIO-2 ~]# netstat -tan | awk '$4~/:80$/{++state[$NF]} END {for(key in state) print key,"\t",state[key]}' LISTEN 1 [root@MIO-2 ~]#
不过netstat命令速度比较慢,如果服务器是单一业务,则可以忽略其他端口的连接数,直接以服务器的全部连接数来进行计算
[root@MIO-2 ~]# netstat -st | grep conn 230 active connections openings 9 passive connection openings 0 failed connection attempts 5 connection resets received 1 connections established 5 connections reset due to unexpected data 3 connections reset due to early user close 4 connections aborted due to timeout