测试系统: Debian

TOP命令: -n 表示只刷新一次

$ top -n 1
top - 16:36:38 up  1:06,  3 users,  load average: 1.95, 1.49, 1.42
Tasks: 180 total,   2 running, 178 sleeping,   0 stopped,   0 zombie
%Cpu(s): 22.9 us,  5.7 sy,  0.0 ni, 71.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   3854.5 total,   2304.4 free,    832.9 used,    717.1 buff/cache
MiB Swap:    100.0 total,    100.0 free,      0.0 used.   2706.4 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1065 root      20   0  328880  96868  64248 R  81.2   2.5  64:45.65 python3
  671 root      20   0  142680  46616  31008 S   6.2   1.2   1:07.32 Xorg
 6383 pi        20   0   10636   2976   2568 R   6.2   0.1   0:00.03 top
    1 root      20   0   32712   8136   6532 S   0.0   0.2   0:04.94 systemd
    2 root      20   0       0      0      0 S   0.0   0.0   0:00.01 kthreadd
    3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp
    4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp
    8 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq
    9 root      20   0       0      0      0 S   0.0   0.0   0:00.34 ksoftirqd/0

其中第3行 %Cpu(s)表示CPU的使用情况
us - 表示用户空间程序的cpu使用率(user)
sy:表示系统空间的cpu使用率,主要是内核程序 (system)。
ni:表示用户空间且通过nice调度过的程序的cpu使用率。
id:空闲cpu(Idel)
wa:cpu运行时在等待io的时间
hi:cpu处理硬中断的数量
si:cpu处理软中断的数量
st:被虚拟机占用的cpu
71.4 id,表示空闲CPU,即CPU未使用率,100%-71.4%=28.6%,则系统的cpu使用率为28.6%。

其中第4行MiB Mem表示内存占用情况


free命令:

$ free 
              total        used        free      shared  buff/cache   available
Mem:        3946964      851752     2360860      198248      734352     2773204
Swap:        102396           0      102396

打印空闲内存数量:

$ free -m | awk '/Mem/ {print $4}'
2304

-m表示以MB为单位,-G表示以GB为单位


查看磁盘使用情况:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 19G 6.0G 12G 34% /
/dev/sdb1 20G 4.3G 15G 23% /opt

打印sda1的占用率:

$ df -h | awk '/sda1/ {print $5}'
34%

-h表示采用合适的单位(由于磁盘比较大,经常以GB为单位,以字节计时,数值太大)


基于python的脚本: 实际上仍按上述命令实现

#!/usr/bin/env python3
import os
# 获取CPU温度(仅树莓派支持)
def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    return(res.replace("temp=","").replace("'C\n","")) 
# 获取内存使用情况信息:
def getRAMinfo():
    p = os.popen('free -m')
    i = 0
    while 1:
        i = i + 1
        line = p.readline()
        if i==2:
            return(line.split()[1:4])
# 获取CPU使用情况
def getCPUuse():
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip())) 
# 获取磁盘使用情况
def getDiskSpace():
    p =os.popen("df -h /")
    i = 0
    while 1:
        i =i + 1
        line =p.readline()
        if i==2:
            return(line.split()[1:5])
#--------------------------------------------------------------
CPU_temp=getCPUtemperature()
CPU_usage=getCPUuse()
RAM_stats = getRAMinfo()
#RAM_total = round(int(RAM_stats[0]),1)
#RAM_used  = round(int(RAM_stats[1]),1)
#RAM_free  = round(int(RAM_stats[2]),1) 
RAM_total = RAM_stats[0]
RAM_used  = RAM_stats[1]
RAM_free  = RAM_stats[2]
DISK_stats = getDiskSpace()
DISK_total = DISK_stats[0]
DISK_used  = DISK_stats[1]
DISK_perc  = DISK_stats[3] 

if __name__ =='__main__':
    print('CPU Temp   = '+CPU_temp)
    print('CPU Use    = '+CPU_usage)
    print('RAM Total  = '+str(RAM_total) +'MB')
    print('RAM Used   = '+str(RAM_used)  +'MB')
    print('RAM Free   = '+str(RAM_free)  +'MB')
    print('DISK Total = '+str(DISK_total)+'B')
    print('DISK Used  = '+str(DISK_used) +'B')
    print('DISK Usage = '+str(DISK_perc))

基于C语言的方法:

#include <stdio.h>
#include <string.h>
#define CPU_IDEL_CMD    "top -n 1 | awk '/Cpu/ {print $8}'"
#define MEM_FREE_CMD    "free -m  | awk '/Mem/ {print $4}'"
int main(int argc, char *argv[])
{
    FILE* fp;
    char buff[100];
    fp = popen(MEM_FREE_CMD,"r");
    bzero(buff,100);
    fread(buff, 1, 50, fp);
    printf("%s", buff);
    fclose(fp);
    return 0;
}