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

Commit

Permalink
style: apply pre-commit isort
Browse files Browse the repository at this point in the history
  • Loading branch information
omrozowicz-splunk authored and Artem Rys committed Aug 24, 2021
1 parent f3d6b2d commit 9c906f9
Show file tree
Hide file tree
Showing 25 changed files with 64 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-test-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ jobs:
env:
VALIDATE_ALL_CODEBASE: false
DEFAULT_BRANCH: main
LINTER_RULES_PATH: /
PYTHON_ISORT_CONFIG_FILE: pyproject.toml
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
review_secrets:
name: security-detect-secrets
Expand Down
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ repos:
rev: 21.7b0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.9.3
hooks:
- id: isort

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ build-backend = "poetry.masonry.api"
[tool.pytest.ini_options]
log_cli = true
log_cli_level = "INFO"

[tool.isort]
profile = "black"
7 changes: 4 additions & 3 deletions splunk_connect_for_snmp_poller/manager/celery_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from celery import Celery
import os
import logging.config
import os

from celery import Celery

logger = logging.getLogger(__name__)

logger.info(f"Start Celery Client")
logger.info("Start Celery Client")

app = Celery(__name__)

Expand Down
7 changes: 3 additions & 4 deletions splunk_connect_for_snmp_poller/manager/hec_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import requests
from celery.utils.log import get_logger

from splunk_connect_for_snmp_poller.manager.static.mib_enricher import (
MibEnricher,
)
from splunk_connect_for_snmp_poller.manager.static.mib_enricher import MibEnricher

logger = get_logger(__name__)

Expand Down Expand Up @@ -104,7 +102,8 @@ def _enrich_event_data(mib_enricher: MibEnricher, variables_binds: dict) -> str:
'non_metric': 'oid-type1="ObjectIdentity" value1-type="OctetString" 1.3.6.1.2.1.2.2.1.6.1=""
value1="" IF-MIB::ifPhysAddress.1="" '}
We need both formats because append_additional_dimensions function was designed to work on metric data only and non metric format is
We need both formats because append_additional_dimensions function was designed to work on metric data only and non
metric format is
difficult to process because of the nature of string type.
@return: non metric varbind with values from additional dimension added. For ex. for additional dimensions:
Expand Down
5 changes: 2 additions & 3 deletions splunk_connect_for_snmp_poller/manager/mib_server_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from splunk_connect_for_snmp_poller.utilities import (
format_value_for_mib_server,
)

from splunk_connect_for_snmp_poller.utilities import format_value_for_mib_server

logger = logging.getLogger(__name__)

Expand Down
9 changes: 4 additions & 5 deletions splunk_connect_for_snmp_poller/manager/poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import schedule
from pysnmp.hlapi import SnmpEngine

from splunk_connect_for_snmp_poller.manager.poller_utilities import (
automatic_realtime_task,
create_poller_scheduler_entry_key,
Expand Down Expand Up @@ -89,11 +90,9 @@ def __check_inventory(self):
frequency = int(ir.frequency_str)
if entry_key in inventory_hosts:
logger.error(
(
f"{ir.host},{ir.version},{ir.community},{ir.profile},{ir.frequency_str} has duplicated "
f"hostname {ir.host} and {ir.profile} in the inventory,"
f" cannot use the same profile twice for the same device"
)
f"{ir.host},{ir.version},{ir.community},{ir.profile},{ir.frequency_str} has duplicated "
f"hostname {ir.host} and {ir.profile} in the inventory,"
f" cannot use the same profile twice for the same device"
)
continue

Expand Down
9 changes: 3 additions & 6 deletions splunk_connect_for_snmp_poller/manager/poller_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

import schedule
from pysnmp.hlapi import ObjectIdentity, ObjectType, UdpTransportTarget, getCmd
from splunk_connect_for_snmp_poller.manager.realtime.oid_constant import (
OidConstant,
)

from splunk_connect_for_snmp_poller.manager.realtime.oid_constant import OidConstant
from splunk_connect_for_snmp_poller.manager.realtime.real_time_data import (
should_redo_walk,
)
Expand Down Expand Up @@ -89,9 +88,7 @@ def parse_inventory_file(inventory_file_path):
def _extract_sys_uptime_instance(
local_snmp_engine, host, version, community, server_config
):
from splunk_connect_for_snmp_poller.manager.task_utilities import (
parse_port,
)
from splunk_connect_for_snmp_poller.manager.task_utilities import parse_port
from splunk_connect_for_snmp_poller.manager.tasks import (
build_authData,
build_contextData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import re


# See http://www.net-snmp.org/docs/mibs/interfaces.html for additional implementation details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
#

from pysnmp.proto.rfc1902 import TimeTicks
from splunk_connect_for_snmp_poller.manager.realtime.oid_constant import (
OidConstant,
)

from splunk_connect_for_snmp_poller.manager.realtime.oid_constant import OidConstant


class __RealTimeData:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
#
import logging

from splunk_connect_for_snmp_poller.manager.realtime.interface_mib import (
InterfaceMib,
)
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__)
Expand Down
25 changes: 9 additions & 16 deletions splunk_connect_for_snmp_poller/manager/task_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@
from pysnmp.proto import rfc1902
from pysnmp.smi import builder, compiler, view
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType

from splunk_connect_for_snmp_poller.manager.const import (
AuthProtocolMap,
PrivProtocolMap,
)
from splunk_connect_for_snmp_poller.manager.hec_sender import (
post_data_to_splunk_hec,
)
from splunk_connect_for_snmp_poller.manager.mib_server_client import (
get_translation,
)
from splunk_connect_for_snmp_poller.manager.hec_sender import post_data_to_splunk_hec
from splunk_connect_for_snmp_poller.manager.mib_server_client import get_translation
from splunk_connect_for_snmp_poller.manager.static.interface_mib_utililities import (
extract_network_interface_data_from_walk,
)
Expand Down Expand Up @@ -188,11 +185,9 @@ def mib_string_handler(mib_list: list) -> VarbindCollection:

else:
raise Exception(
(
f"Invalid mib string - {mib_string}."
f"\nPlease provide a valid mib string in the correct format. "
f"Learn more about the format at https://bit.ly/3qtqzQc"
)
f"Invalid mib string - {mib_string}."
f"\nPlease provide a valid mib string in the correct format. "
f"Learn more about the format at https://bit.ly/3qtqzQc"
)
except Exception as e:
logger.error(
Expand Down Expand Up @@ -278,7 +273,7 @@ def _any_failure_happened(
result = f"error: {errorIndication}"
logger.error(result)
elif errorStatus:
result = "error: %s at %s" % (
result = "error: {} at {}".format(
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
)
Expand Down Expand Up @@ -384,7 +379,7 @@ def walk_handler(
)
break
elif errorStatus:
result = "error: %s at %s" % (
result = "error: {} at {}".format(
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
)
Expand Down Expand Up @@ -458,7 +453,7 @@ def walk_handler_with_enricher(
)
break
elif errorStatus:
result = "error: %s at %s" % (
result = "error: {} at {}".format(
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
)
Expand Down Expand Up @@ -499,11 +494,9 @@ def walk_handler_with_enricher(
_post_walk_data_to_splunk(
merged_result_metric, True, *post_walk_data_to_splunk_arguments
)
logger.info(f"***************** After metric *****************")
_post_walk_data_to_splunk(
merged_result_non_metric, False, *post_walk_data_to_splunk_arguments
)
logger.info(f"***************** After metric *****************")


def _sort_walk_data(
Expand Down
3 changes: 1 addition & 2 deletions splunk_connect_for_snmp_poller/manager/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from celery.utils.log import get_task_logger
from pysnmp.hlapi import ObjectIdentity, ObjectType, SnmpEngine

from splunk_connect_for_snmp_poller.manager.celery_client import app
from splunk_connect_for_snmp_poller.manager.task_utilities import (
VarbindCollection,
Expand All @@ -30,8 +31,6 @@
walk_handler,
walk_handler_with_enricher,
)

# Used to store a single SnmpEngine() instance for each Celery task
from splunk_connect_for_snmp_poller.mongo import WalkedHostsRepository

thread_local = threading.local()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def resolve_host(hostname):
try:
socket.gethostbyname(hostname)
return True if hostname else False
except socket.error:
except OSError:
logger.error(f"Cannot resolve {hostname}")
return False

Expand Down Expand Up @@ -117,7 +117,8 @@ def is_valid_profile(profile):

def is_valid_inventory_line_from_dict(host, version, community, profile, seconds):
logger.info(
f"Validating host = [{host}], version = [{version}], community = [{community}], profile = [{profile}], seconds = [{seconds}]"
f"Validating host = [{host}], version = [{version}], community = [{community}], profile = [{profile}], "
f"seconds = [{seconds}]"
)

if None in [host, version, community, profile, seconds]:
Expand All @@ -131,5 +132,5 @@ def is_valid_inventory_line_from_dict(host, version, community, profile, seconds
and is_valid_second_quantity(seconds.strip())
)
if not valid_inventory_line:
logger.error(f"Invalid inventory line")
logger.error("Invalid inventory line")
return valid_inventory_line
10 changes: 6 additions & 4 deletions splunk_connect_for_snmp_poller/snmp_poller_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
import logging.config

from splunk_connect_for_snmp_poller.manager.poller import Poller
from splunk_connect_for_snmp_poller.utilities import initialize_signals_handler
from splunk_connect_for_snmp_poller.utilities import parse_command_line_arguments
from splunk_connect_for_snmp_poller.utilities import parse_config_file
from splunk_connect_for_snmp_poller.utilities import (
initialize_signals_handler,
parse_command_line_arguments,
parse_config_file,
)

logger = logging.getLogger(__name__)


def main():
logger.info(f"Startup Config")
logger.info("Startup Config")
args = parse_command_line_arguments()
logging.getLogger().setLevel(args.loglevel.upper())

Expand Down
2 changes: 1 addition & 1 deletion splunk_connect_for_snmp_poller/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def parse_command_line_arguments():
def parse_config_file(config_file_path):
logger.debug(f"Config file is {config_file_path}")
try:
with open(config_file_path, "r") as yaml_file:
with open(config_file_path) as yaml_file:
server_config = yaml.safe_load(yaml_file)
logger.debug(f"Server Config is: {server_config}")
except Exception as e:
Expand Down
1 change: 0 additions & 1 deletion tests/test_hec_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
_enrich_event_data,
_enrich_metric_data,
)

from splunk_connect_for_snmp_poller.manager.static.mib_enricher import MibEnricher

MibEnricher = MibEnricher(
Expand Down
9 changes: 2 additions & 7 deletions tests/test_interface_mib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@
import logging
from unittest import TestCase

from splunk_connect_for_snmp_poller.manager.realtime.interface_mib import (
InterfaceMib,
)
from tests.test_utils import (
file_data_path,
load_test_data,
)
from splunk_connect_for_snmp_poller.manager.realtime.interface_mib import InterfaceMib
from tests.test_utils import file_data_path, load_test_data

logger = logging.getLogger(__name__)

Expand Down
5 changes: 3 additions & 2 deletions tests/test_inventory_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging
from unittest import TestCase

from splunk_connect_for_snmp_poller.manager.validator.inventory_validator import (
should_process_inventory_line,
is_valid_inventory_line_from_dict,
should_process_inventory_line,
)
from tests.static_inventory_test_data import InventoryLineBuilder
import logging

logger = logging.getLogger(__name__)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_mib_enricher.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_process_one_valid_no_if_mib_entry(self):
enricher.append_additional_dimensions(translated_metric)
self.assertTrue(len(translated_metric) == 3)
self.assertEqual(
set(["metric_name", "_value", "metric_type"]), translated_metric.keys()
{"metric_name", "_value", "metric_type"}, translated_metric.keys()
)

def test_process_one_valid_if_mib_entry_iwith_zero_index(self):
Expand All @@ -50,7 +50,7 @@ def test_process_one_valid_if_mib_entry_iwith_zero_index(self):
enricher.append_additional_dimensions(translated_metric)
self.assertTrue(len(translated_metric) == 3)
self.assertEqual(
set(["metric_name", "_value", "metric_type"]), translated_metric.keys()
{"metric_name", "_value", "metric_type"}, translated_metric.keys()
)

def test_process_one_valid_if_mib_entry_without_proper_mongo_static_data(self):
Expand All @@ -63,7 +63,7 @@ def test_process_one_valid_if_mib_entry_without_proper_mongo_static_data(self):
enricher.append_additional_dimensions(translated_metric)
self.assertTrue(len(translated_metric) == 3)
self.assertEqual(
set(["metric_name", "_value", "metric_type"]), translated_metric.keys()
{"metric_name", "_value", "metric_type"}, translated_metric.keys()
)

def test_process_one_valid_if_mib_entry(self):
Expand Down
4 changes: 1 addition & 3 deletions tests/test_real_time_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
#
from unittest import TestCase

from splunk_connect_for_snmp_poller.manager.realtime.oid_constant import (
OidConstant,
)
from splunk_connect_for_snmp_poller.manager.realtime.oid_constant import OidConstant
from splunk_connect_for_snmp_poller.manager.realtime.real_time_data import (
should_redo_walk,
)
Expand Down
4 changes: 1 addition & 3 deletions tests/test_static_config_data_for_network_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import logging
from unittest import TestCase

from splunk_connect_for_snmp_poller.manager.realtime.interface_mib import (
InterfaceMib,
)
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,
extract_network_interface_data_from_walk,
Expand Down
Loading

0 comments on commit 9c906f9

Please sign in to comment.