めもめも

このブログに記載の内容は個人の見解であり、必ずしも所属組織の立場、戦略、意見を代表するものではありません。

KVMの仮想ネットワーク接続状況を表示するスクリプト

libvirt の python API リファレンスが無い。。。。。でもがんばりました。

VM中心の表示(tapinfo.py)とブリッジ中心の表示(brinfo.py)を分けました。

[root@rhel55kvm ~]# ./tapinfo.py
Domain           Tap          MAC Address        Network      Bridge
------------------------------------------------------------------------------
rhel55-vm01      vnet0        54:52:00:4a:2e:8b  default      virbr0
rhel55-vm01      vnet1        54:52:00:3d:cb:db               br101
rhel55-vm01      vnet2        54:52:00:00:cf:0c               br0
------------------------------------------------------------------------------
rhel60-vm01      vnet3        54:52:00:34:7f:36  default      virbr0

[root@rhel55kvm ~]# ./brinfo.py
Bridge       Netowrk      Device       Domain           MAC Address
------------------------------------------------------------------------------
br0                       vnet2        rhel55-vm01      54:52:00:00:cf:0c
                          eth0
------------------------------------------------------------------------------
br101                     vnet1        rhel55-vm01      54:52:00:3d:cb:db
                          eth1.101
------------------------------------------------------------------------------
br102                     eth1.102
------------------------------------------------------------------------------
virbr0       default      vnet0        rhel55-vm01      54:52:00:4a:2e:8b
                          vnet3        rhel60-vm01      54:52:00:34:7f:36
------------------------------------------------------------------------------
virbr1       network01
------------------------------------------------------------------------------
virbr2       network02
------------------------------------------------------------------------------
virbr3       network03
------------------------------------------------------------------------------
virbr4       network04
------------------------------------------------------------------------------
virbr5       network05
#!/usr/bin/python
#
#   tapinfo.py : List TAP device info for running VMs
#
#   2010/01/20 ver1.0
#   2010/01/21 ver1.1
#
# Copyright (C) 2010 Etsuji Nakai
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import libvirt
import os
import re
from xml.dom import minidom

Conn = libvirt.open( "qemu:///system" )

def showTaps():
    vnetRe = re.compile( "vnet\d+" )
    fmt = "%-16s %-12s %-18s %-12s %-12s"
    print fmt % ( "Domain", "Tap", "MAC Address", "Network", "Bridge" )
    for id in Conn.listDomainsID():
        print "-" * 78
        vm = Conn.lookupByID( id )
        vmXMLDesc = minidom.parseString( vm.XMLDesc( 0 ) )
        for iface in vmXMLDesc.getElementsByTagName( "interface" ):
            ifaceType = iface.getAttribute( "type" )
            if ifaceType == "network":
                network = iface.getElementsByTagName( "source" )[0].getAttribute( "network" )
                netXMLDesc = minidom.parseString( Conn.networkLookupByName( network ).XMLDesc( 0 ) )
                bridge = netXMLDesc.getElementsByTagName( "bridge" )[ 0 ].getAttribute( "name" )
            elif ifaceType == "bridge":
                network = ""
                bridge = iface.getElementsByTagName( "source" )[0].getAttribute( "bridge" )
            mac = iface.getElementsByTagName( "mac" )[0].getAttribute( "address" )
            device = iface.getElementsByTagName( "target" )[0].getAttribute( "dev" )
            print fmt % ( vm.name(), device, mac, network, bridge )

if __name__ == "__main__":
    showTaps()
    print
#!/usr/bin/python
#
#   brinfo.py :  List bridge info for running VMs
#
#   2011/01/24 ver1.0	
#   2011/01/27 ver1.1   some golfing...
#   2012/05/05 ver1.2   modifed regular expression for RHEL6
#
# Copyright (C) 2010 Etsuji Nakai
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import libvirt
import os
import re
import commands
from xml.dom import minidom

Conn = libvirt.open( "qemu:///system" )
VmName, NetName, MacAddr = {}, {}, {}

def getTaps():
    for id in Conn.listDomainsID():
        vm = Conn.lookupByID( id )
        vmXMLDesc = minidom.parseString( vm.XMLDesc( 0 ) )
        for iface in vmXMLDesc.getElementsByTagName( "interface" ):
            tapDevice = iface.getElementsByTagName( "target" )[0].getAttribute( "dev" )
            VmName[ tapDevice ] = vm.name()
            MacAddr[ tapDevice ] = iface.getElementsByTagName( "mac" )[0].getAttribute( "address" )

def getNetworks():
    for net in Conn.listNetworks():
        network = Conn.networkLookupByName( net )
        NetName[ network.bridgeName() ] = net

def showBridges():
    fmt = "%-12s %-12s %-12s %-16s %-18s"
    bridgeAndDeviceRe = re.compile( "^(\S+)?\s+.*\s+(\S+)?$" )

    print fmt % ( "Bridge", "Netowrk", "Device", "Domain", "MAC Address" )

    brshow = commands.getoutput( "/usr/sbin/brctl show" ).split( "\n" )
    brshow.pop(0)
    for line in brshow:
        ( bridge, device ) = bridgeAndDeviceRe.search( line ).groups()
        if bridge == None: bridge = ""
        if device == None: device = ""

        network = NetName.get( bridge, "" )
        vm = VmName.get( device, "" )
        mac = MacAddr.get( device, "" )

        if bridge != "": print "-" * 78
        print fmt % ( bridge, network, device, vm, mac )


if __name__ == "__main__":
    getTaps()
    getNetworks()
    showBridges()
    print


※ 補足です。libvirt のクラスとメソッド一覧はここにありました。
/usr/share/doc/libvirt-python-0.6.3/libvirtclass.txt

# pydoc libvirt でもいいです。