监控容器节点内存使用率
# -*- encoding: utf-8 -*- from subprocess import Popen, PIPE, STDOUT import time def Run_Cmd(cmd): # 执行命令 p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) stdout, stderr = p.communicate() return p.returncode, stdout.strip() if __name__ == "__main__": docker_id = "0fd446b3d628" cmd = 'docker stats %s --no-stream' %docker_id print("开始执行脚本") mem_list = 0 for i in range(1,121): code, out = Run_Cmd(cmd) mem_list += float(out.split()[22].split("%")[0]) print("第%s次 当前内存值:%s"% (i,out.split()[22])) time.sleep(3) print("总内存位:{}%".format(mem_list)) avg = float(mem_list) / 120 print("120次平均内存使用率:{}%".format(avg))