Skip to content

Instantly share code, notes, and snippets.

@githubfoam
Last active February 18, 2025 12:36
Show Gist options
  • Save githubfoam/ad97a7ad4ad989ae3c3a0d143c1cfbbd to your computer and use it in GitHub Desktop.
Save githubfoam/ad97a7ad4ad989ae3c3a0d143c1cfbbd to your computer and use it in GitHub Desktop.
netbox_experience
--------------------------------------------------------------------------------------------------------------------
Using LibreNMS and NetBox together is a powerful combination for network monitoring and IP address management. Here are some best practices:
Integrate Device Discovery:
Use LibreNMS for automatic device discovery and import discovered devices into NetBox for inventory and documentation.
Configure webhooks or scripts to sync discovered devices from LibreNMS to NetBox.
IPAM Integration:
Manage IP addresses and subnets in NetBox and ensure LibreNMS uses NetBox as the source of truth for device IP assignments.
Sync NetBox’s IP information with LibreNMS for accurate monitoring.
Use Tags and Custom Fields:
Maintain consistency by using the same device tags and labels in both tools.
Add LibreNMS device IDs to NetBox custom fields for easy cross-referencing.
Automate Synchronization:
Use scripts or automation tools (e.g., Ansible) to keep devices, IP addresses, and metadata synchronized between LibreNMS and NetBox.
Schedule hourly or daily sync jobs using cron.
Alerts and Notifications:
Configure LibreNMS alerts to include NetBox device URLs for quick access to inventory details.
Use NetBox webhooks to trigger actions or updates in LibreNMS.
Role-Based Access Control (RBAC):
Implement proper user roles and permissions in both tools to secure access to sensitive configurations and data.
Backup Configurations:
Regularly back up both NetBox and LibreNMS databases and configuration files.
Store backups securely and test restoration procedures.
--------------------------------------------------------------------------------------------------------------------
1. Define Clear Roles for Each Tool
NetBox:
Use NetBox as the source of truth for your network infrastructure.
Manage IP addresses, devices, racks, circuits, and network relationships.
Document network topology, device roles, and configurations.
LibreNMS:
Use LibreNMS for monitoring and alerting.
Focus on performance metrics, availability, and fault detection.
Leverage its auto-discovery and polling capabilities to monitor devices.
Why: This separation ensures that NetBox remains the authoritative source for network design and documentation, while LibreNMS handles real-time monitoring and operational insights.
2. Integrate NetBox and LibreNMS
Sync Device Data:
Use NetBox as the primary source for device inventory and sync it with LibreNMS.
Tools like librenms-netbox-sync can automate the synchronization of devices, IPs, and other details from NetBox to LibreNMS.
Avoid Duplicate Data Entry:
Ensure that devices are added to NetBox first, and then automatically discovered or imported into LibreNMS.
Use APIs:
Both NetBox and LibreNMS provide REST APIs that can be used to programmatically exchange data between the two systems.
Why: Integration reduces manual effort, ensures consistency, and keeps both tools aligned.
3. Leverage NetBox for Device Metadata
Use NetBox to store detailed metadata about devices, such as:
Device roles (e.g., core switch, edge router).
Tenant or customer associations.
Physical location (rack, site, region).
Custom fields for additional context (e.g., maintenance windows, support contacts).
Export this metadata to LibreNMS to enrich monitoring data.
Why: Enriched metadata in LibreNMS helps in better filtering, grouping, and understanding of devices during monitoring.
4. Use NetBox for IPAM and Subnet Management
Manage all IP addresses, subnets, and VLANs in NetBox.
Use NetBox's IPAM features to track IP assignments, reservations, and utilization.
Sync IP information to LibreNMS for monitoring and alerting on IP-related issues (e.g., IP conflicts, subnet utilization).
Why: Centralized IP management in NetBox ensures accuracy and avoids conflicts, while LibreNMS can monitor IP-related metrics.
5. Automate Device Discovery and Updates
Use LibreNMS's auto-discovery feature to detect new devices on the network.
Cross-check discovered devices with NetBox to ensure they are documented and approved.
Automate the process of adding new devices to NetBox and LibreNMS using scripts or tools like Ansible.
Why: Automation reduces manual errors and ensures that all devices are properly documented and monitored.
6. Enrich Alerts with NetBox Data
Use NetBox's API to pull device metadata (e.g., location, role, tenant) into LibreNMS alerts.
Customize LibreNMS alerts to include context from NetBox, such as:
Device location.
Support contact information.
Maintenance schedules.
Why: Context-rich alerts help network engineers quickly identify and resolve issues.
7. Maintain Consistency Across Tools
Regularly audit and reconcile data between NetBox and LibreNMS.
Ensure that device names, IP addresses, and other details match in both systems.
Use scripts or tools to detect and resolve discrepancies.
Why: Consistency ensures that both tools provide accurate and reliable information.
8. Use NetBox for Network Documentation
Document all network assets, connections, and dependencies in NetBox.
Use NetBox's graphical interface to visualize network topology.
Share this documentation with LibreNMS to provide context for monitoring data.
Why: Comprehensive documentation improves troubleshooting and planning.
9. Monitor NetBox Data in LibreNMS
Use LibreNMS to monitor the health and performance of the NetBox server itself.
Track metrics such as CPU, memory, and disk usage for the NetBox host.
Why: Ensuring the availability of NetBox is critical since it serves as the source of truth.
10. Train Your Team
Ensure that your team understands the roles and capabilities of both tools.
Provide training on how to use NetBox for documentation and LibreNMS for monitoring.
Encourage collaboration between teams responsible for documentation and monitoring.
Why: A well-trained team maximizes the value of both tools.
Example Workflow:
Add a Device:
Add the device to NetBox with all relevant details (IP, role, location, etc.).
Sync the device to LibreNMS using an integration tool or script.
Monitor the Device:
Use LibreNMS to monitor the device's performance and availability.
Enrich alerts with NetBox metadata.
Troubleshoot:
Use NetBox to understand the device's role and connections.
Use LibreNMS to identify performance issues or outages.
Update Documentation:
Make changes in NetBox (e.g., IP reassignment, device decommissioning).
Sync changes to LibreNMS.
--------------------------------------------------------------------------------------------------------------------
WEB UI
Profile - API Tokens - Add a Token - Create
If You Also Need the Nmap Tool (System Package):
You need the actual Nmap tool for scanning:
sudo apt update
sudo apt install -y nmap
sudo apt update
sudo apt install -y python3 python3-venv python3-pip
python3 -m venv netboxvenv
source netboxvenv/bin/activate
pip3 install --upgrade pip
Install nmap
source netboxvenv/bin/activate
pip3 install python-nmap
pip3 install requests
python3 netbox_ip_scan.py
--------------------------------------------------------------------------------------------------------------------
sudo docker compose -f netbox-docker/docker-compose.yml exec netbox /opt/netbox/netbox/manage.py createsuperuser
sudo docker compose -f netbox-docker/docker-compose.yml docker compose stop
sudo docker compose -f netbox-docker/docker-compose.yml stop
sudo docker compose -f netbox-docker/docker-compose.yml start
Open the URL http://0.0.0.0:8000/
--------------------------------------------------------------------------------------------------------------------
To use NetBox Community Edition as an IP Address Management (IPAM) tool and scan your IP ranges, follow these steps:
🛠️ Step 1: Install NetBox Community Edition
Deploy NetBox:
You can use Docker or install it directly on a server. For Docker, run:
git clone -b release https://github.com/netbox-community/netbox-docker.git
cd netbox-docker
docker-compose up -d
Access Web Interface:
Open http://<server-ip>:8000 and log in with the admin credentials.
🔍 Step 2: Prepare IP Ranges for Scanning
Identify the IP ranges you want to manage.
In NetBox, navigate to IPAM > Prefixes and add your IP ranges (e.g., 192.168.1.0/24).
⚙️ Step 3: Automate IP Scanning
NetBox doesn't have built-in scanning. Use tools like nmap or NetBox Ansible Collection to automate IP discovery.
Option A: Python Script with NetBox API
import requests
import nmap
NETBOX_URL = "http://<server-ip>:8000"
API_TOKEN = "<your-api-token>"
IP_RANGE = "192.168.1.0/24"
# Scan IP Range
scanner = nmap.PortScanner()
scanner.scan(hosts=IP_RANGE, arguments='-sn')
# Add Discovered IPs to NetBox
for host in scanner.all_hosts():
ip_address = host
headers = {'Authorization': f"Token {API_TOKEN}", 'Content-Type': 'application/json'}
data = {"address": f"{ip_address}/32", "status": "active"}
response = requests.post(f"{NETBOX_URL}/api/ipam/ip-addresses/", headers=headers, json=data)
print(f"{ip_address} -> {response.status_code}")
Run the script:
python3 netbox_ip_scan.py
🔄 Step 4: Schedule Regular Scans (Optional)
Use cron on Linux:
crontab -e
Add a line to run the script every day at midnight:
0 0 * * * /usr/bin/python3 /path/to/netbox_ip_scan.py
✅ Step 5: Verify in NetBox
Go to IPAM > IP Addresses and check the discovered IPs.
Would you like help refining the script or setting up NetBox integrations with Ansible or Nornir? 😊
You said:
yes
ChatGPT said:
To refine the process and integrate NetBox with Ansible or Nornir, here's a step-by-step guide:
🚀 NetBox Integration with Ansible
📦 Step 1: Install Required Packages
On your control node:
pip install ansible netbox-sdk pynetbox
🛠️ Step 2: Create Ansible Inventory
all:
hosts:
netbox_server:
ansible_host: <netbox_ip>
ansible_user: <username>
ansible_password: <password>
⚙️ Step 3: Ansible Playbook to Manage IP Addresses
File: netbox_ipam.yml
- name: Manage IPAM in NetBox
hosts: localhost
gather_facts: no
tasks:
- name: Add IP prefixes to NetBox
netbox.netbox.netbox_prefix:
netbox_url: "http://<netbox_ip>:8000"
netbox_token: "<your_api_token>"
data:
prefix: "192.168.1.0/24"
status: "active"
description: "Office Network"
state: present
- name: Run IP scan and update NetBox
command: nmap -sn 192.168.1.0/24
Run the playbook:
ansible-playbook netbox_ipam.yml
⚡ NetBox Integration with Nornir
📦 Step 1: Install Nornir
pip install nornir pynetbox nmap
🛠️ Step 2: Nornir Configuration
File: config.yaml
inventory:
plugin: SimpleInventory
options:
host_file: "hosts.yaml"
group_file: "groups.yaml"
defaults_file: "defaults.yaml"
File: hosts.yaml
localhost:
hostname: "127.0.0.1"
groups:
- netbox
File: defaults.yaml
username: "admin"
password: "password"
🖧 Step 3: Python Script to Sync with NetBox
File: nornir_netbox_sync.py
from nornir import InitNornir
import pynetbox
import nmap
# Initialize Nornir
nr = InitNornir(config_file="config.yaml")
# NetBox API connection
netbox = pynetbox.api('http://<netbox_ip>:8000', token='<your_api_token>')
# IP Range Scan
ip_range = "192.168.1.0/24"
scanner = nmap.PortScanner()
scanner.scan(hosts=ip_range, arguments='-sn')
# Add discovered IPs to NetBox
for ip in scanner.all_hosts():
ip_address = f"{ip}/32"
if not netbox.ipam.ip_addresses.filter(address=ip_address):
netbox.ipam.ip_addresses.create({'address': ip_address, 'status': 'active'})
print(f"Added {ip_address} to NetBox")
🖍️ Run the Script
python3 nornir_netbox_sync.py
🔍 Verification
Log in to NetBox and check under IPAM > IP Addresses. The discovered IPs should appear with status active.
Would you like to schedule periodic scans or optimize performance for larger networks?
1. Schedule Periodic Scans with cron
Edit cron Jobs
crontab -e
Add Entry to Scan Every Day at 2 AM
0 2 * * * /usr/bin/python3 /path/to/nornir_netbox_sync.py >> /var/log/netbox_scan.log 2>&1
🚀 2. Optimize Nmap Scanning
Speed Up Scans with Nmap Flags
Use these arguments to improve performance:
-T4: Aggressive timing.
-n: No DNS resolution.
--max-retries 2: Reduce retry attempts.
--host-timeout 5s: Timeout after 5 seconds per host.
Updated Code:
scanner.scan(hosts=ip_range, arguments='-sn -T4 -n --max-retries 2 --host-timeout 5s')
📦 3. NetBox Performance Optimization
Enable Database Connection Pooling:
Adjust DATABASE_CONN_MAX_AGE in NetBox's configuration.py.
Use Redis Caching:
NetBox uses Redis for caching. Ensure it's properly configured.
API Pagination:
When querying many IP addresses, use pagination to reduce load.
🔐 4. Security Best Practices
Use Environment Variables for API tokens instead of hardcoding.
Limit Network Scans to only required subnets.
Log Activities to monitor unexpected behavior.
Would you like an extended Python script with better error handling and environment variable management? 😊
You said:
yes
ChatGPT said:
import os
import nmap
import requests
from requests.exceptions import RequestException
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# NetBox API details
NETBOX_URL = os.getenv("NETBOX_URL")
NETBOX_TOKEN = os.getenv("NETBOX_TOKEN")
# IP Ranges to scan
IP_RANGES = [
"192.168.1.0/24",
"192.168.2.0/24",
"10.0.0.0/16"
]
# Initialize scanner
scanner = nmap.PortScanner()
def scan_ip_range(ip_range):
"""Scan the provided IP range for active hosts."""
print(f"Scanning {ip_range}...")
try:
scanner.scan(hosts=ip_range, arguments='-sn -T4 -n --max-retries 2 --host-timeout 5s')
active_hosts = [host for host in scanner.all_hosts() if scanner[host].state() == "up"]
print(f"Active hosts in {ip_range}: {active_hosts}")
return active_hosts
except Exception as e:
print(f"Failed to scan {ip_range}: {e}")
return []
def add_ip_to_netbox(ip_address):
"""Add discovered IP to NetBox."""
url = f"{NETBOX_URL}/api/ipam/ip-addresses/"
headers = {
"Authorization": f"Token {NETBOX_TOKEN}",
"Content-Type": "application/json"
}
payload = {
"address": f"{ip_address}/32",
"status": "active",
"description": "Discovered via automated scan"
}
try:
response = requests.post(url, headers=headers, json=payload, timeout=5)
if response.status_code == 201:
print(f"Added {ip_address} to NetBox.")
elif response.status_code == 400:
print(f"{ip_address} already exists in NetBox.")
else:
print(f"Failed to add {ip_address} to NetBox: {response.text}")
except RequestException as e:
print(f"Error connecting to NetBox: {e}")
def main():
"""Main function to scan and sync IP addresses."""
for ip_range in IP_RANGES:
active_hosts = scan_ip_range(ip_range)
for host in active_hosts:
add_ip_to_netbox(host)
if __name__ == "__main__":
main()
I've added comments to highlight potential improvements, such as validating environment variables, adjusting scan arguments, enhancing timeout handling, and adding logging for better traceability. Let me know if you need further assistance!
--------------------------------------------------------------------------------------------------------------------
To use NetBox Community Edition as an IP Address Management (IPAM) tool and scan IP ranges to list them in NetBox, you can follow these steps:
1. Install NetBox Community Edition
If you haven't already installed NetBox, follow the official installation guide:
NetBox Installation Documentation
Ensure NetBox is running and accessible via a web browser.
2. Add IP Ranges to NetBox
Before scanning, you need to define the IP ranges in NetBox:
Log in to the NetBox web interface.
Navigate to IPAM > Prefixes.
Click Add a Prefix.
Enter the IP range (e.g., 192.168.1.0/24) and assign it to a site or VLAN if applicable.
Save the prefix.
3. Scan the IP Ranges
NetBox itself does not include built-in IP scanning functionality. However, you can use external tools to scan the IP ranges and then import the results into NetBox.
Option A: Use nmap to Scan IP Ranges
Install nmap on your system:
bash
Copy
sudo apt install nmap # For Debian/Ubuntu
sudo yum install nmap # For CentOS/RHEL
Scan the IP range:
bash
Copy
nmap -sn 192.168.1.0/24 -oG - | awk '/Up$/{print $2}' > active_ips.txt
This command will scan the range 192.168.1.0/24 and save active IPs to active_ips.txt.
Parse the results and format them for NetBox.
Option B: Use a Python Script
You can use Python with libraries like python-nmap or scapy to scan IP ranges and output the results in a format compatible with NetBox.
Example Python script using python-nmap:
python
Copy
import nmap
# Initialize the scanner
scanner = nmap.PortScanner()
# Scan the IP range
scanner.scan(hosts='192.168.1.0/24', arguments='-sn')
# Print active IPs
active_ips = []
for host in scanner.all_hosts():
if scanner[host].state() == 'up':
active_ips.append(host)
print("Active IPs:", active_ips)
4. Import Scanned IPs into NetBox
Once you have the list of active IPs, you can add them to NetBox manually or via the API.
Option A: Manual Import
Log in to NetBox.
Navigate to IPAM > IP Addresses.
Click Add an IP Address.
Enter the IP address and assign it to a device, interface, or VLAN as needed.
Repeat for all active IPs.
Option B: Use the NetBox API
You can automate the process using the NetBox API. Here's an example using Python and the requests library:
Install the requests library:
bash
Copy
pip install requests
Use the following script to add IPs to NetBox:
python
Copy
import requests
# NetBox API details
NETBOX_URL = "http://netbox.example.com"
API_TOKEN = "your_api_token_here"
headers = {
"Authorization": f"Token {API_TOKEN}",
"Content-Type": "application/json",
}
# List of active IPs (from your scan)
active_ips = ["192.168.1.1", "192.168.1.2", "192.168.1.100"]
# Add each IP to NetBox
for ip in active_ips:
data = {
"address": ip,
"status": "active", # Set status as needed
}
response = requests.post(f"{NETBOX_URL}/api/ipam/ip-addresses/", headers=headers, json=data)
if response.status_code == 201:
print(f"Successfully added {ip}")
else:
print(f"Failed to add {ip}: {response.text}")
5. Verify in NetBox
After importing the IPs, verify them in the NetBox web interface:
Navigate to IPAM > IP Addresses to see the list of added IPs.
Ensure the IPs are assigned to the correct devices, interfaces, or VLANs.
6. Automate the Process (Optional)
If you want to automate the scanning and importing process:
Set up a cron job or scheduled task to run the scan periodically.
Use the NetBox API to update IP addresses dynamically.
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment