def test_connect_not_socket(self): client = TelegrafUnixSocketClient("/foo/bar") try: client.connect() raise Exception("TelegrafUnixSocketClientException not raised") except TelegrafUnixSocketClientException: pass
def test_connect(self): orig_constructor = socket.socket socket.socket = functools.partial(fake_socket_constructor, FakeSocketObject) client = TelegrafUnixSocketClient("/foo/bar") client.connect(bypass_unix_socket_check=True) client.close() socket.socket = orig_constructor self.assertTrue(client._sock is None)
def test_send1(self): orig_constructor = socket.socket socket.socket = functools.partial(fake_socket_constructor, FakeSocketObject) client = TelegrafUnixSocketClient("/foo/bar") client.connect(bypass_unix_socket_check=True) client.send_measurement("foo", {"field1": 1.23, "field2": 4.56}) self.assertEquals(client._sock.sended, b'foo field1=1.23,field2=4.56\n') client.close() socket.socket = orig_constructor
return result def get_file_count(directory): cmd = "find %s -type f 2>/dev/null |wc -l" % directory x = BashWrapper(cmd) if x: return int(x.stdout) while True: LOGGER.debug("waiting 10s...") time.sleep(10) client = TelegrafUnixSocketClient(SOCKET_PATH) try: client.connect() except Exception: LOGGER.warning("can't connect, wait 10s and try again...") continue var_in_directories = get_var_in_directories() data = {} for directory in var_in_directories: count = None try: count = get_file_count(directory) except Exception: pass if count is None: continue data["file_count"] = count msg = client.send_measurement(