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

Commit

Permalink
fix: lint (ADDON-39580)
Browse files Browse the repository at this point in the history
- initial commit
  • Loading branch information
lstoppa committed Jul 29, 2021
1 parent 7699591 commit f92aa4f
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 2 deletions.
3 changes: 2 additions & 1 deletion splunk_connect_for_snmp_poller/manager/poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,5 @@ def scheduled_task(host, version, community, profile, server_config, splunk_inde
f"Executing scheduled_task for {host} version={version} community={community} profile={profile}"
)

snmp_polling.delay(host, version, community, profile, server_config, splunk_indexes)
#snmp_polling.delay(host, version, community, profile, server_config, splunk_indexes)
snmp_polling(host, version, community, profile, server_config, splunk_indexes)
15 changes: 15 additions & 0 deletions splunk_connect_for_snmp_poller/manager/static/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# 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.
#
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# 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 logging

from splunk_connect_for_snmp_poller.manager.realtime.interface_mib import InterfaceMib
from splunk_connect_for_snmp_poller.utilities import multi_key_lookup

logger = logging.getLogger(__name__)


def __network_interface_enricher_attributes(config_as_dict):
# TODO: we just assume here the whole structre of the poller's configuration
# main file. If such section does not exist we simply do not anything.
return "IF-MIB", multi_key_lookup(
config_as_dict, ("enricher", "oidFamily", "IF-MIB")
)


def extract_network_interface_data_from_config(config_as_dict):
parent_oid, splunk_dimensions = __network_interface_enricher_attributes(
config_as_dict
)
result = []
if splunk_dimensions:
for splunk_dimension in splunk_dimensions:
for key in splunk_dimension.keys():
result.append(
{
"oid_name": f"{InterfaceMib.IF_MIB_METRIC_SUFFIX}{key}",
"splunk_dimension_name": splunk_dimension[key],
}
)
logger.info(f"IF-MIB additional attributes for Splunk: {result}")
return result
2 changes: 1 addition & 1 deletion splunk_connect_for_snmp_poller/manager/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_shared_snmp_engine():


# TODO remove the debugging statement later
@app.task
#@app.task
def snmp_polling(
host, version, community, profile, server_config, index, one_time_flag=False
):
Expand Down
9 changes: 9 additions & 0 deletions splunk_connect_for_snmp_poller/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,12 @@ def file_was_modified(file_path, last_mod_time):
last_mod_time = os.stat(file_path, follow_symlinks=True).st_mtime
return True, last_mod_time
return False, last_mod_time


def multi_key_lookup(dictionary, tuple_of_keys):
from functools import reduce

try:
return reduce(dict.get, tuple_of_keys, dictionary)
except TypeError as error:
return None
99 changes: 99 additions & 0 deletions tests/test_static_config_data_for_network_interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#
# 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_poller.manager.realtime.interface_mib import InterfaceMib
from splunk_connect_for_snmp_poller.manager.static.interface_mib_utililities import (
extract_network_interface_data_from_config,
)

parsed_config_root_with_error = {
"enricher_with_error": {
"oidFamily": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_family_with_error = {
"enricher": {
"oidFamily_with_error": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_if_mib_with_error = {
"enricher": {
"oidFamily": {
"IF-MIB_with_error": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_if_mib_without_elements = {"enricher": {"oidFamily": {"IF-MIB": []}}}

parsed_config_correct = {
"enricher": {
"oidFamily": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}


class ExtractEnricherDataFromConfigTest(TestCase):
def test_config_does_not_exist(self):
result = extract_network_interface_data_from_config(None)
self.assertIsNotNone(result)
self.assertTrue(len(result) == 0)

def test_config_does_not_have_the_required_key(self):
for test_config in (
parsed_config_root_with_error,
parsed_config_family_with_error,
parsed_config_if_mib_with_error,
parsed_config_if_mib_without_elements,
):
result = extract_network_interface_data_from_config(test_config)
self.assertTrue(len(result) == 0)

def test_correct_config(self):
result = extract_network_interface_data_from_config(parsed_config_correct)
self.assertTrue(len(result) == 2)
expected_result = [
{
"oid_name": f"{InterfaceMib.IF_MIB_METRIC_SUFFIX}ifIndex",
"splunk_dimension_name": "interface_index",
},
{
"oid_name": f"{InterfaceMib.IF_MIB_METRIC_SUFFIX}ifDescr",
"splunk_dimension_name": "interface_desc",
},
]
self.assertEqual(result, expected_result)

0 comments on commit f92aa4f

Please sign in to comment.