Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7789824
  • 博文数量: 701
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13233
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
个人简介

天行健,君子以自强不息!

文章分类

全部博文(701)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(178)

2014年(129)

2013年(123)

2012年(61)

分类: 网络与安全

2013-11-16 22:09:58

简介:
需要监控Linux服务器系统性能吗?尝试下面这些系统内置或附件的工具吧。
大多数Linux发行版本都装备了大量的监控工具。这些工具提供了能用作取得相关信息和系统活动的量度指标。
你能使用这些工具发现造成性能问题可能原因。

此次讨论到的工具只是分析和调试服务器下面问题时最基本工具中的一部分。
1.找出瓶颈
2.硬盘(存储)瓶颈
3.CPU及内存瓶颈
4.网络瓶颈

一、 top - 进程活动
top提供一个当前运行系统实时动态的视图,也就是正在运行进程。
在默认情况下,显示系统中CPU使用率最高的任务,并每5秒钟刷新一次。
图01.Linux top命令


常用热键
热键             用途
t                  显示摘要信息开关.
m                 显示内存信息开关.
A                  分类显示系统不同资源的使用大户。有助于快速识别系统中资源消耗多的任务。
f                  添加删除所要显示栏位.
o                  调整所要显示栏位的顺序.
r                  调整一个正在运行的进程Nice值.
k                  结束一个正在运行的进程.
z                  彩色/黑白显示开关
相关链接:How do I Find Out Linux CPU Utilization?


二、vmstat -系统活动、硬件及系统信息
使用vmstat命令可以得到关于进程、内存、内存分页、堵塞IO、traps及CPU活动的信息。
$ vmstat 3
输出样例:
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 2540988 522188 5130400    0    0     2    32    4    2  4  1 96  0  0
 1  0      0 2540988 522188 5130400    0    0     0   720 1199  665  1  0 99  0  0
 0  0      0 2540956 522188 5130400    0    0     0     0 1151 1569  4  1 95  0  0
 0  0      0 2540956 522188 5130500    0    0     0     6 1117  439  1  0 99  0  0
 0  0      0 2540940 522188 5130512    0    0     0   536 1189  932  1  0 98  0  0
 0  0      0 2538444 522188 5130588    0    0     0     0 1187 1417  4  1 96  0  0
 0  0      0 2490060 522188 5130640    0    0     0    18 1253 1123  5  1 94  0  0


显示内存使用详细信息
# vmstat -m


显示内存活动/不活动的信息
# vmstat -a


相关链接:How do I find out Linux Resource utilization to detect system bottlenecks?



三、 w - 显示谁已登录,他们正在做什么?
w命令显示系统当前用户及其运行进程的信息。
# w username
# w vivek
输出样例:
17:58:47 up 5 days, 20:28,  2 users,  load average: 0.36, 0.26, 0.24
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    10.1.3.145       14:55    5.00s  0.04s  0.02s vim /etc/resolv.conf
root     pts/1    10.1.3.145       17:43    0.00s  0.03s  0.00s w

四、uptime - 告诉系统已经运行了多久?
uptime命令过去只显示系统运行多久。
现在,可以显示系统运行多久、当前有多少的用户登录、在过去的1,5,15分钟里平均负载时多少。
# uptime
输入样例:
18:02:41 up 41 days, 23:42,  1 user,  load average: 0.00, 0.00, 0.00
1可以被认为是最优的负载值。负载是会随着系统不同改变得。
单CPU系统1-3和SMP系统6-10都是可能接受的。

五、ps - 显示进程
ps命令显示当前运行进程的快照。使用-A或-e显示所有进程。
# ps -A
输出样例:
PID TTY          TIME CMD
    1 ?        00:00:02 init
    2 ?        00:00:02 migration/0
    3 ?        00:00:01 ksoftirqd/0
    4 ?        00:00:00 watchdog/0
    5 ?        00:00:00 migration/1
    6 ?        00:00:15 ksoftirqd/1
....
.....
 4881 ?        00:53:28 java
 4885 tty1     00:00:00 mingetty
 4886 tty2     00:00:00 mingetty
 4887 tty3     00:00:00 mingetty
 4888 tty4     00:00:00 mingetty
 4891 tty5     00:00:00 mingetty
 4892 tty6     00:00:00 mingetty
 4893 ttyS1    00:00:00 agetty
12853 ?        00:00:00 cifsoplockd
12854 ?        00:00:00 cifsdnotifyd
14231 ?        00:10:34 lighttpd
14232 ?        00:00:00 php-cgi
54981 pts/0    00:00:00 vim
55465 ?        00:00:00 php-cgi
55546 ?        00:00:00 bind9-snmp-stat
55704 pts/1    00:00:00 ps


ps与top非常相似,但ps提供更多的信息。
输出长格式
# ps -Al


输出附加全格式(显示进程在执行时传入的参数)
# ps -AlF


显示进程结构
# ps -AlFH


在进程后显示线程
# ps -AlLm


打印服务器上所有进程
# ps ax
# ps axu


打印进程树
# ps -ejH
# ps axjf
# pstree 


打印安全信息
# ps -eo euser,ruser,suser,fuser,f,comm,label
# ps axZ
# ps -eM


查看使用Vivek用户名运行的进程
# ps -U vivek -u vivek u


设置自定义输出格式
# ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm
# ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
# ps -eopid,tt,user,fname,tmout,f,wchan


只显示Lighttpd的进程ID
# ps -C lighttpd -o pid= 
或者
# pgrep lighttpd 
或者
# pgrep -u vivek php-cgi


显示PID为55977的进程名称
# ps -p 55977 -o comm= 


找出消耗内存最多的前10名进程
# ps -auxf | sort -nr -k 4 | head -10 


找出使用CPU最多的前10名进程
# ps -auxf | sort -nr -k 3 | head -10


六、free - 内存使用情况
free命令显示系统中空闲的、已用的物理内存及swap内存,及被内核使用的buffer。
# free 
输出样例:
total       used       free     shared    buffers     cached
Mem:      12302896    9739664    2563232          0     523124    5154740
-/+ buffers/cache:    4061800    8241096
Swap:      1052248          0    1052248


相关链接:
1. Linux Find Out Virtual Memory PAGESIZE



2. Linux Limit CPU Usage Per Process



3. How much RAM does my Ubuntu / Fedora Linux desktop PC have?



七、iostat - CPU平均负载,硬盘活动
iostat命令可报告中央处理器(CPU)的统计信息,各种设备、分区及网络文件系统输入/输出的统计信息。
# iostat 
输出样例:
Linux 2.6.18-128.1.14.el5 (www03.nixcraft.in)  06/26/2009


avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           3.50    0.09    0.51    0.03    0.00   95.86


Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda              22.04        31.88       512.03   16193351  260102868
sda1              0.00         0.00         0.00       2166        180
sda2             22.04        31.87       512.03   16189010  260102688
sda3              0.00         0.00         0.00       1615          0
相关链接:Linux Track NFS Directory / Disk I/O Stats


八、sar - 搜集和报告系统活动
sar命令用来搜集、报告和储存系统活动信息。查看网路计数器,输入:
# sar -n DEV | more

显示最近24小时网络计数器
# sar -n DEV -f /var/log/sa/sa24 | more


你亦可以用sar显示实时情况
# sar 4 5


输出样例:
Linux 2.6.18-128.1.14.el5 (www03.nixcraft.in)   06/26/2009


06:45:12 PM       CPU     %user     %nice   %system   %iowait    %steal     %idle
06:45:16 PM       all      2.00      0.00      0.22      0.00      0.00     97.78
06:45:20 PM       all      2.07      0.00      0.38      0.03      0.00     97.52
06:45:24 PM       all      0.94      0.00      0.28      0.00      0.00     98.78
06:45:28 PM       all      1.56      0.00      0.22      0.00      0.00     98.22
06:45:32 PM       all      3.53      0.00      0.25      0.03      0.00     96.19
Average:          all      2.02      0.00      0.27      0.01      0.00     97.70

相关链接:How to collect Linux system utilization data into a file



九、mpstat - 多处理器使用率
mpstat命令可以显示所有可用处理器的使用情况,处理器编号从0开始。
mpstat -P ALL显示每个处理器的平均使用率。
# mpstat -P ALL

Understanding mpstat output
The report generated by the mpstat command has the following format:
CPU:      Processor number. 
          The keyword all indicates that statistics are calculated as averages among all processors.
%user:    Show the percentage of CPU utilization 
          that occurred while executing at the user level (application).
%nice:    Show the percentage of CPU utilization 
          that occurred while executing at the user level with nice priority.
%system:  Show the percentage of CPU utilization 
          that occurred while executing at the system level (kernel). 
          Note that this does not include the time spent servicing interrupts or softirqs.
%iowait:  Show the percentage of time 
          that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
%irq:     Show the percentage of time spent by the CPU or CPUs to service interrupts.
%soft:    Show the percentage of time spent by the CPU or CPUs to service softirqs. 
          A softirq (software interrupt) is one of up to 32 enumerated software interrupts
          which can run on multiple CPUs at once.
%idle:    Show the percentage of time 
          that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.
intr/s:   Show the total number of interrupts received per second by the CPU or CPUs.


输出样例:
Linux 2.6.18-128.1.14.el5 (www03.nixcraft.in)   06/26/2009


06:48:11 PM CPU   %user   %nice    %sys %iowait    %irq   %soft %steal   %idle    intr/s
06:48:11 PM all    3.50    0.09    0.34    0.03    0.01    0.17    0.00   95.86   1218.04
06:48:11 PM    0    3.44    0.08    0.31    0.02    0.00    0.12    0.00   96.04   1000.31
06:48:11 PM    1    3.10    0.08    0.32    0.09    0.02    0.11    0.00   96.28     34.93
06:48:11 PM    2    4.16    0.11    0.36    0.02    0.00    0.11    0.00   95.25      0.00
06:48:11 PM    3    3.77    0.11    0.38    0.03    0.01    0.24    0.00   95.46     44.80
06:48:11 PM    4    2.96    0.07    0.29    0.04    0.02    0.10    0.00   96.52     25.91
06:48:11 PM    5    3.26    0.08    0.28    0.03    0.01    0.10    0.00   96.23     14.98
06:48:11 PM    6    4.00    0.10    0.34    0.01    0.00    0.13    0.00   95.42      3.75
06:48:11 PM    7    3.30    0.11    0.39    0.03    0.01    0.46    0.00   95.69     76.89


相关链接:Linux display each multiple SMP CPU processors utilization individually.



十、pmap - 进程的内存使用
pmap命令可以显示进程的内存映射,使用这个命令可以找出造成内存瓶颈的原因。
# pmap -d PID

显示PID为47394进程的内存信息。
# pmap -d 47394

输出样例:
47394:   /usr/bin/php-cgi
Address           Kbytes Mode Offset           Device    Mapping
0000000000400000    2584 r-x-- 0000000000000000 008:00002 php-cgi
0000000000886000     140 rw--- 0000000000286000 008:00002 php-cgi
00000000008a9000      52 rw--- 00000000008a9000 000:00000   [ anon ]
0000000000aa8000      76 rw--- 00000000002a8000 008:00002 php-cgi
000000000f678000    1980 rw--- 000000000f678000 000:00000   [ anon ]
000000314a600000     112 r-x-- 0000000000000000 008:00002 ld-2.5.so
000000314a81b000       4 r---- 000000000001b000 008:00002 ld-2.5.so
000000314a81c000       4 rw--- 000000000001c000 008:00002 ld-2.5.so
000000314aa00000    1328 r-x-- 0000000000000000 008:00002 libc-2.5.so
000000314ab4c000    2048 ----- 000000000014c000 008:00002 libc-2.5.so
.....
......
..
00002af8d48fd000       4 rw--- 0000000000006000 008:00002 xsl.so
00002af8d490c000      40 r-x-- 0000000000000000 008:00002 libnss_files-2.5.so
00002af8d4916000    2044 ----- 000000000000a000 008:00002 libnss_files-2.5.so
00002af8d4b15000       4 r---- 0000000000009000 008:00002 libnss_files-2.5.so
00002af8d4b16000       4 rw--- 000000000000a000 008:00002 libnss_files-2.5.so
00002af8d4b17000 768000 rw-s- 0000000000000000 000:00009 zero (deleted)
00007fffc95fe000      84 rw--- 00007ffffffea000 000:00000   [ stack ]
ffffffffff600000    8192 ----- 0000000000000000 000:00000   [ anon ]
mapped: 933712K    writeable/private: 4304K    shared: 768000K


最后一行非常重要:
* mapped: 933712K 内存映射所占空间大小
* writeable/private: 4304K 私有地址空间大小
* shared: 768000K 共享地址空间大小


相关链接:Linux find the memory used by a program / process using pmap command


阅读(1870) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~