查询Ceph的OSD占用内存

前言

之前写过一篇关于查询OSD的运行的CPU的情况的分享,本篇是讲的获取内存占用的,代码包括两种输出,一种是直接的表格,一种是可以方便解析的json

代码

直接上代码,python才用不久,所以可能代码实现比较低级,主要是看实现的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys
import json
import psutil
import commands
from prettytable import PrettyTable
def main():
if len(sys.argv) == 1:
printosdmemtable("table")
elif sys.argv[1] == 'json':
printosdmemtable("json")

def printosdmemtable(chosse):
data_dic = {}
osd_list={}
row = PrettyTable()
row.header = True
memlist = ["OSD\MEM"]
memchose = [ 'VIRT','RES']
for meminfo in memchose:
memlist.append("%s" %meminfo )
row.field_names = memlist
for root, dirs, files in os.walk('/var/run/ceph/'):
for name in files:
if "osd" in name and "pid" in name :
osdlist = []
osdthlist=[]
for osdmem in range(len(memchose)):
osdlist.append(" ")
pidfile=root+ name
osdid=commands.getoutput('ls %s|cut -d "." -f 2 2>/dev/null' %pidfile )
osdpid = commands.getoutput('cat %s 2>/dev/null' %pidfile)
osd_runmemvsz = commands.getoutput('ps -p %s -o vsz |grep -v VSZ 2>/dev/null' %osdpid)
osd_runmemrsz = commands.getoutput('ps -p %s -o rsz |grep -v RSZ 2>/dev/null' %osdpid)
osdname="osd."+osdid
osdlist.insert(0,osdname)
osdlist[1] = str(int(osd_runmemvsz)/1024)+"KB"
osdlist[2] = str(int(osd_runmemrsz)/1024)+"KB"
vm_dic = {}
vm_dic['VSZ']= str(int(osd_runmemvsz)/1024)+"KB"
vm_dic['RSZ']= str(int(osd_runmemrsz)/1024)+"KB"
osd_list[osdname] = vm_dic
data_dic['osdmemused'] = osd_list
if chosse == "table":
row.add_row(osdlist)
elif chosse == "json":
row = json.dumps(data_dic,separators=(',', ':'))
print row

if __name__ == '__main__':
main()

运行脚本

1
2
3
4
5
6
7
8
9
[root@lab8106 getmem]# python getmem.py 
+---------+-------+------+
| OSD\MEM | VIRT | RES |
+---------+-------+------+
| osd.0 | 825KB | 43KB |
| osd.1 | 826KB | 43KB |
+---------+-------+------+
[root@lab8106 getmem]# python getmem.py json
{"osdmemused":{"osd.1":{"VSZ":"826KB","RSZ":"43KB"},"osd.0":{"VSZ":"825KB","RSZ":"43KB"}}}

附加

如果在/var/run/ceph下面没有生成pid,就在配置文件/etc/ceph/ceph.conf当中提前加好配置文件然后重启进程

pid_file=/var/run/$cluster/$type.$id.pid