Skip to content

Commit

Permalink
Merge pull request #1733 from jmbredal/active_alerts_port_details
Browse files Browse the repository at this point in the history
Add active alerts tab for port details page (Fixes #1509)
  • Loading branch information
John-Magne Bredal committed Jun 8, 2018
2 parents 9cd91c9 + bea9112 commit 505dbb7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
24 changes: 24 additions & 0 deletions python/nav/web/ipdevinfo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from nav.models.arnold import Identity
from nav.models.service import Service
from nav.models.profiles import Account
from nav.models.event import AlertHistory
from nav.ipdevpoll.config import get_job_descriptions
from nav.util import is_valid_ip
from nav.web.ipdevinfo.utils import create_combined_urls
Expand Down Expand Up @@ -597,11 +598,34 @@ def port_details(request, netbox_sysname, port_type=None, port_id=None,
'graphite_error': graphite_error,
'detention': detention,
'sensor_metrics': sensor_metrics,
'alert_info': get_recent_alerts_interface(port)
},
context_instance=RequestContext(
request, processors=[search_form_processor]))


def get_recent_alerts_interface(interface, days_back=7):
"""Returns the most recent linkState events for this interface"""
lowest_end_time = dt.datetime.now() - dt.timedelta(days=days_back)
alerts = AlertHistory.objects.filter(
event_type='linkState',
subid=interface.pk,
end_time__gt=lowest_end_time)
for alert in alerts:
try:
message = alert.messages.filter(type='sms')[0].message
except IndexError:
message = None
alert.message = message

return {
'alerts': alerts,
'count': alerts.count(),
'days_back': days_back,
'has_unresolved_alerts': any(a.is_open() for a in alerts),
}


def port_counter_graph(request, interfaceid, kind='Octets'):
"""Creates an url to Graphite for rendering a graph as an image
Expand Down
66 changes: 66 additions & 0 deletions templates/ipdevinfo/port-details-recent-alerts-frag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<div id="alerts">
{% if port %}

{# Table for listing recent alerts #}
<h4>
Alerts last {{ alert_info.days_back }}
day{{ alert_info.days_back|pluralize }}
</h4>
<table class="listtable">
<thead>
<tr>
<th>Event</th>
<th>Message</th>
<th>Start</th>
<th>End</th>
<th>Downtime</th>
</tr>
</thead>

<tfoot>
<tr>
<th colspan="5">
{{ alert_info.alerts|length }}
alert{{ alert_info.alerts|length|pluralize }}
found
</th>
</tr>
</tfoot>

<tbody>
{% for a in alert_info.alerts %}
<tr>
<td>{{ a.event_type }}</td>
<td>
<a href="{% url 'event-details' a.pk %}" title="See event details">
{{ a.message|default:"N/A" }}
</a>
</td>
<td>{{ a.start_time|date|default:"N/A" }}
{{ a.start_time|time }}</td>
{% if a.is_open %}
<td class="status_down">Unresolved</td>
{% else %}
<td>{{ a.end_time|date|default:"N/A" }}
{{ a.end_time|time }}</td>
{% endif %}
<td>
{% if a.is_stateful %}
{% if a.is_open %}
{{ a.start_time|timesince }}
{% else %}
{{ a.start_time|timesince:a.end_time }}
{% endif %}
{% endif %}
</td>
</tr>
{% empty %}
<tr>
<td>No matching alerts found.</td>
</tr>
{% endfor %}
</tbody>
</table>

{% endif %}
</div>
10 changes: 10 additions & 0 deletions templates/ipdevinfo/port-details.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
}
});
tabs.removeClass('notvisible').show();
// Mark error tab
$('li[data-mark-as-error="True"]', tabs).removeClass('ui-state-default').addClass('ui-state-error');
});
</script>
{% endblock %}
Expand Down Expand Up @@ -61,6 +63,9 @@
<li><a href="#port-details-activity-graphs">Activity graphs</a></li>
<li><a href="#port-details-metrics-list">Metric list</a></li>
<li><a href="#port-details-sensors-list">Port Sensors</a></li>
<li data-mark-as-error="{{ alert_info.has_unresolved_alerts }}">
<a href="#port-details-recent-alerts">Recent alerts</a>
</li>
</ul>


Expand Down Expand Up @@ -118,6 +123,11 @@ <h3>Metrics</h3>
{% include 'ipdevinfo/port-details-sensors-frag.html' %}
</div>

{# Recent alerts for this interface #}
<div id="port-details-recent-alerts">
{% include "ipdevinfo/port-details-recent-alerts-frag.html" %}
</div>


</div>

Expand Down

0 comments on commit 505dbb7

Please sign in to comment.