1.valgrind
Valgrind能做什么?
-检测竞争
Valgrind还能做什么?
–Cachegrind+ cg_annotate
它模拟 CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中 cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数,这些可以看成程序的执行成本。以上数据其实对于app开发意义不大,仅作为参考。
使用方法:
–Callgrind+ callgrind_annotate/kcachegrind
比起Cachegrind而言Callgrind更有用。Callgrind在Cachegrind基础上实现,可以可视化展示函数调用关系,以及每个函数在整个进程运行过程中所占的成本。对于
Valgrind—— memcheck
用法:valgrind --tool=memcheck --leak-check=yes ./grog
Memcheck 工具主要检查下面的程序错误:
-
使用未初始化的内存 (Use of uninitialised memory)
-
使用已经释放了的内存 (Reading/writing memory after it has been free’d)
-
使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
-
对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
-
申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
-
malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
-
src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)
Callgrind收集程序运行时的一些数据,函数调用关系等信息,还可以有选择地进行cache 模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。
它模拟 CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中 cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助。
它主要用来检查多线程程序中出现的竞争问题。Helgrind 寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为” Eraser” 的竞争检测算法,并做了进一步改进,减少了报告错误的次数。
堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。
1、 到www.valgrind.org下载最新版valgrind-3.8.1.tar.bz2
2、 解压安装包:tar –jxvf valgrind-3.8.1.tar.bz2
3、 解压后生成目录valgrind-3.8.1
4、 cd valgrind-3.8.1
5、 ./configure
6、 Make;make installValgrind
使用用法: valgrind [options] prog-and-args [options]: 常用选项,适用于所有Valgrind工具
-
-tool=<name> 最常用的选项。运行 valgrind中名为toolname的工具。默认memcheck。
-
h –help 显示帮助信息。
-
-version 显示valgrind内核的版本,每个工具都有各自的版本。
-
q –quiet 安静地运行,只打印错误信息。
-
v –verbose 更详细的信息, 增加错误数统计。
-
-trace-children=no|yes 跟踪子线程? [no]
-
-track-fds=no|yes 跟踪打开的文件描述?[no]
-
-time-stamp=no|yes 增加时间戳到LOG信息? [no]
-
-log-fd=<number> 输出LOG到描述符文件 [2=stderr]
-
-log-file=<file> 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID
-
-log-file-exactly=<file> 输出LOG信息到 file
-
-log-file-qualifier=<VAR> 取得环境变量的值来做为输出信息的文件名。 [none]
-
-log-socket=ipaddr:port 输出LOG到socket ,ipaddr:port
LOG信息输出
-
-xml=yes 将信息以xml格式输出,只有memcheck可用
-
-num-callers=<number> show <number> callers in stack traces [12]
-
-error-limit=no|yes 如果太多错误,则停止显示新错误? [yes]
-
-error-exitcode=<number> 如果发现错误则返回错误代码 [0=disable]
-
-db-attach=no|yes 当出现错误,valgrind会自动启动调试器gdb。[no]
-
-db-command=<command> 启动调试器的命令行选项[gdb -nw %f %p]
适用于Memcheck工具的相关选项:
-
-leak-check=no|summary|full 要求对leak给出详细信息? [summary]
-
-leak-resolution=low|med|high how much bt merging in leak check [low]
-
-show-reachable=no|yes show reachable blocks in leak check? [no]
下面是一段有问题的C程序代码test.c
#include <stdlib.h>
void f(void)
{
int* x = malloc(10 * sizeof(int));
x[10] = 0; //问题1: 数组下标越界
} //问题2: 内存没有释放int main(void)
{
f();
return 0;
}
1、 编译程序test.c
gcc -Wall test.c -g -o test
2、 使用Valgrind检查程序BUG
valgrind --tool=memcheck --leak-check=full ./test
3、 分析输出的调试信息
==3908== Memcheck, a memory error detector.
==3908== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==3908== Using LibVEX rev 1732, a library for dynamic binary translation.
==3908== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==3908== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==3908== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==3908== For more details, rerun with: -v
==3908==
--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
/*数组越界错误*/
==3908== Invalid write of size 4
==3908== at 0x8048384: f (test.c:6)
==3908== by 0x80483AC: main (test.c:11)
==3908== Address 0x400C050 is 0 bytes after a block of size 40 alloc'd
==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
==3908== by 0x8048377: f (test.c:5)
==3908== by 0x80483AC: main (test.c:11)
==3908==
==3908== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 1)
==3908== malloc/free: in use at exit: 40 bytes in 1 blocks.
==3908== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==3908== For counts of detected errors, rerun with: -v
==3908== searching for pointers to 1 not-freed blocks.
==3908== checked 59,124 bytes.
==3908==
==3908==
/*有内存空间没有释放*/
==3908== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
==3908== by 0x8048377: f (test.c:5)
==3908== by 0x80483AC: main (test.c:11)
==3908==
==3908== LEAK SUMMARY:
==3908== definitely lost: 40 bytes in 1 blocks.
==3908== possibly lost: 0 bytes in 0 blocks.
==3908== still reachable: 0 bytes in 0 blocks.
==3908== suppressed: 0 bytes in 0 blocks.
2.iostat
iostat提供了从设备角度查看输出CPU和磁盘I/O相关的统计信息.
命令格式:
1.iostat的简单实用
iostat可以显示CPU和I/O系统的负载情况及分区状态信息.
直接执行iostat可以显示下面内容:
# iostat
Linux 2.6.9-8.11.EVAL (ts3-150.ts.cn.tlan) 08/08/2007
avg-cpu: %user %nice %sys %iowait %idle
12.01 0.00 2.15 2.30 83.54
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
hda 7.13 200.12 34.73 640119 111076
各个输出项目的含义如下:
avg-cpu段:
%user: 在用户级别运行所使用的CPU的百分比.
%nice: nice操作所使用的CPU的百分比.
%sys: 在系统级别(kernel)运行所使用CPU的百分比.
%iowait: CPU等待硬件I/O时,所占用CPU百分比.
%idle: CPU空闲时间的百分比.
Device段:
tps: 每秒钟发送到的I/O请求数.
Blk_read/s: 每秒读取的block数.
Blk_wrtn/s: 每秒写入的block数.
Blk_read: 读入的block总数.
Blk_wrtn: 写入的block总数.
2.iostat的参数说明
iostat各个参数说明:
-c 仅显示CPU统计信息.与-d选项互斥。
-d 仅显示磁盘统计信息.与-c选项互斥。
-k 以K为单位显示每秒的磁盘请求数,默认单位块。
-p device | ALL 与-x选项互斥,用于显示块设备及系统分区的统计信息.也可以在-p后指定一个设备名,如:
# iostat -p hda
或显示所有设备
# iostat -p ALL
-t 在输出数据时,打印搜集数据的时间。
-V 打印版本号和帮助信息。
-x 输出扩展信息.
3.iostat的输出项说明
读入块的当总数.
Blk_wrtn
写入块的总数.
kB_read/s
每秒从驱动器读入的数据量,单位为K.
kB_wrtn/s
每秒向驱动器写入的数据量,单位为K.
kB_read
读入的数据总量,单位为K.
kB_wrtn
写入的数据总量,单位为K.
rrqm/s
将读入请求合并后,每秒发送到设备的读入请求数.
wrqm/s
将写入请求合并后,每秒发送到设备的写入请求数.
r/s
每秒发送到设备的读入请求数.
w/s
每秒发送到设备的写入请求数.
rsec/s
每秒从设备读入的扇区数.
wsec/s
每秒向设备写入的扇区数.
rkB/s
每秒从设备读入的数据量,单位为K.
wkB/s
每秒向设备写入的数据量,单位为K.
avgrq-sz
发送到设备的请求的平均大小,单位是扇区.
avgqu-sz
发送到设备的请求的平均队列长度.
await
I/O请求平均执行时间.包括发送请求和执行的时间.单位是毫秒.
svctm
发送到设备的I/O请求的平均执行时间.单位是毫秒.
%util
在I/O请求发送到设备期间,占用CPU时间的百分比.用于显示设备的带宽利用率.
当这个值接近100%时,表示设备带宽已经占满.
4.iostat示例
显示一条统计记录,包括所有的CPU和设备.
#iostat -d 2
每隔2秒,显示一次设备统计信息.
#iostat -d 2 6
每隔2秒,显示一次设备统计信息.总共输出6次.
#iostat -x hda hdb 2 6
每隔2秒显示一次hda,hdb两个设备的扩展统计信息,共输出6次.
#iostat -p sda 2 6
每隔2秒显示一次sda及上面所有分区的统计信息,共输出6次.
3.pidstat
简介
pidstat主要用于监控全部或指定进程占用系统资源的情况,如CPU,内存、设备IO、任务切换、线程等。pidstat首次运行时显示自系统启动开始的各项统计信息,之后运行pidstat将显示自上次运行该命令以后的统计信息。用户可以通过指定统计的次数和时间来获得所需的统计信息。
实例讲解
默认参数
执行pidstat,将输出系统启动后所有活动进程的cpu统计信息:
linux:~ # pidstat Linux 2.6.32.12-0.7-default (linux) 06/18/12 _x86_64_ 11:37:19 PID %usr %system %guest %CPU CPU Command …… 11:37:19 11452 0.00 0.00 0.00 0.00 2 bash 11:37:19 11509 0.00 0.00 0.00 0.00 3 dd
指定采样周期和采样次数
pidstat命令指定采样周期和采样次数,命令形式为”pidstat [option] interval [count]”,以下pidstat输出以2秒为采样周期,输出10次cpu使用统计信息:
pidstat 2 10
cpu使用情况统计(-u)
使用-u选项,pidstat将显示各活动进程的cpu使用统计,执行”pidstat -u”与单独执行”pidstat”的效果一样。
内存使用情况统计(-r)
使用-r选项,pidstat将显示各活动进程的内存使用统计:
linux:~ # pidstat -r -p 13084 1 Linux 2.6.32.12-0.7-default (linux) 06/18/12 _x86_64_ 15:08:18 PID minflt/s majflt/s VSZ RSS %MEM Command 15:08:19 13084 133835.00 0.00 15720284 15716896 96.26 mmmm 15:08:20 13084 35807.00 0.00 15863504 15849756 97.07 mmmm 15:08:21 13084 19273.87 0.00 15949040 15792944 96.72 mmmm
以上各列输出的含义如下:
minflt/s: 每秒次缺页错误次数(minor page faults),次缺页错误次数意即虚拟内存地址映射成物理内存地址产生的page fault次数 majflt/s: 每秒主缺页错误次数(major page faults),当虚拟内存地址映射成物理内存地址时,相应的page在swap中,这样的page fault为major page fault,一般在内存使用紧张时产生 VSZ: 该进程使用的虚拟内存(以kB为单位) RSS: 该进程使用的物理内存(以kB为单位) %MEM: 该进程使用内存的百分比 Command: 拉起进程对应的命令
IO情况统计(-d)
使用-d选项,我们可以查看进程IO的统计信息:
linux:~ # pidstat -d 1 2 Linux 2.6.32.12-0.7-default (linux) 06/18/12 _x86_64_ 17:11:36 PID kB_rd/s kB_wr/s kB_ccwr/s Command 17:11:37 14579 124988.24 0.00 0.00 dd 17:11:37 PID kB_rd/s kB_wr/s kB_ccwr/s Command 17:11:38 14579 105441.58 0.00 0.00 dd
输出信息含义
kB_rd/s: 每秒进程从磁盘读取的数据量(以kB为单位) kB_wr/s: 每秒进程向磁盘写的数据量(以kB为单位) Command: 拉起进程对应的命令
针对特定进程统计(-p)
使用-p选项,我们可以查看特定进程的系统资源使用情况:
linux:~ # pidstat -r -p 1 1 Linux 2.6.32.12-0.7-default (linux) 06/18/12 _x86_64_ 18:26:17 PID minflt/s majflt/s VSZ RSS %MEM Command 18:26:18 1 0.00 0.00 10380 640 0.00 init 18:26:19 1 0.00 0.00 10380 640 0.00 init ……
pidstat常用命令
使用pidstat进行问题定位时,以下命令常被用到:
pidstat -u 1 pidstat -r 1 pidstat -d 1
以上命令以1秒为信息采集周期,分别获取cpu、内存和磁盘IO的统计信息。
4.netstat
简介
Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。
输出信息含义
执行netstat后,其输出结果为
Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 2 210.34.6.89:telnet 210.34.6.96:2873 ESTABLISHED tcp 296 0 210.34.6.89:1165 210.34.6.84:netbios-ssn ESTABLISHED tcp 0 0 localhost.localdom:9001 localhost.localdom:1162 ESTABLISHED tcp 0 0 localhost.localdom:1162 localhost.localdom:9001 ESTABLISHED tcp 0 80 210.34.6.89:1161 210.34.6.10:netbios-ssn CLOSE Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node Path unix 1 [ ] STREAM CONNECTED 16178 @000000dd unix 1 [ ] STREAM CONNECTED 16176 @000000dc unix 9 [ ] DGRAM 5292 /dev/log unix 1 [ ] STREAM CONNECTED 16182 @000000df
从整体上看,netstat的输出结果可以分为两个部分:
一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指%0A的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。
另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。
常见参数
-a (all)显示所有选项,默认不显示LISTEN相关
-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化成数字。
-l 仅列出有在 Listen (监听) 的服務状态
-p 显示建立相关链接的程序名
-r 显示路由信息,路由表
-e 显示扩展信息,例如uid等
-s 按各个协议进行统计
-c 每隔一个固定时间,执行该netstat命令。
提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到
实用命令实例
1. 列出所有端口 (包括监听和未监听的)
列出所有端口 netstat -a
# netstat -a | more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN udp 0 0 *:bootpc *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6135 /tmp/.X11-unix/X0 unix 2 [ ACC ] STREAM LISTENING 5140 /var/run/acpid.socket
列出所有 tcp 端口 netstat -at
# netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN
列出所有 udp 端口 netstat -au
# netstat -au Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:bootpc *:* udp 0 0 *:49119 *:* udp 0 0 *:mdns *:*
2. 列出所有处于监听状态的 Sockets
只显示监听端口 netstat -l
# netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:ipp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN udp 0 0 *:49119 *:*
只列出所有监听 tcp 端口 netstat -lt
# netstat -lt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN
只列出所有监听 udp 端口 netstat -lu
# netstat -lu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:49119 *:* udp 0 0 *:mdns *:*
只列出所有监听 UNIX 端口 netstat -lx
# netstat -lx Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6294 private/maildrop unix 2 [ ACC ] STREAM LISTENING 6203 public/cleanup unix 2 [ ACC ] STREAM LISTENING 6302 private/ifmail unix 2 [ ACC ] STREAM LISTENING 6306 private/bsmtp
3. 显示每个协议的统计信息
显示所有端口的统计信息 netstat -s
# netstat -s Ip: 11150 total packets received 1 with invalid addresses 0 forwarded 0 incoming packets discarded 11149 incoming packets delivered 11635 requests sent out Icmp: 0 ICMP messages received 0 input ICMP message failed. Tcp: 582 active connections openings 2 failed connection attempts 25 connection resets received Udp: 1183 packets received 4 packets to unknown port received. .....
显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su
# netstat -st # netstat -su
4. 在 netstat 输出中显示 PID 和进程名称 netstat -p
netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。
# netstat -pt Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 1 0 ramesh-laptop.loc:47212 192.168.185.75:www CLOSE_WAIT 2109/firefox tcp 0 0 ramesh-laptop.loc:52750 lax:www ESTABLISHED 2109/firefox
5. 在 netstat 输出中不显示主机,端口和用户名 (host, port or user)
当你不想让主机,端口和用户名显示,使用 netstat -n。将会使用数字代替那些名称。
同样可以加速输出,因为不用进行比对查询。
# netstat -an
如果只是不想让这三个名称中的一个被显示,使用以下命令
# netsat -a --numeric-ports # netsat -a --numeric-hosts # netsat -a --numeric-users
6. 持续输出 netstat 信息
netstat 将每隔一秒输出网络信息。
# netstat -c Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED tcp 1 1 ramesh-laptop.loc:52564 101.11.169.230:www CLOSING tcp 0 0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED tcp 1 1 ramesh-laptop.loc:42367 101.101.34.101:www CLOSING ^C
7. 显示系统不支持的地址族 (Address Families)
netstat --verbose
在输出的末尾,会有如下的信息
netstat: no support for `AF IPX' on this system. netstat: no support for `AF AX25' on this system. netstat: no support for `AF X25' on this system. netstat: no support for `AF NETROM' on this system.
8. 显示核心路由信息 netstat -r
# netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.1.0 * 255.255.255.0 U 0 0 0 eth2 link-local * 255.255.0.0 U 0 0 0 eth2 default 192.168.1.1 0.0.0.0 UG 0 0 0 eth2
注意: 使用 netstat -rn 显示数字格式,不查询主机名称。
9. 找出程序运行的端口
并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。
# netstat -ap | grep ssh tcp 1 0 dev-db:ssh 101.174.100.22:39213 CLOSE_WAIT - tcp 1 0 dev-db:ssh 101.174.100.22:57643 CLOSE_WAIT -
找出运行在指定端口的进程
# netstat -an | grep ':80'
10. 显示网络接口列表
# netstat -i Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 0 0 0 0 0 0 0 0 BMU eth2 1500 0 26196 0 0 0 26883 6 0 0 BMRU lo 16436 0 4 0 0 0 4 0 0 0 LRU
显示详细信息,像是 ifconfig 使用 netstat -ie:
# netstat -ie Kernel Interface table eth0 Link encap:Ethernet HWaddr 00:10:40:11:11:11 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) Memory:f6ae0000-f6b00000
11. IP和TCP分析
查看连接某服务端口最多的的IP地址
wss8848@ubuntu:~$ netstat -nat | grep "192.168.1.15:22" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20 18 221.136.168.36 3 154.74.45.242 2 78.173.31.236 2 62.183.207.98 2 192.168.1.14 2 182.48.111.215 2 124.193.219.34 2 119.145.41.2 2 114.255.41.30 1 75.102.11.99
TCP各种状态列表
wss8848@ubuntu:~$ netstat -nat |awk '{print $6}' established) Foreign LISTEN TIME_WAIT ESTABLISHED TIME_WAIT SYN_SENT
先把状态全都取出来,然后使用uniq -c统计,之后再进行排序。
wss8848@ubuntu:~$ netstat -nat |awk '{print $6}'|sort|uniq -c 143 ESTABLISHED 1 FIN_WAIT1 1 Foreign 1 LAST_ACK 36 LISTEN 6 SYN_SENT 113 TIME_WAIT 1 established)
最后的命令如下:
netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
分析access.log获得访问前10位的ip地址
awk '{print $1}' access.log |sort|uniq -c|sort -nr|head -10