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

Divide by zero error with very low power feed values #3905

Closed
hSaria opened this issue Jan 13, 2020 · 6 comments · Fixed by #3906
Closed

Divide by zero error with very low power feed values #3905

hSaria opened this issue Jan 13, 2020 · 6 comments · Fixed by #3906
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@hSaria
Copy link
Contributor

hSaria commented Jan 13, 2020

Environment

  • Python version: 3.6.8
  • NetBox version: 2.6.11

Steps to Reproduce

  1. Create power feed with voltage, amperage, and max utilization set to 1
  2. Create device with a power port with no power attributes (e.g. maximum_draw) that connects to the power feed created in step 1
  3. Browse to the power feed details page

Expected Behavior

Page loads

Observed Behavior

ZeroDivisionError exception because the percentage calculation is using available_power which was rounded to 0 with

self.voltage * self.amperage * (self.max_utilization / 100) =
1            * 1             * 0.01                         = 0.01
@jeremystretch
Copy link
Member

I'm not able to replicate this on v2.6.11. The PowerFeed is created as expected:

PowerFeed

Can you post the full stack trace please?

@jeremystretch jeremystretch added the status: revisions needed This issue requires additional information to be actionable label Jan 13, 2020
@hSaria
Copy link
Contributor Author

hSaria commented Jan 13, 2020

Environment:


Request Method: GET
Request URL: http://netbox/dcim/power-feeds/2/

Django Version: 2.2.8
Python Version: 3.6.8
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'cacheops',
 'corsheaders',
 'debug_toolbar',
 'django_filters',
 'django_tables2',
 'django_prometheus',
 'mptt',
 'rest_framework',
 'taggit',
 'taggit_serializer',
 'timezone_field',
 'circuits',
 'dcim',
 'ipam',
 'extras',
 'secrets',
 'tenancy',
 'users',
 'utilities',
 'virtualization',
 'drf_yasg']
Installed Middleware:
('debug_toolbar.middleware.DebugToolbarMiddleware',
 'django_prometheus.middleware.PrometheusBeforeMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'utilities.middleware.ExceptionHandlingMiddleware',
 'utilities.middleware.LoginRequiredMiddleware',
 'utilities.middleware.APIVersionMiddleware',
 'extras.middleware.ObjectChangeMiddleware',
 'django_prometheus.middleware.PrometheusAfterMiddleware')


Template error:
In template /opt/netbox-2.6.11/netbox/templates/_base.html, error at line 0
   division by zero
   1 : {% load static %}
   2 : {% load helpers %}
   3 : <!DOCTYPE html>
   4 : <html lang="en">
   5 : <head>
   6 :     <title>{% block title %}Home{% endblock %} - NetBox</title>
   7 :     <link rel="stylesheet" href="{% static 'bootstrap-3.4.1-dist/css/bootstrap.min.css' %}">
   8 :     <link rel="stylesheet" href="{% static 'font-awesome-4.7.0/css/font-awesome.min.css' %}">
   9 :     <link rel="stylesheet" href="{% static 'jquery-ui-1.12.1/jquery-ui.css' %}">
   10 :     <link rel="stylesheet" href="{% static 'select2-4.0.5/css/select2.min.css' %}">


Traceback:

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/mixins.py" in dispatch
  85.         return super().dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch
  97.         return handler(request, *args, **kwargs)

File "/opt/netbox-2.6.11/netbox/dcim/views.py" in get
  2373.             'powerfeed': powerfeed,

File "/usr/local/lib/python3.6/site-packages/django/shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "/usr/local/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
  62.     return template.render(context, request)

File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
  171.                     return self._render(context)

File "/usr/local/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  96.     return self.nodelist.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "/usr/local/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  96.     return self.nodelist.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  62.                 result = block.nodelist.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
  513.             return self.nodelist.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
  309.                 return nodelist.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/usr/local/lib/python3.6/site-packages/django/template/library.py" in render
  213.         resolved_args, resolved_kwargs = self.get_resolved_arguments(context)

File "/usr/local/lib/python3.6/site-packages/django/template/library.py" in get_resolved_arguments
  177.         resolved_args = [var.resolve(context) for var in self.args]

File "/usr/local/lib/python3.6/site-packages/django/template/library.py" in <listcomp>
  177.         resolved_args = [var.resolve(context) for var in self.args]

File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in resolve
  698.                 new_obj = func(obj, *arg_vals)

File "/opt/netbox-2.6.11/netbox/utilities/templatetags/helpers.py" in percentage
  192.     return round(x / y * 100)

Exception Type: ZeroDivisionError at /dcim/power-feeds/2/
Exception Value: division by zero

@hSaria
Copy link
Contributor Author

hSaria commented Jan 13, 2020

@jeremystretch Yeah, I just tried to recreate this with a new database and it doesn't occur. Let me see what I did differently.

@hSaria
Copy link
Contributor Author

hSaria commented Jan 13, 2020

@jeremystretch updated the steps.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application and removed status: revisions needed This issue requires additional information to be actionable labels Jan 13, 2020
jeremystretch added a commit that referenced this issue Jan 13, 2020
Fixes #3905: divide by zero on power feeds with low values
@hSaria
Copy link
Contributor Author

hSaria commented Jan 14, 2020

@jeremystretch so I found out that this is also happening on the devices view (assign one of the power ports to a power feed. Shall I just keep chasing those down with if conditions or just change the percentage filter to return 0 if the denominator is 0?

Math people, please don't hurt me. I know diving by 0 is not equal to 0; just trying to avoid an exception here.

@jeremystretch
Copy link
Member

@hSaria Let's try to catch all the occurrences. IMO I think I'd rather know when we're dividing by zero than suppress the error.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants