Esempio n. 1
0
    def _run(self):
        """
        Continuous run loop for virt-who on AHV.
        Args:
            None.
        Returns:
            None.
        """
        self.prepare()
        next_update = time()
        initial = True
        wait_result = None
        while self._oneshot or not self.is_terminated():

            delta = next_update - time()

            if initial:
                if self.status:
                    report = virt.StatusReport(self.config)
                    self._send_data(data_to_send=report)
                else:
                    assoc = self.getHostGuestMapping()
                    self._send_data(
                        data_to_send=virt.HostGuestAssociationReport(
                            self.config, assoc))
                initial = False
                continue

            if delta > 0:
                # Wait for update.
                wait_result = self._wait_for_update(60 if initial else delta)
                if wait_result:
                    events = wait_result
                else:
                    events = []
            else:
                events = []

            if len(events) > 0 or delta > 0:
                if self.status:
                    self._send_data(
                        data_to_send=virt.StatusReport(self.config))
                else:
                    assoc = self.getHostGuestMapping()
                    self._send_data(
                        data_to_send=virt.HostGuestAssociationReport(
                            self.config, assoc))

            if self._oneshot:
                break
            else:
                next_update = time() + self.update_interval
Esempio n. 2
0
    def _run(self):
        self._prepare()

        self.hosts = defaultdict(virt.Hypervisor)
        self.vms = defaultdict(virt.Guest)
        next_update = time()
        initial = True
        token = ''

        while self._oneshot or not self.is_terminated():
            delta = next_update - time()
            if initial or delta > 0:
                # Wait for update
                wait_result = self._wait(token, 60 if initial else delta)
                if wait_result:
                    events = wait_result['events']
                    token = wait_result['token']
                else:
                    events = []
                    token = ''
            else:
                events = []

            if initial or len(events) > 0 or delta > 0:
                assoc = self.getHostGuestMapping()
                self._send_data(
                    virt.HostGuestAssociationReport(self.config, assoc))
                initial = False

            if self._oneshot:
                break
            else:
                next_update = time() + self.interval

        self.cleanup()
Esempio n. 3
0
    def _run(self):
        self._prepare()

        version = ''
        last_version = 'last_version'  # Bogus value so version != last_version from the start
        self.hosts = defaultdict(Host)
        self.vms = defaultdict(VM)
        self.clusters = defaultdict(Cluster)
        next_update = time()
        options = {'maxWaitSeconds': 0}

        while self._oneshot or not self.is_terminated():

            if version == '':
                # also, clean all data we have
                self.hosts.clear()
                self.vms.clear()

            try:
                # Make sure that WaitForUpdatesEx finishes even
                # if the ESX shuts down in the middle of waiting
                self.client.set_options(timeout=self.MAX_WAIT_TIME)
                self.logger.debug("calling esx service")
                updateSet = self.client.service.WaitForUpdatesEx(
                    _this=self.sc.propertyCollector,
                    version=version,
                    options=options)
            except (socket.error, URLError, requests.exceptions.Timeout):
                self.logger.debug("Wait for ESX event finished, timeout")
                self._cancel_wait()
                # Get the initial update again
                version = ''
                continue
            except (suds.WebFault, HTTPException) as e:
                suppress_exception = False
                try:
                    if hasattr(e, 'fault'):
                        if e.fault.faultstring == 'The session is not authenticated.':
                            # Do not print the exception if we get 'not authenticated',
                            # it's quite normal behaviour and nothing to worry about
                            suppress_exception = True
                        if e.fault.faultstring == 'The task was canceled by a user.':
                            # Do not print the exception if we get 'canceled by user',
                            # this happens when the wait is terminated when
                            # virt-who is being stopped
                            continue
                except Exception:
                    pass
                if not suppress_exception:
                    self.logger.exception("Waiting for ESX events fails:")
                self._cancel_wait()
                version = ''
                self._prepare()
                continue

            if updateSet is not None:
                version = updateSet.version
                self.applyUpdates(updateSet)

            if hasattr(updateSet, 'truncated') and updateSet.truncated:
                continue

            if last_version != version or time() > next_update:
                assoc = self.getHostGuestMapping()
                self._send_data(
                    virt.HostGuestAssociationReport(self.config, assoc))
                next_update = time() + self.interval
                last_version = version

            if self._oneshot:
                break
            else:
                self.wait(self.interval)

        self.cleanup()