device_lldp_neighbors.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. {% extends 'dcim/device.html' %}
  2. {% block title %}{{ device }} - LLDP Neighbors{% endblock %}
  3. {% block content %}
  4. {% include 'inc/ajax_loader.html' %}
  5. <div class="panel panel-default">
  6. <div class="panel-heading">
  7. <strong>LLDP Neighbors</strong>
  8. </div>
  9. <table class="table table-hover panel-body">
  10. <thead>
  11. <tr>
  12. <th>Interface</th>
  13. <th>Configured Device</th>
  14. <th>Configured Interface</th>
  15. <th>LLDP Device</th>
  16. <th>LLDP Interface</th>
  17. </tr>
  18. </thead>
  19. <tbody>
  20. {% for iface in interfaces %}
  21. <tr id="{{ iface.name }}">
  22. <td>{{ iface }}</td>
  23. {% if iface.connected_endpoint.device %}
  24. <td class="configured_device" data="{{ iface.connected_endpoint.device }}">
  25. <a href="{% url 'dcim:device' pk=iface.connected_endpoint.device.pk %}">{{ iface.connected_endpoint.device }}</a>
  26. </td>
  27. <td class="configured_interface" data="{{ iface.connected_endpoint }}">
  28. <span title="{{ iface.connected_endpoint.get_type_display }}">{{ iface.connected_endpoint }}</span>
  29. </td>
  30. {% elif iface.connected_endpoint.circuit %}
  31. {% with circuit=iface.connected_endpoint.circuit %}
  32. <td colspan="2">
  33. <i class="fa fa-fw fa-globe" title="Circuit"></i>
  34. <a href="{{ circuit.get_absolute_url }}">{{ circuit.provider }} {{ circuit }}</a>
  35. </td>
  36. {% endwith %}
  37. {% else %}
  38. <td colspan="2">None</td>
  39. {% endif %}
  40. <td class="device"></td>
  41. <td class="interface"></td>
  42. </tr>
  43. {% endfor %}
  44. </tbody>
  45. </table>
  46. </div>
  47. {% endblock %}
  48. {% block javascript %}
  49. <script type="text/javascript">
  50. $(document).ready(function() {
  51. $.ajax({
  52. url: "{% url 'dcim-api:device-napalm' pk=device.pk %}?method=get_lldp_neighbors",
  53. dataType: 'json',
  54. success: function(json) {
  55. $.each(json['get_lldp_neighbors'], function(iface, neighbors) {
  56. var neighbor = neighbors[0];
  57. var row = $('#' + iface.split(".")[0].replace(/([\/:])/g, "\\$1"));
  58. // Glean configured hostnames/interfaces from the DOM
  59. var configured_device = row.children('td.configured_device').attr('data');
  60. var configured_interface = row.children('td.configured_interface').attr('data');
  61. var configured_interface_short = null;
  62. if (configured_interface) {
  63. // Match long-form IOS names against short ones (e.g. Gi0/1 == GigabitEthernet0/1).
  64. configured_interface_short = configured_interface.replace(/^([A-Z][a-z])[^0-9]*([0-9\/]+)$/, "$1$2");
  65. }
  66. // Clean up hostnames/interfaces learned via LLDP
  67. var neighbor_host = neighbor['hostname'] || ""; // sanitize hostname if it's null to avoid breaking the split func
  68. var neighbor_port = neighbor['port'] || ""; // sanitize port if it's null to avoid breaking the split func
  69. var lldp_device = neighbor_host.split(".")[0]; // Strip off any trailing domain name
  70. var lldp_interface = neighbor_port.split(".")[0]; // Strip off any trailing subinterface ID
  71. // Add LLDP neighbors to table
  72. row.children('td.device').html(lldp_device);
  73. row.children('td.interface').html(lldp_interface);
  74. // Apply colors to rows
  75. if (!configured_device && lldp_device) {
  76. row.addClass('info');
  77. } else if (configured_device == lldp_device && configured_interface == lldp_interface) {
  78. row.addClass('success');
  79. } else if (configured_device == lldp_device && configured_interface_short == lldp_interface) {
  80. row.addClass('success');
  81. } else {
  82. row.addClass('danger');
  83. }
  84. });
  85. },
  86. error: function(xhr) {
  87. alert(xhr.responseText);
  88. }
  89. });
  90. });
  91. </script>
  92. {% endblock %}