docker容器监控宿主机的内存及cpu占用

  |   2 评论   |   101 浏览

先将宿主机的目录/proc目录挂载到容器中

1.CPU占用获取

解析/proc/stat文件

 1root@wenbo:~# cat /proc/stat
 2cpu  74 0 51 11109 6 0 36 0 0 0
 3cpu0 7 0 11 1380 1 0 18 0 0 0
 4cpu1 1 0 0 1403 1 0 14 0 0 0
 5cpu2 20 0 9 1371 0 0 4 0 0 0
 6cpu3 32 0 5 1369 1 0 0 0 0 0
 7cpu4 1 0 3 1395 0 0 0 0 0 0
 8cpu5 1 0 13 1395 0 0 0 0 0 0
 9cpu6 11 0 10 1387 0 0 0 0 0 0
10cpu7 1 0 0 1405 0 0 0 0 0 0
11intr 5285 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12ctxt 11774
13btime 1630895617
14processes 350
15procs_running 1
16procs_blocked 0
17softirq 6025 0 417 0 59 0 0 2695 1434 0 1420

cpu0-cpu7代表每个核的使用情况,第一行的cpu是总的使用情况,所以我们监控只需要用到第一行的数据

参考网上查找的信息,8列分别为:

列名 说明
user 从系统启动开始累计到当前时刻,处于用户态的运行时间,不包含 nice值为负进程。
nice 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间
system 从系统启动开始累计到当前时刻,处于核心态的运行时间
idle 从系统启动开始累计到当前时刻,除IO等待时间以外的其它等待时间
iowait IO等待时间
irq 从系统启动开始累计到当前时刻,硬中断时间
softirq 从系统启动开始累计到当前时刻,软中断时间
stealstolen 虚拟化环境中运行时在其他操作系统中花费的时间

计算占用率的公式是取相隔时间较短的两组变量c1和c2

c1的8列相加为total1

c2的8列相加为total2

total=total2-total1

c1的idle为idle1

c2的idle为idle2

idle=idle2-idle1

CPU占用率百分比计算:(total - idle) / total * 100

2.内存占用获取

解析/proc/meminfo文件

 1root@wenbo:~# cat /proc/meminfo
 2MemTotal:        2029436 kB
 3MemFree:           78232 kB
 4MemAvailable:     647692 kB
 5Buffers:           21228 kB
 6Cached:           655656 kB
 7SwapCached:            0 kB
 8Active:           183384 kB
 9Inactive:        1635580 kB
10Active(anon):        268 kB
11Inactive(anon):  1133696 kB
12Active(file):     183116 kB
13Inactive(file):   501884 kB
14Unevictable:           0 kB
15Mlocked:               0 kB
16SwapTotal:       1048576 kB
17SwapFree:        1048576 kB
18Dirty:                16 kB
19Writeback:             0 kB
20AnonPages:       1140128 kB
21Mapped:           153172 kB
22Shmem:               280 kB
23KReclaimable:      32392 kB
24Slab:              63528 kB
25SReclaimable:      32392 kB
26SUnreclaim:        31136 kB
27KernelStack:        8288 kB
28PageTables:         5692 kB
29NFS_Unstable:          0 kB
30Bounce:                0 kB
31WritebackTmp:          0 kB
32CommitLimit:     2063292 kB
33Committed_AS:    3292456 kB
34VmallocTotal:   34359738367 kB
35VmallocUsed:       28088 kB
36VmallocChunk:          0 kB
37Percpu:             2496 kB
38AnonHugePages:    866304 kB
39ShmemHugePages:        0 kB
40ShmemPmdMapped:        0 kB
41FileHugePages:         0 kB
42FilePmdMapped:         0 kB
43HugePages_Total:       0
44HugePages_Free:        0
45HugePages_Rsvd:        0
46HugePages_Surp:        0
47Hugepagesize:       2048 kB
48Hugetlb:               0 kB
49DirectMap4k:       27648 kB
50DirectMap2M:     2068480 kB
51DirectMap1G:     2097152 kB

内存占用率百分比计算:(MemTotal - MemFree - Buffers - Cached - SReclaimable - Shmem) / MemTotal * 100


作者:wenbo

评论

发表评论


取消