采集容器内存并写到excel
2024-09-04 05:24:04
# coding=utf-8
import os
import commands
import re
from pyExcelerator import * def execute(cmd):
status, output = commands.getstatusoutput(cmd)
if status != 0:
raise Exception('status is %s, output is %s' % (status, output))
return output def get_docker_name():
infos = execute("docker ps |awk '{print $1, $NF}'").split('\n')
regex = re.compile('\s+')
id_name = {}
for info in infos:
docker_id, docker_name = regex.split(info)
id_name[docker_id] = docker_name
return id_name def get_docker_mem():
regex = re.compile('\s+')
ret = execute('docker stats --no-stream').split('\n')
result_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'docker_res.xlsx')
id_name = get_docker_name()
w = Workbook()
ws = w.add_sheet('node_1_data')
ws.write(0, 0, 'docker_id')
ws.write(0, 1, 'docker_name')
ws.write(0, 2, 'mem(MB)')
index = 1
for docker in ret:
info = regex.split(docker)
docker_id = info[0]
mem = info[2]
unit = info[3]
if unit.startswith('G'):
mem = float(mem) * 1024
if unit.startswith('K'):
mem = float(mem) / 1024
try:
mem = float(mem)
except:
pass
name = id_name[docker_id]
ws.write(index, 0, docker_id)
ws.write(index, 1, name)
ws.write(index, 2, mem)
index += 1
w.save(result_name) if __name__ == '__main__':
get_docker_mem()
最新文章
- Linux find命令的用法实践
- 最近在做外贸网站的时候,需要大量的字体来充实页面,就学习了怎么引用Google Fonts
- Lucene.net站内搜索—5、搜索引擎第一版实现
- 【转】使用Sublime + PlantUML高效地画图
- Google OKR 目标管理体系学习
- ubuntu的命令day1
- JavaWeb项目开发案例精粹-第2章投票系统-006view层
- SharePoint 学习记事(三)
- 原型模式(Prototype Pattern)
- LINQ之路(2):LINQ to SQL本质
- vmware中Ubuntu不能全屏展示的问题
- Mego开发文档 - 索引
- Linux 中磁盘阵列RAID10配置
- Java程序第二次作业
- Mongo学习笔记
- nginx 内置变量
- cordova文件传输系统插件使用:cordova-plugin-file-transfer
- android kotlin Gradle DSL method not found: '1.2.51()'错误,be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
- 中大 9095. Islands
- SQL-结构化查询语言(2)