示例#1
0
    def _execute(self):
        # type: () -> RawAgentData
        if self._use_only_cache:
            raise MKAgentError(
                "Got no data: No usable cache file present at %s" %
                self._cache_file_path())

        verify_ipaddress(self._ipaddress)
        assert self._ipaddress

        with TCPDataFetcher(
                socket.AF_INET6
                if self._host_config.is_ipv6_primary else socket.AF_INET,
            (self._ipaddress, self.port),
                self.timeout,
                self._host_config.agent_encryption,
        ) as fetcher:
            output = fetcher.data()
            if not output:  # may be caused by xinetd not allowing our address
                raise MKEmptyAgentData("Empty output from agent at %s:%d" %
                                       (self._ipaddress, self.port))
            if len(output) < 16:
                raise MKAgentError("Too short output from agent: %r" % output)
            return output
        raise MKAgentError("Failed to read data")
示例#2
0
    def _execute(self):
        # type: () -> RawAgentData
        if self._use_only_cache:
            raise MKAgentError(
                "Got no data: No usable cache file present at %s" %
                self._cache_file_path())

        self._verify_ipaddress()

        output = self._fetch_raw_data(
            socket.socket(
                socket.AF_INET6 if self._host_config.is_ipv6_primary else
                socket.AF_INET, socket.SOCK_STREAM), (
                    self._ipaddress,
                    self.port,
                ), self.timeout, self._logger)

        if not output:  # may be caused by xinetd not allowing our address
            raise MKEmptyAgentData("Empty output from agent at TCP port %s" %
                                   self.port)

        if len(output) < 16:
            raise MKAgentError("Too short output from agent: %r" % output)

        output = self._decrypt(output, self._host_config.agent_encryption)
        return output
示例#3
0
文件: tcp.py 项目: surajrb/checkmk
    def _raw_data(self):
        # type: () -> RawAgentData
        self._logger.debug("Reading data from agent")
        if not self._socket:
            return b""

        def recvall(sock):
            # type: (socket.socket) -> bytes
            buffer = []  # type: List[bytes]
            while True:
                data = sock.recv(4096, socket.MSG_WAITALL)
                if not data:
                    break
                buffer.append(data)
            return b"".join(buffer)

        try:
            output = recvall(self._socket)
            if not output:  # may be caused by xinetd not allowing our address
                raise MKEmptyAgentData("Empty output from agent at %s:%d" %
                                       self._address)
            return output
        except socket.error as e:
            if cmk.utils.debug.enabled():
                raise
            raise MKAgentError("Communication failed: %s" % e)
示例#4
0
    def _execute(self):
        # type: () -> RawAgentData
        if self._use_only_cache:
            raise MKAgentError(
                "Got no data: No usable cache file present at %s" %
                self._cache_file_path())

        self._verify_ipaddress()

        port = self._get_port()

        encryption_settings = self._host_config.agent_encryption

        socktype = (socket.AF_INET6
                    if self._host_config.is_ipv6_primary else socket.AF_INET)
        s = socket.socket(socktype, socket.SOCK_STREAM)

        timeout = self._get_timeout()

        output_lines = []  # type: List[bytes]
        self._logger.debug("Connecting via TCP to %s:%d (%ss timeout)" %
                           (self._ipaddress, port, timeout))
        try:
            s.settimeout(timeout)
            s.connect((self._ipaddress, port))
            s.settimeout(None)

            self._logger.debug("Reading data from agent")

            while True:
                data = s.recv(4096, socket.MSG_WAITALL)

                if data and len(data) > 0:
                    output_lines.append(data)
                else:
                    break

        except socket.error as e:
            if cmk.utils.debug.enabled():
                raise
            raise MKAgentError("Communication failed: %s" % e)
        finally:
            s.close()

        output = b''.join(output_lines)

        if len(output
               ) == 0:  # may be caused by xinetd not allowing our address
            raise MKEmptyAgentData("Empty output from agent at TCP port %d" %
                                   port)

        if len(output) < 16:
            raise MKAgentError("Too short output from agent: %r" % output)

        output_is_plaintext = output.startswith(b"<<<")
        if encryption_settings[
                "use_regular"] == "enforce" and output_is_plaintext:
            raise MKAgentError(
                "Agent output is plaintext but encryption is enforced by configuration"
            )
        if not output_is_plaintext and encryption_settings["use_regular"] in [
                "enforce", "allow"
        ]:
            try:
                # simply check if the protocol is an actual number
                protocol = int(output[0:2])

                output = self._decrypt_package(
                    output[2:], encryption_settings["passphrase"], protocol)
            except ValueError:
                raise MKAgentError("Unsupported protocol version: %s" %
                                   str(output[:2]))
            except Exception as e:
                if encryption_settings["use_regular"] == "enforce":
                    raise MKAgentError("Failed to decrypt agent output: %s" %
                                       e)

                # of course the package might indeed have been encrypted but
                # in an incorrect format, but how would we find that out?
                # In this case processing the output will fail

        return output
示例#5
0
 def test_with_MKEmptyAgentData_exception(self, source):
     assert source.summarize(result.Error(MKEmptyAgentData())) == (2,
                                                                   "(!!)",
                                                                   [])
示例#6
0
 def test_with_MKEmptyAgentData_exception(self, source):
     source.exception = MKEmptyAgentData()
     assert source.get_summary_result() == (2, "(!!)", [])