Merge remote-tracking branch 'upstream/base_new_methods' into base_new_methods
This commit is contained in:
commit
f20229b75f
2 files changed
+127
-38
No files matched your search
+106
-35
@@ -11,8 +11,9 @@
|
|||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
# License for the specific language governing permissions and limitations under
|
# License for the specific language governing permissions and limitations under
|
||||||
# the License.
|
# the License.
|
||||||
|
import re
|
||||||
|
|
||||||
from pyFG.fortios import FortiOS, FortiConfig
|
from pyFG.fortios import FortiOS, FortiConfig, logger
|
||||||
from pyFG.exceptions import FailedCommit, CommandExecutionException
|
from pyFG.exceptions import FailedCommit, CommandExecutionException
|
||||||
|
|
||||||
from base import NetworkDriver
|
from base import NetworkDriver
|
||||||
@@ -36,7 +37,6 @@ def execute_get(device, cmd, separator=':', auto=False):
|
|||||||
|
|
||||||
|
|
||||||
class FortiOSDriver(NetworkDriver):
|
class FortiOSDriver(NetworkDriver):
|
||||||
|
|
||||||
def __init__(self, hostname, username, password, timeout=60):
|
def __init__(self, hostname, username, password, timeout=60):
|
||||||
self.hostname = hostname
|
self.hostname = hostname
|
||||||
self.username = username
|
self.username = username
|
||||||
@@ -174,39 +174,110 @@ class FortiOSDriver(NetworkDriver):
|
|||||||
|
|
||||||
return interface_statistics
|
return interface_statistics
|
||||||
|
|
||||||
# def get_bgp_neighbors(self):
|
def get_bgp_neighbors(self):
|
||||||
# bgp_sum = self.device.execute_command('get router info bgp sum')
|
|
||||||
# re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
|
def search_line_in_lines(search, lines):
|
||||||
# neighbors = {n.split()[0]: n.split()[1:] for n in bgp_sum if re.match(re_neigh, n)}
|
for l in lines:
|
||||||
#
|
if search in l:
|
||||||
# peers = dict()
|
return l
|
||||||
#
|
|
||||||
# self.device.load_config('router bgp')
|
families = ['ipv4', 'ipv6']
|
||||||
#
|
terms = dict({'accepted_prefixes': 'accepted', 'sent_prefixes': 'announced'})
|
||||||
# for neighbor, parameters in neighbors.iteritems():
|
command_sum = 'get router info bgp sum'
|
||||||
# neigh_conf = self.device.running_config['router bgp']['neighbor']['{}'.format(neighbor)]
|
command_detail = 'get router info bgp neighbor {}'
|
||||||
# peers[neighbor] = dict()
|
command_received = 'get router info bgp neighbors {} received-routes | grep prefixes '
|
||||||
# peers[neighbor]['remote_as'] = int(neigh_conf.get_param('remote-as'))
|
peers = dict()
|
||||||
# peers[neighbor]['is_enabled'] = neigh_conf.get_param('shutdown') != 'enable' or False
|
|
||||||
# peers[neighbor]['is_up'] = 'never' != parameters[7] or False
|
bgp_sum = self.device.execute_command(command_sum)
|
||||||
# peers[neighbor]['uptime'] = convert_uptime_string_seconds(parameters[7])
|
re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
|
||||||
# peers[neighbor]['accepted_prefixes'] = int(self.device.execute_command('get router info bgp neighbor {} | grep "accepted prefixes"'.format(neighbor))[0].split()[0])
|
neighbors = {n.split()[0]: n.split()[1:] for n in bgp_sum if re.match(re_neigh, n)}
|
||||||
# peers[neighbor]['sent_prefixes'] = int(self.device.execute_command('get router info bgp neighbor {} | grep "announced prefixes"'.format(neighbor))[0].split()[0])
|
|
||||||
#
|
self.device.load_config('router bgp')
|
||||||
# received = self.device.execute_command('get router info bgp neighbors {} received-routes | grep prefixes'.format(neighbor))[0].split()
|
|
||||||
# if len(received) > 0:
|
for neighbor, parameters in neighbors.iteritems():
|
||||||
# peers[neighbor]['received_prefixes'] = int(received[-1])
|
logger.debug('NEW PEER')
|
||||||
# else:
|
neigh_conf = self.device.running_config['router bgp']['neighbor']['{}'.format(neighbor)]
|
||||||
# # Soft-reconfig is not enabled
|
|
||||||
# peers[neighbor]['received_prefixes'] = 0
|
neighbor_dict = peers.get(neighbor, dict())
|
||||||
#
|
|
||||||
# return {
|
if not neighbor_dict:
|
||||||
# 'default': {
|
neighbor_dict['local_as'] = int(bgp_sum[0].split()[7])
|
||||||
# 'local_as': int(bgp_sum[0].split()[7]),
|
neighbor_dict['remote_as'] = int(neigh_conf.get_param('remote-as'))
|
||||||
# 'router_id': bgp_sum[0].split()[3],
|
neighbor_dict['is_up'] = 'never' != parameters[7] or False
|
||||||
# 'peers': peers
|
neighbor_dict['is_enabled'] = neigh_conf.get_param('shutdown') != 'enable' or False
|
||||||
# }
|
neighbor_dict['description'] = ''
|
||||||
# }
|
neighbor_dict['uptime'] = convert_uptime_string_seconds(parameters[7])
|
||||||
|
neighbor_dict['ipv4'] = dict()
|
||||||
|
neighbor_dict['ipv6'] = dict()
|
||||||
|
|
||||||
|
detail_output = [x.lower() for x in self.device.execute_command(command_detail.format(neighbor))]
|
||||||
|
m = re.search('remote router id (.+?)\n', '\n'.join(detail_output))
|
||||||
|
if m:
|
||||||
|
neighbor_dict['remote_id'] = m.group(1)
|
||||||
|
else:
|
||||||
|
raise Exception('cannot find remote router id for %s' % neighbor)
|
||||||
|
|
||||||
|
for family in families:
|
||||||
|
# find block
|
||||||
|
x = detail_output.index(' for address family: {} unicast'.format(family))
|
||||||
|
block = detail_output[x:]
|
||||||
|
|
||||||
|
for term, fortiname in terms.iteritems():
|
||||||
|
text = search_line_in_lines('%s prefixes' % fortiname, block)
|
||||||
|
t = [int(s) for s in text.split() if s.isdigit()][0]
|
||||||
|
neighbor_dict[family][term] = t
|
||||||
|
|
||||||
|
received = self.device.execute_command(
|
||||||
|
command_received.format(neighbor))[0].split()
|
||||||
|
if len(received) > 0:
|
||||||
|
neighbor_dict[family]['received_prefixes'] = received[-1]
|
||||||
|
else:
|
||||||
|
# Soft-reconfig is not enabled
|
||||||
|
neighbor_dict[family]['received_prefixes'] = 0
|
||||||
|
peers[neighbor] = neighbor_dict
|
||||||
|
|
||||||
|
return {
|
||||||
|
'default': {
|
||||||
|
'router_id': bgp_sum[0].split()[3],
|
||||||
|
'peers': peers
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_interfaces_counters(self):
|
||||||
|
cmd = self.device.execute_command('fnsysctl ifconfig')
|
||||||
|
if_name = None
|
||||||
|
interface_counters = dict()
|
||||||
|
for line in cmd:
|
||||||
|
data = line.split('\t')
|
||||||
|
if (data[0] == '' or data[0] == ' ') and len(data) == 1:
|
||||||
|
continue
|
||||||
|
elif data[0] != '':
|
||||||
|
if_name = data[0]
|
||||||
|
interface_counters[if_name] = dict()
|
||||||
|
elif (data[1].startswith('RX packets') or data[1].startswith('TX packets')) and if_name:
|
||||||
|
if_data = data[1].split(' ')
|
||||||
|
direction = if_data[0].lower()
|
||||||
|
interface_counters[if_name][direction + '_unicast_packets'] = if_data[1].split(':')[1]
|
||||||
|
interface_counters[if_name][direction + '_errors'] = if_data[2].split(':')[1]
|
||||||
|
interface_counters[if_name][direction + '_discards'] = if_data[2].split(':')[1]
|
||||||
|
interface_counters[if_name][direction + '_multicast_packets'] = -1
|
||||||
|
interface_counters[if_name][direction + '_broadcast_packets'] = -1
|
||||||
|
elif data[1].startswith('RX bytes'):
|
||||||
|
if_data = data[1].split(' ')
|
||||||
|
interface_counters[if_name]['rx_octets'] = if_data[1].split(':')[1]
|
||||||
|
try:
|
||||||
|
interface_counters[if_name]['tx_octets'] = if_data[6].split(':')[1]
|
||||||
|
except IndexError:
|
||||||
|
interface_counters[if_name]['tx_octets'] = if_data[7].split(':')[1]
|
||||||
|
return interface_counters
|
||||||
|
|
||||||
def get_lldp_neighbors(self):
|
def get_lldp_neighbors(self):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
# def get_environment(self):
|
||||||
|
# sensors_output = self.device.execute_command('execute sensor list')
|
||||||
|
# from pprint import pprint
|
||||||
|
# pprint(sensors_output)
|
||||||
|
#
|
||||||
|
# return {}
|
||||||
+21
-3
@@ -36,9 +36,23 @@ junos_route_instance_view:
|
|||||||
fields:
|
fields:
|
||||||
name: instance-name
|
name: instance-name
|
||||||
|
|
||||||
junos_bgp_table:
|
junos_bgp_uptime_table:
|
||||||
rpc: get-bgp-summary-information
|
rpc: get-bgp-summary-information
|
||||||
|
args:
|
||||||
|
instance: ''
|
||||||
item: bgp-peer
|
item: bgp-peer
|
||||||
|
key: peer-address
|
||||||
|
view: junos_bgp_uptime_view
|
||||||
|
|
||||||
|
junos_bgp_uptime_view:
|
||||||
|
fields:
|
||||||
|
uptime: { elapsed-time/@seconds: int }
|
||||||
|
tables: bgp-rib/name
|
||||||
|
|
||||||
|
junos_bgp_table:
|
||||||
|
rpc: get-bgp-neighbor-information
|
||||||
|
item: bgp-peer
|
||||||
|
|
||||||
key: peer-address
|
key: peer-address
|
||||||
view: junos_bgp_view
|
view: junos_bgp_view
|
||||||
|
|
||||||
@@ -48,10 +62,14 @@ junos_bgp_view:
|
|||||||
description: description
|
description: description
|
||||||
is_up: { peer-state: True=Established }
|
is_up: { peer-state: True=Established }
|
||||||
is_enabled: { peer-state: False=True }
|
is_enabled: { peer-state: False=True }
|
||||||
uptime: { elapsed-time/@seconds: int }
|
|
||||||
received_prefixes: { bgp-rib/received-prefix-count: int }
|
received_prefixes: { bgp-rib/received-prefix-count: int }
|
||||||
accepted_prefixes: { bgp-rib/accepted-prefix-count: int }
|
accepted_prefixes: { bgp-rib/accepted-prefix-count: int }
|
||||||
sent_prefixes: { bgp-rib/aadvertised-prefix-count: int }
|
sent_prefixes: { bgp-rib/advertised-prefix-count: int }
|
||||||
|
tables: bgp-rib/name
|
||||||
|
local_as: { local-as: int }
|
||||||
|
peer_as: { peer-as: int }
|
||||||
|
local_id: local-id
|
||||||
|
remote_id: peer-id
|
||||||
|
|
||||||
####
|
####
|
||||||
#### LLDP table
|
#### LLDP table
|
||||||
|
|||||||
Reference in new issue
Block a user