From 7b53176371204d44ecf81f45311949b3a501cde3 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 30 Apr 2021 13:31:38 +0200 Subject: [PATCH] Rework get_environment (the command 'execute sensor detail' is not present in everz device) --- napalm_fortios/fortios.py | 108 +++----- .../normal/execute_sensor_detail.txt | 255 ------------------ .../normal/execute_sensor_list.txt | 30 +++ .../normal/expected_result.json | 2 +- 4 files changed, 70 insertions(+), 325 deletions(-) delete mode 100644 test/unit/mocked_data/test_get_environment/normal/execute_sensor_detail.txt create mode 100644 test/unit/mocked_data/test_get_environment/normal/execute_sensor_list.txt diff --git a/napalm_fortios/fortios.py b/napalm_fortios/fortios.py index 54957cc..2220a46 100644 --- a/napalm_fortios/fortios.py +++ b/napalm_fortios/fortios.py @@ -436,29 +436,6 @@ class FortiOSDriver(NetworkDriver): def get_environment(self): - def parse_string(line): - return re.sub(' +', ' ', line.lower()) - - def search_disabled(line): - m = re.search("(.+?) (.+?) alarm=(.+?) \(scanning disabled\)", line) - return m.group(2) - - def search_normal(line): - m = re.search("(.+?) (.+?) alarm=(.+?) value=(.+?) threshold_status=(.+?)", line) - return m - - def get_fans(fan_lines): - output = dict() - for fan_line in fan_lines: - if 'disabled' in fan_line: - name = search_disabled(fan_line) - output[name] = dict(status=False) - continue - - m = search_normal(fan_line) - output[m.group(2)] = dict(status=True) - return output - def get_cpu(cpu_lines): output = dict() for l in cpu_lines: @@ -474,46 +451,39 @@ class FortiOSDriver(NetworkDriver): total, used = int(memory_line[1]) >> 20, int(memory_line[2]) >> 20 # byte to MB return dict(available_ram=total, used_ram=used) - def get_temperature(temperature_lines, detail_block): - output = dict() - for temp_line in temperature_lines: - if 'disabled' in temp_line: - sensor_name = search_disabled(temp_line) - output[sensor_name] = {'is_alert': False, 'is_critical': False, - 'temperature': 0.0} - continue - - m = search_normal(temp_line) - sensor_name, temp_value, status = m.group(2), m.group(4), int(m.group(5)) - is_alert = True if status == 1 else False - - # find block - fullline = self._search_line_in_lines(sensor_name, detail_block) - index_line = detail_block.index(fullline) - sensor_block = detail_block[index_line:] - - v = int(self._search_line_in_lines('upper_non_recoverable', - sensor_block).split('=')[1]) - temp_value = int(temp_value) - - output[sensor_name] = dict(temperature=float(temp_value), is_alert=is_alert, - is_critical=True if temp_value > v else False) - - return output - out = dict() - sensors_block = [parse_string(x) for x in - self._execute_command_with_vdom('execute sensor detail', vdom='global') - if x] + #execute sensor detail is not available + sensors_block = self._execute_command_with_vdom('execute sensor list', vdom='global') - # temp - temp_lines = [x for x in sensors_block - if any([True for y in ['dts', 'temp', 'adt7490'] if y in x])] - out['temperature'] = get_temperature(temp_lines, sensors_block) + temperatures=dict() + fans=dict() + powers = dict() - # fans - out['fans'] = get_fans([x for x in sensors_block if 'fan' in x and 'temp' not in x]) + for line in sensors_block: + m = re.search("([0-9]+?) (.+\s[0-9]+?) (.+?) value=([0-9\.]+)", line) + if m: + if "TMP" in m.group(2).strip(): + sensor_name, temp_value=m.group(3).strip(), m.group(4) + temp_value=float(temp_value) + temperatures[sensor_name] = dict(temperature=temp_value, is_alert=False, is_critical=False) + elif "FAN" in m.group(2): + fans[m.group(2)+" - "+m.group(3).strip()] = dict(status=True) + else: + m = re.search("([0-9]+?)\s(.+?)\s+alarm=([0-9]+)\s+value=([0-9\.]+)\s+threshold_status=([0-9]+)", line) + if m: + if "TMP" in m.group(2) or "Temp" in m.group(2): + sensor_name, temp_value=m.group(2).strip(), m.group(4) + temp_value=float(temp_value) + temperatures[sensor_name] = dict(temperature=temp_value, is_alert=int(m.group(5)), is_critical=int(m.group(3))) + elif "FAN" in m.group(2).upper(): + fans[m.group(2).strip()] = dict(status=(not bool(int(m.group(3))))) + elif m.group(2)[0]=="+": + powers[ m.group(2).strip()] = dict(status=(not bool(int(m.group(3)))), capacity=-1.0, output=-1.0) + + out['fans'] = fans + out['temperature'] = temperatures + out['power'] = powers # cpu out['cpu'] = get_cpu( @@ -522,16 +492,15 @@ class FortiOSDriver(NetworkDriver): vdom='global')[1:] if x]) # memory - memory_command = 'diag hard sys mem | grep Mem:' - t = [x for x in - re.split('\s+', self._execute_command_with_vdom(memory_command, - vdom='global')[0]) if x] - out['memory'] = get_memory(t) - - # power, not implemented - sensors = [x.split()[1] for x in sensors_block if x.split()[0].isdigit()] - psus = {x for x in sensors if x.startswith('ps')} - out['power'] = {t: {'status': True, 'capacity': -1.0, 'output': -1.0} for t in psus} + memory_block = self._execute_command_with_vdom('diag hard sys mem | grep Mem', vdom='global') + out['memory'] = dict(available_ram=-1, used_ram=-1) + for line in memory_block: + if line.strip().startswith("MemTotal:"): + out['memory']["available_ram"]=(int(line.split()[1])>>10) + if line.strip().startswith("MemFree:"): + out['memory']["used_ram"]=(int(line.split()[1])>>10) + if line.strip().startswith("Mem:"): + out['memory']=get_memory(line.split()) return out @@ -555,3 +524,4 @@ class FortiOSDriver(NetworkDriver): } arp_table.append(entry) return arp_table + diff --git a/test/unit/mocked_data/test_get_environment/normal/execute_sensor_detail.txt b/test/unit/mocked_data/test_get_environment/normal/execute_sensor_detail.txt deleted file mode 100644 index f5b1554..0000000 --- a/test/unit/mocked_data/test_get_environment/normal/execute_sensor_detail.txt +++ /dev/null @@ -1,255 +0,0 @@ - 1 P1V8_SFR_V alarm=0 value=1.8135 threshold_status=0 - type=2/1 - upper_non_recoverable=1.9801 - upper_critical=1.9507 - upper_non_critical=1.8919 - lower_non_critical=1.7155 - lower_critical=1.6567 - lower_non_recoverable=1.6273 - 2 AD_1.2VA alarm=0 value=1.2059 threshold_status=0 - type=2/1 - upper_non_recoverable=1.3235 - upper_critical=1.3039 - upper_non_critical=1.2647 - lower_non_critical=1.1373 - lower_critical=1.1079 - lower_non_recoverable=1.0883 - 3 AD_+12V alarm=0 value=12.1065 threshold_status=0 - type=2/1 - upper_non_recoverable=13.7585 - upper_critical=13.1685 - upper_non_critical=12.6375 - lower_non_critical=11.3985 - lower_critical=10.8085 - lower_non_recoverable=10.2185 - 4 AD_1V_10G alarm=0 value=1.0099 threshold_status=0 - type=2/1 - upper_non_recoverable=1.1079 - upper_critical=1.0883 - upper_non_critical=1.0589 - lower_non_critical=0.9511 - lower_critical=0.9217 - lower_non_recoverable=0.9021 - 5 AD_1.5VA alarm=0 value=1.4999 threshold_status=0 - type=2/1 - upper_non_recoverable=1.6567 - upper_critical=1.6273 - upper_non_critical=1.5783 - lower_non_critical=1.4313 - lower_critical=1.3823 - lower_non_recoverable=1.3529 - 6 AD_1P05_PCH alarm=0 value=1.0491 threshold_status=0 - type=2/1 - upper_non_recoverable=1.1569 - upper_critical=1.1373 - upper_non_critical=1.1079 - lower_non_critical=0.9903 - lower_critical=0.9707 - lower_non_recoverable=0.9511 - 7 AD_1.2VSW alarm=0 value=1.2157 threshold_status=0 - type=2/1 - upper_non_recoverable=1.3235 - upper_critical=1.3039 - upper_non_critical=1.2843 - lower_non_critical=1.1373 - lower_critical=1.1079 - lower_non_recoverable=1.0883 - 8 VCCP alarm=0 value=1.0788 threshold_status=0 - type=2/1 - upper_non_recoverable=1.5117 - upper_critical=1.4649 - upper_non_critical=1.4064 - lower_non_critical=0.6459 - lower_critical=0.6225 - lower_non_recoverable=0.5991 - 9 P1V1_VTT alarm=0 value=1.1118 threshold_status=0 - type=2/1 - upper_non_recoverable=1.2086 - upper_critical=1.1822 - upper_non_critical=1.1558 - lower_non_critical=1.0502 - lower_critical=1.015 - lower_non_recoverable=0.9886 -10 +12V alarm=0 value=12.089762 threshold_status=0 - type=2/1 - upper_non_recoverable=13.825762 - upper_critical=13.205762 - upper_non_critical=12.647762 - lower_non_critical=11.469762 - lower_critical=10.911762 - lower_non_recoverable=10.291762 -11 +5V alarm=0 value=5.05405 threshold_status=0 - type=2/1 - upper_non_recoverable=5.52565 - upper_critical=5.44705 - upper_non_critical=5.42085 - lower_non_critical=4.79205 - lower_critical=4.63485 - lower_non_recoverable=4.53005 -12 P3V3_STBY alarm=0 value=3.33765 threshold_status=0 - type=2/1 - upper_non_recoverable=3.63005 - upper_critical=3.56125 - upper_non_critical=3.47525 - lower_non_critical=3.13125 - lower_critical=3.02805 - lower_non_recoverable=2.95925 -13 DTS CPU alarm=0 value=54 threshold_status=0 - type=1/1 - upper_non_recoverable=85 - upper_critical=80 - upper_non_critical=75 -14 ADT7490 Local alarm=0 value=48 threshold_status=0 - type=1/1 - upper_non_recoverable=80 - upper_critical=75 - upper_non_critical=70 -15 ADT7490 Rem1 alarm=0 value=58 threshold_status=0 - type=1/1 - upper_non_recoverable=85 - upper_critical=80 - upper_non_critical=75 -16 ADT7490 Rem2 alarm=0 value=56 threshold_status=0 - type=1/1 - upper_non_recoverable=82 - upper_critical=77 - upper_non_critical=72 -17 LM75 alarm=0 value=47 threshold_status=0 - type=1/1 - upper_non_recoverable=83 - upper_critical=78 - upper_non_critical=73 -18 Fan 1 alarm=0 value=6700 threshold_status=0 - type=4/1 - upper_non_recoverable=25000 - upper_critical=24000 - upper_non_critical=22000 - lower_non_critical=4000 - lower_critical=3000 - lower_non_recoverable=2000 -19 Fan 2 alarm=0 value=6700 threshold_status=0 - type=4/1 - upper_non_recoverable=25000 - upper_critical=24000 - upper_non_critical=22000 - lower_non_critical=4000 - lower_critical=3000 - lower_non_recoverable=2000 -20 Fan 3 alarm=0 value=6900 threshold_status=0 - type=4/1 - upper_non_recoverable=25000 - upper_critical=24000 - upper_non_critical=22000 - lower_non_critical=4000 - lower_critical=3000 - lower_non_recoverable=2000 -21 Fan 4 alarm=0 value=6900 threshold_status=0 - type=4/1 - upper_non_recoverable=25000 - upper_critical=24000 - upper_non_critical=22000 - lower_non_critical=4000 - lower_critical=3000 - lower_non_recoverable=2000 -22 Fan 5 alarm=0 value=6200 threshold_status=0 - type=4/1 - upper_non_recoverable=25000 - upper_critical=24000 - upper_non_critical=22000 - lower_non_critical=4000 - lower_critical=3000 - lower_non_recoverable=2000 -23 PS1 Temp alarm=0 value=41 threshold_status=0 - type=1/1 - upper_non_recoverable=78 - upper_critical=73 - upper_non_critical=68 -24 PS1 Fan 1 alarm=0 value=6784 threshold_status=0 - type=4/1 - upper_non_recoverable=32000 - upper_critical=29056 - upper_non_critical=26112 - lower_non_critical=1024 - lower_critical=768 - lower_non_recoverable=512 -25 PS1 VIN alarm=0 value=230 threshold_status=0 - type=2/1 - upper_non_recoverable=255 - upper_critical=254 - upper_non_critical=250 - lower_non_critical=38 - lower_critical=36 - lower_non_recoverable=34 -26 PS1 VOUT alarm=0 value=12.032 threshold_status=0 - type=2/1 - upper_non_recoverable=13.229 - upper_critical=12.977 - upper_non_critical=12.662 - lower_non_critical=11.528 - lower_critical=11.15 - lower_non_recoverable=10.898 -27 PS1 Status alarm=0 - type=8/111 - upper_non_recoverable=0 - upper_critical=0 - upper_non_critical=0 -28 PS2 Temp alarm=0 (scanning disabled) - type=1/1 - upper_non_recoverable=78 - upper_critical=73 - upper_non_critical=68 -29 PS2 Fan 1 alarm=0 (scanning disabled) - type=4/1 - upper_non_recoverable=32000 - upper_critical=29056 - upper_non_critical=26112 - lower_non_critical=1024 - lower_critical=768 - lower_non_recoverable=512 -30 PS2 VIN alarm=0 (scanning disabled) - type=2/1 - upper_non_recoverable=255 - upper_critical=254 - upper_non_critical=250 - lower_non_critical=38 - lower_critical=36 - lower_non_recoverable=34 -31 PS2 VOUT alarm=0 (scanning disabled) - type=2/1 - upper_non_recoverable=13.229 - upper_critical=12.977 - upper_non_critical=12.662 - lower_non_critical=11.528 - lower_critical=11.15 - lower_non_recoverable=10.898 -32 PS2 Status alarm=0 (not detected) - type=8/111 - upper_non_recoverable=0 - upper_critical=0 - upper_non_critical=0 -33 INA219 PS1 Vsht alarm=0 value=0.00832 threshold_status=0 - type=2/1 - upper_non_recoverable=0.03232 - upper_critical=0.03104 - upper_non_critical=0.03008 -34 INA219 PS1 Vbus alarm=0 value=12.032 threshold_status=0 - type=2/1 - upper_non_recoverable=13.312 - upper_critical=13.056 - upper_non_critical=12.672 - lower_non_critical=11.392 - lower_critical=11.136 - lower_non_recoverable=10.88 -35 INA219 PS2 Vsht alarm=0 value=0.00032 threshold_status=0 - type=2/1 - upper_non_recoverable=0.03232 - upper_critical=0.03104 - upper_non_critical=0.03008 -36 INA219 PS2 Vbus alarm=0 value=12.032 threshold_status=0 - type=2/1 - upper_non_recoverable=13.312 - upper_critical=13.056 - upper_non_critical=12.672 - lower_non_critical=11.392 - lower_critical=11.136 - lower_non_recoverable=10.88 diff --git a/test/unit/mocked_data/test_get_environment/normal/execute_sensor_list.txt b/test/unit/mocked_data/test_get_environment/normal/execute_sensor_list.txt new file mode 100644 index 0000000..1be7b7f --- /dev/null +++ b/test/unit/mocked_data/test_get_environment/normal/execute_sensor_list.txt @@ -0,0 +1,30 @@ +1 +3.3V alarm=0 value=3.2856 threshold_status=0 +2 +5V alarm=0 value=5.0235 threshold_status=0 +3 +12V alarm=0 value=12.136 threshold_status=0 +4 CPU VCCP alarm=0 value=0.9609 threshold_status=0 +5 CPU VTT alarm=0 value=1.0589 threshold_status=0 +6 CPU PVSA alarm=0 value=0.9315 threshold_status=0 +7 P1V8 alarm=0 value=1.7841 threshold_status=0 +8 P1V5 alarm=0 value=1.4999 threshold_status=0 +9 PCH +1.05V alarm=0 value=1.04 threshold_status=0 +10 VCC 2V5 alarm=0 value=2.432 threshold_status=0 +11 MAIN 12V alarm=0 value=11.904 threshold_status=0 +12 VCC 1V15 alarm=0 value=1.136 threshold_status=0 +13 DDR3 VTT alarm=0 value=0.736 threshold_status=0 +14 RPS 12V alarm=1 value=0 threshold_status=0x7 +15 NCT +3.3V alarm=0 value=3.216 threshold_status=0 +16 NCT VBAT alarm=0 value=3.264 threshold_status=0 +17 NCT +3.3VSB alarm=0 value=3.216 threshold_status=0 +18 NCT VTT alarm=0 value=1.04 threshold_status=0 +19 DTS CPU alarm=0 value=50 threshold_status=0 +20 CPU Core 0 alarm=0 value=49 threshold_status=0 +21 CPU Core 1 alarm=0 value=50 threshold_status=0 +22 TD1 alarm=0 value=37 threshold_status=0 +23 TD2 alarm=0 value=25 threshold_status=0 +24 FAN_TMP_3 alarm=0 value=35 threshold_status=0 +25 LM75 U72 alarm=0 value=28 threshold_status=0 +26 LM75 U65 alarm=0 value=31 threshold_status=0 +27 LM75 U62 alarm=0 value=32 threshold_status=0 +28 FAN1 alarm=0 value=4900 threshold_status=0 +29 FAN2 alarm=0 value=5000 threshold_status=0 +30 FAN3 alarm=0 value=4700 threshold_status=0 diff --git a/test/unit/mocked_data/test_get_environment/normal/expected_result.json b/test/unit/mocked_data/test_get_environment/normal/expected_result.json index ac06b26..382a66b 100644 --- a/test/unit/mocked_data/test_get_environment/normal/expected_result.json +++ b/test/unit/mocked_data/test_get_environment/normal/expected_result.json @@ -1 +1 @@ -{"fans": {"ps2 fan 1": {"status": false}, "ps1 fan 1": {"status": true}, "fan 2": {"status": true}, "fan 3": {"status": true}, "fan 1": {"status": true}, "fan 4": {"status": true}, "fan 5": {"status": true}}, "cpu": {"CPU2": {"%usage": 4.0}, "CPU3": {"%usage": 4.0}, "CPU0": {"%usage": 8.0}, "CPU1": {"%usage": 4.0}}, "temperature": {"adt7490 rem1": {"is_critical": false, "temperature": 58.0, "is_alert": false}, "adt7490 rem2": {"is_critical": false, "temperature": 56.0, "is_alert": false}, "dts cpu": {"is_critical": false, "temperature": 54.0, "is_alert": false}, "ps2 temp": {"is_alert": false, "temperature": 0.0, "is_critical": false}, "ps1 temp": {"is_critical": false, "temperature": 41.0, "is_alert": false}, "adt7490 local": {"is_critical": false, "temperature": 48.0, "is_alert": false}}, "power": {"ps2": {"status": true, "output": -1.0, "capacity": -1.0}, "ps1": {"status": true, "output": -1.0, "capacity": -1.0}}, "memory": {"available_ram": 7962, "used_ram": 1148}} +{'fans': {'FAN1': {'status': true}, 'FAN2': {'status': true}, 'FAN3': {'status': true}}, 'temperature': {'FAN_TMP_3': {'temperature': 35.0, 'is_alert': 0, 'is_critical': 0}}, 'power': {'+3.3V': {'status': true, 'capacity': -1.0, 'output': -1.0}, '+5V': {'status': true, 'capacity': -1.0, 'output': -1.0}, '+12V': {'status': true, 'capacity': -1.0, 'output': -1.0}}, 'cpu': {'CPU0': {'%usage': 5.0}, 'CPU1': {'%usage': 2.0}, 'CPU2': {'%usage': 11.0}, 'CPU3': {'%usage': 2.0}}, 'memory': {'available_ram': 3039, 'used_ram': 1661}} \ No newline at end of file