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, ) object_identity_factory = ClassInstantiationSpy(ObjectIdentity) with pytest.raises(Exception): config.parse_metrics(metrics, check.warning) # Simple OID metrics = [{"OID": "1.2.3", "name": "foo"}] table, _, _ = config.parse_metrics( metrics, check.warning, object_identity_factory=object_identity_factory) assert len(table) == 1 object_identity_factory.assert_called_once_with("1.2.3") object_identity_factory.reset() # 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"}] table, _, _ = config.parse_metrics( metrics, check.warning, object_identity_factory=object_identity_factory) assert len(table) == 1 object_identity_factory.assert_called_once_with("foo_mib", "foo") object_identity_factory.reset() # 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", "symbols": ["foo", "bar"]}] table, _, _ = config.parse_metrics( metrics, check.warning, object_identity_factory=object_identity_factory) assert len(table) == 2 object_identity_factory.assert_any_call("foo_mib", "foo") object_identity_factory.assert_any_call("foo_mib", "bar") object_identity_factory.reset() # MIB with table, symbols, bad metrics_tags metrics = [{ "MIB": "foo_mib", "table": "foo", "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", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo" }] }] with pytest.raises(Exception): config.parse_metrics(metrics, check.warning) # Table with manual OID metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": [{ "OID": "1.2.3", "name": "foo" }] }] table, _, _ = config.parse_metrics( metrics, check.warning, object_identity_factory=object_identity_factory) assert len(table) == 1 object_identity_factory.assert_any_call("1.2.3") object_identity_factory.reset() # MIB with table, symbols, metrics_tags index metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo", "index": "1" }] }] table, _, _ = config.parse_metrics( metrics, check.warning, object_identity_factory=object_identity_factory) assert len(table) == 2 object_identity_factory.assert_any_call("foo_mib", "foo") object_identity_factory.assert_any_call("foo_mib", "bar") object_identity_factory.reset() # MIB with table, symbols, metrics_tags column metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo", "column": "baz" }] }] table, _, _ = config.parse_metrics( metrics, check.warning, object_identity_factory=object_identity_factory) assert len(table) == 3 object_identity_factory.assert_any_call("foo_mib", "foo") object_identity_factory.assert_any_call("foo_mib", "bar") object_identity_factory.assert_any_call("foo_mib", "baz") object_identity_factory.reset() # MIB with table, symbols, metrics_tags column with OID metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo", "column": { "name": "baz", "OID": "1.5.6" } }], }] table, _, _ = config.parse_metrics( metrics, check.warning, object_identity_factory=object_identity_factory) assert len(table) == 3 object_identity_factory.assert_any_call("1.5.6") object_identity_factory.assert_any_call("foo_mib", "foo") object_identity_factory.assert_any_call("foo_mib", "bar") object_identity_factory.reset()
def test_parse_metrics(hlapi_mock): instance = common.generate_instance_config(common.SUPPORTED_METRIC_TYPES) check = SnmpCheck('snmp', {}, [instance]) # Unsupported metric metrics = [{"foo": "bar"}] config = InstanceConfig( { "ip_address": "127.0.0.1", "community_string": "public", "metrics": [{ "OID": "1.2.3" }] }, check.warning, check.log, [], None, {}, {}, ) hlapi_mock.reset_mock() with pytest.raises(Exception): config.parse_metrics(metrics, False, check.warning, check.log) # Simple OID metrics = [{"OID": "1.2.3"}] table, raw, mibs = config.parse_metrics(metrics, False, check.warning, check.log) assert table == {} assert mibs == set() assert len(raw) == 1 hlapi_mock.ObjectIdentity.assert_called_once_with("1.2.3") hlapi_mock.reset_mock() # MIB with no symbol or table metrics = [{"MIB": "foo_mib"}] with pytest.raises(Exception): config.parse_metrics(metrics, False, check.warning, check.log) # MIB with symbol metrics = [{"MIB": "foo_mib", "symbol": "foo"}] table, raw, mibs = config.parse_metrics(metrics, False, check.warning, check.log) assert raw == [] assert mibs == {"foo_mib"} assert len(table) == 1 hlapi_mock.ObjectIdentity.assert_called_once_with("foo_mib", "foo") hlapi_mock.reset_mock() # MIB with table, no symbols metrics = [{"MIB": "foo_mib", "table": "foo"}] with pytest.raises(Exception): config.parse_metrics(metrics, False, check.warning, check.log) # MIB with table and symbols metrics = [{"MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"]}] table, raw, mibs = config.parse_metrics(metrics, True, check.warning, check.log) assert raw == [] assert mibs == set() assert len(table) == 1 assert len(list(table.values())[0]) == 2 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.reset_mock() # MIB with table, symbols, bad metrics_tags metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{}] }] with pytest.raises(Exception): config.parse_metrics(metrics, False, check.warning, check.log) # MIB with table, symbols, bad metrics_tags metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo" }] }] with pytest.raises(Exception): config.parse_metrics(metrics, False, check.warning, check.log) # MIB with table, symbols, metrics_tags index metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo", "index": "1" }] }] table, raw, mibs = config.parse_metrics(metrics, True, check.warning, check.log) assert raw == [] assert mibs == set() assert len(table) == 1 assert len(list(table.values())[0]) == 2 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.reset_mock() # MIB with table, symbols, metrics_tags column metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo", "column": "baz" }] }] table, raw, mibs = config.parse_metrics(metrics, True, check.warning, check.log) assert raw == [] assert mibs == set() assert len(table) == 1 assert len(list(table.values())[0]) == 3 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "baz") hlapi_mock.reset_mock()
def test_parse_metrics(lcd_mock): # type: (Any) -> None lcd_mock.configure.return_value = ('addr', None) config = InstanceConfig( {"ip_address": "127.0.0.1", "community_string": "public", "metrics": [{"OID": "1.2.3", "name": "foo"}]} ) # Unsupported metric. metrics = [{"foo": "bar"}] # type: list with pytest.raises(Exception): config.parse_metrics(metrics) # Simple OID metrics = [{"OID": "1.2.3", "name": "foo"}] oids, _, _, parsed_metrics = config.parse_metrics(metrics) assert len(oids) == 1 assert len(parsed_metrics) == 1 foo = parsed_metrics[0] assert isinstance(foo, ParsedSymbolMetric) assert foo.name == 'foo' # MIB with no symbol or table metrics = [{"MIB": "foo_mib"}] with pytest.raises(Exception): config.parse_metrics(metrics) # MIB with symbol metrics = [{"MIB": "foo_mib", "symbol": "foo"}] oids, _, _, parsed_metrics = config.parse_metrics(metrics) assert len(oids) == 1 assert len(parsed_metrics) == 1 foo = parsed_metrics[0] assert isinstance(foo, ParsedSymbolMetric) assert foo.name == 'foo' # MIB with table, no symbols metrics = [{"MIB": "foo_mib", "table": "foo"}] with pytest.raises(Exception): config.parse_metrics(metrics) # MIB with table and symbols metrics = [{"MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"]}] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_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) # 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) # Table with manual OID metrics = [{"MIB": "foo_mib", "table": "foo_table", "symbols": [{"OID": "1.2.3", "name": "foo"}]}] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_oids) == 1 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"}], }, ] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_oids) == 2 assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' assert len(foo.index_tags) == 1 index_tag = foo.index_tags[0] assert index_tag.index == '1' assert index_tag.parsed_metric_tag.name == 'test' assert isinstance(bar, ParsedTableMetric) assert bar.name == 'bar' index_tag = bar.index_tags[0] assert index_tag.index == '1' assert index_tag.parsed_metric_tag.name == 'test' # MIB with table, symbols, metrics_tags column metrics = [ { "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"], "metric_tags": [{"tag": "test", "column": "baz"}], } ] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_oids) == 3 assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' column_tag = foo.column_tags[0] assert column_tag.column == 'baz' assert column_tag.parsed_metric_tag.name == 'test' assert isinstance(bar, ParsedTableMetric) assert bar.name == 'bar' column_tag = bar.column_tags[0] assert column_tag.column == 'baz' assert column_tag.parsed_metric_tag.name == 'test' # 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"}}], } ] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_oids) == 3 assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' column_tag = foo.column_tags[0] assert column_tag.column == 'baz' assert column_tag.parsed_metric_tag.name == 'test' assert isinstance(bar, ParsedTableMetric) assert bar.name == 'bar' column_tag = bar.column_tags[0] assert column_tag.column == 'baz' assert column_tag.parsed_metric_tag.name == 'test'
def test_parse_metrics(hlapi_mock): # Unsupported metric metrics = [{"foo": "bar"}] with pytest.raises(Exception): InstanceConfig.parse_metrics(metrics, False, warning) # Simple OID metrics = [{"OID": "1.2.3"}] table, raw, mibs = InstanceConfig.parse_metrics(metrics, False, warning) assert table == [] assert mibs == set() assert len(raw) == 1 hlapi_mock.ObjectIdentity.assert_called_once_with("1.2.3") hlapi_mock.reset_mock() # MIB with no symbol or table metrics = [{"MIB": "foo_mib"}] with pytest.raises(Exception): InstanceConfig.parse_metrics(metrics, False, warning) # MIB with symbol metrics = [{"MIB": "foo_mib", "symbol": "foo"}] table, raw, mibs = InstanceConfig.parse_metrics(metrics, False, warning) assert raw == [] assert mibs == {"foo_mib"} assert len(table) == 1 hlapi_mock.ObjectIdentity.assert_called_once_with("foo_mib", "foo") hlapi_mock.reset_mock() # MIB with table, no symbols metrics = [{"MIB": "foo_mib", "table": "foo"}] with pytest.raises(Exception): InstanceConfig.parse_metrics(metrics, False, warning) # MIB with table and symbols metrics = [{"MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"]}] table, raw, mibs = InstanceConfig.parse_metrics(metrics, True, warning) assert raw == [] assert mibs == set() assert len(table) == 2 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.reset_mock() # MIB with table, symbols, bad metrics_tags metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{}] }] with pytest.raises(Exception): InstanceConfig.parse_metrics(metrics, False, warning) # MIB with table, symbols, bad metrics_tags metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo" }] }] with pytest.raises(Exception): InstanceConfig.parse_metrics(metrics, False, warning) # MIB with table, symbols, metrics_tags index metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo", "index": "1" }] }] table, raw, mibs = InstanceConfig.parse_metrics(metrics, True, warning) assert raw == [] assert mibs == set() assert len(table) == 2 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.reset_mock() # MIB with table, symbols, metrics_tags column metrics = [{ "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "foo", "column": "baz" }] }] table, raw, mibs = InstanceConfig.parse_metrics(metrics, True, warning) assert raw == [] assert mibs == set() assert len(table) == 3 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "baz") hlapi_mock.reset_mock()
def test_parse_metrics(lcd_mock, caplog): # type: (Any) -> None lcd_mock.configure.return_value = ('addr', None) config = InstanceConfig({ "ip_address": "127.0.0.1", "community_string": "public", "metrics": [{ "OID": "1.2.3", "name": "foo" }] }) # Unsupported metric. metrics = [{"foo": "bar"}] # type: list with pytest.raises(Exception): config.parse_metrics(metrics) # Simple OID metrics = [{"OID": "1.2.3", "name": "foo"}] oids, _, _, parsed_metrics = config.parse_metrics(metrics) assert len(oids) == 1 assert len(parsed_metrics) == 1 foo = parsed_metrics[0] assert isinstance(foo, ParsedSymbolMetric) assert foo.name == 'foo' # MIB with no symbol or table metrics = [{"MIB": "foo_mib"}] with pytest.raises(Exception): config.parse_metrics(metrics) # MIB with symbol metrics = [{"MIB": "foo_mib", "symbol": "foo"}] oids, _, _, parsed_metrics = config.parse_metrics(metrics) assert len(oids) == 1 assert len(parsed_metrics) == 1 foo = parsed_metrics[0] assert isinstance(foo, ParsedSymbolMetric) assert foo.name == 'foo' # MIB with table, no symbols metrics = [{"MIB": "foo_mib", "table": "foo"}] with pytest.raises(Exception): config.parse_metrics(metrics) # MIB with table and symbols but no metric_tags caplog.at_level(logging.WARNING) metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"] }] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_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' assert ( "foo_table table doesn't have a 'metric_tags' section, all its metrics will use the same tags." in caplog.text) # 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) # 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) # Table with manual OID metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": [{ "OID": "1.2.3", "name": "foo" }], "metric_tags": [{ "tag": "test", "index": "1" }], }] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_oids) == 1 assert len(parsed_metrics) == 1 foo = parsed_metrics[0] assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' index_tag = foo.index_tags[0] assert index_tag.index == '1' assert index_tag.parsed_metric_tag.name == 'test' # MIB with table, symbols, metrics_tags index metrics = [ { "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "test", "index": "1" }], }, ] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_oids) == 2 assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' assert len(foo.index_tags) == 1 index_tag = foo.index_tags[0] assert index_tag.index == '1' assert index_tag.parsed_metric_tag.name == 'test' assert isinstance(bar, ParsedTableMetric) assert bar.name == 'bar' index_tag = bar.index_tags[0] assert index_tag.index == '1' assert index_tag.parsed_metric_tag.name == 'test' # MIB with table, symbols, metrics_tags column metrics = [{ "MIB": "foo_mib", "table": "foo_table", "symbols": ["foo", "bar"], "metric_tags": [{ "tag": "test", "column": "baz" }], }] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_oids) == 3 assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' column_tag = foo.column_tags[0] assert column_tag.column == 'baz' assert column_tag.parsed_metric_tag.name == 'test' assert isinstance(bar, ParsedTableMetric) assert bar.name == 'bar' column_tag = bar.column_tags[0] assert column_tag.column == 'baz' assert column_tag.parsed_metric_tag.name == 'test' # 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" } }], }] _, next_oids, _, parsed_metrics = config.parse_metrics(metrics) assert len(next_oids) == 3 assert len(parsed_metrics) == 2 foo, bar = parsed_metrics assert isinstance(foo, ParsedTableMetric) assert foo.name == 'foo' column_tag = foo.column_tags[0] assert column_tag.column == 'baz' assert column_tag.parsed_metric_tag.name == 'test' assert isinstance(bar, ParsedTableMetric) assert bar.name == 'bar' column_tag = bar.column_tags[0] assert column_tag.column == 'baz' assert column_tag.parsed_metric_tag.name == 'test' # Invalid extract value pattern metrics = [{ "MIB": "foo_mib", "symbol": { "OID": "1.2.3", "name": "hey", "extract_value": "[aa-" } }] with pytest.raises(Exception, match="Failed to compile regular expression"): config.parse_metrics(metrics)
def test_parse_metrics(hlapi_mock): instance = common.generate_instance_config(common.SUPPORTED_METRIC_TYPES) check = SnmpCheck('snmp', {}, [instance]) # Unsupported metric metrics = [{"foo": "bar"}] config = InstanceConfig( {"ip_address": "127.0.0.1", "community_string": "public", "metrics": [{"OID": "1.2.3", "name": "foo"}]}, warning=check.warning, log=check.log, ) hlapi_mock.reset_mock() with pytest.raises(Exception): config.parse_metrics(metrics, check.warning, check.log) # Simple OID metrics = [{"OID": "1.2.3", "name": "foo"}] table, _, _ = config.parse_metrics(metrics, check.warning, check.log) assert len(table) == 1 hlapi_mock.ObjectIdentity.assert_called_once_with("1.2.3") hlapi_mock.reset_mock() # MIB with no symbol or table metrics = [{"MIB": "foo_mib"}] with pytest.raises(Exception): config.parse_metrics(metrics, check.warning, check.log) # MIB with symbol metrics = [{"MIB": "foo_mib", "symbol": "foo"}] table, _, _ = config.parse_metrics(metrics, check.warning, check.log) assert len(table) == 1 hlapi_mock.ObjectIdentity.assert_called_once_with("foo_mib", "foo") hlapi_mock.reset_mock() # MIB with table, no symbols metrics = [{"MIB": "foo_mib", "table": "foo"}] with pytest.raises(Exception): config.parse_metrics(metrics, check.warning, check.log) # MIB with table and symbols metrics = [{"MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"]}] table, _, _ = config.parse_metrics(metrics, check.warning, check.log) assert len(table) == 1 assert len(table[0]) == 2 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.reset_mock() # MIB with table, symbols, bad metrics_tags metrics = [{"MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{}]}] with pytest.raises(Exception): config.parse_metrics(metrics, check.warning, check.log) # MIB with table, symbols, bad metrics_tags metrics = [{"MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{"tag": "foo"}]}] with pytest.raises(Exception): config.parse_metrics(metrics, check.warning, check.log) # Table with manual OID metrics = [{"MIB": "foo_mib", "table": "foo", "symbols": [{"OID": "1.2.3", "name": "foo"}]}] table, _, _ = config.parse_metrics(metrics, check.warning, check.log) assert len(table) == 1 assert len(table[0]) == 1 hlapi_mock.ObjectIdentity.assert_any_call("1.2.3") hlapi_mock.reset_mock() # MIB with table, symbols, metrics_tags index metrics = [ {"MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{"tag": "foo", "index": "1"}]} ] table, _, _ = config.parse_metrics(metrics, check.warning, check.log) assert len(table) == 1 assert len(table[0]) == 2 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.reset_mock() # MIB with table, symbols, metrics_tags column metrics = [ {"MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{"tag": "foo", "column": "baz"}]} ] table, _, _ = config.parse_metrics(metrics, check.warning, check.log) assert len(table) == 1 assert len(table[0]) == 3 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "baz") hlapi_mock.reset_mock() # MIB with table, symbols, metrics_tags column with OID metrics = [ { "MIB": "foo_mib", "table": "foo", "symbols": ["foo", "bar"], "metric_tags": [{"tag": "foo", "column": {"name": "baz", "OID": "1.5.6"}}], } ] table, _, _ = config.parse_metrics(metrics, check.warning, check.log) assert len(table) == 1 assert len(table[0]) == 3 hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "foo") hlapi_mock.ObjectIdentity.assert_any_call("foo_mib", "bar") hlapi_mock.ObjectIdentity.assert_any_call("1.5.6") hlapi_mock.reset_mock()
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')]