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

feat: create default profile definition in MIB server #56

Merged
merged 8 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ COPY dist/*.whl /tmp
COPY config.yaml /work/config.yaml
COPY lookups /work/lookups
COPY mibs /work/mibs
COPY profiles /work/profiles
RUN pip3.8 install $(ls /tmp/*.whl); rm -f /tmp/*.whl

EXPOSE 5000
Expand Down
3 changes: 2 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ snmp:
dir: "/work/mibs/pysnmp"
load_list: "lookups/mibs_list.csv"
mibs_path: "/work/mibs"
profiles_path: "/work/profiles"
mongo:
oid:
database: "mib_server"
collection: "oids"
mib:
database: "files"
collection: "mib_files"
collection: "mib_files"
37 changes: 37 additions & 0 deletions 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
profiles:
# 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:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
profiles:
# 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']
7 changes: 6 additions & 1 deletion splunk_connect_for_snmp_mib_server/mib_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ########################################################################

import logging

from flask import Flask, request
from flask_autoindex import AutoIndex

from splunk_connect_for_snmp_mib_server.profiles import merge_profiles
from splunk_connect_for_snmp_mib_server.translator import Translator

logger = logging.getLogger(__name__)
Expand All @@ -39,6 +39,7 @@ def __init__(self, args, server_config):
def build_flask_app(self):
app = Flask(__name__)
mibs_path = self._server_config["snmp"]["mibs"]["mibs_path"]
profiles_path = self._server_config["snmp"]["mibs"]["profiles_path"]
files_index = AutoIndex(app, mibs_path, add_url_rules=False)

@app.route("/")
Expand Down Expand Up @@ -68,6 +69,10 @@ def translator():
result = self._translator.format_text_event(var_binds)
return result

@app.route("/profiles", methods=["GET"])
def get_profiles():
return merge_profiles(profiles_path, "profiles")

return app

def run_mib_server(self):
Expand Down
31 changes: 31 additions & 0 deletions splunk_connect_for_snmp_mib_server/profiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# ########################################################################
# 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.
# ########################################################################

import os

import yaml


def merge_profiles(directory, root_name):
result = {}
merged_profiles = {}
for root, directories, files in os.walk(directory, topdown=False):
for name in files:
with open(os.path.join(root, name), "r") as stream:
data = yaml.safe_load(stream)
merged_profiles.update(data[root_name])
result[root_name] = merged_profiles
return result
31 changes: 31 additions & 0 deletions tests/local-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# 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.
#
snmp:
mibs:
dir: "mibs/pysnmp"
load_list: "lookups/mibs_list.csv"
mibs_path: "mibs"
profiles_path: "profiles"
mongo:
oid:
database: "mib_server"
collection: "oids"
mib:
database: "files"
collection: "mib_files"
profile:
database: "profiles"
collection: "profiles"
37 changes: 37 additions & 0 deletions tests/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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
profiles:
# 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/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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
profiles:
# 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']
30 changes: 30 additions & 0 deletions tests/test_profile_merge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ########################################################################
# 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.
# ########################################################################

from unittest import TestCase

from splunk_connect_for_snmp_mib_server.profiles import merge_profiles


class ProfileLoaderTest(TestCase):
def test_multiple_files_merging(self):
merged_profiles = merge_profiles("tests/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()
5 changes: 4 additions & 1 deletion tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ def test_format_trap_non_existing_oid(self):
value_type = input_var_binds_list[i]["val_type"]
oid = input_var_binds_list[i]["oid"]
value = input_var_binds_list[i]["val"]
current = f'oid-type{i+1}="{oid_type}" value{i+1}-type="{value_type}" {oid}="{value}" value{i+1}="{value}"'
current = (
f'oid-type{i + 1}="{oid_type}"'
f' value{i + 1}-type="{value_type}" {oid}="{value}" value{i + 1}="{value}"'
)
# these two additional spaces are not an error
untranslated += f"{current} "
if i < len(input_var_binds_list) - 1:
Expand Down