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

Commit 92e091e

Browse files
authored
Fix/default freq dynamic (#160)
* fix: apply default frequency for dynamic profiles * fix: fixes * fix: fixes * refactor: PR comments * refactor: PR comments
1 parent 8e24841 commit 92e091e

File tree

6 files changed

+36
-25
lines changed

6 files changed

+36
-25
lines changed

splunk_connect_for_snmp_poller/manager/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@
3636
"AES256BLMT": config.usmAesBlumenthalCfb256Protocol,
3737
"NONE": config.usmNoPrivProtocol,
3838
}
39+
40+
DEFAULT_POLLING_FREQUENCY = 60

splunk_connect_for_snmp_poller/manager/poller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def process_unmatched_devices(self, server_config):
292292

293293
if any(descr):
294294
assigned_profiles = assign_profiles_to_device(
295-
profiles["profiles"], descr
295+
profiles["profiles"], descr, host
296296
)
297297
processed_devices.add(host)
298298

splunk_connect_for_snmp_poller/manager/poller_utilities.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import schedule
2121
from pysnmp.hlapi import ObjectIdentity, ObjectType, UdpTransportTarget, getCmd
2222

23+
from splunk_connect_for_snmp_poller.manager.const import DEFAULT_POLLING_FREQUENCY
2324
from splunk_connect_for_snmp_poller.manager.data.inventory_record import InventoryRecord
2425
from splunk_connect_for_snmp_poller.manager.realtime.oid_constant import OidConstant
2526
from splunk_connect_for_snmp_poller.manager.realtime.real_time_data import (
@@ -76,20 +77,20 @@ def parse_inventory_file(inventory_file_path, profiles, fetch_frequency=True):
7677
agent["version"],
7778
agent["community"],
7879
agent["profile"],
79-
get_frequency(agent, profiles, 60)
80+
get_frequency(agent, profiles, DEFAULT_POLLING_FREQUENCY)
8081
if fetch_frequency and agent["profile"] != DYNAMIC_PROFILE
8182
else None,
8283
)
8384

8485

8586
def get_frequency(agent, profiles, default_frequency):
86-
if "profile" in agent:
87-
frequency = multi_key_lookup(
88-
profiles, ("profiles", agent["profile"], "frequency")
89-
)
90-
if frequency:
91-
return frequency
92-
logger.debug(f'Default frequency was assigned for agent = {agent.get("host")}')
87+
frequency = multi_key_lookup(profiles, ("profiles", agent["profile"], "frequency"))
88+
if frequency:
89+
return frequency
90+
logger.debug(
91+
f'Default frequency={DEFAULT_POLLING_FREQUENCY} was assigned for agent={agent.get("host")}, '
92+
f'profile={agent["profile"]}'
93+
)
9394
return default_frequency
9495

9596

splunk_connect_for_snmp_poller/manager/profile_matching.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import yaml
2020

21+
from splunk_connect_for_snmp_poller.manager.const import DEFAULT_POLLING_FREQUENCY
2122
from splunk_connect_for_snmp_poller.manager.mib_server_client import get_mib_profiles
2223
from splunk_connect_for_snmp_poller.manager.realtime.oid_constant import OidConstant
2324
from splunk_connect_for_snmp_poller.utilities import multi_key_lookup
@@ -33,20 +34,28 @@ def extract_desc(realtime_collection):
3334
return sys_descr, sys_object_id
3435

3536

36-
def assign_profiles_to_device(profiles, device_desc):
37+
def assign_profiles_to_device(profiles, device_desc, host):
3738
result = []
3839
for profile in profiles:
39-
if "patterns" in profiles[profile]:
40-
match_profile_with_device(device_desc, profile, profiles, result)
40+
if profiles[profile].get("patterns"):
41+
match_profile_with_device(device_desc, profile, profiles, result, host)
4142
return result
4243

4344

44-
def match_profile_with_device(device_desc, profile, profiles, result):
45+
def match_profile_with_device(device_desc, profile, profiles, result, host):
4546
for pattern in profiles[profile]["patterns"]:
4647
compiled = re.compile(pattern)
4748
for desc in device_desc:
4849
if desc and compiled.match(desc):
49-
result.append((profile, profiles[profile]["frequency"]))
50+
if "frequency" in profiles[profile]:
51+
frequency = profiles[profile]["frequency"]
52+
else:
53+
frequency = DEFAULT_POLLING_FREQUENCY
54+
logger.debug(
55+
f"Default frequency={DEFAULT_POLLING_FREQUENCY} was assigned for agent={host}, "
56+
f"profile={profile}"
57+
)
58+
result.append((profile, frequency))
5059
return
5160

5261

tests/test_poller_utilities.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ def test_return_frequency(self):
5454
result = get_frequency(agent, profiles, 60)
5555
self.assertEqual(result, 20)
5656

57-
def test_return_default_frequency_when_agent_does_not_have_profile(self):
58-
agent = {}
59-
profiles = {"profiles": {"some_profile": {"frequency": 20}}}
60-
result = get_frequency(agent, profiles, 60)
61-
self.assertEqual(result, 60)
62-
6357
def test_return_default_frequency_when_agent_profile_not_present_in_profiles(self):
6458
agent = {"profile": "some_profile"}
6559
profiles = {"profiles": {"some_profile_2": {"frequency": 20}}}

tests/test_profile_matching.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def test_return_tuple_with_none_values(self):
5858
def test_assign_profile_to_device(self):
5959
profiles = {"zeus": {"patterns": [".*zeus.*"], "frequency": 20}}
6060

61-
result = assign_profiles_to_device(profiles, ("My zeus device", None))
61+
result = assign_profiles_to_device(
62+
profiles, ("My zeus device", None), "localhost"
63+
)
6264

6365
self.assertEqual(len(result), 1)
6466
profile, frequency = next(iter(result))
@@ -72,7 +74,7 @@ def test_assign_multiple_profile_to_device(self):
7274
}
7375

7476
result = assign_profiles_to_device(
75-
profiles, ("My zeus device, linux 2.3.4", None)
77+
profiles, ("My zeus device, linux 2.3.4", None), "localhost"
7678
)
7779

7880
self.assertEqual(len(result), 2)
@@ -90,7 +92,9 @@ def test_assign_multiple_profile_to_device_from_different_descs(self):
9092
"linux": {"patterns": [".*linux.*"], "frequency": 30},
9193
}
9294

93-
result = assign_profiles_to_device(profiles, ("My zeus device", "linux 2.3.4"))
95+
result = assign_profiles_to_device(
96+
profiles, ("My zeus device", "linux 2.3.4"), "localhost"
97+
)
9498

9599
self.assertEqual(len(result), 2)
96100
profile, frequency = result[0]
@@ -104,6 +108,7 @@ def test_assign_multiple_profile_to_device_from_different_descs(self):
104108
def test_no_assignment_when_patterns_are_missing(self):
105109
profiles = {"zeus": {"frequency": 20}}
106110

107-
result = assign_profiles_to_device(profiles, ("My zeus device", None))
108-
111+
result = assign_profiles_to_device(
112+
profiles, ("My zeus device", None), "localhost"
113+
)
109114
self.assertEqual(len(result), 0)

0 commit comments

Comments
 (0)