Exemple #1
0
    def run(self, hostname=None, ipaddress=None, get_raw_data=False):
        """Wrapper for self._execute() that unifies several things:

        a) Exception handling
        b) Caching of raw data
        c) CPU tracking

        Exceptions: All exceptions are caught and written to self._exception. The caller
        should use self.get_summary_result() to get the summary result of this data source
        which also includes information about the happed exception. In case the --debug
        mode is enabled, the exceptions are raised. self._exception is re-initialized
        to None when this method is called.

        Both hostname and ipaddress are optional, used for virtual
        Check_MK clusters."""

        if hostname is not None:
            self._hostname = hostname
        if ipaddress is not None:
            self._ipaddress = ipaddress

        self._exception = None
        self._host_sections = None
        self._persisted_sections = None

        try:
            cpu_tracking.push_phase(self._cpu_tracking_id())

            persisted_sections_from_disk = self._load_persisted_sections()
            self._persisted_sections = persisted_sections_from_disk

            raw_data, is_cached_data = self._get_raw_data()

            self._host_sections = host_sections = self._convert_to_sections(raw_data)
            assert isinstance(host_sections, HostSections)

            if get_raw_data:
                return raw_data

            # Add information from previous persisted infos
            host_sections = self._update_info_with_persisted_sections(persisted_sections_from_disk,
                                                                      host_sections, is_cached_data)
            self._persisted_sections = host_sections.persisted_sections

            return host_sections

        except MKTerminate:
            raise

        except Exception as e:
            self._logger.log(VERBOSE, "ERROR: %s", e)
            if cmk.utils.debug.enabled():
                raise
            self._exception = e
        finally:
            cpu_tracking.pop_phase()

        if get_raw_data:
            return ""
        return HostSections()
def test_cpu_tracking_add_times(monkeypatch):
    monkeypatch.setattr("time.time", lambda: 0.0)
    cpu_tracking.start("busy")
    monkeypatch.setattr("time.time", lambda: 2.0)

    cpu_tracking.push_phase("agent")
    monkeypatch.setattr("time.time", lambda: 5.0)
    cpu_tracking.pop_phase()

    cpu_tracking.push_phase("agent")
    monkeypatch.setattr("time.time", lambda: 7.0)
    cpu_tracking.pop_phase()

    cpu_tracking.end()

    times = cpu_tracking.get_times()
    assert len(times) == 3

    assert times["TOTAL"][4] == 7.0
    assert times["busy"][4] == 2.0
    assert times["agent"][4] == 5.0
def test_pop_without_tracking():
    assert cpu_tracking._is_not_tracking()
    cpu_tracking.pop_phase()
    assert cpu_tracking._is_not_tracking()