def test_connect_not_socket(self): client = TelegrafUnixSocketClient("/foo/bar") try: client.connect() raise Exception("TelegrafUnixSocketClientException not raised") except TelegrafUnixSocketClientException: pass
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
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)
if os.path.isdir(full_path): result.append(full_path) 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
from datetime import datetime from telegraf_unixsocket_client import TelegrafUnixSocketClient from mflog import getLogger MFMODULE_RUNTIME_HOME = os.environ["MFMODULE_RUNTIME_HOME"] SOCKET_PATH = os.path.join(MFMODULE_RUNTIME_HOME, "var", "telegraf.socket") LOGGER = getLogger("telegraf_collector_custom_diskio") old_read_bytes = None old_write_bytes = None old_dt = None first = True while True: LOGGER.debug("waiting 10s...") time.sleep(10) client = TelegrafUnixSocketClient(SOCKET_PATH) try: client.connect() except Exception: LOGGER.warning("can't connect to %s, wait 10s and try again...", SOCKET_PATH) continue stats = psutil.disk_io_counters(perdisk=False) new_dt = datetime.now() new_read_bytes = stats.read_bytes new_write_bytes = stats.write_bytes if first: first = False else: delta = (new_dt - old_dt).total_seconds() read_bytes_per_second = (new_read_bytes - old_read_bytes) / delta
if not results: LOGGER.warning("can't execute %s: %s" % (CMD, results)) return None lines = results.stdout.splitlines() stats = {x: 0 for x in STATES_VALUES} for line in lines[1:]: tmp = line.strip().split() if tmp[0] not in STATES_KEYS: stats["tcp_none"] = stats.get("tcp_none") + 1 continue stats[STATES[tmp[0]]] = stats.get(STATES[tmp[0]]) + 1 stats["tcp_total"] = sum(stats.values()) return stats while True: LOGGER.debug("waiting 10s...") time.sleep(10) client = TelegrafUnixSocketClient(SOCKET_PATH) try: client.connect() except Exception: LOGGER.warning("can't connect to %s, wait 10s and try again...", SOCKET_PATH) continue stats = get_stats() if stats: msg = client.send_measurement("custom_netstat", stats) LOGGER.debug("sended msg: %s" % msg) client.close()
stats[plugin][key] = sum([ x[key] for x in processes if x['plugin'] == search_plugin and not is_cmdline_monitoring(x['cmdline']) ]) else: stats[plugin][key] = sum([ x[key] for x in processes if x['plugin'] == search_plugin and is_cmdline_monitoring(x['cmdline']) ]) return stats while True: LOGGER.debug("waiting 10s...") time.sleep(10) client = TelegrafUnixSocketClient(SOCKET_PATH) try: client.connect() except Exception: LOGGER.warning("can't connect to %s, wait 10s and try again...", SOCKET_PATH) continue stats = get_stats() if stats: for plugin, fields_dict in stats.items(): msg = client.send_measurement("metwork_module", fields_dict, extra_tags={"plugin": plugin}) LOGGER.debug("sended msg: %s" % msg) client.close()
status = f.read().strip().lower() if status in ('running', ): status_code = 2 elif status in ('error', 'unknown'): status_code = 0 else: status_code = 1 except Exception: pass return {"status": status, "status_code": status_code} while True: LOGGER.debug("waiting 10s...") time.sleep(10) client = TelegrafUnixSocketClient(SOCKET_PATH) try: client.connect() except Exception: LOGGER.warning("can't connect to %s, wait 10s and try again...", SOCKET_PATH) continue stats = get_stats() if stats: for plugin, fields_dict in stats.items(): msg = client.send_measurement("metwork_module", fields_dict, extra_tags={"plugin": plugin}) LOGGER.debug("sended msg: %s" % msg) versions = get_versions() status = get_status()