Tools for monitoring CPU and memory usage of router containers during OSPF tests.
Monitor a single router in real-time:
./monitor_resources.sh clab-ospf6_grid5x5-router_00_00Monitor multiple routers for comparison:
./monitor_resources.sh \
clab-ospf6_grid5x5-router_00_00 \
clab-ospf6_grid5x5-router_00_01Output: CSV file resource_usage.csv with timestamp, container name, CPU%, memory usage, and memory%
Generate a plot from the collected data:
# Using system Python with dependencies
python plot_resources.py resource_usage.csv -o usage_graph.png
# Using uv (recommended)
uv run plot_resources.py resource_usage.csv -o usage_graph.pngOutput: PNG image showing CPU and memory usage over time
Real-time container resource monitoring with CSV export.
Features:
- Monitor single or multiple containers simultaneously
- Configurable sampling interval
- CSV output with timestamps
- Graceful shutdown (Ctrl+C)
- Container validation
Usage:
./monitor_resources.sh [OPTIONS] CONTAINER [CONTAINER...]
OPTIONS:
-i, --interval SECONDS Sampling interval (default: 1)
-o, --output FILE Output CSV file (default: resource_usage.csv)
-h, --help Show helpExamples:
# Basic usage
./monitor_resources.sh clab-ospf6_grid5x5-router_00_00
# Custom interval (every 2 seconds)
./monitor_resources.sh -i 2 clab-ospf6_grid5x5-router_00_00
# Custom output file
./monitor_resources.sh -o test_data.csv clab-ospf6_grid5x5-router_00_00
# Background monitoring (run for 60 seconds)
./monitor_resources.sh clab-ospf6_grid5x5-router_00_00 &
MONITOR_PID=$!
sleep 60
kill $MONITOR_PIDVisualize resource usage data from CSV files.
Features:
- Dual-axis plots (CPU% and Memory%)
- Multi-container support (one subplot per container)
- Statistics display (average and max values)
- Multiple output formats (PNG, PDF, SVG)
Dependencies:
# Install with uv
uv pip install matplotlib pandasUsage:
python plot_resources.py [OPTIONS] CSV_FILE
OPTIONS:
-o, --output FILE Output image file
-t, --title TEXT Custom plot titleExamples:
# Display plot interactively
python plot_resources.py resource_usage.csv
# Save to PNG
python plot_resources.py resource_usage.csv -o graph.png
# Save to PDF with custom title
python plot_resources.py resource_usage.csv -o graph.pdf -t "LSA-only Test"Monitor the non-frozen router (router_00_00) during LSA-only tests:
# 1. Generate and deploy topology
uv run topo-gen generate grid 5 --lsa-only -y
sudo containerlab deploy -t ospf6_grid5x5/clab.yml
# 2. Start monitoring
./monitor_resources.sh -o lsa_only_test.csv clab-ospf6_grid5x5-router_00_00 &
MONITOR_PID=$!
# 3. Wait for convergence (or run your tests)
sleep 120
# 4. Stop monitoring
kill $MONITOR_PID
# 5. Generate visualization
uv run plot_resources.py lsa_only_test.csv -o lsa_only_graph.png -t "Router 00_00 - LSA-only Mode"Compare resource usage between a normal router and a frozen router:
# Monitor both routers
./monitor_resources.sh -o comparison.csv \
clab-ospf6_grid5x5-router_00_00 \
clab-ospf6_grid5x5-router_00_01 &
sleep 120
kill %1
# Visualize comparison
uv run plot_resources.py comparison.csv -o comparison.pngExpected Results:
router_00_00(normal): Higher CPU usage due to SPF calculationsrouter_00_01(frozen): Lower CPU usage (no SPF due to throttle)
Monitor resource spikes during network events:
# Start monitoring
./monitor_resources.sh -o stress_test.csv clab-ospf6_grid5x5-router_00_00 &
# Trigger link flap
docker exec clab-ospf6_grid5x5-router_01_01 ip link set eth1 down
sleep 5
docker exec clab-ospf6_grid5x5-router_01_01 ip link set eth1 up
# Continue monitoring for 60 seconds
sleep 60
kill %1
# Visualize
uv run plot_resources.py stress_test.csv -o stress_test.pngThe monitoring script generates CSV files with the following columns:
| Column | Description | Example |
|---|---|---|
| Timestamp | UTC timestamp | 2026-01-18 02:00:00 |
| Container | Container name | clab-ospf6_grid5x5-router_00_00 |
| CPU% | CPU usage percentage | 12.5 |
| MemUsage | Current memory usage with unit | 45.2MiB |
| MemLimit | Memory limit with unit | 1.5GiB |
| Mem% | Memory usage percentage | 3.01 |
Example:
Timestamp,Container,CPU%,MemUsage,MemLimit,Mem%
2026-01-18 02:00:00,clab-ospf6_grid5x5-router_00_00,12.5,45.2MiB,1.5GiB,3.01
2026-01-18 02:00:01,clab-ospf6_grid5x5-router_00_00,15.3,45.5MiB,1.5GiB,3.03Error: Container 'xxx' not found or not running
Solution: Verify container name and status
# List running containers
docker ps --format '{{.Names}}'
# Check if containerlab topology is deployed
sudo containerlab inspect -t ospf6_grid5x5/clab.ymlError: Permission denied when running Docker commands
Solution: Run with sudo or add user to docker group
# Add user to docker group (requires logout/login)
sudo usermod -aG docker $USER
# Or use sudo
sudo ./monitor_resources.sh clab-ospf6_grid5x5-router_00_00Error: ModuleNotFoundError: No module named 'matplotlib'
Solution: Install dependencies
uv pip install matplotlib pandasIf the plot doesn't display in a headless environment (SSH without X11):
# Always save to file instead
python plot_resources.py data.csv -o output.pngThe monitoring tools have minimal performance impact:
docker stats: ~0.5% CPU overhead- Sampling interval: 1 second is safe for most scenarios
- CSV writing: Negligible I/O impact
For extremely large topologies (100+ containers), consider:
- Increasing sampling interval to 2-5 seconds
- Monitoring only critical routers
- Using dedicated monitoring containers
For production environments, consider:
- Prometheus + cAdvisor: For persistent metrics storage
- Grafana: For real-time dashboards
- InfluxDB: For time-series data
The scripts in this project are designed for quick testing and development workflows.