Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: assign profiles #1133

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sonar.projectKey=github-mirrors.splunk-connect-for-snmp

sonar.exclusions=test/**/*, integration_tests/**/*, **/*.js, **/*.yaml, ui_tests/**/*, docs/**/*, examples/*
sonar.exclusions=test/**/*, integration_tests/**/*, **/*.js, **/*.yaml, **/*.yml, ui_tests/**/*, docs/**/*, examples/*
sonar.test.inclusions=test/*
92 changes: 52 additions & 40 deletions splunk_connect_for_snmp/inventory/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,40 +156,26 @@ def assign_profiles(ir, profiles, target):
assigned_profiles: dict[int, list[str]] = {}
address = transform_address_to_key(ir.address, ir.port)
computed_profiles = []
if ir.smart_profiles:
for profile_name, profile in profiles.items():
assign_smart_profiles(assigned_profiles, ir, profiles, target)

if not is_smart_profile_valid(profile_name, profile):
continue
logger.debug(f"ir.profiles {ir.profiles}")
logger.debug(f"profiles {profiles}")
check_profiles_type(address, assigned_profiles, computed_profiles, ir, profiles)

# skip this profile it is static
if profile["condition"]["type"] == "base" and POLL_BASE_PROFILES:
logger.debug(f"Adding base profile {profile_name}")
add_profile_to_assigned_list(
assigned_profiles, profile["frequency"], profile_name
)
mandatory_profiles = [
(profile_name, profile.get("frequency"))
for profile_name, profile in profiles.items()
if profile.get("condition", {}).get("type") == "mandatory"
]
for m_profile_name, m_profile_frequency in mandatory_profiles:
add_profile_to_assigned_list(
assigned_profiles, m_profile_frequency, m_profile_name
)

elif profile["condition"]["type"] == "field":
logger.debug(f"profile is a field condition {profile_name}")
if "state" in target and (
profile["condition"]["field"].replace(".", "|") in target["state"]
):
cs = target["state"][
profile["condition"]["field"].replace(".", "|")
]
if "value" in cs:
for pattern in profile["condition"]["patterns"]:
result = re.search(pattern, cs["value"])
if result:
logger.debug(f"Adding smart profile {profile_name}")
add_profile_to_assigned_list(
assigned_profiles,
profile["frequency"],
profile_name,
)
return assigned_profiles, computed_profiles

logger.debug(f"ir.profiles {ir.profiles}")
logger.debug(f"profiles {profiles}")

def check_profiles_type(address, assigned_profiles, computed_profiles, ir, profiles):
for profile_name in ir.profiles:
if profile_name in profiles:
profile = profiles[profile_name]
Expand All @@ -216,17 +202,43 @@ def assign_profiles(ir, profiles, target):
f"profile {profile_name} was assigned for the host: {address}, no such profile in the config"
)

mandatory_profiles = [
(profile_name, profile.get("frequency"))
for profile_name, profile in profiles.items()
if profile.get("condition", {}).get("type") == "mandatory"
]
for m_profile_name, m_profile_frequency in mandatory_profiles:
add_profile_to_assigned_list(
assigned_profiles, m_profile_frequency, m_profile_name
)

return assigned_profiles, computed_profiles
def assign_smart_profiles(assigned_profiles, ir, profiles, target):
if ir.smart_profiles:
for profile_name, profile in profiles.items():

if not is_smart_profile_valid(profile_name, profile):
continue

# skip this profile it is static
if profile["condition"]["type"] == "base" and POLL_BASE_PROFILES:
logger.debug(f"Adding base profile {profile_name}")
add_profile_to_assigned_list(
assigned_profiles, profile["frequency"], profile_name
)

elif profile["condition"]["type"] == "field":
logger.debug(f"profile is a field condition {profile_name}")
assign_field_smart_profile(
assigned_profiles, profile, profile_name, target
)


def assign_field_smart_profile(assigned_profiles, profile, profile_name, target):
if "state" in target and (
profile["condition"]["field"].replace(".", "|") in target["state"]
):
cs = target["state"][profile["condition"]["field"].replace(".", "|")]
if "value" in cs:
for pattern in profile["condition"]["patterns"]:
result = re.search(pattern, cs["value"])
if result:
logger.debug(f"Adding smart profile {profile_name}")
add_profile_to_assigned_list(
assigned_profiles,
profile["frequency"],
profile_name,
)


def is_smart_profile_valid(profile_name, profile):
Expand Down
Loading