1. 程式人生 > 其它 >檢視Linux系統記憶體、CPU、磁碟使用率和詳細資訊

檢視Linux系統記憶體、CPU、磁碟使用率和詳細資訊

轉載自:https://www.csdn.net/tags/NtjaAgysMjc1MzgtYmxvZwO0O0OO0O0O.html

一、檢視記憶體佔用

1、free

# free -m

以MB為單位顯示記憶體使用情況

[root@localhost ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:          11852        1250        8668         410        1934        9873
Swap:          6015           0        6015

# free -h

以GB為單位顯示記憶體使用情況

[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:            11G        1.2G        8.5G        410M        1.9G        9.6G
Swap:          5.9G          0B        5.9G

# free -t

以總和的形式查詢記憶體的使用資訊

[root@localhost ~]# free -t
              total        used        free      shared  buff/cache   available
Mem:       12137332     1285344     8870628      420268     1981360    10105740
Swap:       6160380           0     6160380
Total:     18297712     1285344    15031008

# free -s 5

週期性的查詢記憶體使用資訊

每5秒執行一次命令

[root@localhost ~]# free -s 5
              total        used        free      shared  buff/cache   available
Mem:       12137332     1280796     8875008      420268     1981528    10110136
Swap:       6160380           0     6160380

解釋:

Mem:記憶體的使用情況總覽表

total:機器總的實體記憶體

used:用掉的記憶體

free:空閒的實體記憶體

注:實體記憶體(total)=系統看到的用掉的記憶體(used)+系統看到空閒的記憶體(free)

2、檢視某個pid的實體記憶體使用情況

# cat /proc/PID/status | grep VmRSS

[root@localhost ~]# pidof nginx
27327 27326
[root@localhost ~]#
[root@localhost ~]# cat /proc/27327/status | grep VmRSS
VmRSS:      2652 kB
[root@localhost ~]#
[root@localhost ~]# cat /proc/27326/status | grep VmRSS
VmRSS:      1264 kB
[root@localhost ~]#
[root@localhost ~]# pidof java
1973
[root@localhost ~]# cat /proc/1973/status | grep VmRSS
VmRSS:   1166852 kB

由上面可知,nginx服務程序的兩個pid所佔實體記憶體為"2652+1264=3916k"

3、檢視本機所有程序的記憶體佔比之和

# cat mem_per.sh

[root@localhost ~]# cat mem_per.sh
#!/bin/bash
ps auxw|awk '{if (NR>1){print $4}}' > /opt/mem_list
awk '{MEM_PER+=$1}END{print MEM_PER}'  /opt/mem_list
[root@localhost ~]#
[root@localhost ~]# chmod 755 mem_per.sh
[root@localhost ~]#
[root@localhost ~]# sh mem_per.sh
64.4
[root@localhost ~]# 

指令碼配置解釋:
ps -auxw|awk '{print $3}'   表示列出本機所有程序的cpu利用率情況,結果中第一行帶"%CPU"字元
ps -auxw|awk '{print $4}'   表示列出本機所有程序的記憶體利用率情況,結果中第一行帶"%MEM"字元
ps auxw|awk '{if (NR>1){print $4}}  表示將"ps auxw"結果中的第一行過濾(NR>1)掉,然後列印第4行

二、檢視CPU使用情況

1、top

top後鍵入P看一下誰佔用最大

# top -d 5

週期性的查詢CPU使用資訊

每5秒重新整理一次

top - 02:37:55 up 4 min,  1 user,  load average: 0.02, 0.10, 0.05
Tasks: 355 total,   1 running, 354 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.0 us,  2.8 sy,  0.0 ni, 94.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
# us:表示使用者空間程式的cpu使用率(沒有通過nice排程)
# sy:表示系統空間的cpu使用率,主要是核心程式。
# id:空閒cpu
KiB Mem :  1868660 total,  1081340 free,   578388 used,   208932 buff/cache
KiB Swap:  4194300 total,  4194300 free,        0 used.  1123992 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                  
  2220 mysql     20   0 1307796 471172   7608 S  0.6 25.2   0:02.31 mysqld                                                                                   
   349 root      20   0       0      0      0 S  0.4  0.0   0:01.14 kworker/0:3                                                                              
   644 root      20   0       0      0      0 S  0.4  0.0   0:00.17 xfsaild/dm-0                                                                             
  3489 root      20   0  146432   2268   1440 R  0.4  0.1   0:00.11 top                                                                                      
     1 root      20   0   44500   7120   2596 S  0.2  0.4   0:01.69 systemd                                                                                  
   283 root      39  19       0      0      0 S  0.2  0.0   0:00.18 khugepaged                                                                               
  2621 root      20   0  141264   5140   3896 S  0.2  0.3   0:00.18 sshd                                                                                     
     2 root      20   0       0      0      0 S  0.0  0.0   0:00.01 kthreadd                                                                                 
     3 root      20   0       0      0      0 S  0.0  0.0   0:00.01 ksoftirqd/0                                                                              
     4 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0                                                                              
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                                                             
     6 root      20   0       0      0      0 S  0.0  0.0   0:00.02 kworker/u256:0                                                                           
     7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0                                                                              
     8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                                                                   
     9 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/0                                                                                  
    10 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/1                                                                                  
    11 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/2                                                                                  

 

2、ps auxw(檢視本機的程序所佔cpu和mem的百分比情況)

使用"ps auxw" 可以檢視到本機的程序所佔cpu和mem的百分比情況

# ps auxw | head -1

%CPU 程序的cpu佔用率
%MEM 程序的記憶體佔用率

[root@localhost ~]# ps auxw | head -1
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
[root@localhost ~]#

檢視java程序所佔本機的cpu百分比, 如下為0.2%

# ps auxw |grep -v grep|grep -w java|awk '{print $3}'

[root@localhost ~]# ps auxw |grep -v grep|grep -w java|awk '{print $3}'
0.2

檢視java程序所佔本機的記憶體百分比, 如下為30.0%

# ps auxw |grep -v grep|grep -w java|awk '{print $4}'

[root@localhost ~]# ps auxw |grep -v grep|grep -w java|awk '{print $4}'
30.0

3、檢視本機所有程序的CPU佔比之和

# cat cpu_per.sh

[root@localhost ~]# cat cpu_per.sh
#!/bin/bash
ps auxw|awk '{if (NR>1){print $3}}' > /opt/cpu_list
awk '{CPU_PER+=$1}END{print CPU_PER}'  /opt/cpu_list
[root@localhost ~]#
[root@localhost ~]# chmod 755 cpu_per.sh
[root@localhost ~]#
[root@localhost ~]# sh cpu_per.sh
44.5
[root@localhost ~]#

三、檢視cpu資訊(資訊記錄在/proc/cpuinfo中)

# 總核數 = 物理CPU個數 X 每顆物理CPU的核數

# 總邏輯CPU數 = 物理CPU個數 X 每顆物理CPU的核數 X 超執行緒數

1、檢視虛擬機器邏輯CPU的個數

# cat /proc/cpuinfo| grep "processor"| wc -l

[root@localhost ~]# cat /proc/cpuinfo| grep "processor"| wc -l
6

 

2、檢視物理CPU個數

# cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

[root@localhost ~]# cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
1

 

3、列出CPU詳細資訊

# lscpu

伺服器1:

[root@localhost ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                6
On-line CPU(s) list:   0-5
Thread(s) per core:    1
Core(s) per socket:    6
座:                 1
NUMA 節點:         1
廠商 ID:           GenuineIntel
CPU 系列:          6
型號:              15
型號名稱:        Intel(R) Core(TM)2 Duo CPU     T7700  @ 2.40GHz
步進:              11
CPU MHz:             2194.916
BogoMIPS:            4389.83
超管理器廠商:  KVM
虛擬化型別:     完全
L1d 快取:          32K
L1i 快取:          32K
L2 快取:           4096K
NUMA 節點0 CPU:    0-5
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good nopl eagerfpu pni ssse3 cx16 pcid sse4_2 x2apic hypervisor lahf_lm

 

伺服器2:

[root@bogon ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    1
Core(s) per socket:    8
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 79
Model name:            Intel(R) Xeon(R) CPU E7-4830 v4 @ 2.00GHz
Stepping:              1
CPU MHz:               1995.192
BogoMIPS:              3990.38
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              35840K
NUMA node0 CPU(s):     0-7
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx hypervisor lahf_lm 3dnowprefetch epb dtherm arat pln pts

 

4、檢視每個物理CPU中core的個數(即核數)

# cat /proc/cpuinfo| grep "cpu cores"| uniq

[root@localhost ~]# cat /proc/cpuinfo| grep "cpu cores"| uniq
cpu cores       : 6

三、Linux下檢視哪些程序佔用的CPU和記憶體資源最多的方法

1、獲取佔用CPU資源最多的10個程序

 # ps aux | head -1; ps aux | grep -v PID | sort -rn -k +3 | head -10

[root@localhost ~]# ps aux | head -1; ps aux | grep -v PID | sort -rn -k +3 | head -10
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      9416  0.8  0.0 163544  6284 ?        Ss   08:38   0:00 sshd: root@pts/0
root      3783  0.7  0.0  12072  2032 ?        S    May26 357:27 /bin/bash /usr/local/VMOptimizationTools/sangfor_guest_datareport
root      9545  0.6  0.0 163224  5904 ?        Ss   08:38   0:00 sshd: root@notty
root      3966  0.3  0.0  12704  2676 ?        S    May26 183:12 /bin/bash /usr/local/VMOptimizationTools/sangfor_update_ipc_callback
root      3784  0.3  0.0  12560  2552 ?        S    May26 182:42 /bin/bash /usr/local/VMOptimizationTools/sangfor_sfping
33       10431  0.3  0.1 409704 17832 ?        S    08:38   0:00 /usr/sbin/apache2 -k start
root      3986  0.2  0.0  12452  2280 ?        S    May26 122:23 /bin/bash /usr/local/VMOptimizationTools/sangfor_vm_proxyd_w
root      3781  0.2  0.0  12740  2672 ?        S    May26 115:59 /bin/bash /usr/local/VMOptimizationTools/sangfor_vm_proxyd
500      23785  0.2  2.0 1790172 249528 ?      Ss   Jun25  11:30 oraclehelowin (LOCAL=NO)
root      4053  0.1  0.0  12508  2520 ?        S    May26  75:16 /bin/bash /usr/local/VMOptimizationTools/sangfor_watchdog

 

2、獲取佔用記憶體資源最多的10個程序

# ps aux | head -1; ps aux | grep -v PID | sort -rn -k +4 | head -10

[root@localhost ~]# ps aux | head -1; ps aux | grep -v PID | sort -rn -k +4 | head -10
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
500      32304  0.0  5.7 1794892 700976 ?      Ss   May31   2:53 ora_dbw0_helowin
500       4785  0.0  4.5 1797912 550132 ?      Ss   May29   4:03 ora_dbw0_helowin
500       4796  0.0  3.5 1798308 426468 ?      Ss   May29   2:11 ora_smon_helowin
500      25850  0.0  2.5 1810144 307340 ?      Ss   Jun17   0:10 oraclehelowin (LOCAL=NO)
500      32471  0.0  2.4 1810184 299704 ?      Ss   Jun14   0:30 oraclehelowin (LOCAL=NO)
500       3927  0.0  2.3 1791308 283440 ?      Ss   Jun26   0:13 oraclehelowin (LOCAL=NO)
500       5432  0.0  2.1 1794272 261692 ?      Ss   May29   7:16 ora_cjq0_helowin
500      23785  0.2  2.0 1790172 249528 ?      Ss   Jun25  11:30 oraclehelowin (LOCAL=NO)
500      19092  0.0  2.0 1793248 253080 ?      Ss   Jun21   0:28 oraclehelowin (LOCAL=NO)
500      32310  0.0  1.8 1794224 229200 ?      Ss   May31   1:25 ora_smon_helowin

 

3、檢視佔用cpu最高的程序

# ps aux | head -1; ps aux | grep -v PID | sort -rn -k +3 | head -1

[root@localhost ~]# ps aux | head -1; ps aux | grep -v PID | sort -rn -k +3 | head -1
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      3783  0.7  0.0  12072  2032 ?        S    May26 357:32 /bin/bash /usr/local/VMOptimizationTools/sangfor_guest_datareport

4、獲取佔用記憶體資源最高的程序

# ps aux | head -1; ps aux | grep -v PID | sort -rn -k +4 | head -1

[root@localhost ~]# ps aux | head -1; ps aux | grep -v PID | sort -rn -k +4 | head -1
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
500      32304  0.0  5.7 1794892 700976 ?      Ss   May31   2:53 ora_dbw0_helowin

四、Linux下檢視某些程序的啟動時間和執行時長

比如檢視java程序的啟動時間和執行時長

# ps -ef|grep -v grep|grep -w java|awk '{print $2}'

# ps -eo pid,lstart,etime | grep 1973

其中:

Mon Jun 24 09:25:41 2019       為java程序的啟動時間

4-00:16:55                     為java程序的執行時長,天-小時-分鐘-秒

[root@localhost ~]# ps -ef|grep -v grep|grep -w java|awk '{print $2}'
1973
[root@localhost ~]#
[root@localhost ~]# ps -eo pid,lstart,etime | grep 1973
 1973 Mon Jun 24 09:25:41 2019  4-00:16:55
[root@localhost ~]#
[root@localhost ~]# date
Fri Jun 28 09:42:48 CST 2019

 

檢視所有程序的啟動事件、執行時長

# ps -eo user,pid,lstart,etime,cmd

檢視nginx程序啟動的精確時間和啟動後執行的時長

# ps -eo pid,lstart,etime,cmd|grep nginx

[root@nginx-proxy-client ~]# ps -eo pid,lstart,etime,cmd|grep nginx
 1418 Mon Jun 24 13:38:18 2019  3-20:21:05 nginx: master process /usr/local/nginx/sbin/nginx
 1419 Mon Jun 24 13:38:18 2019  3-20:21:05 nginx: worker process
 5543 Fri Jun 28 09:59:23 2019       00:00 grep --color=auto nginx
[root@nginx-proxy-client ~]#
[root@nginx-proxy-client ~]# date
2019年 06月 28日 星期五 09:59:45 CST
[root@nginx-proxy-client ~]#

五、檢視網路情況

# ifconfig

[root@localhost ~]# ifconfig 
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.80  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::20c:29ff:fe4c:ff47  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4c:ff:47  txqueuelen 1000  (Ethernet)
        RX packets 7866  bytes 632606 (617.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 215  bytes 31932 (31.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 8  bytes 400 (400.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 400 (400.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# ip a

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:4c:ff:47 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.80/24 brd 192.168.2.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe4c:ff47/64 scope link 
       valid_lft forever preferred_lft forever

四、檢視磁碟以及分割槽情況

# df -Th

檢視分割槽、掛載情況

[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        15G  2.8G   13G  19% /
devtmpfs                devtmpfs  903M     0  903M   0% /dev
tmpfs                   tmpfs     913M     0  913M   0% /dev/shm
tmpfs                   tmpfs     913M  8.6M  904M   1% /run
tmpfs                   tmpfs     913M     0  913M   0% /sys/fs/cgroup
/dev/sda1               xfs       297M  115M  183M  39% /boot
tmpfs                   tmpfs     183M     0  183M   0% /run/user/0

# lsblk

檢視磁碟情況

[root@localhost ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  300M  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   15G  0 lvm  /
  └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
sr0              11:0    1    4G  0 rom  

# fdisk -l

檢視詳細的硬碟分割槽情況

[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0004a0a8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448    40478719    19931136   8e  Linux LVM

Disk /dev/mapper/centos-root: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

end