首页 > 代码库 > 系统资源的观察

系统资源的观察

  • free :观察内存使用情况

          [root@www ~]# free -m(MB)

                                   total       used       free     shared    buffers     cached

               Mem:           988        393        595          0         45        162

               -/+ buffers/cache:        184        803

               Swap:         1999          0       1999

          内存使用高 说明内存使用率很高 效率高

          而swap使用很高(超过20%)时 就又要考虑加内存了

  • uname:查阅系统与核心相关资讯

          [root@www ~]# uname -a

          Linux www.bird.wjd 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

  • uptime:观察系统启动时间与工作负载

          [root@www ~]# uptime

          19:53:15 up 19:45,  1 user,  load average: 0.00, 0.00, 0.00

  • netstat :追踪网络或插槽文件

     [root@www ~]# netstat -tnlp

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:22                  0.0.0.0:*                   LISTEN      1309/sshd

tcp        0      0             0.0.0.0:8888              0.0.0.0:*                   LISTEN      4681/nginx

tcp        0      0             127.0.0.1:25              0.0.0.0:*                   LISTEN      1399/master

tcp        0      0             ::1:20000                   :::*                            LISTEN      4941/nc

tcp        0      0             :::22                           :::*                            LISTEN      1309/sshd

tcp        0      0             :::23                           :::*                            LISTEN      1320/xinetd

tcp        0      0             ::1:25                          :::*                           LISTEN      1399/master

    [root@www ~]# ps -ef | grep nginx

root      4681     1  0 05:03 ?        00:00:00 nginx: master process nginx

nginx     4682  4681  0 05:03 ?        00:00:00 nginx: worker process

root      6611  6462  0 20:00 pts/0    00:00:00 grep --color=auto nginx

[root@www ~]# killall -9 nginx

[root@www ~]# ps -ef | grep nginx

root      6614  6462  0 20:00 pts/0    00:00:00 grep --color=auto nginx

  • dmesg :分析核心产生的信息

          系统启动时会去侦测系统的硬件 dmesg读取内存中核心侦测的硬件信息

          [root@www ~]# dmesg | more     所有

          [root@www ~]# dmesg | grep -i sd     硬盘

          [root@www ~]# dmesg | grep -i eth     网卡

  • vmstat :侦测系统资源变化

          [root@www ~]# vmstat 1 3 (每一秒侦测一次 一共给侦测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     610980  47132     166952    0    0     3     4     27   13  0  0 100  0   0

 0  0      0     610948  47132     166972    0    0     0     0     30   13  0  0 100  0   0

 0  0      0     610948  47132     166972    0    0     0     0     35   17  0  0 100  0   0

  • 内存栏位 (procs) 的项目分别为:
    r :等待运行中的程序数量;b:不可被唤醒的程序数量。这两个项目越多,代表系统越忙碌 (因为系统太忙,所以很多程序就无法被运行或一直在等待而无法被唤醒之故)。

  • 内存栏位 (memory) 项目分别为:
    swpd:虚拟内存被使用的容量; free:未被使用的内存容量; buff:用於缓冲内存; cache:用於高速缓存。 这部份则与 free 是相同的。

  • 内存置换空间 (swap) 的项目分别为:
    si:由磁碟中将程序取出的量; so:由於内存不足而将没用到的程序写入到磁碟的 swap 的容量。 如果 si/so 的数值太大,表示内存内的数据常常得在磁碟与主内存之间传来传去,系统效能会很差!

  • 磁碟读写 (io) 的项目分别为:
    bi:由磁碟写入的区块数量; bo:写入到磁碟去的区块数量。如果这部份的值越高,代表系统的 I/O 非常忙碌!

  • 系统 (system) 的项目分别为:
    in:每秒被中断的程序次数; cs:每秒钟进行的事件切换次数;这两个数值越大,代表系统与周边设备的沟通非常频繁! 这些周边设备当然包括磁碟、网络卡、时间钟等。

  • CPU 的项目分别为:
    us:非核心层的 CPU 使用状态; sy:核心层所使用的 CPU 状态; id:闲置的状态; wa:等待 I/O 所耗费的 CPU 状态; st:被虚拟机器 (virtual machine) 所盗用的 CPU 使用状态 (2.6.11 以后才支持)。

     查看已开启的文件或运行程序开启的文件

    文件看程序

     [root@www ~]# fuser -uv /usr/local/nginx/logs/error.log

                     用户     进程号   权限   命令

     /usr/local/nginx/logs/error.log:

                     root       6677      F.... (root)nginx

                     nginx     6678      F.... (nginx)nginx

     程序看文件

     [root@www ~]# lsof -u root | grep nginx

     找出正在运行的程序的pid

     [root@www lsof_4.76_src]# pidof nginx

     6678 6677 


系统资源的观察