proxmox新版本使用了lxc容器,導致以前的vzlist命令無法使用,於是自己寫了一個腳本來獲取所有半虛擬化主機的信息狀態


#!/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

賦予執行權限,在任何地方都可以執行 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM