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

Commit 277a050

Browse files
fix: change way of checking if profile is an OID to use regex (#112)
* fix: change way of checking if profile is an OID to use regex, add unit tests
1 parent 7540a5b commit 277a050

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

splunk_connect_for_snmp_poller/manager/task_utilities.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616
import json
1717
import os
18+
import re
1819
from collections import namedtuple
1920

2021
from celery.utils.log import get_task_logger
@@ -778,3 +779,14 @@ def build_contextData(version, community, server_config):
778779
return ContextData(contextEngineId, contextName)
779780
except Exception as e:
780781
logger.error(f"Error happend while building ContextData: {e}")
782+
783+
784+
def is_oid(profile: str) -> bool:
785+
"""
786+
This function checks if profile is an OID. OID is defined as a string of format:
787+
- ex. 1.3.6.1.2.1.2
788+
- ex. 1.3.6.2.1.*
789+
@param profile: variable from inventory file, can be a name of profile or an OID
790+
@return: if the profile is of OID structure
791+
"""
792+
return bool(re.match(r"^\d(\.\d*)*(\.\*)?$", profile))

splunk_connect_for_snmp_poller/manager/tasks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
VarbindCollection,
2525
build_authData,
2626
build_contextData,
27+
is_oid,
2728
mib_string_handler,
2829
parse_port,
2930
snmp_bulk_handler,
@@ -150,7 +151,7 @@ def snmp_polling(
150151

151152
try:
152153
# Perform SNNP Polling for string profile in inventory.csv
153-
if "." not in profile:
154+
if not is_oid(profile):
154155
logger.info(
155156
f"Executing SNMP Polling for Varbinds in config.yaml for {host} profile={profile}"
156157
)

tests/test_task_utilities.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from splunk_connect_for_snmp_poller.manager.task_utilities import (
1919
_sort_walk_data,
2020
is_metric_data,
21+
is_oid,
2122
parse_port,
2223
)
2324

@@ -110,3 +111,23 @@ def test__sort_walk_data_non_metric(self):
110111
self.assertEqual(merged_result_metric, [])
111112
self.assertEqual(merged_result, [varbind_metric_dict])
112113
self.assertEqual(merged_result_non_metric, [varbind])
114+
115+
def test_is_oid_asterisk(self):
116+
oid = "1.3.6.1.2.1.2.2.1.1.*"
117+
self.assertTrue(is_oid(oid))
118+
119+
def test_is_oid(self):
120+
oid = "1.3.6.1.2.1"
121+
self.assertTrue(is_oid(oid))
122+
123+
def test_is_oid_multinumber(self):
124+
oid = "1.3.6.1.2.195.218.254.105.56134.205.188.8.43.5190"
125+
self.assertTrue(is_oid(oid))
126+
127+
def test_is_oid_profile(self):
128+
oid = "router"
129+
self.assertFalse(is_oid(oid))
130+
131+
def test_is_oid_profile_with_dot(self):
132+
oid = "router.12"
133+
self.assertFalse(is_oid(oid))

0 commit comments

Comments
 (0)