diff --git a/splunk_connect_for_snmp_mib_server/profiles.py b/splunk_connect_for_snmp_mib_server/profiles.py index 435a804a..89d6a37e 100644 --- a/splunk_connect_for_snmp_mib_server/profiles.py +++ b/splunk_connect_for_snmp_mib_server/profiles.py @@ -13,19 +13,26 @@ # See the License for the specific language governing permissions and # limitations under the License. # ######################################################################## - +import logging import os +from yaml.parser import ParserError + import yaml +logger = logging.getLogger(__name__) def merge_profiles(directory, root_name): result = {} merged_profiles = {} for root, directories, files in os.walk(directory, topdown=False): - for name in files: + for name in sorted(files): with open(os.path.join(root, name), "r") as stream: - data = yaml.safe_load(stream) - merged_profiles.update(data[root_name]) + try: + data = yaml.safe_load(stream) + merged_profiles.update(data[root_name]) + except ParserError as pe: + logger.warning(f"Error while parsing file {os.path.join(root, name)} : {pe}") + result[root_name] = merged_profiles return result diff --git a/tests/profiles/malformed_profile/default.yaml b/tests/profiles/malformed_profile/default.yaml new file mode 100644 index 00000000..68a91e25 --- /dev/null +++ b/tests/profiles/malformed_profile/default.yaml @@ -0,0 +1,37 @@ +# Copyright 2021 Splunk Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +profiles: + basev1: + frequency: 10 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - '1.3.6.1.2.1.2.*' + basev1l2: + frequency: 20 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - ['IF-MIB','ifHCInOctets'] + - ['IF-MIB','ifHCOutOctets'] + - ['IF-MIB','ifInErrors'] + - ['IF-MIB','ifOutErrors'] + - ['IF-MIB','ifInDiscards'] + - ['IF-MIB','ifOutDiscards'] \ No newline at end of file diff --git a/tests/profiles/local.yaml b/tests/profiles/malformed_profile/local.yaml similarity index 100% rename from tests/profiles/local.yaml rename to tests/profiles/malformed_profile/local.yaml diff --git a/tests/profiles/default.yaml b/tests/profiles/one_profile/default.yaml similarity index 100% rename from tests/profiles/default.yaml rename to tests/profiles/one_profile/default.yaml diff --git a/tests/profiles/same_name_profiles/default.yaml b/tests/profiles/same_name_profiles/default.yaml new file mode 100644 index 00000000..4fe621bc --- /dev/null +++ b/tests/profiles/same_name_profiles/default.yaml @@ -0,0 +1,37 @@ +# Copyright 2021 Splunk Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +profiles: + basev1: + frequency: 10 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - '1.3.6.1.2.1.2.*' + basev1l2: + frequency: 20 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - ['IF-MIB','ifHCInOctets'] + - ['IF-MIB','ifHCOutOctets'] + - ['IF-MIB','ifInErrors'] + - ['IF-MIB','ifOutErrors'] + - ['IF-MIB','ifInDiscards'] + - ['IF-MIB','ifOutDiscards'] \ No newline at end of file diff --git a/tests/profiles/same_name_profiles/local.yaml b/tests/profiles/same_name_profiles/local.yaml new file mode 100644 index 00000000..85bdde7e --- /dev/null +++ b/tests/profiles/same_name_profiles/local.yaml @@ -0,0 +1,42 @@ +# Copyright 2021 Splunk Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +profiles: + basev1: + patterns: + - '*MY_DEFAULT_DEVICE_3*' + frequency: 60 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - '1.3.6.1.2.1.2.*' + basev1l2: + patterns: + - '*MY_DEFAULT_DEVICE_3*' + - '*MY_DEFAULT_DEVICE_NAME_3*' + frequency: 120 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - ['IF-MIB','ifHCInOctets'] + - ['IF-MIB','ifHCOutOctets'] + - ['IF-MIB','ifInErrors'] + - ['IF-MIB','ifOutErrors'] + - ['IF-MIB','ifInDiscards'] + - ['IF-MIB','ifOutDiscards'] \ No newline at end of file diff --git a/tests/profiles/two_profiles/default.yaml b/tests/profiles/two_profiles/default.yaml new file mode 100644 index 00000000..4fe621bc --- /dev/null +++ b/tests/profiles/two_profiles/default.yaml @@ -0,0 +1,37 @@ +# Copyright 2021 Splunk Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +profiles: + basev1: + frequency: 10 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - '1.3.6.1.2.1.2.*' + basev1l2: + frequency: 20 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - ['IF-MIB','ifHCInOctets'] + - ['IF-MIB','ifHCOutOctets'] + - ['IF-MIB','ifInErrors'] + - ['IF-MIB','ifOutErrors'] + - ['IF-MIB','ifInDiscards'] + - ['IF-MIB','ifOutDiscards'] \ No newline at end of file diff --git a/tests/profiles/two_profiles/local.yaml b/tests/profiles/two_profiles/local.yaml new file mode 100644 index 00000000..8c622e6c --- /dev/null +++ b/tests/profiles/two_profiles/local.yaml @@ -0,0 +1,42 @@ +# Copyright 2021 Splunk Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +profiles: + basev2: + patterns: + - '*MY_DEFAULT_DEVICE_3*' + frequency: 60 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - '1.3.6.1.2.1.2.*' + basev2l2: + patterns: + - '*MY_DEFAULT_DEVICE_3*' + - '*MY_DEFAULT_DEVICE_NAME_3*' + frequency: 120 + varBinds: + # Syntax: [ "MIB-Files", "MIB object name" "MIB index number"] + - ['SNMPv2-MIB', 'sysDescr'] + - ['SNMPv2-MIB', 'sysUpTime',0] + - ['SNMPv2-MIB', 'sysName'] + - ['IF-MIB','ifHCInOctets'] + - ['IF-MIB','ifHCOutOctets'] + - ['IF-MIB','ifInErrors'] + - ['IF-MIB','ifOutErrors'] + - ['IF-MIB','ifInDiscards'] + - ['IF-MIB','ifOutDiscards'] \ No newline at end of file diff --git a/tests/test_profile_merge.py b/tests/test_profile_merge.py index 006e2f24..b0b56ca9 100644 --- a/tests/test_profile_merge.py +++ b/tests/test_profile_merge.py @@ -21,10 +21,34 @@ class ProfileLoaderTest(TestCase): def test_multiple_files_merging(self): - merged_profiles = merge_profiles("tests/profiles", "profiles")["profiles"] + merged_profiles = merge_profiles("tests/profiles/two_profiles", "profiles")["profiles"] assert len(merged_profiles.keys()) == 4 assert "basev1" in merged_profiles.keys() assert "basev2" in merged_profiles.keys() assert "basev1l2" in merged_profiles.keys() assert "basev2l2" in merged_profiles.keys() + + def test_one_file(self): + merged_profiles = merge_profiles("tests/profiles/one_profile", "profiles")["profiles"] + + assert len(merged_profiles.keys()) == 2 + assert "basev1" in merged_profiles.keys() + assert "basev1l2" in merged_profiles.keys() + + def test_same_name_profiles_were_overwritten_in_alphabetical_order(self): + merged_profiles = merge_profiles("tests/profiles/same_name_profiles", "profiles")["profiles"] + + assert len(merged_profiles.keys()) == 2 + assert "basev1" in merged_profiles.keys() + assert "basev1l2" in merged_profiles.keys() + + assert merged_profiles['basev1']['frequency'] == 60 + assert merged_profiles['basev1l2']['frequency'] == 120 + + def test_malformed_file_is_ignored(self): + merged_profiles = merge_profiles("tests/profiles/malformed_profile", "profiles")["profiles"] + + assert len(merged_profiles.keys()) == 2 + assert "basev2" in merged_profiles.keys() + assert "basev2l2" in merged_profiles.keys()