|
| 1 | +# Gateway Monitor |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +A professional-grade web application to monitor and compare multiple WAN connections. It tracks real-time latency using `mtr` and monitors Bufferbloat (latency under load) using the official Ookla Speedtest CLI. |
| 6 | + |
| 7 | +[](https://opensource.org/licenses/MIT) |
| 8 | + |
| 9 | +## Features |
| 10 | +- **Latency Monitoring**: Continuous tracking of network health using `mtr` (My Traceroute). |
| 11 | +- **Bufferbloat Tracking**: Integrated Ookla Speedtest to measure how your connection handles heavy load. |
| 12 | +- **Dynamic Host Management**: Add, rename, or delete gateways (WANs) directly from the web dashboard. |
| 13 | +- **Per-Host Configuration**: Set individual Speedtest Server IDs for each gateway to ensure the most accurate results for diverse connections (e.g., Fiber vs Starlink). |
| 14 | +- **Comparison Visualizations**: Side-by-side charts for multiple connections with smart `(LOADED)` tooltip indicators. |
| 15 | +- **Speed & Bloat Analytics**: Combined visualization of download speed, upload speed, and added latency on a single timeline. |
| 16 | +- **Bufferbloat Grading System**: Automated scoring (A+ to F) based on added latency, calibrated for competitive gaming standards. |
| 17 | +- **CSV Export**: Comprehensive export options for individual or combined datasets, now including a hop-specific filter for MTR data. |
| 18 | +- **Mobile Responsive**: Clean, modern dashboard built with Bootstrap 5 and Chart.js. |
| 19 | + |
| 20 | +## Security Recommendations |
| 21 | + |
| 22 | +- **API Keys**: Keep your gateway API keys private. They are required for nodes to ingest data. |
| 23 | +- **Maintenance Script**: The `api/maintenance.php` script handles data pruning. For production, it is highly recommended to protect this script with a secret key or restrict access to localhost/trusted IPs to prevent unauthorized database pruning. |
| 24 | +- **Database Access**: Ensure your `includes/db.php` is not accessible from the web (it is excluded by `.gitignore`, but double-check your web server configuration). |
| 25 | + |
| 26 | +## Architecture |
| 27 | + |
| 28 | +The system uses a **Centralized Server** and multiple **Remote Nodes**: |
| 29 | +1. **Central Server**: Hosts the MySQL database and the PHP-based web dashboard. |
| 30 | +2. **Remote Nodes**: Any Linux machine (Ubuntu/Debian recommended) connected to a gateway you want to monitor. These run the collection scripts via `cron`. |
| 31 | + |
| 32 | +## Installation & Setup |
| 33 | + |
| 34 | +### 1. Server Setup (Ubuntu/Debian) |
| 35 | + |
| 36 | +#### A. Install LAMP Stack |
| 37 | +Ensure your server has Apache, MySQL, and PHP 8.1+ installed. |
| 38 | +```bash |
| 39 | +sudo apt update |
| 40 | +sudo apt install apache2 mysql-server php php-mysql libapache2-mod-php |
| 41 | +``` |
| 42 | + |
| 43 | +#### B. Database Initialization |
| 44 | +1. Log into MySQL and create the database: |
| 45 | + ```sql |
| 46 | + CREATE DATABASE gateway_monitor; |
| 47 | + ``` |
| 48 | +2. Import the schema: |
| 49 | + ```bash |
| 50 | + mysql -u root -p gateway_monitor < sql/schema.sql |
| 51 | + ``` |
| 52 | + |
| 53 | +#### C. Web Files |
| 54 | +1. Clone or copy this repository to `/var/www/html/gateway_monitor`. |
| 55 | +2. Configure database access: |
| 56 | + ```bash |
| 57 | + cp includes/db.php.example includes/db.php |
| 58 | + nano includes/db.php |
| 59 | + ``` |
| 60 | + *Update the host, database name, user, and password.* |
| 61 | + |
| 62 | +### 2. Dashboard Configuration |
| 63 | + |
| 64 | +1. Open the dashboard in your browser (e.g., `http://your-server/gateway_monitor`). |
| 65 | +2. Click the **"Hosts"** button (CPU icon) in the navbar. |
| 66 | +3. **Add your Gateways**: Enter a name for each WAN connection (e.g., "Starlink", "Fiber Backup"). |
| 67 | +4. **Copy API Keys**: Once added, each host will have a unique API Key. You will need these for the remote nodes. |
| 68 | +5. **Configure Views**: Use the dropdowns in the navbar to switch between latency metrics, specific hops, and speed test history. |
| 69 | +6. (Optional) Click **"Settings"** (Gear icon) to adjust global defaults like the default dashboard metric, test interval, and data retention. |
| 70 | + |
| 71 | +### 3. Remote Node Setup (On each WAN) |
| 72 | + |
| 73 | +On the machine physically connected to the WAN you want to monitor: |
| 74 | + |
| 75 | +#### A. Install Dependencies |
| 76 | +```bash |
| 77 | +# Install MTR and Curl (Requires mtr 0.93+ for JSON support) |
| 78 | +sudo apt install mtr-tiny curl |
| 79 | +
|
| 80 | +# Install Official Ookla Speedtest CLI (Required for JSON output) |
| 81 | +curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash |
| 82 | +sudo apt install speedtest |
| 83 | +``` |
| 84 | + |
| 85 | +#### B. Configure Scripts |
| 86 | +1. Copy `scripts/monitor.sh` and `scripts/speedtest.sh` to the remote machine. |
| 87 | +2. Make them executable: `chmod +x *.sh`. |
| 88 | +3. Edit the configuration at the top of both scripts: |
| 89 | + * `API_URL`: The URL to your server's API folder (e.g., `http://1.2.3.4/gateway_monitor/api`). |
| 90 | + * `API_KEY`: The unique key you copied from the dashboard for **this specific host**. |
| 91 | +
|
| 92 | +#### C. Schedule Cron Jobs |
| 93 | +Run `crontab -e` and add: |
| 94 | +```bash |
| 95 | +# Run Latency Monitor every minute |
| 96 | +* * * * * /path/to/monitor.sh |
| 97 | +
|
| 98 | +# Run Bufferbloat Controller every minute (Smart Scheduling) |
| 99 | +* * * * * /path/to/speedtest.sh |
| 100 | +``` |
| 101 | +
|
| 102 | +--- |
| 103 | +
|
| 104 | +## Detailed Usage |
| 105 | +
|
| 106 | +### Managing Speedtest Servers |
| 107 | +Accuracy in Bufferbloat testing depends on using a reliable nearby server. |
| 108 | +- **Global Default**: Set in the "Settings" modal. Used by all hosts if they don't have a specific ID set. |
| 109 | +- **Per-Host Override**: Set in the "Manage Gateways" modal. Ideal if your gateways are in different geographic regions or use different ISPs. |
| 110 | +- *Find Server IDs at: [speedtest.net/performance/servers](https://www.speedtest.net/performance/servers)* |
| 111 | + |
| 112 | +### Smart Scheduling |
| 113 | +The `speedtest.sh` script runs every minute but only executes a test when instructed by the API. |
| 114 | +- The API follows the `speedtest_interval` (default 60 mins). |
| 115 | +- To bypass the timer and test immediately, run: `./scripts/speedtest.sh --force`. |
| 116 | + |
| 117 | +### Bufferbloat Grading & Load Detection |
| 118 | + |
| 119 | +*Note: Place your screenshots in `assets/img/` with the filenames shown above to display them in this README.* |
| 120 | + |
| 121 | +The app measures **"Added Latency"** (Loaded Latency - Idle Latency). To keep graphs clean, visual shading has been replaced with smart markers and tooltips: |
| 122 | +- **Orange Vertical Lines**: These mark the exact time a Speedtest was performed on any WAN connection. |
| 123 | +- **(LOADED) Status**: When hovering over MTR latency points, the tooltip will display `(LOADED)` if that sample was captured during an active speed test. This status is also visible in the **MTR Raw Data** table. Correlated automatically via timestamps. |
| 124 | + |
| 125 | +### Raw Data Exploration & Export |
| 126 | + |
| 127 | +Below the chart, two tabs provide granular access to your data: |
| 128 | +- **MTR Raw Data**: Shows every hop for every MTR run. Includes filters for specific Hosts and Hops. |
| 129 | +- **Speed Test History**: Shows full details of every speed test, including raw latencies and Jitter. |
| 130 | +- **Consolidated Exports**: Each tab features a "Download CSV" button that respects your active filters (Time, Host, and Hop). |
| 131 | + |
| 132 | +#### Scoring Tiers: |
| 133 | +- **A+ (< 2ms)**: Perfect. Zero perceptible delay under load. |
| 134 | +- **A (< 10ms)**: Competitive. Ideal for pro gaming. |
| 135 | +- **B (< 25ms)**: Solid. Great for casual gaming. |
| 136 | +- **C (< 50ms)**: Fair. Noticeable "muddy" feeling. |
| 137 | +- **D (< 100ms)**: Poor. Significant lag spikes. |
| 138 | +- **F (> 100ms)**: Unplayable. |
| 139 | + |
| 140 | +### Maintenance |
| 141 | +The system includes an automatic cleanup mechanism: |
| 142 | +- **Data Retention**: Set in the "Settings" modal (default 30 days). |
| 143 | +- **Auto-Pruning**: The API automatically deletes records older than the retention period to keep the database lean and the dashboard fast. |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Understanding Metrics |
| 148 | +- **Average (Avg)**: The mean latency. Good for general health. |
| 149 | +- **Worst**: The highest latency recorded. Critical for identifying lag spikes. |
| 150 | +- **StDev (Standard Deviation)**: Measures jitter. High StDev means an unstable connection. |
| 151 | +- **Packet Loss (%)**: Percentage of packets that never returned. |
| 152 | +- **Bufferbloat (Added Latency)**: The extra delay introduced when your connection is under heavy load. Measured in ms. |
| 153 | +- **Speed Test (Mbps)**: Real-world bandwidth for Download and Upload. These are displayed together on a single graph for easier comparison. |
| 154 | + |
| 155 | +## Troubleshooting |
| 156 | +- **No Data on Charts**: |
| 157 | + 1. Run `./scripts/monitor.sh` manually on the remote node. |
| 158 | + 2. Check for errors like "mtr: command not found" or "Unauthorized". |
| 159 | + 3. Ensure `mtr` version 0.93+ is installed (`mtr --version`). Older versions do not support the `--json` flag. |
| 160 | + 4. If using an older OS, you may need to compile `mtr` from source or use a newer repository. |
| 161 | +- **Speedtest Fails**: |
| 162 | + 1. Run `./scripts/speedtest.sh --force` manually. |
| 163 | + 2. Ensure the official Ookla Speedtest CLI is installed, NOT the `speedtest-cli` python package. |
| 164 | + 3. Verify your API Key and `API_URL` are correct in the script. |
| 165 | +- **Permission Denied**: |
| 166 | + 1. Ensure the scripts are executable: `chmod +x scripts/*.sh`. |
| 167 | + 2. Some systems require `sudo` for `mtr` if not correctly configured with capabilities. If so, update your crontab to use `sudo`. |
| 168 | + |
| 169 | +## Verifying the Installation |
| 170 | +1. **Check API**: Run `./scripts/speedtest.sh --force` manually on a node. It should report "Ingestion complete." |
| 171 | +2. **Check Database**: |
| 172 | + ```sql |
| 173 | + SELECT name, last_speed_test FROM hosts; |
| 174 | + ``` |
| 175 | +3. **Check Dashboard**: Open `index.php`. You should see the chart and Bufferbloat Report Card. |
| 176 | + |
| 177 | +--- |
| 178 | + |
| 179 | +## Contributing |
| 180 | + |
| 181 | +Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines. |
| 182 | + |
| 183 | +## License |
| 184 | + |
| 185 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments