Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Graphite queries and error handling in availability stats for device groups #2434

Merged
merged 4 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions python/nav/metrics/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
metric_path_for_packet_loss,
metric_path_for_roundtrip_time,
)
from nav.util import chunks

_logger = logging.getLogger(__name__)

MAX_TARGETS_PER_REQUEST = 100


def get_metric_average(target, start="-5min", end="now", ignore_unknown=True):
"""Calculates the average value of a metric over a given period of time
Expand Down Expand Up @@ -197,7 +200,9 @@ def get_netboxes_availability(

def populate_for_interval(result, targets, netboxes, start_time, end_time):
"""Populate results based on a time interval"""
avg = get_metric_average(targets, start=start_time, end=end_time)
avg = {}
for request in chunks(targets, MAX_TARGETS_PER_REQUEST):
avg.update(get_metric_average(request, start=start_time, end=end_time))

for netbox in netboxes:
root = result[netbox.id]
Expand All @@ -217,7 +222,9 @@ def populate_for_interval(result, targets, netboxes, start_time, end_time):
def populate_for_time_frame(result, targets, netboxes, time_frames):
"""Populate results based on a list of time frames"""
for time_frame in time_frames:
avg = get_metric_average(targets, start="-1%s" % time_frame)
avg = {}
for request in chunks(targets, MAX_TARGETS_PER_REQUEST):
avg.update(get_metric_average(request, start="-1%s" % time_frame))

for netbox in netboxes:
root = result[netbox.id]
Expand Down
15 changes: 12 additions & 3 deletions python/nav/web/info/netboxgroup/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from nav.web.utils import create_title
from nav.models.manage import NetboxGroup, Netbox, NetboxCategory
from nav.metrics.data import get_netboxes_availability
from nav.metrics.errors import GraphiteUnreachableError


class NetboxGroupForm(SearchForm):
Expand Down Expand Up @@ -95,9 +96,16 @@ def group_detail(request, groupid):
"""
group = get_object_or_404(NetboxGroup, pk=groupid)
netboxes = group.netbox_set.select_related('organization', 'category', 'type')
availabilities = get_netboxes_availability(
netboxes, data_sources=['availability'], time_frames=['week', 'month']
)
availabilities = {}
graphite_error = False

try:
availabilities = get_netboxes_availability(
netboxes, data_sources=['availability'], time_frames=['week', 'month']
)
except GraphiteUnreachableError:
graphite_error = True

navpath = get_netboxgroup_path([(group.pk,)])

return render(
Expand All @@ -109,6 +117,7 @@ def group_detail(request, groupid):
'availabilities': availabilities,
'navpath': navpath,
'title': create_title(navpath),
'graphite_error': graphite_error,
},
)

Expand Down
10 changes: 8 additions & 2 deletions python/nav/web/info/room/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,24 @@ def render_deviceinfo(request, roomid):
all_netboxes = room.netbox_set.select_related(
"type", "category", "organization"
).order_by("sysname")
availabilities = {}
graphite_error = False

try:
availabilities = get_netboxes_availability(
all_netboxes, data_sources=["availability"], time_frames=["week", "month"]
)
except GraphiteUnreachableError:
availabilities = None
graphite_error = True

return render(
request,
"info/room/roominfo_devices.html",
{"netboxes": all_netboxes, "availabilities": availabilities},
{
"netboxes": all_netboxes,
"availabilities": availabilities,
'graphite_error': graphite_error,
},
)


Expand Down
19 changes: 13 additions & 6 deletions python/nav/web/templates/info/fragments/netboxes.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@
<img src="{{ STATIC_URL }}images/lys/{% if netbox.up == 'y' %}green.png{% else %}red.png{% endif %}">
</td>

{% with data=availabilities|get_value:netbox.id %}
{% with availability=data|get_value:'availability' %}
<td class="numeric">{{ availability|get_value:'week'|floatformat:'2' }}%</td>
<td class="numeric">{{ availability|get_value:'month'|floatformat:'2' }}%</td>
{% endwith %}
{% endwith %}
{% if availabilities %}
{% with data=availabilities|get_value:netbox.id %}
{% with availability=data|get_value:'availability' %}
<td class="numeric">{{ availability|get_value:'week'|floatformat:'2' }}%</td>
<td class="numeric">{{ availability|get_value:'month'|floatformat:'2' }}%</td>
{% endwith %}
{% endwith %}
{% elif graphite_error %}
<td><span class="label alert">Graphite unreachable</span></td>
<td><span class="label alert">Graphite unreachable</span></td>
{% else %}
N/A
{% endif %}
</tr>
{% endfor %}
</tbody>
Expand Down