Merge pull request #92 from bigzaqui/base_new_methods

get bgp neighbors for fortios
This commit is contained in:
David Barroso committed 2015-12-01 10:38:19 +01:00
commit 34f951f48e
1 file changed
+81 -38
+81 -38
View File
@@ -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
@@ -26,9 +27,9 @@ def execute_get(device, cmd, separator=':', auto=False):
if auto: if auto:
if ':' in output[0]: if ':' in output[0]:
separator=':' separator = ':'
elif '\t' in output[0]: elif '\t' in output[0]:
separator='\t' separator = '\t'
else: else:
raise Exception('Unknown separator for block:\n{}'.format(output)) raise Exception('Unknown separator for block:\n{}'.format(output))
@@ -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
@@ -166,7 +166,7 @@ class FortiOSDriver(NetworkDriver):
ifs['is_enabled'] = self.device.running_config['system interface'][iface].get_param('status') != 'down' ifs['is_enabled'] = self.device.running_config['system interface'][iface].get_param('status') != 'down'
ifs['description'] = self.device.running_config['system interface'][iface].get_param('description') ifs['description'] = self.device.running_config['system interface'][iface].get_param('description')
ifs['last_flapped'] = None ifs['last_flapped'] = None
#ifs['mode'] = 'routed' # ifs['mode'] = 'routed'
ifs['last_flapped'] = None ifs['last_flapped'] = None
interface_statistics[iface] = ifs interface_statistics[iface] = ifs
except CommandExecutionException: except CommandExecutionException:
@@ -174,39 +174,82 @@ 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_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 {}