def test_oid(): # type: () -> None oid = OID((1, 3, 6, 1, 2, 1, 0)) assert oid.as_tuple() == (1, 3, 6, 1, 2, 1, 0) assert oid == OID((1, 3, 6, 1, 2, 1, 0)) assert oid != OID((1, 3, 6, 1, 4, 0)) assert repr(oid) == "OID('1.3.6.1.2.1.0')" assert str(oid) == '1.3.6.1.2.1.0'
def test_oid_mib_symbol(identity, name, prefix): # type: (ObjectIdentity, str, tuple) -> None if callable(identity): identity = identity() oid = OID(identity) mib_view_controller = MibViewController(SnmpEngine().getMibBuilder()) oid.resolve(mib_view_controller) symbol = oid.get_mib_symbol() assert symbol.name == name assert symbol.prefix == prefix
def test_oid_from_unresolved_instance(value, expected_tuple): # type: (Any, Tuple[int, ...]) -> None oid = OID(value) with pytest.raises(UnresolvedOID): oid.as_tuple() object_type = oid.as_object_type() # Verify returned ObjectType instance is valid by decoding it. mib_view_controller = MibViewController(SnmpEngine().getMibBuilder()) resolved = object_type.resolveWithMib(mib_view_controller) assert OID(resolved).as_tuple() == expected_tuple
def test_oid_parse_invalid(value): # type: (Any) -> None with pytest.raises(CouldNotDecodeOID): OID(value)
def test_oid_parse_valid(value, expected_tuple): # type: (Any, Tuple[int, ...]) -> None mib_view_controller = MibViewController(SnmpEngine().getMibBuilder()) if callable(value): value = value(mib_view_controller) assert OID(value).as_tuple() == expected_tuple
def test_parse_metrics(lcd_mock): # type: (Any) -> None lcd_mock.configure.return_value = ('addr', None) instance = common.generate_instance_config(common.SUPPORTED_METRIC_TYPES) check = SnmpCheck('snmp', {}, [instance]) # Unsupported metric metrics = [{"foo": "bar"}] # type: list config = InstanceConfig( { "ip_address": "127.0.0.1", "community_string": "public", "metrics": [{ "OID": "1.2.3", "name": "foo" }] }, warning=check.warning, ) with pytest.raises(Exception): config.parse_metrics(metrics, check.warning) # Simple OID metrics = [{"OID": "1.2.3", "name": "foo"}] oids, _, parsed_metrics = config.parse_metrics(metrics, check.warning) assert oids == [OID('1.2.3')] assert len(parsed_metrics) == 1 foo = parsed_metrics[0] assert isinstance(foo, ParsedMetric) assert foo.name == 'foo' # MIB with no symbol or table metrics = [{"MIB": "foo_mib"}] with pytest.raises(Exception): config.parse_metrics(metrics, check.warning) # MIB with symbol metrics = [{"MIB": "foo_mib", "symbol": "foo"}] oids, _, parsed_metrics = config.parse_metrics(metrics, check.warning) assert len(oids) == 1 assert len(parsed_metrics) == 1 foo = parsed_metrics[0] assert isinstance(foo, ParsedMetric) assert foo.name == 'foo' # MIB with table, no symbols metrics = [{"MIB": "foo_mib", "table": "foo"}] with pytest.raises(Exception): config.parse_metrics(metrics, check.warning) # MIB with table and symbols metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"] }] oids, _, parsed_metrics = config.parse_metrics(metrics, check.warning) assert len(oids) == 2 assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' assert isinstance(foo, ParsedTableMetric) assert bar.name == 'bar' # MIB with table, symbols, bad metrics_tags metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"], "metric_tags": [{}] }] with pytest.raises(Exception): config.parse_metrics(metrics, check.warning) # MIB with table, symbols, bad metrics_tags metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "test" }] }] with pytest.raises(Exception): config.parse_metrics(metrics, check.warning) # Table with manual OID metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": [{ "OID": "1.2.3", "name": "foo" }] }] oids, _, parsed_metrics = config.parse_metrics(metrics, check.warning) assert oids == [OID('1.2.3')] assert len(parsed_metrics) == 1 foo = parsed_metrics[0] assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' # MIB with table, symbols, metrics_tags index metrics = [ { "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "test", "index": "1" }], }, ] oids, _, parsed_metrics = config.parse_metrics(metrics, check.warning) assert len(oids) == 2 assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' assert foo.index_tags == [('test', '1')] assert isinstance(bar, ParsedTableMetric) assert bar.name == 'bar' assert bar.index_tags == [('test', '1')] # MIB with table, symbols, metrics_tags column metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "test", "column": "baz" }], }] oids, _, parsed_metrics = config.parse_metrics(metrics, check.warning) assert len(oids) == 3 assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' assert foo.column_tags == [('test', 'baz')] assert isinstance(bar, ParsedTableMetric) assert bar.name == 'bar' assert bar.column_tags == [('test', 'baz')] # MIB with table, symbols, metrics_tags column with OID metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "test", "column": { "name": "baz", "OID": "1.5.6" } }], }] oids, _, parsed_metrics = config.parse_metrics(metrics, check.warning) assert len(oids) == 3 assert OID('1.5.6') in oids assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' assert foo.column_tags == [('test', 'baz')] assert isinstance(bar, ParsedTableMetric) assert bar.name == 'bar' assert foo.column_tags == [('test', 'baz')]