通过进程id找到进程对应的容器并统计每个进程的内存占用写到excel里
# coding=utf-8
import re
import os
import commands
import json
import psutil
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_all_container_ids_name():
infos = execute("docker ps |awk '{print $1, $NF}'").split('\n')
all_ids = {}
regex = re.compile('\s+')
for info in infos:
docker_id, docker_name = regex.split(info)
short_id = docker_id.strip()
if short_id.strip().startswith('CON'):
continue
full_id = execute("docker inspect -f '{{.Id}}' %s" % short_id)
state = execute("cat /run/runc/%s/state.json" % full_id)
f = json.loads(state)
cgroup_paths = f['cgroup_paths']['pids']
pids_path = os.path.join(cgroup_paths, 'cgroup.procs')
ids = execute("cat %s" % pids_path).split('\n')
for prgress_id in ids:
pr_id = prgress_id.strip()
all_ids[pr_id] = {'id': short_id, 'name': docker_name}
return all_ids def get_process_info(p):
try:
# cpu = int(p.cpu_percent(interval=1))
rss = p.memory_info().rss
name = p.name()
pid = p.pid
return '%s,%s,%s\n' % (pid, name, rss)
except Exception as e:
print e.message def get_all_process_info():
"""取出全部进程的进程名,进程ID,进程实际内存, 虚拟内存,CPU使用率
"""
node_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'node_test.log')
instances = ''
all_processes = list(psutil.process_iter())
for proc in all_processes:
ret = get_process_info(proc)
if ret:
instances += ret
with open(node_name, 'w') as fp:
fp.writelines(instances) def get_docker_name():
file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'node_test.log')
result_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data.txt')
id_name_relation = get_all_container_ids_name()
tmp = ''
regex = re.compile(',')
with open(file_name, 'r') as fp:
for progress_info in fp.readlines():
progress_id, mem, progress_name = regex.split(progress_info)[0], regex.split(progress_info)[2], \
regex.split(progress_info)[1]
if progress_id in id_name_relation:
tmp += '%s %s %s %s %s\n' % (progress_id, id_name_relation[progress_id]['id'],
id_name_relation[progress_id]['name'], progress_name, mem)
else:
tmp += '%s %s %s %s %s\n' % (progress_id, 'sys_progress', 'sys_progress', progress_name, mem)
with open(result_name, 'w') as fp:
fp.writelines(tmp) def ge_excel():
file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data.txt')
result_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data.xlsx')
regex = re.compile(' ')
w = Workbook() # 创建一个工作簿
ws = w.add_sheet('node_1_data') # 创建一个工作表
ws.write(0, 0, 'pid')
ws.write(0, 1, 'docker_id')
ws.write(0, 2, 'docker_name')
ws.write(0, 3, 'progress_name')
ws.write(0, 4, 'mem(MB)')
index = 1
with open(file_name, 'r') as fp:
for info in fp.readlines():
progress_info = info.strip()
if progress_info:
progress_id, docker_id, docker_name, progress_name, mem = regex.split(progress_info)
ws.write(index, 0, progress_id)
ws.write(index, 1, docker_id)
ws.write(index, 2, docker_name)
ws.write(index, 3, progress_name)
ws.write(index, 4, float(mem) / 1024 / 1024)
index += 1
w.save(result_name) def delete_tmp_file():
data_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data.txt')
node_test_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'node_test.log')
if os.path.exists(data_name):
os.remove(data_name)
if os.path.exists(node_test_name):
os.remove(node_test_name) if __name__ == '__main__':
delete_tmp_file()
get_all_process_info()
get_docker_name()
ge_excel()
delete_tmp_file()
通过进程id找到进程对应的容器并统计每个进程的内存占用写到excel里的更多相关文章
- 采集容器内存并写到excel
# coding=utf-8 import os import commands import re from pyExcelerator import * def execute(cmd): sta ...
- Linux/Unix分配进程ID的方法以及源代码实现
在Linux/Unix系统中.每一个进程都有一个非负整型表示的唯一进程ID.尽管是唯一的.可是进程的ID能够重用.当一个进程终止后,其进程ID就能够再次使用了. 大多数Linux/Unix系统採用延迟 ...
- Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存
Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存 参考:<linux编程从入门到精通>,<Linux C程序设计大全>,<unix环境高级编程> ...
- jstack:将Process Explorer中看到的进程ID做16进制转换,到ThreadDump中加上0x 前缀即能找到对应线程(转)
原文链接:http://www.iteye.com/topic/1133941 症状: 使用Eclipse win 64位版本,indigo及kepler都重现了,使用tomcat 6.0.39,jd ...
- Linux 内核进程管理之进程ID
Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数据和结构,Linux 内核所有涉及到进程和程序的所有算法都是围绕该数据结构建立的,是内核中最重要的数据结构之一.该数据结构 ...
- Linux 内核进程管理之进程ID 。图解
http://www.cnblogs.com/hazir/tag/kernel/ Linux 内核进程管理之进程ID Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数 ...
- Linux进程ID号--Linux进程的管理与调度(三)【转】
Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数据和结构,Linux 内核所有涉及到进程和程序的所有算法都是围绕该数据结构建立的,是内核中最重要的数据结构之一. 该数据结 ...
- Linux进程ID号--Linux进程的管理与调度(三)
转自:http://blog.csdn.net/gatieme/article/category/6225543 日期 内核版本 架构 作者 GitHub CSDN 2016-05-12 Linux- ...
- Linux 内核进程管理之进程ID【转】
转自:http://www.cnblogs.com/hazir/p/linux_kernel_pid.html Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数据和结构, ...
随机推荐
- [BZOJ3236]:[Ahoi2013]作业(莫队+分块)
题目传送门 题目描述 此时已是凌晨两点,刚刚做了$Codeforces$的小$A$掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是一个小时可与做完的数学作业,接下来是分别都是一个小时可 ...
- ANativeWindow_fromSurface
c++后台若使用ANativeWindow_fromSurface将surface转化为ANativeWindow: 需要头文件:#include <android/native_window_ ...
- hypermesh中怎么设置支反力(反作用力)
Analysis page >> Control cards >> Global output request 勾选 SPCF 和 GPFORCE .
- PCIE手札
PCIE兼容了大部分PCI总线的特性,区别在于使用串行差分总线代替了并行总线,并实现了协议分层.PCIE的带宽与LANE数量和时钟频率相关,时钟频率支持2.5G和5G,Lane支持x1/x2/x4/x ...
- tjuthesis 图标题左对齐修改办法
图标题格式默认是居中的. 将 format 文件里定义图表标题样式部分的 \centering 删去,可变为左对齐. 如下: %% 定制浮动图形和表格标题样式\makeatletter\long\de ...
- Django学习之模板
一.常用语法 1.变量 2.Filters 3.自定义filter 4.Tags 5.csrf_token 6.注释 7.注意事项 二.母板 2.继承母板 3.块(block) 4.组件 5.静态文件 ...
- xcode archive灰色,无法打包的解决办法
问题如图: 解决办法:目前的运行配置是使用模拟器,改成“iOS Device”即可 step1: step2: 修改后archive选项变为黑色,可点击状态了
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_5_InvalidClassException异常_原理
序列化一遍 反序列化再来一遍 age的修饰符改为public 直接反序列化就会抛出异常 序列化的时候会给Person.class添加序列号,serialVersionUID,.反序列化需要对比这个se ...
- Krypton Suite of .NET WinForms Controls
The Krypton Suite of .NET WinForms controls are now freely available for use in personal or commeric ...
- 关于jdbc和数据库连接池的关系(不是封装的关系)
你都说是数据库连接池了.那就是连接数据库用的.JDBC是java封装的对数据库的操作.当然你可以自己进一步封装.数据库连接池是JDBC使用的前提,如果连数据库连接池都没连上,JDBC的操作就谈不上了. ...