bin/pvl.hosts-lldp
changeset 398 de275bf6db70
parent 395 9de553b50128
equal deleted inserted replaced
397:8455f42d8926 398:de275bf6db70
    48 
    48 
    49         try :
    49         try :
    50             local = agent.local
    50             local = agent.local
    51         except snmp.SNMPError as ex :
    51         except snmp.SNMPError as ex :
    52             log.warning("%s: %s", host, ex)
    52             log.warning("%s: %s", host, ex)
       
    53             continue
       
    54         
       
    55         if not local :
       
    56             log.info("%s: no lldp support", host)
    53             continue
    57             continue
    54 
    58 
    55         log.info("%s: %s", host, local)
    59         log.info("%s: %s", host, local)
    56 
    60 
    57         if local['sys_name'] != host.host :
    61         if local['sys_name'] != host.host :
   115 
   119 
   116         hosts_by_chassis[chassis] = host
   120         hosts_by_chassis[chassis] = host
   117     
   121     
   118     # second pass to discver links
   122     # second pass to discver links
   119     for host, agent in _hosts_lldp :
   123     for host, agent in _hosts_lldp :
   120         for port, remote in agent.remotes() :
   124         try :
       
   125             remotes = list(agent.remotes())
       
   126         except snmp.SNMPError as ex :
       
   127             log.warn("%s: broken lldp remotes: %s", host, ex)
       
   128             continue
       
   129 
       
   130         for port, remote in remotes :
   121             port = agent.port(port)
   131             port = agent.port(port)
   122 
   132 
   123             remote_chassis = remote['chassis']
   133             remote_chassis = remote['chassis']
   124             remote_host = hosts_by_chassis.get(remote_chassis)
   134             remote_host = hosts_by_chassis.get(remote_chassis)
   125             
   135             
   143         vlan_untagged = { }
   153         vlan_untagged = { }
   144             
   154             
   145         # multiple taggd vlans / port
   155         # multiple taggd vlans / port
   146         vlan_tagged = collections.defaultdict(set)
   156         vlan_tagged = collections.defaultdict(set)
   147 
   157 
   148         for vlan, (tagged, untagged) in agent.vlans() :
   158         for vlan, (tagged, untagged) in agent.vlan_ports() :
   149             log.info("%s: %s: %s + %s", host, vlan, tagged, untagged)
   159             log.info("%s: %s: %s + %s", host, vlan, tagged, untagged)
   150             
   160             
   151             for port in tagged :
   161             for port in tagged :
   152                 vlan_tagged[port].add(vlan)
   162                 vlan_tagged[port].add(vlan)
   153             
   163             
   172     """
   182     """
   173 
   183 
   174     for host, agent in hosts_bridge(options, hosts) :
   184     for host, agent in hosts_bridge(options, hosts) :
   175         ports = collections.defaultdict(list)
   185         ports = collections.defaultdict(list)
   176 
   186 
   177         for ether, port in agent.fdb() :
   187         try :
   178             if port :
   188             vlan_fdb_ports = list(agent.vlan_fdb_ports())
   179                 ports[port].append(ether)
   189         except snmp.SNMPError as ex :
   180         
   190             log.warn("%s: broken dot1q fdb: %s", host, ex)
       
   191             continue
       
   192 
       
   193         if vlan_fdb_ports :
       
   194             log.info("%s: have dot1q ports", host)
       
   195 
       
   196             for ether, port, vlan in agent.vlan_fdb_ports() :
       
   197                 if not port :
       
   198                     # XXX: unknown?
       
   199                     continue
       
   200 
       
   201                 ports[(port, vlan)].append(ether)
       
   202         else :
       
   203             try :
       
   204                 fdb_ports = list(agent.fdb_ports())
       
   205             except snmp.SNMPError as ex :
       
   206                 log.warn("%s: broken dot1q fdb: %s", host, ex)
       
   207                 continue
       
   208             
       
   209             # fallback to dot1d fdb
       
   210             log.info("%s: fallback to dot1d", host)
       
   211 
       
   212             for ether, port in agent.fdb_ports() :
       
   213                 if not port :
       
   214                     # XXX: unknown?
       
   215                     continue
       
   216 
       
   217                 ports[(port, None)].append(ether)
       
   218 
   181         yield host, ports
   219         yield host, ports
   182 
   220 
   183 COLOR_VLANS = {
   221 COLOR_VLANS = {
   184     1:      'grey',         # pvl-lan
   222     1:      'grey',         # pvl-lan
   185     2:      'blue',         # pvl-lan2
   223     2:      'blue',         # pvl-lan2
   392     else :
   430     else :
   393         for host, local, remote, remote_host in items :
   431         for host, local, remote, remote_host in items :
   394             if remote_host :
   432             if remote_host :
   395                 print "{host:30} {host.location:>30} {local[port]:>25} <-> {remote[port]:<25} {remote_host.location:>30} # {remote[chassis]} ({remote_host})".format(host=host, local=local, remote=remote, remote_host=remote_host)
   433                 print "{host:30} {host.location:>30} {local[port]:>25} <-> {remote[port]:<25} {remote_host.location:>30} # {remote[chassis]} ({remote_host})".format(host=host, local=local, remote=remote, remote_host=remote_host)
   396             else :
   434             else :
   397                 print "{host:30} {host.location:>30} {local[port]:>25} <-- {remote[port]:<25} {empty:30} # {remote[chassis]} ({remote[sys_name]})".format(host=host, local=local, remote=remote, empty='')
   435                 print "{host:30} {host.location:>30} {local[port]:>25} --> {remote[port]:<25} {empty:30} # {remote[chassis]} ({remote[sys_name]})".format(host=host, local=local, remote=remote, empty='')
   398        
   436        
       
   437         for host, ports in vlans.iteritems() :
       
   438             for port, (untag, tagged) in ports.iteritems() :
       
   439                 print "{host:30} {host.location:>30} {port:25} {untag}{tagged}".format(host=host, port=port,
       
   440                         untag       = '({untag}) '.format(untag=untag) if untag else '',
       
   441                         tagged      = ' '.join('<{tag}>'.format(tag=tag) for tag in tagged),
       
   442                 )
       
   443 
   399         for host, ports in leafs :
   444         for host, ports in leafs :
   400             for port, ethers in ports.iteritems() :
   445             for (port, vlan), ethers in ports.iteritems() :
   401                 print "{host:30} {host.location:>30} {port:25} <== # {ethers}".format(host=host, port=port, ethers=' '.join(ethers))
   446                 print "{host:30} {host.location:>30} {port:25} <-- {vlan} # {ethers}".format(
       
   447                         host        = host,
       
   448                         port        = port,
       
   449                         vlan        = '<{vlan}>'.format(vlan=vlan) if vlan else '',
       
   450                         ethers      = ' '.join(ethers),
       
   451                 )
   402 
   452 
   403 if __name__ == '__main__':
   453 if __name__ == '__main__':
   404     pvl.args.main(main)
   454     pvl.args.main(main)