Created
April 27, 2017 11:07
-
-
Save anastop/e533628e5624c81b8822fda512aefaab to your computer and use it in GitHub Desktop.
Parse XML Libvirt Domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/xml" | |
"fmt" | |
"strconv" | |
"time" | |
) | |
func parseDomCfg(domcfg []byte) (dompid int, vcpupids []int, err error) { | |
type Vcpu struct { | |
Id string `xml:"id, attr"` | |
Pid string `xml:"pid,attr"` | |
} | |
type Domstatus struct { | |
XMLName xml.Name `xml:"domstatus"` | |
Pid string `xml:"pid,attr"` | |
Vcpus []Vcpu `xml:"vcpus>vcpu"` | |
} | |
var ds Domstatus | |
err = xml.Unmarshal(domcfg, &ds) | |
dompid, _ = strconv.Atoi(ds.Pid) | |
for _, vcpu := range ds.Vcpus { | |
pid, _ := strconv.Atoi(vcpu.Pid) | |
vcpupids = append(vcpupids, pid) | |
} | |
return dompid, vcpupids, err | |
} | |
var cfgstr string = `<!-- | |
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE | |
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: | |
virsh edit DPI | |
or other application using the libvirt API. | |
--> | |
<domstatus state='running' reason='booted' pid='81393'> | |
<monitor path='/var/lib/libvirt/qemu/domain-DPI/monitor.sock' json='1' type='unix'/> | |
<vcpus> | |
<vcpu pid='81397'/> | |
<vcpu pid='81398'/> | |
<vcpu pid='81399'/> | |
<vcpu pid='81400'/> | |
<vcpu pid='81401'/> | |
<vcpu pid='81402'/> | |
<vcpu pid='81403'/> | |
<vcpu pid='81404'/> | |
<vcpu pid='81405'/> | |
<vcpu pid='81406'/> | |
<vcpu pid='81407'/> | |
<vcpu pid='81408'/> | |
<vcpu pid='81409'/> | |
<vcpu pid='81410'/> | |
<vcpu pid='81411'/> | |
<vcpu pid='81412'/> | |
<vcpu pid='81413'/> | |
<vcpu pid='81414'/> | |
<vcpu pid='81415'/> | |
<vcpu pid='81416'/> | |
<vcpu pid='81417'/> | |
<vcpu pid='81418'/> | |
<vcpu pid='81419'/> | |
<vcpu pid='81420'/> | |
</vcpus> | |
<qemuCaps> | |
<flag name='kvm'/> | |
<flag name='mem-path'/> | |
<flag name='drive-serial'/> | |
<flag name='chardev'/> | |
<flag name='monitor-json'/> | |
<flag name='balloon'/> | |
<flag name='device'/> | |
<flag name='sdl'/> | |
<flag name='smp-topology'/> | |
<flag name='netdev'/> | |
<flag name='rtc'/> | |
<flag name='vhost-net'/> | |
<flag name='no-hpet'/> | |
<flag name='no-kvm-pit'/> | |
<flag name='pci-configfd'/> | |
<flag name='nodefconfig'/> | |
<flag name='boot-menu'/> | |
<flag name='fsdev'/> | |
<flag name='name-process'/> | |
<flag name='drive-readonly'/> | |
<flag name='smbios-type'/> | |
<flag name='vga-qxl'/> | |
<flag name='spice'/> | |
<flag name='vga-none'/> | |
<flag name='boot-index'/> | |
<flag name='hda-duplex'/> | |
<flag name='drive-aio'/> | |
<flag name='pci-multibus'/> | |
<flag name='pci-bootindex'/> | |
<flag name='ccid-emulated'/> | |
<flag name='ccid-passthru'/> | |
<flag name='chardev-spicevmc'/> | |
<flag name='virtio-tx-alg'/> | |
<flag name='device-qxl-vga'/> | |
<flag name='pci-multifunction'/> | |
<flag name='virtio-blk-pci.ioeventfd'/> | |
<flag name='sga'/> | |
<flag name='virtio-blk-pci.event_idx'/> | |
<flag name='virtio-net-pci.event_idx'/> | |
<flag name='cache-directsync'/> | |
<flag name='piix3-usb-uhci'/> | |
<flag name='piix4-usb-uhci'/> | |
<flag name='usb-ehci'/> | |
<flag name='ich9-usb-ehci1'/> | |
<flag name='vt82c686b-usb-uhci'/> | |
<flag name='pci-ohci'/> | |
<flag name='usb-redir'/> | |
<flag name='usb-hub'/> | |
<flag name='no-shutdown'/> | |
<flag name='cache-unsafe'/> | |
<flag name='rombar'/> | |
<flag name='ich9-ahci'/> | |
<flag name='no-acpi'/> | |
<flag name='fsdev-readonly'/> | |
<flag name='virtio-blk-pci.scsi'/> | |
<flag name='blk-sg-io'/> | |
<flag name='drive-copy-on-read'/> | |
<flag name='cpu-host'/> | |
<flag name='fsdev-writeout'/> | |
<flag name='drive-iotune'/> | |
<flag name='system_wakeup'/> | |
<flag name='scsi-disk.channel'/> | |
<flag name='scsi-block'/> | |
<flag name='transaction'/> | |
<flag name='block-job-async'/> | |
<flag name='scsi-cd'/> | |
<flag name='ide-cd'/> | |
<flag name='no-user-config'/> | |
<flag name='hda-micro'/> | |
<flag name='dump-guest-memory'/> | |
<flag name='nec-usb-xhci'/> | |
<flag name='balloon-event'/> | |
<flag name='bridge'/> | |
<flag name='lsi'/> | |
<flag name='virtio-scsi-pci'/> | |
<flag name='blockio'/> | |
<flag name='disable-s3'/> | |
<flag name='disable-s4'/> | |
<flag name='usb-redir.filter'/> | |
<flag name='ide-drive.wwn'/> | |
<flag name='scsi-disk.wwn'/> | |
<flag name='seccomp-sandbox'/> | |
<flag name='reboot-timeout'/> | |
<flag name='dump-guest-core'/> | |
<flag name='seamless-migration'/> | |
<flag name='block-commit'/> | |
<flag name='vnc'/> | |
<flag name='drive-mirror'/> | |
<flag name='usb-redir.bootindex'/> | |
<flag name='usb-host.bootindex'/> | |
<flag name='blockdev-snapshot-sync'/> | |
<flag name='qxl'/> | |
<flag name='VGA'/> | |
<flag name='cirrus-vga'/> | |
<flag name='vmware-svga'/> | |
<flag name='device-video-primary'/> | |
<flag name='usb-serial'/> | |
<flag name='usb-net'/> | |
<flag name='add-fd'/> | |
<flag name='nbd-server'/> | |
<flag name='virtio-rng'/> | |
<flag name='rng-random'/> | |
<flag name='rng-egd'/> | |
<flag name='dtb'/> | |
<flag name='megasas'/> | |
<flag name='ipv6-migration'/> | |
<flag name='machine-opt'/> | |
<flag name='machine-usb-opt'/> | |
<flag name='tpm-passthrough'/> | |
<flag name='tpm-tis'/> | |
<flag name='pci-bridge'/> | |
<flag name='vfio-pci'/> | |
<flag name='vfio-pci.bootindex'/> | |
<flag name='scsi-generic'/> | |
<flag name='scsi-generic.bootindex'/> | |
<flag name='mem-merge'/> | |
<flag name='vnc-websocket'/> | |
<flag name='drive-discard'/> | |
<flag name='mlock'/> | |
<flag name='vnc-share-policy'/> | |
<flag name='device-del-event'/> | |
<flag name='dmi-to-pci-bridge'/> | |
<flag name='i440fx-pci-hole64-size'/> | |
<flag name='q35-pci-hole64-size'/> | |
<flag name='usb-storage'/> | |
<flag name='usb-storage.removable'/> | |
<flag name='virtio-mmio'/> | |
<flag name='ich9-intel-hda'/> | |
<flag name='kvm-pit-lost-tick-policy'/> | |
<flag name='boot-strict'/> | |
<flag name='pvpanic'/> | |
<flag name='spice-file-xfer-disable'/> | |
<flag name='spiceport'/> | |
<flag name='usb-kbd'/> | |
<flag name='host-pci-multidomain'/> | |
<flag name='msg-timestamp'/> | |
<flag name='active-commit'/> | |
<flag name='change-backing-file'/> | |
<flag name='memory-backend-ram'/> | |
<flag name='numa'/> | |
<flag name='memory-backend-file'/> | |
<flag name='usb-audio'/> | |
<flag name='rtc-reset-reinjection'/> | |
<flag name='splash-timeout'/> | |
<flag name='iothread'/> | |
<flag name='migrate-rdma'/> | |
<flag name='ivshmem'/> | |
<flag name='drive-iotune-max'/> | |
<flag name='VGA.vgamem_mb'/> | |
<flag name='vmware-svga.vgamem_mb'/> | |
<flag name='qxl.vgamem_mb'/> | |
<flag name='qxl-vga.vgamem_mb'/> | |
<flag name='pc-dimm'/> | |
<flag name='machine-vmport-opt'/> | |
<flag name='aes-key-wrap'/> | |
<flag name='dea-key-wrap'/> | |
<flag name='pci-serial'/> | |
<flag name='vhost-user-multiqueue'/> | |
<flag name='migration-event'/> | |
<flag name='ioh3420'/> | |
<flag name='x3130-upstream'/> | |
<flag name='xio3130-downstream'/> | |
<flag name='rtl8139'/> | |
<flag name='e1000'/> | |
<flag name='virtio-net'/> | |
<flag name='gic-version'/> | |
<flag name='incoming-defer'/> | |
<flag name='virtio-gpu'/> | |
<flag name='virtio-keyboard'/> | |
<flag name='virtio-mouse'/> | |
<flag name='virtio-tablet'/> | |
<flag name='virtio-input-host'/> | |
<flag name='ich9-disable-s3'/> | |
<flag name='ich9-disable-s4'/> | |
<flag name='vserport-change-event'/> | |
<flag name='virtio-balloon-pci.deflate-on-oom'/> | |
</qemuCaps> | |
<devices> | |
<device alias='sound0-codec0'/> | |
<device alias='virtio-disk0'/> | |
<device alias='virtio-serial0'/> | |
<device alias='video0'/> | |
<device alias='serial0'/> | |
<device alias='balloon0'/> | |
<device alias='sound0'/> | |
<device alias='channel0'/> | |
<device alias='net0'/> | |
<device alias='usb'/> | |
</devices> | |
<domain type='kvm' id='3'> | |
<name>DPI</name> | |
<uuid>8b23110c-2735-4309-a882-e7bfb5e55a95</uuid> | |
<memory unit='KiB'>33554432</memory> | |
<currentMemory unit='KiB'>33554432</currentMemory> | |
<vcpu placement='static'>24</vcpu> | |
<resource> | |
<partition>/machine</partition> | |
</resource> | |
<os> | |
<type arch='x86_64' machine='pc-i440fx-xenial'>hvm</type> | |
</os> | |
<features> | |
<acpi/> | |
<apic/> | |
</features> | |
<cpu mode='custom' match='exact'> | |
<model fallback='allow'>Broadwell</model> | |
</cpu> | |
<clock offset='utc'> | |
<timer name='rtc' tickpolicy='catchup'/> | |
<timer name='pit' tickpolicy='delay'/> | |
<timer name='hpet' present='no'/> | |
</clock> | |
<on_poweroff>destroy</on_poweroff> | |
<on_reboot>restart</on_reboot> | |
<on_crash>restart</on_crash> | |
<pm> | |
<suspend-to-mem enabled='no'/> | |
<suspend-to-disk enabled='no'/> | |
</pm> | |
<devices> | |
<emulator>/usr/bin/kvm-spice</emulator> | |
<disk type='file' device='disk'> | |
<driver name='qemu' type='qcow2'/> | |
<source file='/store/images/nfvacc-test-mtcbench.img'/> | |
<backingStore/> | |
<target dev='vda' bus='virtio'/> | |
<boot order='1'/> | |
<alias name='virtio-disk0'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> | |
</disk> | |
<controller type='usb' index='0' model='ich9-ehci1'> | |
<alias name='usb'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/> | |
</controller> | |
<controller type='usb' index='0' model='ich9-uhci1'> | |
<alias name='usb'/> | |
<master startport='0'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0' multifunction='on'/> | |
</controller> | |
<controller type='usb' index='0' model='ich9-uhci2'> | |
<alias name='usb'/> | |
<master startport='2'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x1'/> | |
</controller> | |
<controller type='usb' index='0' model='ich9-uhci3'> | |
<alias name='usb'/> | |
<master startport='4'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x2'/> | |
</controller> | |
<controller type='pci' index='0' model='pci-root'> | |
<alias name='pci.0'/> | |
</controller> | |
<controller type='virtio-serial' index='0'> | |
<alias name='virtio-serial0'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> | |
</controller> | |
<interface type='network'> | |
<mac address='52:54:00:77:fe:d3'/> | |
<source network='nfvacc-odl-mtcb'/> | |
<actual type='network'> | |
<source bridge='virbr1'/> | |
</actual> | |
<target dev='vnet0'/> | |
<model type='virtio'/> | |
<alias name='net0'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> | |
</interface> | |
<serial type='pty'> | |
<source path='/dev/pts/2'/> | |
<target port='0'/> | |
<alias name='serial0'/> | |
</serial> | |
<console type='pty' tty='/dev/pts/2'> | |
<source path='/dev/pts/2'/> | |
<target type='serial' port='0'/> | |
<alias name='serial0'/> | |
</console> | |
<channel type='spicevmc'> | |
<target type='virtio' name='com.redhat.spice.0' state='disconnected'/> | |
<alias name='channel0'/> | |
<address type='virtio-serial' controller='0' bus='0' port='1'/> | |
</channel> | |
<input type='mouse' bus='ps2'/> | |
<input type='keyboard' bus='ps2'/> | |
<graphics type='spice' port='5900' autoport='yes' listen='127.0.0.1'> | |
<listen type='address' address='127.0.0.1' fromConfig='1'/> | |
<image compression='off'/> | |
</graphics> | |
<sound model='ich6'> | |
<alias name='sound0'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> | |
</sound> | |
<video> | |
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1'/> | |
<alias name='video0'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> | |
</video> | |
<memballoon model='virtio'> | |
<alias name='balloon0'/> | |
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/> | |
</memballoon> | |
</devices> | |
<seclabel type='dynamic' model='apparmor' relabel='yes'> | |
<label>libvirt-8b23110c-2735-4309-a882-e7bfb5e55a95</label> | |
<imagelabel>libvirt-8b23110c-2735-4309-a882-e7bfb5e55a95</imagelabel> | |
</seclabel> | |
</domain> | |
</domstatus> | |
` | |
func main() { | |
for i := 0; i < 240; i++ { | |
time.Sleep(1 * time.Second) | |
dompid, _, _ := parseDomCfg([]byte(cfgstr)) | |
fmt.Printf("%d %d\n", i, dompid) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment