(self)
Collect libvirt data
Collect libvirt data
Collect libvirt data
def collect(self):
        """
        Collect libvirt data
        """
        if libvirt is None:
            self.log.error('Unable to import either libvirt')
            return {}
        # Open a restricted (non-root) connection to the hypervisor
        conn = libvirt.openReadOnly(None)
        # Get hardware info
        conninfo = conn.getInfo()
        # Initialize variables
        memallocated = 0
        coresallocated = 0
        totalcores = 0
        results = {}
        domIds = conn.listDomainsID()
        if 0 in domIds:
            # Total cores
            domU = conn.lookupByID(0)
            totalcores = domU.info()[3]
        # Free Space
        s = os.statvfs('/')
        freeSpace = (s.f_bavail * s.f_frsize) / 1024
        # Calculate allocated memory and cores
        for i in domIds:
            # Ignore 0
            if i == 0:
                continue
            domU = conn.lookupByID(i)
            dominfo = domU.info()
            memallocated += dominfo[2]
            if i > 0:
                coresallocated += dominfo[3]
        results = {
            'InstalledMem': conninfo[1],
            'MemAllocated': memallocated / 1024,
            'MemFree': conninfo[1] - (memallocated / 1024),
            'AllocatedCores': coresallocated,
            'DiskFree': freeSpace,
            'TotalCores': totalcores,
            'FreeCores': (totalcores - coresallocated)
        }
        for k in results.keys():
            self.publish(k, results[k], 0)
def collect ( self ) : if libvirt is None : self . log . error ( 'Unable to import either libvirt' ) return { } # Open a restricted (non-root) connection to the hypervisor conn = libvirt . openReadOnly ( None ) # Get hardware info conninfo = conn . getInfo ( ) # Initialize variables memallocated = 0 coresallocated = 0 totalcores = 0 results = { } domIds = conn . listDomainsID ( ) if 0 in domIds : # Total cores domU = conn . lookupByID ( 0 ) totalcores = domU . info ( ) [ 3 ] # Free Space s = os . statvfs ( '/' ) freeSpace = ( s . f_bavail * s . f_frsize ) / 1024 # Calculate allocated memory and cores for i in domIds : # Ignore 0 if i == 0 : continue domU = conn . lookupByID ( i ) dominfo = domU . info ( ) memallocated += dominfo [ 2 ] if i > 0 : coresallocated += dominfo [ 3 ] results = { 'InstalledMem' : conninfo [ 1 ] , 'MemAllocated' : memallocated / 1024 , 'MemFree' : conninfo [ 1 ] - ( memallocated / 1024 ) , 'AllocatedCores' : coresallocated , 'DiskFree' : freeSpace , 'TotalCores' : totalcores , 'FreeCores' : ( totalcores - coresallocated ) } for k in results . keys ( ) : self . publish ( k , results [ k ] , 0 )