首页 > 系统相关 >监控容器节点内存使用率

监控容器节点内存使用率

时间:2022-12-15 15:45:53浏览次数:34  
标签:__ mem cmd list 内存 print 使用率 节点

# -*- 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))

  

标签:__,mem,cmd,list,内存,print,使用率,节点
From: https://www.cnblogs.com/QQmini/p/16985155.html

相关文章