Skip to content

Commit

Permalink
[change] Change RAM OIDs, add load info
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Jul 26, 2021
1 parent 0f37717 commit 07f3e59
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 62 deletions.
30 changes: 15 additions & 15 deletions netengine/backends/snmp/airos.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,56 +398,56 @@ def local_time(self, snmpdump=None):

def RAM_total(self, snmpdump=None):
"""
Returns the total RAM of the device
Returns the total RAM of the device in bytes
"""
total = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.1.0', snmpdump=snmpdump)
return int(total)
return int(total * 1024)

def RAM_free(self, snmpdump=None):
"""
Returns the free RAM of the device
Returns the free RAM of the device in bytes
"""
free = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.2.0', snmpdump=snmpdump)
return int(free)
return int(free * 1024)

def RAM_buffered(self, snmpdump=None):
"""
Returns the buffered RAM of the device
Returns the buffered RAM of the device in bytes
"""
buffered = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.3.0', snmpdump=snmpdump)
return int(buffered)
return int(buffered * 1024)

def RAM_cached(self, snmpdump=None):
"""
Returns the cached RAM of the device
Returns the cached RAM of the device in bytes
"""
cached = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.4.0', snmpdump=snmpdump)
return int(cached)
return int(cached * 1024)

def load(self, snmpdump=None):
"""
Returns an array with load average values respectively in the last
minute, in the last 5 minutes and in the last 15 minutes
"""
array = self.next('1.3.6.1.4.1.10002.1.1.1.4.2.1.3.', snmpdump=snmpdump)[3]
one = int(array[0][0][1])
five = int(array[1][0][1])
fifteen = int(array[2][0][1])
one = float(array[0][0][1]) / 100
five = float(array[1][0][1]) / 100
fifteen = float(array[2][0][1]) / 100
return [one, five, fifteen]

def SWAP_total(self, snmpdump=None):
"""
Returns the total SWAP of the device
Returns the total SWAP of the device in bytes
"""
total = self.get_value('1.3.6.1.4.1.10002.1.1.1.2.1.0', snmpdump=snmpdump)
return int(total)
return int(total * 1024)

def SWAP_free(self, snmpdump=None):
"""
Returns the free SWAP of the device
Returns the free SWAP of the device in bytes
"""
free = self.get_value('1.3.6.1.4.1.10002.1.1.1.2.2.0', snmpdump=snmpdump)
return int(free)
return int(free * 1024)

def resources_to_dict(self, snmpdump=None):
"""
Expand Down
80 changes: 40 additions & 40 deletions netengine/backends/snmp/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,19 @@ def interface_addr_and_mask(self, snmpdump=None):

results = {}

# TODO: Add ipv6 addresses
for i in range(0, len(interface_ip_address)):
a = interface_ip_address[i][0][1].asNumbers()
ip_address = '.'.join(str(a[i]) for i in range(0, len(a)))
b = interface_netmask[i][0][1].asNumbers()
netmask = '.'.join(str(b[i]) for i in range(0, len(b)))

name = self._interface_dict[int(interface_index[i][0][1])]

results[name] = {'address': ip_address, 'netmask': netmask}
results[name] = {
'family': 'ipv4',
'address': ip_address,
'mask': netmask,
}

self._interface_addr_and_mask = results

Expand Down Expand Up @@ -358,11 +362,8 @@ def interfaces_to_dict(self, snmpdump=None):
logger.info('... mtu ...')
mtu = int(self.interfaces_mtu(snmpdump=snmpdump)[i]['mtu'])
logger.info('... if_ip ...')
if_ip = (
self.interface_addr_and_mask(snmpdump=snmpdump)
.get(name, {})
.get('address', '')
)
addr = self.interface_addr_and_mask(snmpdump=snmpdump).get(name)
addresses = [addr] if addr is not None else []

result = self._dict(
{
Expand All @@ -374,7 +375,7 @@ def interfaces_to_dict(self, snmpdump=None):
'rx_bytes': rx_bytes,
'tx_bytes': tx_bytes,
"mtu": mtu,
"ip": if_ip,
"addresses": addresses,
},
}
)
Expand Down Expand Up @@ -435,82 +436,81 @@ def local_time(self, snmpdump=None):

def RAM_total(self, snmpdump=None):
"""
returns the total RAM of the device
returns the total RAM of the device in bytes
"""
return int(self.get_value('1.3.6.1.2.1.25.2.3.1.5.1', snmpdump=snmpdump))
return int(self.get_value('1.3.6.1.4.1.2021.4.5.0', snmpdump=snmpdump)) * 1024

def RAM_shared(self, snmpdump=None):
"""
returns the shared RAM of the device
returns the shared RAM of the device in bytes
"""
return int(self.get_value('1.3.6.1.2.1.25.2.3.1.6.8', snmpdump=snmpdump))
return int(self.get_value('1.3.6.1.4.1.2021.4.13.0', snmpdump=snmpdump)) * 1024

def RAM_cached(self, snmpdump=None):
"""
returns the cached RAM of the device
returns the cached RAM of the device in bytes
"""
return int(self.get_value('1.3.6.1.2.1.25.2.3.1.5.7', snmpdump=snmpdump))

def RAM_used(self, snmpdump=None):
"""
returns the used RAM of the device
"""
return int(self.get_value('1.3.6.1.2.1.25.2.3.1.6.1', snmpdump=snmpdump))
return int(self.get_value('1.3.6.1.4.1.2021.4.15.0', snmpdump=snmpdump)) * 1024

def RAM_free(self, snmpdump=None):
"""
returns the free RAM of the device
returns the free RAM of the device in bytes
"""
return int(
self.RAM_total(snmpdump=snmpdump)
- (self.RAM_used(snmpdump=snmpdump) - self.RAM_cached(snmpdump=snmpdump))
)
return int(self.get_value('1.3.6.1.4.1.2021.4.11.0', snmpdump=snmpdump)) * 1024

def SWAP_total(self, snmpdump=None):
def RAM_buffered(self, snmpdump=None):
"""
returns the total SWAP of the device
returns the buffered RAM of the device in bytes
"""
return int(self.get_value('1.3.6.1.2.1.25.2.3.1.5.10', snmpdump=snmpdump))
return int(self.get_value('1.3.6.1.4.1.2021.4.14.0', snmpdump=snmpdump)) * 1024

def SWAP_used(self, snmpdump=None):
def SWAP_total(self, snmpdump=None):
"""
returns the used SWAP of the device
returns the total SWAP of the device in bytes
"""
return int(self.get_value('1.3.6.1.2.1.25.2.3.1.6.10', snmpdump=snmpdump))
return int(self.get_value('1.3.6.1.4.1.2021.4.3.0', snmpdump=snmpdump)) * 1024

def SWAP_free(self, snmpdump=None):
"""
returns the free SWAP of the device
returns the free SWAP of the device in bytes
"""
SWAP_free = self.SWAP_total(snmpdump=snmpdump) - self.SWAP_used(
snmpdump=snmpdump
)
return SWAP_free
return int(self.get_value('1.3.6.1.4.1.2021.4.4.0', snmpdump=snmpdump)) * 1024

def CPU_count(self, snmpdump=None):
"""
returns the count of CPUs of the device
"""
return len(self.next('1.3.6.1.2.1.25.3.3.1.2.', snmpdump=snmpdump)[3])

def load(self, snmpdump=None):
"""
Returns an array with load average values respectively in the last
minute, in the last 5 minutes and in the last 15 minutes
"""
array = self.next('1.3.6.1.4.1.2021.10.1.3.', snmpdump=snmpdump)[3]
one = float(array[0][0][1])
five = float(array[1][0][1])
fifteen = float(array[2][0][1])
return [one, five, fifteen]

def resources_to_dict(self, snmpdump=None):
"""
returns an ordered dict with hardware resources information
"""
result = self._dict(
{
'load': self.load(snmpdump=snmpdump),
'cpus': self.CPU_count(snmpdump=snmpdump),
'memory': {
'total': self.RAM_total(snmpdump=snmpdump),
'shared': self.RAM_shared(snmpdump=snmpdump),
'used': self.RAM_used(snmpdump=snmpdump),
'free': self.RAM_free(snmpdump=snmpdump),
'cached': self.RAM_cached(snmpdump=snmpdump),
'buffered': self.RAM_buffered(snmpdump=snmpdump),
},
'swap': {
'total': self.SWAP_total(snmpdump=snmpdump),
'free': self.SWAP_free(snmpdump=snmpdump),
'used': self.SWAP_used(snmpdump=snmpdump),
},
}
)
Expand Down Expand Up @@ -574,12 +574,12 @@ def neighbors(self, snmpdump=None):

def to_dict(self, snmpdump=None, autowalk=True):
if autowalk:
snmpdump = Trie(self.walk('1.3.6'))
snmpdump = Trie(self.walk('1.2'))
result = self._dict(
{
'type': 'DeviceMonitoring',
'general': {
'name': self.name(snmpdump=snmpdump),
'hostname': self.name(snmpdump=snmpdump),
'uptime': self.uptime(snmpdump=snmpdump),
'local_time': self.local_time(snmpdump=snmpdump),
},
Expand Down
16 changes: 15 additions & 1 deletion tests/static/test-openwrt-snmp-oid.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,19 @@
"type": "bytes",
"value": "\\x07\\xe5\\x06\\x0b\\x06\\x00\r\\x00+\\x00\\x00"
},
"1.3.6.1.2.1.31.1.1.1.1.5": "br-lan"
"1.3.6.1.2.1.31.1.1.1.1.5": "br-lan",
"1.3.6.1.4.1.2021.10.1.3.1": "0.07",
"1.3.6.1.4.1.2021.10.1.3.2": "0.13",
"1.3.6.1.4.1.2021.10.1.3.3": "0.12",
"1.3.6.1.4.1.2021.4.3.0": "0",
"1.3.6.1.4.1.2021.4.4.0": "0",
"1.3.6.1.4.1.2021.4.5.0": "60012",
"1.3.6.1.4.1.2021.4.6.0": "32932",
"1.3.6.1.4.1.2021.4.11.0": "32932",
"1.3.6.1.4.1.2021.4.12.0": "16000",
"1.3.6.1.4.1.2021.4.13.0": "96",
"1.3.6.1.4.1.2021.4.14.0": "2300",
"1.3.6.1.4.1.2021.4.15.0": "7600",
"1.3.6.1.4.1.2021.4.18.0": "0",
"1.3.6.1.4.1.2021.4.19.0": "0"
}
6 changes: 3 additions & 3 deletions tests/test_snmp/test_airos.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ def test_load(self):
load = self.device.load()
self.assertIsInstance(load, list)
self.assertEqual(len(load), 3)
self.assertIsInstance(load[0], int)
self.assertIsInstance(load[1], int)
self.assertIsInstance(load[2], int)
self.assertIsInstance(load[0], float)
self.assertIsInstance(load[1], float)
self.assertIsInstance(load[2], float)

def tearDown(self):
patch.stopall()
11 changes: 8 additions & 3 deletions tests/test_snmp/test_openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ def test_RAM_shared(self):
def test_RAM_cached(self):
self.assertIsInstance(self.device.RAM_cached(), int)

def test_RAM_used(self):
self.assertIsInstance(self.device.RAM_used(), int)

def test_RAM_free(self):
self.assertIsInstance(self.device.RAM_free(), int)

Expand Down Expand Up @@ -125,5 +122,13 @@ def test_netjson_compliance(self):
validate(instance=device_dict, schema=schema)
validate(instance=json.loads(device_json), schema=schema)

def test_load(self):
load = self.device.load()
self.assertIsInstance(load, list)
self.assertEqual(len(load), 3)
self.assertIsInstance(load[0], float)
self.assertIsInstance(load[1], float)
self.assertIsInstance(load[2], float)

def tearDown(self):
patch.stopall()
1 change: 1 addition & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _get_nextcmd_list(return_value):
[[[MockOid('1.3.6.1.2.1.4.35.1.4')]], OctetString('0x040e3cca555f')],
[[[MockOid('1.3.6.1.2.1.4.35.1.7')]], 1],
],
'1.3.6.1.4.1.2021.10.1.3.': [[[0, '0.87']], [[0, '0.37']], [[0, '0.14']],],
}
oid = args[2]
return _get_nextcmd_list(res[oid])

0 comments on commit 07f3e59

Please sign in to comment.