From aab28d03ba26b980c97e4e44ff2da8637121b90d Mon Sep 17 00:00:00 2001 From: Brady Lamprecht Date: Wed, 19 Dec 2018 16:26:24 -0700 Subject: [PATCH 1/4] Changing 'reports' from a volume to a mapped directory This will allow eaiser interaction with the report functionality of the 'netbox' container. --- docker-compose.yml | 2 +- reports/devices.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 reports/devices.py diff --git a/docker-compose.yml b/docker-compose.yml index 3809277..ebcfab3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,10 +15,10 @@ services: - ./startup_scripts:/opt/netbox/startup_scripts:ro - ./initializers:/opt/netbox/initializers:ro - ./configuration:/etc/netbox/config:ro + - ./reports:/etc/netbox/reports:ro - netbox-nginx-config:/etc/netbox-nginx/ - netbox-static-files:/opt/netbox/netbox/static - netbox-media-files:/opt/netbox/netbox/media - - netbox-report-files:/etc/netbox/reports:ro netbox-worker: <<: *netbox depends_on: diff --git a/reports/devices.py b/reports/devices.py new file mode 100644 index 0000000..2dee036 --- /dev/null +++ b/reports/devices.py @@ -0,0 +1,46 @@ +from dcim.constants import CONNECTION_STATUS_PLANNED, DEVICE_STATUS_ACTIVE +from dcim.models import ConsolePort, Device, PowerPort +from extras.reports import Report + + +class DeviceConnectionsReport(Report): + description = "Validate the minimum physical connections for each device" + + def test_console_connection(self): + + # Check that every console port for every active device has a connection defined. + for console_port in ConsolePort.objects.select_related('device').filter(device__status=DEVICE_STATUS_ACTIVE): + if console_port.connected_endpoint is None: + self.log_failure( + console_port.device, + "No console connection defined for {}".format(console_port.name) + ) + elif console_port.connection_status == CONNECTION_STATUS_PLANNED: + self.log_warning( + console_port.device, + "Console connection for {} marked as planned".format(console_port.name) + ) + else: + self.log_success(console_port.device) + + def test_power_connections(self): + + # Check that every active device has at least two connected power supplies. + for device in Device.objects.filter(status=DEVICE_STATUS_ACTIVE): + connected_ports = 0 + for power_port in PowerPort.objects.filter(device=device): + if power_port.connected_endpoint is not None: + connected_ports += 1 + if power_port.connection_status == CONNECTION_STATUS_PLANNED: + self.log_warning( + device, + "Power connection for {} marked as planned".format(power_port.name) + ) + if connected_ports < 2: + self.log_failure( + device, + "{} connected power supplies found (2 needed)".format(connected_ports) + ) + else: + self.log_success(device) + From 34cd2cc5775ff4e328ced15833ff3a79b4d10903 Mon Sep 17 00:00:00 2001 From: Brady Lamprecht Date: Wed, 2 Jan 2019 16:45:02 +0000 Subject: [PATCH 2/4] Renamed `devices.py` to `devices.py.example` to set the example report to be disabled by default --- reports/{devices.py => devices.py.example} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename reports/{devices.py => devices.py.example} (100%) diff --git a/reports/devices.py b/reports/devices.py.example similarity index 100% rename from reports/devices.py rename to reports/devices.py.example From 0bae952410e35a03d2c2b84573ce450318ed2d69 Mon Sep 17 00:00:00 2001 From: Brady Lamprecht Date: Wed, 2 Jan 2019 17:12:46 -0700 Subject: [PATCH 3/4] Documenting reporting usage in `README.md` Updated `README.md` on how to use the reporting feature of NetBox within this container. --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 7f88500..21c5288 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,15 @@ However, if you don't need this functionality, leave these blank. [napalm-doc]: http://napalm.readthedocs.io/en/latest/index.html [netbox-napalm-doc]: https://netbox.readthedocs.io/en/latest/configuration/optional-settings/#napalm_username +### Customizable Reporting ### + +NetBox includes [customized reporting][netbox-reports-doc] that allows the user to write Python code and determine the validity of the data within NetBox. The `REPORTS_ROOT` variable is setup as a mapped directory within this Docker container to `/reports/` and includes the example directly from the documentation for `devices.py`. However, it has been renamed to `devices.py.example` which prevents NetBox from recognizing it as a valid report. This was done to avoid unnessary issues from being opened when the default does not work for someone's expectations. + +To re-enable this default report, simply rename `devices.py.example` to `devices.py` and browse within the WebUI to `/extras/reports/`. +You can also dynamically add any other report to this same directory and NetBox will be able to see it without restarting the container. + +[netbox-reports-doc]: https://netbox.readthedocs.io/en/stable/additional-features/reports/ + ### Custom Initialization Code (e.g. Automatically Setting Up Custom Fields) When using `docker-compose`, all the python scripts present in `/opt/netbox/startup_scripts` will automatically be executed after the application boots in the context of `./manage.py`. From db7daee86e168450e8cca2d9f03239b076107b2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Mon, 7 Jan 2019 10:43:41 +0100 Subject: [PATCH 4/4] formatted --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 21c5288..ecfd8a9 100644 --- a/README.md +++ b/README.md @@ -131,9 +131,12 @@ However, if you don't need this functionality, leave these blank. [napalm-doc]: http://napalm.readthedocs.io/en/latest/index.html [netbox-napalm-doc]: https://netbox.readthedocs.io/en/latest/configuration/optional-settings/#napalm_username -### Customizable Reporting ### +### Customizable Reporting -NetBox includes [customized reporting][netbox-reports-doc] that allows the user to write Python code and determine the validity of the data within NetBox. The `REPORTS_ROOT` variable is setup as a mapped directory within this Docker container to `/reports/` and includes the example directly from the documentation for `devices.py`. However, it has been renamed to `devices.py.example` which prevents NetBox from recognizing it as a valid report. This was done to avoid unnessary issues from being opened when the default does not work for someone's expectations. +NetBox includes [customized reporting][netbox-reports-doc] that allows the user to write Python code and determine the validity of the data within NetBox. +The `REPORTS_ROOT` variable is setup as a mapped directory within this Docker container to `/reports/` and includes the example directly from the documentation for `devices.py`. +However, it has been renamed to `devices.py.example` which prevents NetBox from recognizing it as a valid report. +This was done to avoid unnessary issues from being opened when the default does not work for someone's expectations. To re-enable this default report, simply rename `devices.py.example` to `devices.py` and browse within the WebUI to `/extras/reports/`. You can also dynamically add any other report to this same directory and NetBox will be able to see it without restarting the container.