diff --git a/napalm/fortios.py b/napalm/fortios.py index 0e65c9a..a97a6e4 100644 --- a/napalm/fortios.py +++ b/napalm/fortios.py @@ -174,12 +174,13 @@ class FortiOSDriver(NetworkDriver): return interface_statistics - def get_bgp_neighbors(self): + @staticmethod + def _search_line_in_lines(search, lines): + for l in lines: + if search in l: + return l - def search_line_in_lines(search, lines): - for l in lines: - if search in l: - return l + def get_bgp_neighbors(self): families = ['ipv4', 'ipv6'] terms = dict({'accepted_prefixes': 'accepted', 'sent_prefixes': 'announced'}) @@ -224,7 +225,7 @@ class FortiOSDriver(NetworkDriver): block = detail_output[x:] for term, fortiname in terms.iteritems(): - text = search_line_in_lines('%s prefixes' % fortiname, block) + text = self._search_line_in_lines('%s prefixes' % fortiname, block) t = [int(s) for s in text.split() if s.isdigit()][0] neighbor_dict['address_family'][family][term] = t @@ -275,10 +276,92 @@ class FortiOSDriver(NetworkDriver): def get_lldp_neighbors(self): return {} + def get_environment(self): - # def get_environment(self): - # sensors_output = self.device.execute_command('execute sensor list') - # from pprint import pprint - # pprint(sensors_output) - # - # return {} + 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: + m = re.search('(.+?) states: (.+?)% user (.+?)% system (.+?)% nice (.+?)% idle', l) + cpuname = m.group(1) + idle = m.group(5) + output[cpuname] = 100 - int(idle) + return output + + def get_memory(memory_line): + total, used = int(memory_line[1]) >> 20, int(memory_line[2]) >> 20 # convert from 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]) + + output[sensor_name] = dict(temperature=float(temp_value), is_alert=is_alert, + is_critical=True if v > temp_value else False) + + return output + + out = dict() + + sensors_block = [parse_string(x) for x in self.device.execute_command('execute sensor detail') if x] + + # 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) + + + # fans + out['fans'] = get_fans([x for x in sensors_block if 'fan' in x and 'temp' not in x]) + + # cpu + out['cpu'] = get_cpu( + [x for x in self.device.execute_command('get system performance status | grep CPU')[1:] if x]) + + # memory + memory_command = 'diag hard sys mem | grep Mem:' + t = [x for x in re.split('\s+', self.device.execute_command(memory_command)[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} + + return out diff --git a/test/unit/fortios/mock_data/diag_hard_sys_mem_|_grep_Mem:.txt b/test/unit/fortios/mock_data/diag_hard_sys_mem_|_grep_Mem:.txt new file mode 100644 index 0000000..894f1f6 --- /dev/null +++ b/test/unit/fortios/mock_data/diag_hard_sys_mem_|_grep_Mem:.txt @@ -0,0 +1 @@ +Mem: 8349081600 1204281344 7144800256 0 137478144 329146368 102465536 diff --git a/test/unit/fortios/mock_data/execute_sensor_detail.txt b/test/unit/fortios/mock_data/execute_sensor_detail.txt new file mode 100644 index 0000000..f5b1554 --- /dev/null +++ b/test/unit/fortios/mock_data/execute_sensor_detail.txt @@ -0,0 +1,255 @@ + 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/fortios/mock_data/get_system_performance_status_|_grep_cpu.txt b/test/unit/fortios/mock_data/get_system_performance_status_|_grep_cpu.txt new file mode 100644 index 0000000..2bf96c7 --- /dev/null +++ b/test/unit/fortios/mock_data/get_system_performance_status_|_grep_cpu.txt @@ -0,0 +1,5 @@ +CPU states: 0% user 5% system 0% nice 95% idle +CPU0 states: 0% user 8% system 0% nice 92% idle +CPU1 states: 0% user 4% system 0% nice 96% idle +CPU2 states: 0% user 4% system 0% nice 96% idle +CPU3 states: 0% user 4% system 0% nice 96% idle