通过进程id找到进程对应的容器并统计每个进程的内存占用写到excel里

时间:2023-03-10 03:32:36
通过进程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()