device_lldp_neighbors.html 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 %}
  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_form_factor_display }}">{{ iface.connected_endpoint }}</span>
  29. </td>
  30. {% else %}
  31. <td colspan="2">None</td>
  32. {% endif %}
  33. <td class="device"></td>
  34. <td class="interface"></td>
  35. </tr>
  36. {% endfor %}
  37. </tbody>
  38. </table>
  39. </div>
  40. {% endblock %}
  41. {% block javascript %}
  42. <script type="text/javascript">
  43. $(document).ready(function() {
  44. $.ajax({
  45. url: "{% url 'dcim-api:device-napalm' pk=device.pk %}?method=get_lldp_neighbors",
  46. dataType: 'json',
  47. success: function(json) {
  48. $.each(json['get_lldp_neighbors'], function(iface, neighbors) {
  49. var neighbor = neighbors[0];
  50. var row = $('#' + iface.split(".")[0].replace(/([\/:])/g, "\\$1"));
  51. // Glean configured hostnames/interfaces from the DOM
  52. var configured_device = row.children('td.configured_device').attr('data');
  53. var configured_interface = row.children('td.configured_interface').attr('data');
  54. var configured_interface_short = null;
  55. if (configured_interface) {
  56. // Match long-form IOS names against short ones (e.g. Gi0/1 == GigabitEthernet0/1).
  57. configured_interface_short = configured_interface.replace(/^([A-Z][a-z])[^0-9]*([0-9\/]+)$/, "$1$2");
  58. }
  59. // Clean up hostnames/interfaces learned via LLDP
  60. var neighbor_host = neighbor['hostname'] || ""; // sanitize hostname if it's null to avoid breaking the split func
  61. var neighbor_port = neighbor['port'] || ""; // sanitize port if it's null to avoid breaking the split func
  62. var lldp_device = neighbor_host.split(".")[0]; // Strip off any trailing domain name
  63. var lldp_interface = neighbor_port.split(".")[0]; // Strip off any trailing subinterface ID
  64. // Add LLDP neighbors to table
  65. row.children('td.device').html(lldp_device);
  66. row.children('td.interface').html(lldp_interface);
  67. // Apply colors to rows
  68. if (!configured_device && lldp_device) {
  69. row.addClass('info');
  70. } else if (configured_device == lldp_device && configured_interface == lldp_interface) {
  71. row.addClass('success');
  72. } else if (configured_device == lldp_device && configured_interface_short == lldp_interface) {
  73. row.addClass('success');
  74. } else {
  75. row.addClass('danger');
  76. }
  77. });
  78. },
  79. error: function(xhr) {
  80. alert(xhr.responseText);
  81. }
  82. });
  83. });
  84. </script>
  85. {% endblock %}