From 8cd70894336cf7da49219aacd1b53dd6dd22b840 Mon Sep 17 00:00:00 2001 From: purhan Date: Tue, 15 Jun 2021 15:16:07 +0530 Subject: [PATCH] [change] Add monitoring info to AirOS backend --- netengine/backends/snmp/airos.py | 91 ++++++++++++++++++++++++++----- tests/static/test-airos-snmp.json | 10 +++- tests/test_snmp/test_airos.py | 25 ++++++++- 3 files changed, 108 insertions(+), 18 deletions(-) diff --git a/netengine/backends/snmp/airos.py b/netengine/backends/snmp/airos.py index e194c5d..17aa7e1 100644 --- a/netengine/backends/snmp/airos.py +++ b/netengine/backends/snmp/airos.py @@ -7,7 +7,8 @@ import binascii import logging -from datetime import timedelta +import time +from datetime import datetime from .base import SNMP @@ -98,15 +99,6 @@ def uptime(self): """ return int(self.get_value('1.3.6.1.2.1.1.3.0')) // 100 - @property - def uptime_tuple(self): - """ - returns (days, hours, minutes) - """ - td = timedelta(seconds=self.uptime) - - return td.days, td.seconds // 3600, (td.seconds // 60) % 60 - @property def interfaces_number(self): """ @@ -381,6 +373,17 @@ def wireless_links(self): final.append(result) return final + @property + def local_time(self): + """ + returns the local time of the host device as a timestamp + """ + epoch = str(self.get('1.3.6.1.4.1.41112.1.4.8.1.0')[3][0][1]) + timestamp = int( + time.mktime(datetime.strptime(epoch, '%Y-%m-%d %H:%M:%S').timetuple()) + ) + return timestamp + @property def RAM_total(self): """ @@ -397,14 +400,74 @@ def RAM_free(self): free = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.2.0') return int(free) + @property + def RAM_buffered(self): + """ + Returns the buffered RAM of the device + """ + buffered = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.3.0') + return int(buffered) + + @property + def RAM_cached(self): + """ + Returns the cached RAM of the device + """ + cached = self.get_value('1.3.6.1.4.1.10002.1.1.1.1.4.0') + return int(cached) + + @property + def load(self): + """ + Returns an array with load average values respectively in the last + minute, in the last 5 minutes and in the last 15 minutes + """ + one = int(self.get_value('1.3.6.1.4.1.10002.1.1.1.4.2.1.3.1')) + five = int(self.get_value('1.3.6.1.4.1.10002.1.1.1.4.2.1.3.2')) + fifteen = int(self.get_value('1.3.6.1.4.1.10002.1.1.1.4.2.1.3.3')) + return [one, five, fifteen] + + @property + def SWAP_total(self): + """ + Returns the total SWAP of the device + """ + total = self.get_value('1.3.6.1.4.1.10002.1.1.1.2.1.0') + return int(total) + + @property + def SWAP_free(self): + """ + Returns the free SWAP of the device + """ + free = self.get_value('1.3.6.1.4.1.10002.1.1.1.2.2.0') + return int(free) + + @property + def resources_to_dict(self): + """ + returns an ordered dict with hardware resources information + """ + result = self._dict( + { + 'load': self.load, + 'memory': { + 'total': self.RAM_total, + 'buffered': self.RAM_buffered, + 'free': self.RAM_free, + 'cached': self.RAM_cached, + }, + 'swap': {'total': self.SWAP_total, 'free': self.SWAP_free}, + } + ) + return result + def to_dict(self): return self._dict( { 'type': 'DeviceMonitoring', - 'general': {'uptime': self.uptime}, - 'resources': { - 'memory': {'total': self.RAM_total, 'free': self.RAM_free}, - }, + 'general': {'uptime': self.uptime, 'local_time': self.local_time}, + 'resources': self.resources_to_dict, 'interfaces': self.interfaces_to_dict, } ) diff --git a/tests/static/test-airos-snmp.json b/tests/static/test-airos-snmp.json index 6890337..2f6f0ec 100644 --- a/tests/static/test-airos-snmp.json +++ b/tests/static/test-airos-snmp.json @@ -46,5 +46,13 @@ "1.3.6.1.2.1.2.2.1.5.2": "0", "1.3.6.1.2.1.2.2.1.5.3": "0", "1.3.6.1.2.1.2.2.1.5.4": "0", - "1.3.6.1.2.1.2.2.1.5.5": "0" + "1.3.6.1.2.1.2.2.1.5.5": "0", + "1.3.6.1.4.1.41112.1.4.8.1.0": "2020-02-03 13:01:14", + "1.3.6.1.4.1.10002.1.1.1.1.3.0": "2648", + "1.3.6.1.4.1.10002.1.1.1.1.4.0": "0", + "1.3.6.1.4.1.10002.1.1.1.4.2.1.3.1": "64", + "1.3.6.1.4.1.10002.1.1.1.4.2.1.3.2": "69", + "1.3.6.1.4.1.10002.1.1.1.4.2.1.3.3": "32", + "1.3.6.1.4.1.10002.1.1.1.2.1.0": "0", + "1.3.6.1.4.1.10002.1.1.1.2.2.0": "0" } diff --git a/tests/test_snmp/test_airos.py b/tests/test_snmp/test_airos.py index a9b5315..70ee9e0 100644 --- a/tests/test_snmp/test_airos.py +++ b/tests/test_snmp/test_airos.py @@ -73,7 +73,6 @@ def test_properties(self): device.model device.os device.uptime - device.uptime_tuple def test_name(self): self.assertIsInstance(self.device.name, str) @@ -141,8 +140,28 @@ def test_firmware(self): def test_uptime(self): self.assertIsInstance(self.device.uptime, int) - def test_uptime_tuple(self): - self.assertIsInstance(self.device.uptime_tuple, tuple) + def test_RAM_buffered(self): + self.assertIsInstance(self.device.RAM_buffered, int) + + def test_RAM_cached(self): + self.assertIsInstance(self.device.RAM_cached, int) + + def test_SWAP_total(self): + self.assertIsInstance(self.device.SWAP_total, int) + + def test_SWAP_free(self): + self.assertIsInstance(self.device.SWAP_free, int) + + def test_local_time(self): + self.assertIsInstance(self.device.local_time, int) + + def test_load(self): + load = self.device.load + self.assertIsInstance(load, list) + self.assertEquals(len(load), 3) + self.assertIsInstance(load[0], int) + self.assertIsInstance(load[1], int) + self.assertIsInstance(load[2], int) def tearDown(self): patch.stopall()