#!/usr/bin/env python
#encoding:utf-8
# desc:用来描述各个主机信息 import os #CTID NPROC STATUS IP_ADDR HOSTNAME configDir = '/etc/pve/lxc' #获取所有的配置文件
def fileListFunc(filePathList):
fileList = []
for filePath in filePathList:
for top, dirs, nondirs in os.walk(filePath):
for item in nondirs:
#fileList.append(os.path.join(top, item))
fileList.append(item)
return fileList #根据配置文件获取ID号
def getAllID():
idList = []
for id in fileListFunc([configDir]):
idList.append(id.split('.')[0])
return sorted(idList) #根据ID号获取主机状态
def getStat(id):
statInfo = os.popen('lxc-info -n ' + str(id)).read()
if 'RUNNING' in statInfo:
return 'running'
else:
return 'stoping' #根据ID号获取主机IP地址
def getIP(id):
ip = '-'
statInfo = os.popen('lxc-info -n ' + str(id)).read()
for line in statInfo.split('\n'):
if 'IP' in line:
ip = line.split(':')[1].strip()
return ip #根据ID号获取主机名
def getHostName(id):
hostname = '-'
statInfo = os.popen('cat /etc/pve/lxc/' + str(id) + '.conf').read()
for line in statInfo.split('\n'):
if line.startswith('hostname'):
hostname = line.split(':')[1].strip()
return hostname def pnull(n,str):
sn = len(str)
pn = n - sn
if pn <=0:
pn = 1
return ' ' * pn def main():
print 'CTID STATUS IP_ADDR HOSTNAME'
for id in getAllID():
stat = getStat(id)
ip = getIP(id)
hostname = getHostName(id)
print id + pnull(10,id) + stat + pnull(10,stat) + ip + pnull(16,ip) + hostname + pnull(10,hostname) if __name__ == '__main__':
main()

文件命令为vzlist

cat /usr/bin/vzlist

赋予执行权限,在任何地方都可以执行

05-11 23:01