查看系统负载命令
[root@daixuan ~]# w
06:52:04 up 20:56,  2 users,  load average: 0.00, 0.00, 0.00                        1分钟,5分钟,15分钟之内系统的平均负载
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT             jcpu进行和子进程占用的时间0.26s
root     tty1     -                Sat23    1:33m  0.26s  0.26s -bash
root     pts/0    192.168.101.17   06:12    0.00s  0.13s  0.01s w


[root@daixuan ~]# uptime
07:04:24 up 21:08,  2 users,  load average: 0.00, 0.00, 0.00                       与 w的第一样一样,获取负载值
[root@daixuan ~]# cat /proc/cpuinfo                                                           0代表单核,1二核,.......processor 7代表第8核
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 58
model name      : Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz
stepping        : 9
microcode       : 23
cpu MHz         : 2594.229
cache size      : 3072 KB


vmstat详解
r run表示有多少个任务在跑,一秒钟之内平均有多少进程在使用CPU,排队或者计算
b代表被I/O,磁盘,网络阻塞的任务的个数
memory内存,swpd多少数据量被拿到交换分区,值大并且不断变化,则内存溢出或者有问题
buff  剩余量
cache 剩余量
内存就是参照物,不管是si还是bi都是到内存里去的,si 就是交换分区里出来到内存里的,bi就是硬盘出来到内存里的,也就是读磁盘。so bo 同理
r :表示运行和等待cpu时间片的进程数,如果长期大于服务器cpu的个数,则说明cpu不够用了;
b :表示等待资源的进程数,比如等待I/O, 内存等,这列的值如果长时间大于1,则需要关注一下了
si :由交换区swap进入内存的数量;
so :由内存进入交换区swap的数量;
bi :从块设备读取数据的量(读磁盘);长期超过1000,有问题了
bo: 从块设备写入数据的量(写磁盘);
wa :表示I/O等待所占用cpu时间百分比
interput 1秒钟中断的次数
us 用户进程占CPU的百分比
sy内核进程占用百分比
idle 空闲的





[root@daixuan ~]# vmstat 1               1秒钟显示一次,一直进行下去
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 1733408  76268  65036    0    0     1     1    6    5  0  0 100  0  0
0  0      0 1733400  76268  65060    0    0     0     0   24   19  0  0 100  0  0
0  0      0 1733400  76268  65060    0    0     0     0   16   16  0  0 100  0  0
0  0      0 1733400  76268  65060    0    0     0     0   13   14  0  0 100  0  0
0  0      0 1733400  76268  65060    0    0     0     0   15   16  0  0 100  0  0
0  0      0 1733400  76268  65060    0    0     0     0   15   17  0  0 100  0  0
0  0      0 1733276  76276  65056    0    0     0    12   19   18  0  0 100  0  0
0  0      0 1733276  76284  65052    0    0     0    40   33   26  0  0 99  0  0
^C
[root@daixuan ~]# vmstat 1 5           1秒钟显示一次,总共显示5次
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 1733284  76284  65060    0    0     1     1    6    5  0  0 100  0  0
0  0      0 1733276  76284  65060    0    0     0     0   20   16  0  0 100  0  0
0  0      0 1733276  76292  65056    0    0     0    40   30   21  0  0 100  0  0
0  0      0 1733276  76292  65060    0    0     0     0   18   16  0  0 100  0  0
0  0      0 1733276  76292  65060    0    0     0     0   16   17  0  0 100  0  0


top 可以查看很多信息,负载、内存、cpu使用率、进程信息等和w的区别很大

[root@daixuan ~]# top -d 1                                           1秒刷新一次
[root@daixuan ~]# top              默认3秒一次  shift+m=M 内存占用最大的排在最上面,P切换回按CPU使用率排序,T按时间排序,q键退出
top - 07:07:54 up 21:12,  2 users,  load average: 0.00, 0.00, 0.00
Tasks: 108 total,   1 running, 107 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.2%sy, 0.0%ni, 99.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1938632k total,   205604k used,  1733028k free,    76148k buffers
Swap:  4194300k total,        0k used,  4194300k free,    65024k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
4570 root      20   0  2692 1128  884 R  1.0  0.1   0:00.06 top
    1 root      20   0  2900 1420 1208 S  0.0  0.1   0:04.63 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.05 kthreadd

PR   0 到39            PR越小,优先级越高
NI   -20到19          
VIRT 虚拟内存
RES  物理内存
SHR 共享内存
s 进程状态
CPU使用百分比
内存使用百分比
time时间
PID是进程的ID号,
[root@daixuan ~]# ls /proc/                                                                    1,10.11.1148都是PID
1     1642  1997  30    453   910          ioports       partitions
10    1652  2     31    4572  94           irq           sched_debug
11    1685  20    32    4593  95           kallsyms      schedstat
1148  1686  21    33    46    acpi         kcore         scsi
[root@daixuan ~]# top -bn1                                                                  一次性显示出来,不是动态显示
top - 08:27:00 up 22:31,  3 users,  load average: 0.00, 0.00, 0.00
Tasks: 112 total,   1 running, 111 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1938632k total,   208612k used,  1730020k free,    77184k buffers
Swap:  4194300k total,        0k used,  4194300k free,    65084k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
4613 root      20   0  2716 1012  776 R  2.0  0.1   0:00.01 top
    1 root      20   0  2900 1420 1208 S  0.0  0.1   0:04.63 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.05 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.06 migration/0
    4 root      20   0     0    0    0 S  0.0  0.0   0:00.15 ksoftirqd/0
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/0
top哪个选项可以展示最后一列为更加详细的进程?
[root@daixuan ~]# top -c
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
4620 root      20   0  2692 1132  888 R  0.3  0.1   0:00.06 top -c
    1 root      20   0  2900 1420 1208 S  0.0  0.1   0:04.63 /sbin/init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.05 [kthreadd]
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.06 [migration/0]
    4 root      20   0     0    0    0 S  0.0  0.0   0:00.15 [ksoftirqd/0]


sar命令

[root@daixuan ~]# yum install -y sysstat                      安装sysstat包,安装sar命令
[root@daixuan ~]# sar -n DEV                                      查看网卡的流量,10分钟记录一次
Linux 2.6.32-573.el6.i686 (daixuan)     2015年11月17日  _i686_  (2 CPU)
00时00分01秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp                                                      /s  rxmcst/s
00时10分01秒        lo      0.00      0.00      0.00      0.00      0.00      0.                                                      00      0.00
00时10分01秒      eth0      1.58      0.01      0.13      0.00      0.00      0.                                                      00      0.00
00时20分01秒        lo      0.00      0.00      0.00      0.00      0.00      0.                                                      00      0.00
[root@daixuan ~]# sar -n DEV 1 2                                1s钟显示一次,共显示2次
Linux 2.6.32-573.el6.i686 (daixuan)     2015年11月17日  _i686_  (2 CPU)
13时46分46秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
13时46分47秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
13时46分47秒      eth0      3.00      0.00      0.27      0.00      0.00      0.00      0.00

13时46分47秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
13时46分48秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
13时46分48秒      eth0      1.00      1.00      0.06      0.41      0.00      0.00      0.00

平均时间:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均时间:        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均时间:      eth0      2.00      0.50      0.17      0.20      0.00      0.00      0.00

1Byte=8bits  
家庭使用的10M带宽,是使用bite来形容的指的是10Mb/s,下载速度是使MB/s来形容的,8倍的关系,1MB/s=8Mb/s,(10Mb/s)/8=1.25MB/s字节每秒rxkB/s  B/s= Byte/s=字节每秒=8bit/s,进网口流量
txkB/s  B/s= Byte/s=字节每秒=8bit/s,出网口流量
byte(字节)是用来衡量磁盘文件大小
1kbyt=1024byte
1Mbyte=1024kbyte

1Gbyte=1024Mbyte

例:如果带宽是10M,目前的txbyt/s=778321.00,请计算带宽占满了吗?
1byt=8b,10M带宽指10Mb/s
778321byt/s=778kbyt/s=0.778Mbyt/s
0.778Mbyt/s<1.25MB/s
0.778*8(Mb/s)=6.2Mb/s 相当于使用了6M带宽,小于10M带宽(10Mb/s)
没有占满10M带宽

[root@daixuan ~]# ls /var/log/sa                                       查看按天为单位生成10个sa开头的历史记录
sa16  sa17  sar16
[root@daixuan ~]# sar -n DEV -f /var/log/sa/sa16             -f指定历史文件,查看网卡流量
Linux 2.6.32-573.el6.i686 (daixuan)     2015年11月16日  _i686_  (2 CPU)

22时00分01秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
22时10分01秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22时10分01秒      eth0      0.15      0.04      0.01      0.00      0.00      0.00      0.00
22时20分01秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

[root@daixuan ~]# sar -q                                                       查看历史负载
Linux 2.6.32-573.el6.i686 (daixuan)     2015年11月17日  _i686_  (2 CPU)
00时00分01秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15
12时10分01秒         0       116      0.00      0.02      0.03
12时20分01秒         0       116      0.00      0.00      0.00
12时30分01秒         0       116      0.00      0.00      0.00


[root@daixuan ~]# sar -b                                                        查看磁盘
Linux 2.6.32-573.el6.i686 (daixuan)     2015年11月17日  _i686_  (2 CPU)
00时00分01秒       tps      rtps      wtps   bread/s   bwrtn/s
00时10分01秒      0.18      0.01      0.17      0.20      2.04
00时20分01秒      0.15      0.00      0.15      0.00      1.73
00时30分01秒      0.38      0.06      0.31      0.53      4.78

[root@daixuan ~]# sar -p
[root@daixuan ~]# sar                                                             直接显示一些CPU状态
Linux 2.6.32-573.el6.i686 (daixuan)     2015年11月17日  _i686_  (2 CPU)
00时00分01秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
00时10分01秒     all      0.00      0.00      0.05      0.02      0.00     99.92
00时20分01秒     all      0.00      0.00      0.05      0.02      0.00     99.94
00时30分01秒     all      0.04      0.00      0.42      0.04      0.00     99.50


free命令

[root@daixuan ~]# free                                     查看内存使用情况, 1870912=shared+buffers+cached=248+61648+153748
                total             used        free               shared    buffers     cached
Mem:       1938632     283116    1655516        248      61648     153748      1938632 =1870912+67720 
-/+ buffers/cache:       67720      1870912  剩余的实际内存1870912=shared+buffers+cached=248+61648+153748
Swap:      4194300          0    4194300注:如果free列第二行的数字为0,说明内存已经耗尽
[root@daixuan ~]# free -m                                查看内存使用情况,以Mbyte为单位
[root@daixuan ~]# free -g                                 查看内存使用情况,以Gbyte为单位
[root@daixuan ~]# free -h                                 -h,人性化设计,方便查看
             total       used       free     shared    buffers     cached
Mem:    1.8G       277M     1.6G     248K        61M       150M
-/+ buffers/cache:66M       1.8G
Swap:   4.0G         0B         4.0G
buffer和cache相同点都是占用内存空间,不通点是buffer是将要写入磁盘的数据,cache是从磁盘提前取出的数据
buffers         数据将要写入磁盘,放在内占用的空间buffer,缓冲,      内存——>磁盘
cached         磁盘的部分数据拿出放在内存占用的空间叫cache,缓存,磁盘——>内存


PS

user:运行该进程的用户
PID:标志数
CPU:占用CPU百分比
MEM:占用内存百分比
VSZ:虚拟内存的大小
RSS:真实内存的大小
TTY:从哪里启动的 tty1 tty2
STAT:进程状态,S表示休眠Sleep,s表示主进程或叫父进程,<表示优先级比较高,N表示进程是低优先级,+表示是前台运行的进程,R正在运行running,L内存中被锁Lock,l多线程,Z僵尸进程,X死掉的进程,T暂停的进程,D不能中断的进程
START:启动日期
Time:一共用了CPU时间
COMMAND:进程的名称

[root@daixuan ~]# ps aux
USER       PID %CPU %MEM    VSZ   RSS   TTY      STAT START   TIME  COMMAND
root         1      0.0       0.0       2900  1420  ?          Ss    12:15     0:01  /sbin/init
root         2       0.0      0.0           0     0       ?           S    12:15      0:00  [kthreadd]
[root@daixuan ~]# ps -elf
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root         1     0  0  80   0 -   725 -      12:15 ?        00:00:01 /sbin/init
1 S root         2     0  0  80   0 -     0 -      12:15 ?        00:00:00 [kthreadd]
1 S root         3     2  0 -40   - -     0 -      12:15 ?        00:00:00 [migration/0]
[root@daixuan ~]# ps aux | grep nginx                                   查看nginx进程有没有启动
root      3673  1.0  0.0   6048   748 pts/0    D+   16:10   0:00 grep nginx
[root@daixuan ~]# ps aux | grep php                                      查看PHP进程有没有启动
root      3677  0.0  0.0   6048   780 pts/0    S+   16:10   0:00 grep php


netstat查看网络连接状况

[root@daixuan ~]# netstat -lnp                                           查看监听的端口             
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:111                 0.0.0.0:*                   LISTEN      1774/rpcbind
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      2094/sshd

[root@daixuan ~]# netstat -an                                            查看服务端和客服端进行的通信情况和状态
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     7447   @/com/ubuntu/upstart
unix  2      [ ]         DGRAM                    11163  @/org/freedesktop/hal/udev_event
unix  2      [ ]         DGRAM                    7880   @/org/kernel/udev/udevd
unix  2      [ ACC ]     STREAM     LISTENING     11133  @/var/run/hald/dbus-WFu0oXWoGe