Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Commit

Permalink
feat: PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
weliasz committed Sep 24, 2021
1 parent 4528553 commit f33a02e
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 5 deletions.
15 changes: 11 additions & 4 deletions splunk_connect_for_snmp_mib_server/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions tests/profiles/malformed_profile/default.yaml
Original file line number Diff line number Diff line change
@@ -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']
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions tests/profiles/same_name_profiles/default.yaml
Original file line number Diff line number Diff line change
@@ -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']
42 changes: 42 additions & 0 deletions tests/profiles/same_name_profiles/local.yaml
Original file line number Diff line number Diff line change
@@ -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']
37 changes: 37 additions & 0 deletions tests/profiles/two_profiles/default.yaml
Original file line number Diff line number Diff line change
@@ -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']
42 changes: 42 additions & 0 deletions tests/profiles/two_profiles/local.yaml
Original file line number Diff line number Diff line change
@@ -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']
26 changes: 25 additions & 1 deletion tests/test_profile_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit f33a02e

Please sign in to comment.