From e0e81ed7154d7de3f105fea3184465f8ca33acab Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Sat, 27 Jan 2024 10:06:46 -0600 Subject: [PATCH] Finish porting the Sonar integration over --- src/integrationSonar.py | 17 ++++++++------- .../lqos_config/src/etc/python_migration.rs | 8 +++++-- src/rust/lqos_config/src/etc/v15/example.toml | 3 +++ .../src/etc/v15/sonar_integration.rs | 8 +++++-- src/rust/lqos_python/src/lib.rs | 21 +++++++++++++++++++ 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/integrationSonar.py b/src/integrationSonar.py index 8de14288..9044d221 100644 --- a/src/integrationSonar.py +++ b/src/integrationSonar.py @@ -2,9 +2,10 @@ from pythonCheck import checkPythonVersion checkPythonVersion() import requests import subprocess -from liblqos_python import sonar_api_key, sonar_api_url, snmp_community -from ispConfig import sonar_airmax_ap_model_ids,sonar_active_status_ids,sonar_ltu_ap_model_ids -all_models = sonar_airmax_ap_model_ids + sonar_ltu_ap_model_ids +from liblqos_python import sonar_api_key, sonar_api_url, snmp_community, sonar_airmax_ap_model_ids, \ + sonar_ltu_ap_model_ids, sonar_active_status_ids +#from ispConfig import sonar_airmax_ap_model_ids,sonar_active_status_ids,sonar_ltu_ap_model_ids +all_models = sonar_airmax_ap_model_ids() + sonar_ltu_ap_model_ids() from integrationCommon import NetworkGraph, NetworkNode, NodeType from multiprocessing.pool import ThreadPool @@ -37,7 +38,7 @@ def sonarRequest(query,variables={}): return sonar_list def getActiveStatuses(): - if not sonar_active_status_ids: + if not sonar_active_status_ids(): query = """query getActiveStatuses { account_statuses (activates_account: true) { entities { @@ -53,7 +54,7 @@ def getActiveStatuses(): status_ids.append(status['id']) return status_ids else: - return sonar_active_status_ids + return sonar_active_status_ids() # Sometimes the IP will be under the field data for an item and sometimes it will be assigned to the inventory item itself. def findIPs(inventory_item): @@ -185,7 +186,7 @@ def getAccounts(sonar_active_status_ids): }""" active_status_ids = [] - for status_id in sonar_active_status_ids: + for status_id in sonar_active_status_ids(): active_status_ids.append({ "attribute": "account_status_id", "operator": "EQ", @@ -247,9 +248,9 @@ def getAccounts(sonar_active_status_ids): def mapApCpeMacs(ap): macs = [] macs_output = None - if ap['model'] in sonar_airmax_ap_model_ids: #Tested with Prism Gen2AC and Rocket M5. + if ap['model'] in sonar_airmax_ap_model_ids(): #Tested with Prism Gen2AC and Rocket M5. macs_output = subprocess.run(['snmpwalk', '-Os', '-v', '1', '-c', snmp_community(), ap['ip'], '.1.3.6.1.4.1.41112.1.4.7.1.1.1'], capture_output=True).stdout.decode('utf8') - if ap['model'] in sonar_ltu_ap_model_ids: #Tested with LTU Rocket + if ap['model'] in sonar_ltu_ap_model_ids(): #Tested with LTU Rocket macs_output = subprocess.run(['snmpwalk', '-Os', '-v', '1', '-c', snmp_community(), ap['ip'], '.1.3.6.1.4.1.41112.1.10.1.4.1.11'], capture_output=True).stdout.decode('utf8') if macs_output: name_output = subprocess.run(['snmpwalk', '-Os', '-v', '1', '-c', snmp_community(), ap['ip'], '.1.3.6.1.2.1.1.5.0'], capture_output=True).stdout.decode('utf8') diff --git a/src/rust/lqos_config/src/etc/python_migration.rs b/src/rust/lqos_config/src/etc/python_migration.rs index 797bbde7..92454f3f 100644 --- a/src/rust/lqos_config/src/etc/python_migration.rs +++ b/src/rust/lqos_config/src/etc/python_migration.rs @@ -100,8 +100,9 @@ pub struct PythonMigration { pub sonar_api_url: String, pub sonar_api_key: String, pub snmp_community: String, - // TODO: It isn't clear what types `sonar_api_key,sonar_airmax_ap_model_ids,sonar_active_status_ids,sonar_ltu_ap_model_ids` - // are supposed to be. + pub sonar_airmax_ap_model_ids: Vec, + pub sonar_ltu_ap_model_ids: Vec, + pub sonar_active_status_ids: Vec, // TODO: httpRestIntegrationConfig } @@ -177,6 +178,9 @@ impl PythonMigration { cfg.sonar_api_key = from_python(&py, "sonar_api_key").unwrap_or("".to_string()); cfg.sonar_api_url = from_python(&py, "sonar_api_url").unwrap_or("".to_string()); cfg.snmp_community = from_python(&py, "snmp_community").unwrap_or("public".to_string()); + cfg.sonar_active_status_ids = from_python(&py, "sonar_active_status_ids").unwrap_or(vec![]); + cfg.sonar_airmax_ap_model_ids = from_python(&py, "sonar_airmax_ap_model_ids").unwrap_or(vec![]); + cfg.sonar_ltu_ap_model_ids = from_python(&py, "sonar_ltu_ap_model_ids").unwrap_or(vec![]); Ok(()) } diff --git a/src/rust/lqos_config/src/etc/v15/example.toml b/src/rust/lqos_config/src/etc/v15/example.toml index 0fbdfe3a..184ae217 100644 --- a/src/rust/lqos_config/src/etc/v15/example.toml +++ b/src/rust/lqos_config/src/etc/v15/example.toml @@ -90,3 +90,6 @@ enable_sonar = false sonar_api_key = "" sonar_api_url = "" snmp_community = "public" +airmax_model_ids = [ "" ] +ltu_model_ids = [ "" ] +active_status_ids = [ "" ] diff --git a/src/rust/lqos_config/src/etc/v15/sonar_integration.rs b/src/rust/lqos_config/src/etc/v15/sonar_integration.rs index 94cc16e7..1dfb5ca4 100644 --- a/src/rust/lqos_config/src/etc/v15/sonar_integration.rs +++ b/src/rust/lqos_config/src/etc/v15/sonar_integration.rs @@ -6,8 +6,9 @@ pub struct SonarIntegration { pub sonar_api_url: String, pub sonar_api_key: String, pub snmp_community: String, - // TODO: It isn't clear what types `sonar_api_key,sonar_airmax_ap_model_ids,sonar_active_status_ids,sonar_ltu_ap_model_ids` - // are supposed to be. + pub airmax_model_ids: Vec, + pub ltu_model_ids: Vec, + pub active_status_ids: Vec, } impl Default for SonarIntegration { @@ -17,6 +18,9 @@ impl Default for SonarIntegration { sonar_api_url: "".to_string(), sonar_api_key: "".to_string(), snmp_community: "public".to_string(), + airmax_model_ids: vec![], + ltu_model_ids: vec![], + active_status_ids: vec![], } } } \ No newline at end of file diff --git a/src/rust/lqos_python/src/lib.rs b/src/rust/lqos_python/src/lib.rs index d0340d67..a7afa1a6 100644 --- a/src/rust/lqos_python/src/lib.rs +++ b/src/rust/lqos_python/src/lib.rs @@ -78,6 +78,9 @@ fn liblqos_python(_py: Python, m: &PyModule) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(sonar_api_url))?; m.add_wrapped(wrap_pyfunction!(sonar_api_key))?; m.add_wrapped(wrap_pyfunction!(snmp_community))?; + m.add_wrapped(wrap_pyfunction!(sonar_airmax_ap_model_ids))?; + m.add_wrapped(wrap_pyfunction!(sonar_ltu_ap_model_ids))?; + m.add_wrapped(wrap_pyfunction!(sonar_active_status_ids))?; Ok(()) } @@ -581,4 +584,22 @@ fn sonar_api_key() -> PyResult { fn snmp_community() -> PyResult { let config = lqos_config::load_config().unwrap(); Ok(config.sonar_integration.snmp_community) +} + +#[pyfunction] +fn sonar_airmax_ap_model_ids() -> PyResult> { + let config = lqos_config::load_config().unwrap(); + Ok(config.sonar_integration.airmax_model_ids) +} + +#[pyfunction] +fn sonar_ltu_ap_model_ids() -> PyResult> { + let config = lqos_config::load_config().unwrap(); + Ok(config.sonar_integration.ltu_model_ids) +} + +#[pyfunction] +fn sonar_active_status_ids() -> PyResult> { + let config = lqos_config::load_config().unwrap(); + Ok(config.sonar_integration.active_status_ids) } \ No newline at end of file