Skip to content

Commit 01de297

Browse files
committed
[change] Add monitoring info to AirOS backend
1 parent c9962e2 commit 01de297

3 files changed

Lines changed: 108 additions & 18 deletions

File tree

‎netengine/backends/snmp/airos.py‎

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import binascii
99
import logging
10-
from datetime import timedelta
10+
import time
11+
from datetime import datetime, timedelta
1112

1213
from .base import SNMP
1314

@@ -98,15 +99,6 @@ def uptime(self):
9899
"""
99100
return int(self.get_value('1.3.6.1.2.1.1.3.0')) // 100
100101

101-
@property
102-
def uptime_tuple(self):
103-
"""
104-
returns (days, hours, minutes)
105-
"""
106-
td = timedelta(seconds=self.uptime)
107-
108-
return td.days, td.seconds // 3600, (td.seconds // 60) % 60
109-
110102
@property
111103
def interfaces_number(self):
112104
"""
@@ -381,6 +373,17 @@ def wireless_links(self):
381373
final.append(result)
382374
return final
383375

376+
@property
377+
def local_time(self):
378+
"""
379+
returns the local time of the host device as a timestamp
380+
"""
381+
epoch = str(self.get('1.3.6.1.4.1.41112.1.4.8.1.0')[3][0][1])
382+
timestamp = int(
383+
time.mktime(datetime.strptime(epoch, '%Y-%m-%d %H:%M:%S').timetuple())
384+
)
385+
return timestamp
386+
384387
@property
385388
def RAM_total(self):
386389
"""
@@ -397,14 +400,74 @@ def RAM_free(self):
397400
free = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.2.0')
398401
return int(free)
399402

403+
@property
404+
def RAM_buffered(self):
405+
"""
406+
Returns the buffered RAM of the device
407+
"""
408+
buffered = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.3.0')
409+
return int(buffered)
410+
411+
@property
412+
def RAM_cached(self):
413+
"""
414+
Returns the cached RAM of the device
415+
"""
416+
cached = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.4.0')
417+
return int(cached)
418+
419+
@property
420+
def load(self):
421+
"""
422+
Returns an array with load average values respectively in the last
423+
minute, in the last 5 minutes and in the last 15 minutes
424+
"""
425+
one = int(self.get_value('1.3.6.1.4.1.10002.1.1.1.4.2.1.3.1'))
426+
five = int(self.get_value('1.3.6.1.4.1.10002.1.1.1.4.2.1.3.2'))
427+
fifteen = int(self.get_value('1.3.6.1.4.1.10002.1.1.1.4.2.1.3.3'))
428+
return [one, five, fifteen]
429+
430+
@property
431+
def SWAP_total(self):
432+
"""
433+
Returns the total SWAP of the device
434+
"""
435+
total = self.get_value('1.3.6.1.4.1.10002.1.1.1.2.1.0')
436+
return int(total)
437+
438+
@property
439+
def SWAP_free(self):
440+
"""
441+
Returns the free SWAP of the device
442+
"""
443+
free = self.get_value('1.3.6.1.4.1.10002.1.1.1.2.2.0')
444+
return int(free)
445+
446+
@property
447+
def resources_to_dict(self):
448+
"""
449+
returns an ordered dict with hardware resources information
450+
"""
451+
result = self._dict(
452+
{
453+
'load': self.load,
454+
'memory': {
455+
'total': self.RAM_total,
456+
'buffered': self.RAM_buffered,
457+
'free': self.RAM_free,
458+
'cached': self.RAM_cached,
459+
},
460+
'swap': {'total': self.SWAP_total, 'free': self.SWAP_free},
461+
}
462+
)
463+
return result
464+
400465
def to_dict(self):
401466
return self._dict(
402467
{
403468
'type': 'DeviceMonitoring',
404-
'general': {'uptime': self.uptime},
405-
'resources': {
406-
'memory': {'total': self.RAM_total, 'free': self.RAM_free},
407-
},
469+
'general': {'uptime': self.uptime, 'local_time': self.local_time},
470+
'resources': self.resources_to_dict,
408471
'interfaces': self.interfaces_to_dict,
409472
}
410473
)

‎tests/static/test-airos-snmp.json‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,13 @@
4646
"1.3.6.1.2.1.2.2.1.5.2": "0",
4747
"1.3.6.1.2.1.2.2.1.5.3": "0",
4848
"1.3.6.1.2.1.2.2.1.5.4": "0",
49-
"1.3.6.1.2.1.2.2.1.5.5": "0"
49+
"1.3.6.1.2.1.2.2.1.5.5": "0",
50+
"1.3.6.1.4.1.41112.1.4.8.1.0": "2020-02-03 13:01:14",
51+
"1.3.6.1.4.1.10002.1.1.1.1.3.0": "2648",
52+
"1.3.6.1.4.1.10002.1.1.1.1.4.0": "0",
53+
"1.3.6.1.4.1.10002.1.1.1.4.2.1.3.1": "64",
54+
"1.3.6.1.4.1.10002.1.1.1.4.2.1.3.2": "69",
55+
"1.3.6.1.4.1.10002.1.1.1.4.2.1.3.3": "32",
56+
"1.3.6.1.4.1.10002.1.1.1.2.1.0": "0",
57+
"1.3.6.1.4.1.10002.1.1.1.2.2.0": "0"
5058
}

‎tests/test_snmp/test_airos.py‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ def test_properties(self):
7373
device.model
7474
device.os
7575
device.uptime
76-
device.uptime_tuple
7776

7877
def test_name(self):
7978
self.assertIsInstance(self.device.name, str)
@@ -141,8 +140,28 @@ def test_firmware(self):
141140
def test_uptime(self):
142141
self.assertIsInstance(self.device.uptime, int)
143142

144-
def test_uptime_tuple(self):
145-
self.assertIsInstance(self.device.uptime_tuple, tuple)
143+
def test_RAM_buffered(self):
144+
self.assertIsInstance(self.device.RAM_buffered, int)
145+
146+
def test_RAM_cached(self):
147+
self.assertIsInstance(self.device.RAM_cached, int)
148+
149+
def test_SWAP_total(self):
150+
self.assertIsInstance(self.device.SWAP_total, int)
151+
152+
def test_SWAP_free(self):
153+
self.assertIsInstance(self.device.SWAP_free, int)
154+
155+
def test_local_time(self):
156+
self.assertIsInstance(self.device.local_time, int)
157+
158+
def test_load(self):
159+
load = self.device.load
160+
self.assertIsInstance(load, list)
161+
self.assertEquals(len(load), 3)
162+
self.assertIsInstance(load[0], int)
163+
self.assertIsInstance(load[1], int)
164+
self.assertIsInstance(load[2], int)
146165

147166
def tearDown(self):
148167
patch.stopall()

0 commit comments

Comments
 (0)