Skip to content

Commit b84c2df

Browse files
committed
Add DHCP graphs to the VLAN and Prefix views in NAV
..a VLAN/Prefix page has DHCP graphs displayed if NAV has collected some recent enough DHCP stats for IP addresses that intersect that VLAN/Prefix.
1 parent d156e0a commit b84c2df

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

python/nav/models/manage.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from nav.models.fields import CIDRField
6464
import nav.models.event
6565
from nav.oids import get_enterprise_id
66+
import nav.dhcpstats.common
6667

6768

6869
_logger = logging.getLogger(__name__)
@@ -1475,6 +1476,16 @@ def get_graph_url(self):
14751476
def get_absolute_url(self):
14761477
return reverse('prefix-details', args=[self.pk])
14771478

1479+
def get_dhcp_graph_urls(self):
1480+
"""
1481+
Creates urls to graphs showing range/pool/subnet utilization, with one
1482+
url (and one graph) per set of ranges/pools/subnets in graphite with the
1483+
same ip_version, server_name and group where at least one
1484+
range/pool/subnet intersects this prefix.
1485+
"""
1486+
prefix = IPy.IP(self.net_address)
1487+
return nav.dhcpstats.common.fetch_graph_urls_for_prefixes([prefix])
1488+
14781489

14791490
class Vlan(models.Model):
14801491
"""From NAV Wiki: The vlan table defines the IP broadcast domain / vlan. A
@@ -1572,6 +1583,16 @@ def get_graph_url(self, family=4):
15721583

15731584
return json_graph_url(*series, title=title)
15741585

1586+
def get_dhcp_graph_urls(self):
1587+
"""
1588+
Creates urls to graphs showing range/pool/subnet utilization, with one
1589+
url (and one graph) per set of ranges/pools/subnets in graphite with the
1590+
same ip_version, server_name and group where at least one
1591+
range/pool/subnet intersects this vlan.
1592+
"""
1593+
prefixes = [IPy.IP(prefix.net_address) for prefix in self.prefixes.all()]
1594+
return nav.dhcpstats.common.fetch_graph_urls_for_prefixes(prefixes)
1595+
15751596

15761597
class NetType(models.Model):
15771598
"""From NAV Wiki: The nettype table defines network type;lan, core, link,

python/nav/web/info/prefix/views.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from nav.web import utils
2828
from nav.web.auth.utils import get_account
29+
from nav.metrics.errors import GraphiteUnreachableError
2930
from nav.models.manage import Prefix, Usage, PrefixUsage
3031
from ..forms import SearchForm
3132

@@ -140,6 +141,16 @@ def prefix_details(request, prefix_id):
140141
context['form'] = PrefixUsageForm(instance=prefix)
141142
context['can_edit'] = authorize_user(request)
142143

144+
try:
145+
dhcp_graph_urls = prefix.get_dhcp_graph_urls()
146+
graphite_error = False
147+
except GraphiteUnreachableError:
148+
dhcp_graph_urls = []
149+
graphite_error = True
150+
151+
context['dhcp_graph_urls'] = dhcp_graph_urls
152+
context['graphite_error'] = graphite_error
153+
143154
return render(request, 'info/prefix/details.html', context)
144155

145156

python/nav/web/info/vlan/views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from nav.models.manage import Prefix, Vlan
3030
from nav.web.utils import create_title
31+
from nav.metrics.errors import GraphiteUnreachableError
3132
from nav.metrics.graphs import get_simple_graph_url
3233
from nav.metrics.names import join_series
3334
from nav.metrics.templates import metric_path_for_prefix
@@ -115,6 +116,13 @@ def vlan_details(request, vlanid):
115116

116117
navpath = get_path([(str(vlan), '')])
117118

119+
try:
120+
dhcp_graph_urls = vlan.get_dhcp_graph_urls()
121+
graphite_error = False
122+
except GraphiteUnreachableError:
123+
dhcp_graph_urls = []
124+
graphite_error = True
125+
118126
return render(
119127
request,
120128
'info/vlan/vlandetails.html',
@@ -126,6 +134,8 @@ def vlan_details(request, vlanid):
126134
'has_v4': has_v4,
127135
'has_v6': has_v6,
128136
'title': create_title(navpath),
137+
'dhcp_graph_urls': dhcp_graph_urls,
138+
'graphite_error': graphite_error,
129139
},
130140
)
131141

python/nav/web/templates/info/prefix/details.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
$('.prefixgraph').each(function (index, element) {
1717
new GraphFetcher($(element), element.dataset.url);
1818
});
19+
$('.dhcpgraph').each(function (index, element) {
20+
new GraphFetcher($(element), element.dataset.url);
21+
});
1922
});
2023
</script>
2124

@@ -202,6 +205,21 @@ <h3>Prefix details for {{ prefix.net_address }}</h3>
202205
</div>
203206
</div>
204207

208+
{% if dhcp_graph_urls or graphite_error %}
209+
{% if graphite_error %}
210+
<span class="label alert">Graphite unreachable</span>
211+
{% else %}
212+
<div class="large-6 column">
213+
{% for graph in dhcp_graph_urls %}
214+
<div class="dhcpgraph graphitegraph" data-url="{{ graph }}">
215+
<div class="rickshaw-container"></div>
216+
</div>
217+
{% endfor %}
218+
</div>
219+
{% endif %}
220+
{% endif %}
221+
222+
205223
</div>
206224

207225
{% endblock %}

python/nav/web/templates/info/vlan/vlandetails.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
$('.prefixgraph').each(function (index, element) {
1717
new GraphFetcher($(element), element.dataset.url);
1818
});
19+
$('.dhcpgraph').each(function (index, element) {
20+
new GraphFetcher($(element), element.dataset.url);
21+
});
1922
});
2023
</script>
2124
{% endblock %}
@@ -154,4 +157,20 @@ <h2>Prefix graphs</h2>
154157
{% endfor %}
155158
</ul>
156159

160+
{% comment %} Graph containing DHCP statistics for total, assigned and declined addresses {% endcomment %}
161+
162+
{% if dhcp_graph_urls or graphite_error %}
163+
<h2>DHCP graphs</h2>
164+
{% if graphite_error %}
165+
<span class="label alert">Graphite unreachable</span>
166+
{% else %}
167+
<ul class="small-block-grid-1 large-block-grid-2">
168+
{% for graph in dhcp_graph_urls %}
169+
<li class="dhcpgraph graphitegraph" data-url="{{ graph }}">
170+
<div class="rickshaw-container"></div>
171+
</li>
172+
{% endfor %}
173+
</ul>
174+
{% endif %}
175+
{% endif %}
157176
{% endblock %}

0 commit comments

Comments
 (0)