device_lldp_neighbors.html 4.0 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.connection %}
  24. {% with iface.connected_interface as connected_iface %}
  25. <td class="configured_device" data="{{ connected_iface.device }}">
  26. <a href="{% url 'dcim:device' pk=connected_iface.device.pk %}">{{ connected_iface.device }}</a>
  27. </td>
  28. <td class="configured_interface" data="{{ connected_iface }}">
  29. <span title="{{ connected_iface.get_form_factor_display }}">{{ connected_iface }}</span>
  30. </td>
  31. {% endwith %}
  32. {% else %}
  33. <td colspan="2">None</td>
  34. {% endif %}
  35. <td class="device"></td>
  36. <td class="interface"></td>
  37. </tr>
  38. {% endfor %}
  39. </tbody>
  40. </table>
  41. </div>
  42. {% endblock %}
  43. {% block javascript %}
  44. <script type="text/javascript">
  45. $(document).ready(function() {
  46. $.ajax({
  47. url: "{% url 'dcim-api:device-napalm' pk=device.pk %}?method=get_lldp_neighbors",
  48. dataType: 'json',
  49. success: function(json) {
  50. $.each(json['get_lldp_neighbors'], function(iface, neighbors) {
  51. var neighbor = neighbors[0];
  52. var row = $('#' + iface.split(".")[0].replace(/([\/:])/g, "\\$1"));
  53. // Glean configured hostnames/interfaces from the DOM
  54. var configured_device = row.children('td.configured_device').attr('data');
  55. var configured_interface = row.children('td.configured_interface').attr('data');
  56. var configured_interface_short = null;
  57. if (configured_interface) {
  58. // Match long-form IOS names against short ones (e.g. Gi0/1 == GigabitEthernet0/1).
  59. configured_interface_short = configured_interface.replace(/^([A-Z][a-z])[^0-9]*([0-9\/]+)$/, "$1$2");
  60. }
  61. // Clean up hostnames/interfaces learned via LLDP
  62. var lldp_device = neighbor['hostname'].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 %}