Skip to content

Commit

Permalink
[change] Add monitoring info to AirOS backend
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Jun 15, 2021
1 parent c9962e2 commit 01de297
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 18 deletions.
91 changes: 77 additions & 14 deletions netengine/backends/snmp/airos.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import binascii
import logging
from datetime import timedelta
import time
from datetime import datetime, timedelta

from .base import SNMP

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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,
}
)
10 changes: 9 additions & 1 deletion tests/static/test-airos-snmp.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
25 changes: 22 additions & 3 deletions tests/test_snmp/test_airos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

0 comments on commit 01de297

Please sign in to comment.