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

Adds utilization on vlan groups #12263

Closed
wants to merge 16 commits into from
Closed
3 changes: 2 additions & 1 deletion netbox/ipam/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,13 @@ class VLANGroupSerializer(NetBoxModelSerializer):
scope_id = serializers.IntegerField(allow_null=True, required=False, default=None)
scope = serializers.SerializerMethodField(read_only=True)
vlan_count = serializers.IntegerField(read_only=True)
utilization = serializers.CharField(source='get_utilization', read_only=True)

class Meta:
model = VLANGroup
fields = [
'id', 'url', 'display', 'name', 'slug', 'scope_type', 'scope_id', 'scope', 'min_vid', 'max_vid',
'description', 'tags', 'custom_fields', 'created', 'last_updated', 'vlan_count',
'description', 'tags', 'custom_fields', 'created', 'last_updated', 'vlan_count', 'utilization'
]
validators = []

Expand Down
9 changes: 9 additions & 0 deletions netbox/ipam/models/vlans.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ def get_next_available_vid(self):
return available_vids[0]
return None

def get_utilization(self):
"""
Return the percentage of utilization for this vlan group.
"""
assigned_vlan = VLAN.objects.filter(group=self.id).count()
jeremystretch marked this conversation as resolved.
Show resolved Hide resolved
if assigned_vlan != 0:
return round(assigned_vlan / (self.max_vid - self.min_vid + 1) * 100, 2)
return 0.0


class VLAN(PrimaryModel):
"""
Expand Down
8 changes: 6 additions & 2 deletions netbox/ipam/tables/vlans.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class VLANGroupTable(NetBoxTable):
url_params={'group_id': 'pk'},
verbose_name='VLANs'
)
get_utilization = columns.UtilizationColumn(
orderable=False,
verbose_name='Utilization'
)
tags = columns.TagColumn(
url_name='ipam:vlangroup_list'
)
Expand All @@ -81,9 +85,9 @@ class Meta(NetBoxTable.Meta):
model = VLANGroup
fields = (
'pk', 'id', 'name', 'scope_type', 'scope', 'min_vid', 'max_vid', 'vlan_count', 'slug', 'description',
'tags', 'created', 'last_updated', 'actions',
'tags', 'created', 'last_updated', 'actions', 'get_utilization',
)
default_columns = ('pk', 'name', 'scope_type', 'scope', 'vlan_count', 'description')
default_columns = ('pk', 'name', 'scope_type', 'scope', 'vlan_count', 'get_utilization', 'description')


#
Expand Down
4 changes: 4 additions & 0 deletions netbox/templates/ipam/vlangroup.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ <h5 class="card-header">VLAN Group</h5>
<a href="{% url 'ipam:vlan_list' %}?group_id={{ object.pk }}">{{ vlans_count }}</a>
</td>
</tr>
<tr>
<th scope="row">Utilization</th>
<td>{% utilization_graph object.get_utilization %}</td>
</tr>
</table>
</div>
</div>
Expand Down