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

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

  • 相关阅读:
    最近学习的 Node.js 之 http
    最近学习的 Node.js 基础:安装、环境配置、forever
    关于MySQL5.7 几天的总结(简单分区 & json类型)
    不会点git真不行啊.
    python爬虫基础_scrapy
    python爬虫基础_webwechat
    python爬虫基础_requests和bs4
    python之django母板页面
    python之django基础
    python网络之web框架
  • 原文地址:https://www.cnblogs.com/djoker/p/8459226.html
Copyright © 2011-2022 走看看