Module: check_mk
Branch: master
Commit: 1fae4027129ebfddd659313dfd60f1acb03c87f6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=1fae4027129ebf…
Author: Tom Baerwinkel <tb(a)mathias-kettner.de>
Date: Thu Dec 7 13:20:40 2017 +0100
Fix test and improve exception handling and data collection in tcp.py
Change-Id: Ibd5bb2d88357dcf3e7cba70e1a088e74094a8db8
---
cmk_base/data_sources/tcp.py | 31 +++++++++++--------------------
tests/cmk_base/test_data_sources.py | 2 +-
2 files changed, 12 insertions(+), 21 deletions(-)
diff --git a/cmk_base/data_sources/tcp.py b/cmk_base/data_sources/tcp.py
index f5053e0..f827855 100644
--- a/cmk_base/data_sources/tcp.py
+++ b/cmk_base/data_sources/tcp.py
@@ -91,36 +91,27 @@ class TCPDataSource(CheckMKAgentDataSource):
encryption_settings = config.agent_encryption_of(hostname)
- s = socket.socket(config.is_ipv6_primary(hostname) and socket.AF_INET6 or
socket.AF_INET,
- socket.SOCK_STREAM)
+ socktype = (socket.AF_INET6 if config.is_ipv6_primary(hostname)
+ else socket.AF_INET)
+ s = socket.socket(socktype, socket.SOCK_STREAM)
timeout = self._get_timeout(hostname)
s.settimeout(timeout)
+ output = []
self._logger.debug("[%s] Connecting via TCP to %s:%d (%ss timeout)" %
(self.id(), ipaddress, port, s.gettimeout()))
- s.connect((ipaddress, port))
- # Immediately close sending direction. We do not send any data
- # s.shutdown(socket.SHUT_WR)
- try:
- s.setblocking(1)
- except:
- pass
- output = ""
try:
+ s.connect((ipaddress, port))
while True:
- out = s.recv(4096, socket.MSG_WAITALL)
- if out and len(out) > 0:
- output += out
+ data = s.recv(4096, socket.MSG_WAITALL)
+
+ if data and len(data) > 0:
+ output.append(data)
else:
break
- except Exception, e:
- # Python seems to skip closing the socket under certain
- # conditions, leaving open filedescriptors and sockets in
- # CLOSE_WAIT. This happens one a timeout (ALERT signal)
+ finally:
s.close()
- raise
-
- s.close()
+ output = ''.join(output)
if len(output) == 0: # may be caused by xinetd not allowing our address
raise MKAgentError("Empty output from agent at TCP port %d" %
port)
diff --git a/tests/cmk_base/test_data_sources.py b/tests/cmk_base/test_data_sources.py
index bf2af67..cba1986 100644
--- a/tests/cmk_base/test_data_sources.py
+++ b/tests/cmk_base/test_data_sources.py
@@ -369,7 +369,7 @@ def test_automation_discovery_caching(test_cfg, scan, cache,
raise_errors, monke
def test_automation_diag_host_caching(test_cfg, monkeypatch):
_patch_data_source_run(monkeypatch)
- args = ["ds-test-host1", "agent", "127.0.0.1", None,
6557, 10, 5, None ]
+ args = ["ds-test-host1", "agent", "127.0.0.1", None,
6557, 10, 5, 5, None ]
cmk_base.automations.check_mk.AutomationDiagHost().execute(args)
assert _counter_run == 2