zoukankan      html  css  js  c++  java
  • 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('
    '):
            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('
    '):
            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

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

  • 相关阅读:
    shell 10流程控制
    shell 9test命令
    shell 8字符串与文件内容处理
    shell 7输入输出
    shell 6基本运算符
    JS-JQ实现TAB选项卡
    JS-JQ实现页面滚动时元素智能定位(顶部-其他部位)
    js获取框架(IFrame)的内容
    codeforces 660C C. Hard Process(二分)
    codeforces 660B B. Seating On Bus(模拟)
  • 原文地址:https://www.cnblogs.com/djoker/p/8459226.html
Copyright © 2011-2022 走看看