Skip to content

Commit 13d837a

Browse files
committed
chore: fixed some minor & major incompatible bugs
1 parent a931d55 commit 13d837a

File tree

9 files changed

+91
-39
lines changed

9 files changed

+91
-39
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
# python black formatting
33
[tool.black]
44
line-length = 80
5+
6+
[tool.pytest.ini_options]
7+
asyncio_mode = "auto"

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ graphql_server==3.0.0b5
3232
# Testing
3333
mock
3434
pytest
35-
pytest-asyncio
35+
pytest-asyncio>=0.24,<0.25
3636

3737
# Other
3838
more-itertools

switchmap/poller/snmp/mib/cisco/mib_ciscoprocess.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ async def system(self):
4343
)
4444

4545
# Populate final results with better structure
46-
if cpu_data and not isinstance(cpu_data, Exception):
46+
if cpu_data is not None and not isinstance(cpu_data, Exception):
4747
final["CISCO-PROCESS-MIB"]["cpmCPUTotal5minRev"] = cpu_data
4848

49-
if memory_used_data and not isinstance(memory_used_data, Exception):
49+
if memory_used_data is not None and not isinstance(
50+
memory_used_data, Exception
51+
):
5052
final["CISCO-PROCESS-MIB"][
5153
"ciscoMemoryPoolUsed"
5254
] = memory_used_data
5355

54-
if memory_free_data and not isinstance(memory_free_data, Exception):
56+
if memory_free_data is not None and not isinstance(
57+
memory_free_data, Exception
58+
):
5559
final["CISCO-PROCESS-MIB"][
5660
"ciscoMemoryPoolFree"
5761
] = memory_free_data
@@ -83,16 +87,16 @@ async def cpmcputotal5minrev(self, oidonly=False):
8387
return data_dict
8488

8589
async def memorypoolused(self, oidonly=False):
86-
"""Get total used memory from CISCO-MEMORY-POOL-MIB (ciscoMemoryPoolUsed).
90+
"""Get total used memory from CISCO-MEMORY-POOL-MIB.
8791
8892
Args:
8993
oidonly (bool): If True, return the OID string instead of querying.
94+
9095
Returns:
9196
int | str | dict: Sum of used memory in bytes,
9297
OID string if oidonly=True,
93-
or empty dict on error.
98+
or None on error.
9499
"""
95-
96100
# Process OID - Enhanced memory pool used (high capacity)
97101
oid = ".1.3.6.1.4.1.9.9.48.1.1.1.5"
98102

@@ -110,17 +114,16 @@ async def memorypoolused(self, oidonly=False):
110114
return None
111115

112116
async def memorypoolfree(self, oidonly=False):
113-
"""Get total free memory from CISCO-MEMORY-POOL-MIB (ciscoMemoryPoolFree).
117+
"""Get total free memory from CISCO-MEMORY-POOL-MIB.
114118
115119
Args:
116120
oidonly (bool): If True, return the OID string instead of querying.
121+
117122
Returns:
118123
int | str | dict: Sum of free memory in bytes,
119124
OID string if oidonly=True,
120-
or empty dict on error.
125+
or None on error.
121126
"""
122-
# Initialize key variables
123-
124127
# Process OID - Enhanced memory pool free (high capacity)
125128
oid = ".1.3.6.1.4.1.9.9.48.1.1.1.6"
126129

switchmap/poller/snmp/mib/generic/mib_if.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ async def ifstackstatus(self, oidonly=False):
716716

717717
async def ifInUcastPkts(self, oidonly=False):
718718
"""Get inbound unicast packet counters for each interface.
719+
719720
Args:
720721
oidonly: Return OID's value, not results, if True
721722
Returns:
@@ -738,6 +739,7 @@ async def ifInUcastPkts(self, oidonly=False):
738739

739740
async def ifOutUcastPkts(self, oidonly=False):
740741
"""Get Outbound unicast packet counters for each interface.
742+
741743
Args:
742744
oidonly: Return OID's value, not results, if True
743745
Returns:
@@ -760,6 +762,7 @@ async def ifOutUcastPkts(self, oidonly=False):
760762

761763
async def ifInErrors(self, oidonly=False):
762764
"""Return dict of IFMIB ifInErrors for each ifIndex for device.
765+
763766
Args:
764767
oidonly: Return OID's value, not results, if True
765768
Returns:
@@ -782,6 +785,7 @@ async def ifInErrors(self, oidonly=False):
782785

783786
async def ifInDiscards(self, oidonly=False):
784787
"""Return dict of IFMIB ifInDiscards for each ifIndex for device.
788+
785789
Args:
786790
oidonly: Return OID's value, not results, if True
787791
Returns:

switchmap/poller/snmp/mib/juniper/mib_juniperprocess.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"Module for JUNIPER-PROCESS-MIB." ""
1+
"""Module for JUNIPER-PROCESS-MIB."""
22

33
from collections import defaultdict
44
from switchmap.poller.snmp.base_query import Query
@@ -42,15 +42,19 @@ async def system(self):
4242
return_exceptions=True,
4343
)
4444
# Populate final results
45-
if cpu_data and not isinstance(cpu_data, Exception):
45+
if cpu_data is not None and not isinstance(cpu_data, Exception):
4646
final["JUNIPER-MIB"]["jnxOperatingCPU"] = cpu_data
4747

48-
if memory_used_data and not isinstance(memory_used_data, Exception):
48+
if memory_used_data is not None and not isinstance(
49+
memory_used_data, Exception
50+
):
4951
final["JUNIPER-MIB"][
5052
"jnxOperatingMemoryUsed"
5153
] = memory_used_data
5254

53-
if memory_free_data and not isinstance(memory_free_data, Exception):
55+
if memory_free_data is not None and not isinstance(
56+
memory_free_data, Exception
57+
):
5458
final["JUNIPER-MIB"][
5559
"jnxOperatingMemoryFree"
5660
] = memory_free_data

switchmap/poller/snmp/snmp_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, snmp_object):
2323
"""Instantiate the class.
2424
2525
Args:
26-
snmp_object: SNMP interact class object from async_snmp_manager.py
26+
snmp_object: SNMP interact class object from snmp_manager.py
2727
2828
Returns:
2929
None

switchmap/poller/snmp/snmp_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ async def walk(
431431
blank values.
432432
433433
Returns:
434-
result: Dictionary of tuples (OID, value)
434+
result: Dictionary of {OID: value} pairs
435435
"""
436436
(_, _, result) = await self.query(
437437
oid_to_get,
@@ -689,7 +689,7 @@ async def _session(self, walk_operation=False):
689689
else:
690690
log.log2warning(
691691
1218,
692-
f"Unknown auth protocol '{auth.privprotocol}',"
692+
f"Unknown privacy protocol '{auth.privprotocol}',"
693693
f"leaving unset",
694694
)
695695
priv_protocol = None

switchmap/server/db/ingest/update/device.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from copy import deepcopy
66
from operator import attrgetter
77

8+
from sqlalchemy import false
9+
from sqlalchemy.orm.base import instance_dict
10+
811
# Application imports
912
from switchmap.core import log
1013
from switchmap.core import general
@@ -529,9 +532,15 @@ def macport(self, test=False):
529532
# Initialize variables
530533
valid_mac = None
531534

532-
# Create lowercase version of MAC address
535+
# Normalize possible bytes OctetString to hex
536+
raw_mac = (
537+
next_mac.hex()
538+
if isinstance(next_mac, (bytes, bytearray))
539+
else next_mac
540+
)
541+
533542
# Handle double-encoded MAC addresses from async poller
534-
decoded_mac = decode_mac_address(next_mac)
543+
decoded_mac = decode_mac_address(raw_mac)
535544
mactest = general.mac(decoded_mac)
536545
if bool(mactest.valid) is False:
537546
continue
@@ -613,8 +622,18 @@ def ipport(self, test=False):
613622

614623
# Iterate over the MACs found
615624
for item in sorted(_macs):
625+
# Normalize possible bytes OctetString to hex and decode
626+
raw_mac = (
627+
item.hex()
628+
if isinstance(item, (bytes, bytearray))
629+
else item
630+
)
631+
decoded = decode_mac_address(raw_mac)
632+
mtest = general.mac(decoded)
633+
if bool(mtest.valid) is False:
634+
continue
616635
# Ensure the MAC exists in the database
617-
mac_exists = _mac.exists(self._device.idx_zone, item)
636+
mac_exists = _mac.exists(self._device.idx_zone, mtest.mac)
618637
if bool(mac_exists) is True:
619638
# Get all the IP addresses for the mac
620639
found = _macip.idx_ips_exist(mac_exists.idx_mac)
@@ -664,24 +683,43 @@ def systemstat(self, test=False):
664683
# Log start
665684
self.log("SystemStat")
666685

667-
# Extract CISCO MIB data from system section
686+
# Extract vendor-specific MIB data from system section
668687
system_data = self._data.get("system", {})
669688

670-
# Get CPU data from CISCO-PROCESS-MIB
689+
# Initialize variables
671690
cpu_5min = None
672-
cisco_process = system_data.get("CISCO-PROCESS-MIB", {})
673-
cpu_total_5min = cisco_process.get("cpmCPUTotal5minRev", {})
674-
if cpu_total_5min:
675-
# Get the first CPU value (usually CPU '1')
676-
cpu_5min = next(iter(cpu_total_5min.values()), None)
677-
678-
# Get Memory data from CISCO-MEMORY-POOL-MIB
679691
mem_used = None
680692
mem_free = None
681-
cisco_memory = system_data.get("CISCO-MEMORY-POOL-MIB", {})
682-
if cisco_memory:
683-
mem_used = cisco_memory.get("ciscoMemoryPoolUsed")
684-
mem_free = cisco_memory.get("ciscoMemoryPoolFree")
693+
694+
# Get CPU and Memory data from CISCO-PROCESS-MIB
695+
cisco_process = system_data.get("CISCO-PROCESS-MIB", {})
696+
if cisco_process:
697+
# Get CPU data
698+
cpu_total_5min = cisco_process.get("cpmCPUTotal5minRev", {})
699+
if cpu_total_5min:
700+
# Get the first CPU value (usually CPU '1')
701+
cpu_5min = next(iter(cpu_total_5min.values()), None)
702+
703+
# Get Memory data from CISCO-PROCESS-MIB
704+
if not mem_used:
705+
mem_used = cisco_process.get("ciscoMemoryPoolUsed")
706+
if not mem_free:
707+
mem_free = cisco_process.get("ciscoMemoryPoolFree")
708+
709+
# Get CPU and Memory data from JUNIPER-MIB
710+
juniper_process = system_data.get("JUNIPER-MIB", {})
711+
if juniper_process:
712+
# Get CPU data
713+
juniper_cpu = juniper_process.get("jnxOperatingCPU", {})
714+
if juniper_cpu and cpu_5min is None:
715+
# Get the first CPU value
716+
cpu_5min = next(iter(juniper_cpu.values()), None)
717+
718+
# Get Memory data
719+
if not mem_used:
720+
mem_used = juniper_process.get("jnxOperatingMemoryUsed")
721+
if not mem_free:
722+
mem_free = juniper_process.get("jnxOperatingMemoryFree")
685723

686724
# Only create record if we have some data
687725
if cpu_5min is not None or mem_used is not None or mem_free is not None:
@@ -701,8 +739,8 @@ def systemstat(self, test=False):
701739
else:
702740
# Insert new record
703741
if not test:
704-
_systemstat.insert_row(inserts)
705-
# Log completion
742+
_systemstat.insert_row(row)
743+
706744
self.log("SystemStat", updated=True)
707745

708746
# Mark as completed

switchmap/server/db/table/systemstat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
def idx_exists(idx):
1212
"""Determine whether primary key exists.
13+
1314
Args:
1415
idx: idx_systemstat
1516
Returns:
@@ -25,16 +26,14 @@ def idx_exists(idx):
2526

2627
# Return
2728
for row in rows:
28-
#! i think we are just finding if row exist we are ret bool
2929
result = _rows.systemstat(row)
30-
#! why breaking just after appending result for single row
31-
#! are we just checking if there exists a single row or not
3230
break
3331
return result
3432

3533

3634
def device_exists(idx_device):
3735
"""Determine whether SystemStat record exists for a device.
36+
3837
Args:
3938
idx_device: Device index
4039
Returns:
@@ -90,6 +89,7 @@ def insert_row(rows):
9089

9190
def update_row(idx, row):
9291
"""Update a systemstat table entry.
92+
9393
Args:
9494
idx: idx_systemstat value
9595
row: SystemStat object

0 commit comments

Comments
 (0)