Understanding Netbox Database Structure

Pedro DPedro D
3 min read

While trying to wrap my head around some of the NetBox database relationships, I wished for a database schema diagram. I looked through the documentation and code repo but found nothing. I needed a tool to automatically generate diagrams based on an existing database.

💡
Check how to install Netbox as Docker here

A quick research led me to:

SchemaSpy

  • Features: An open-source Java-based tool that analyzes your database metadata and generates interactive HTML diagrams and reports.

  • Good for: Automatically generating detailed reports and schema diagrams from an existing database

— DBeaver

  • Features: A universal database management tool that supports multiple database systems (MySQL, PostgreSQL, Oracle, etc.). It can generate schema diagrams from an existing database.

  • Good for: Handling multiple database systems and visualizing complex schemas.

— DbSchema

  • Features: A universal database tool that can reverse engineer a database to create a visual schema or help you design a schema and then export it as SQL.

  • Good for: Users who need both a visual representation and SQL export functionality.


SchemaSpy

Documentation

I set up a fresh install of netbox docker, and downloaded schemaspy (docker image) and its dependencies.

Create an output directory

mkdir schemaspy
cd schemaspy

Create a DB file

The db information for your netbox is under /env/postgres.env file:

POSTGRES_DB=netbox
POSTGRES_PASSWORD=FAKE_PASSWORD
POSTGRES_USER=netbox

To understand the values that we can pass for the db:

# type of database. Run with -dbhelp for details
schemaspy.t=pgsql11
# database properties: host, port number, name user, password
schemaspy.host=postgres
schemaspy.port=5432
schemaspy.db=netbox
schemaspy.u=netbox
schemaspy.p=FAKE_PASSWORD

Run

The documentation listed all the necessary options. You just need to provide the path to the drive, the database credentials, and the output directory and format. Then, it will generate a report of the database structure.

docker run \
    -v "$PWD/output:/output" \
    --network netbox-docker_default \
    -v "./postgresdb.properties:/schemaspy.properties" \
    schemaspy/schemaspy:latest

Output

It produced a browsable report in the output directory, filled with interesting information about the database.

When I checked the "Relationships" tab, it displayed a diagram of the tables and their relationships—exactly what I was looking for!

Result

Two diagrams are generated, a “compact” and a “large” (can verify under /diagrams/summary). The Netbox version at this time of this writing, NetBox Community v4.1.6

Review

The diagrams look good, but there are many arrow intersections, making them hard to read. The export file is in HTML, so I can't drag and drop to move the tables around.

The following is an image for the dcim_device table. You can see that this would not scale very nicely with the amount of arrows.

This is a useful tool, but I'm curious if I can export the diagram to a popular format that other programs can read, like MS Visio, Pencil, or another diagram tool.

0
Subscribe to my newsletter

Read articles from Pedro D directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pedro D
Pedro D