#!/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
賦予執行權限,在任何地方都可以執行