import json
from collections import OrderedDict # so that the timestamp is first entry

# LOG = common.get_logger("block_info")

try:
    conn = libvirt.openReadOnly()
    if conn is None:
        raise Exception('Failed to open connection to the hypervisor')

    for id in conn.listDomainsID():
        dom = conn.lookupByID(id)

        block_info = {}
        block_info = {"devices": []}
        for dev in common.domain_xml(dom).findall("devices/disk/target"):
            devname = dev.get("dev")
            block_info["devices"].append(devname)
            info = dom.blockInfo(devname)
            block_info[devname] = {
                # logical size in bytes of the image
                # (how much storage the guest will see)
                "capacity": info[0],

                # host storage in bytes occupied by
                # the image (such as highest allocated
                # extent if there are no holes, similar to 'du')
                "allocation": info[1],

                # host physical size in bytes of the
                # image container (last offset, similar to 'ls')
import libvirt
import json

# LOG = common.get_logger("interface_stats")

try:
    conn = libvirt.openReadOnly()
    if conn is None:
        raise Exception('Failed to open connection to the hypervisor')

    for id in conn.listDomainsID():
        dom = conn.lookupByID(id)

        interface_stats = {}
        interface_stats = {"devices": []}
        for dev in common.domain_xml(dom).findall("devices/interface/target"):
            devname = dev.get("dev")
            interface_stats["devices"].append(devname)
            stats = dom.interfaceStats(devname)
            interface_stats[devname] = {
                "rx_bytes": stats[0],
                "rx_packets": stats[1],
                "rx_errs": stats[2],
                "rx_drop": stats[3],
                "tx_bytes": stats[4],
                "tx_packets": stats[5],
                "tx_errs": stats[6],
                "tx_drop": stats[7]
            }
            print json.dumps({
                "timestamp": common.now(),