Ejemplo n.º 1
0
 def get_bgp_neighbors(self):
     instances = junos_views.junos_route_instance_table(self.device)
     uptime_table = junos_views.junos_bgp_uptime_table(self.device)
     bgp_neighbors = junos_views.junos_bgp_table(self.device)
     keys =['local_as', 'remote_as', 'is_up', 'is_enabled', 'description', 'remote_id']
     bgp_neighbor_data = {}
     for instance, instance_data in instances.get().items():
         if instance.startswith('__'):
             # junos internal instances
             continue
         instance_name = "global" if instance == 'master' else instance
         bgp_neighbor_data[instance_name] = {'peers': {}}
         for neighbor, data in bgp_neighbors.get(instance=instance).items():
             neighbor_data = {k: v for k, v in data}
             peer_ip = neighbor.split('+')[0]
             if 'router_id' not in bgp_neighbor_data[instance_name]:
                 # we only need to set this once
                 bgp_neighbor_data[instance_name]['router_id'] = unicode(neighbor_data['local_id'])
             peer = {key:self._parse_value(value) for key, value in neighbor_data.iteritems() if key in keys}
             peer['address_family'] = self._parse_route_stats(neighbor_data)
             bgp_neighbor_data[instance_name]['peers'][peer_ip] = peer
         for neighbor, uptime in uptime_table.get(instance=instance).items():
             bgp_neighbor_data[instance_name]['peers'][neighbor]['uptime'] = uptime[0][1]
     for key in bgp_neighbor_data.keys():
         if not bgp_neighbor_data[key]['peers']:
             del bgp_neighbor_data[key]
     return bgp_neighbor_data
Ejemplo n.º 2
0
    def get_bgp_neighbors(self):
        # Setup the views
        instances = junos_views.junos_route_instance_table(self.device)
        uptime_table = junos_views.junos_bgp_uptime_table(self.device)
        bgp_neigbors = junos_views.junos_bgp_table(self.device)
        # prepare data
        bgp_neigbor_data = dict()
        for instance, instance_data in instances.get().items():
            if instance.startswith('__'):
                # junos internal instances
                continue

            instance_name = "global" if instance == 'master' else instance

            bgp_neigbor_data[instance_name] = dict(peers=dict())
            for neighbor, neighbor_data in bgp_neigbors.get(instance=instance).items():
                structured_neighbor_data = {k: v for k, v in neighbor_data}
                peer = neighbor.split('+')[0]
                bgp_neigbor_data[instance_name]['peers'][peer] = dict()
                for key in ['local_as', 'remote_as', 'is_up', 'is_enabled', 'description', 'remote_id']:
                    bgp_neigbor_data[instance_name]['peers'][peer][key] = structured_neighbor_data[key] or u''
                if 'router_id' not in bgp_neigbor_data[instance_name].keys():
                    # we only need to set this once
                    bgp_neigbor_data[instance_name]['router_id'] = structured_neighbor_data['local_id']
                if structured_neighbor_data['is_up'] is False:
                    # if the session is down there is no table data to parse
                    continue
                elif isinstance(structured_neighbor_data['tables'], list):
                    for idx, table in enumerate(structured_neighbor_data['tables']):
                        family = self._get_address_family(table)
                        bgp_neigbor_data[instance_name]['peers'][peer][family] = dict()
                        for metric in ['received_prefixes', 'accepted_prefixes', 'sent_prefixes']:
                            bgp_neigbor_data[instance_name]['peers'][peer][family][metric] = structured_neighbor_data[metric][idx]
                else:
                    family = self._get_address_family(structured_neighbor_data['tables'])
                    bgp_neigbor_data[instance_name]['peers'][peer][family] = dict()
                    bgp_neigbor_data[instance_name]['peers'][peer][family]['received_prefixes'] = structured_neighbor_data['received_prefixes']
                    bgp_neigbor_data[instance_name]['peers'][peer][family]['accepted_prefixes'] = structured_neighbor_data['accepted_prefixes']
                    bgp_neigbor_data[instance_name]['peers'][peer][family]['sent_prefixes'] = structured_neighbor_data['sent_prefixes']
            for neighbor, uptime in uptime_table.get(instance=instance).items():
                bgp_neigbor_data[instance_name]['peers'][neighbor]['uptime'] = uptime[0][1]
        return bgp_neigbor_data