def test_network_basic(net_io_counters): from datadog_checks.network import Network # delayed import for good patching aggregator = MetricsAggregator( HOSTNAME, interval=1.0, histogram_aggregates=None, histogram_percentiles=None, ) total_rates, expected_rates = generate_expected_rates(Network.ATTR_MAP) c = Network("network", {}, {}, aggregator) c.check({}) metrics = c.aggregator.flush( )[:-1] # we remove the datadog.agent.running metric assert len(metrics) == 0 # all rates for now time.sleep(1) c.check({}) metrics = c.aggregator.flush( )[:-1] # we remove the datadog.agent.running metric assert len(metrics) == total_rates for metric in metrics: assert metric['metric'] in expected_rates assert len(metric['points']) == 1 assert metric['host'] == HOSTNAME assert metric['type'] == GAUGE assert is_metric_expected(expected_rates, metric)
def check(): return Network(common.SERVICE_CHECK_NAME, {}, {})
def check(): return Network(common.SERVICE_CHECK_NAME, {}, [deepcopy(common.INSTANCE)])
CX_STATE_GAUGES_VALUES = { 'system.net.udp4.connections': 2, 'system.net.udp6.connections': 3, 'system.net.tcp4.established': 1, 'system.net.tcp4.opening': 0, 'system.net.tcp4.closing': 0, 'system.net.tcp4.listening': 2, 'system.net.tcp4.time_wait': 2, 'system.net.tcp6.established': 1, 'system.net.tcp6.opening': 0, 'system.net.tcp6.closing': 1, 'system.net.tcp6.listening': 1, 'system.net.tcp6.time_wait': 1, } network_check = Network('network', {}, {}) @pytest.fixture def ss_subprocess_mock(*args, **kwargs): if args[0][-1] == '-4' and args[0][-3] == '-u': file_name = 'ss_ipv4_udp' elif args[0][-1] == '-4' and args[0][-3] == '-t': file_name = 'ss_ipv4_tcp' elif args[0][-1] == '-6' and args[0][-3] == '-u': file_name = 'ss_ipv6_udp' elif args[0][-1] == '-6' and args[0][-3] == '-t': file_name = 'ss_ipv6_tcp' with open(file_name, FIXTURE_DIR) as f: contents = f.read()
def network_check(): return Network('network', {}, {})
def test_cx_state_psutil(aggregator, network_check): sconn = namedtuple( 'sconn', ['fd', 'family', 'type', 'laddr', 'raddr', 'status', 'pid']) conn = [ sconn( fd=-1, family=socket.AF_INET, type=socket.SOCK_STREAM, laddr=('127.0.0.1', 50482), raddr=('127.0.0.1', 2638), status='ESTABLISHED', pid=1416 ), sconn( fd=-1, family=socket.AF_INET6, type=socket.SOCK_STREAM, laddr=('::', 50482), raddr=('::', 2638), status='ESTABLISHED', pid=42 ), sconn( fd=-1, family=socket.AF_INET6, type=socket.SOCK_STREAM, laddr=('::', 49163), raddr=(), status='LISTEN', pid=1416 ), sconn( fd=-1, family=socket.AF_INET, type=socket.SOCK_STREAM, laddr=('0.0.0.0', 445), raddr=(), status='LISTEN', pid=4 ), sconn( fd=-1, family=socket.AF_INET6, type=socket.SOCK_STREAM, laddr=('::1', 56521), raddr=('::1', 17123), status='TIME_WAIT', pid=0 ), sconn( fd=-1, family=socket.AF_INET6, type=socket.SOCK_DGRAM, laddr=('::', 500), raddr=(), status='NONE', pid=892 ), sconn( fd=-1, family=socket.AF_INET6, type=socket.SOCK_STREAM, laddr=('::1', 56493), raddr=('::1', 17123), status='TIME_WAIT', pid=0 ), sconn( fd=-1, family=socket.AF_INET, type=socket.SOCK_STREAM, laddr=('127.0.0.1', 54541), raddr=('127.0.0.1', 54542), status='ESTABLISHED', pid=20500 ), ] results = { 'system.net.tcp6.time_wait': 2, 'system.net.tcp4.listening': 1, 'system.net.tcp6.closing': 0, 'system.net.tcp4.closing': 0, 'system.net.tcp4.time_wait': 0, 'system.net.tcp6.established': 1, 'system.net.tcp4.established': 2, 'system.net.tcp6.listening': 1, 'system.net.tcp4.opening': 0, 'system.net.udp4.connections': 0, 'system.net.udp6.connections': 1, 'system.net.tcp6.opening': 0, } with mock.patch('datadog_checks.network.network.psutil') as mock_psutil: mock_psutil.net_connections.return_value = conn network_check = Network('network', {}, {}) network_check._setup_metrics({}) network_check._cx_state_psutil() for _, m in iteritems(aggregator._metrics): assert results[m[0].name] == m[0].value