Sistem monitoring jaringan berbasis Python untuk simulasi jaringan menggunakan GNS3 + VirtualBox. Memonitor status perangkat, traffic bandwidth, dan metrics SNMP secara realtime dengan manajemen device dinamis dari database dan backup otomatis ke Supabase.
- Arsitektur
- Stack & Library
- Persyaratan Sistem
- Struktur Project
- Instalasi
- Konfigurasi
- Migrasi Database
- Menjalankan Program
- API Endpoints
- Menu CLI
- Backup Supabase
- Troubleshooting
Internet
│
Cloudflare Zero Trust Tunnel
│
└── monitoring.domain.com → FastAPI (port 8000)
di server Debian
Laravel (hosting terpisah)
└── hit API via HTTPS → monitoring.domain.com
├── GET /status → status semua device
├── POST /ping → ping manual
├── POST /reboot → reboot device via SSH
├── GET /api/devices → list device dari DB
├── POST /api/devices → tambah device baru
├── PUT /api/devices/{id} → update device
├── PATCH /api/devices/{id}/toggle → aktif/nonaktif
├── DELETE /api/devices/{id} → hapus + archive ke Supabase
└── POST /api/backup/manual → trigger backup ke Supabase
Server Debian (GNS3)
├── main.py → polling SNMP, ping, bandwidth (scheduler)
├── menu.py → CLI menu monitoring & manajemen device
├── migrate.py → setup schema & seed device ke database
├── test_api.py → test semua endpoint API via CLI
├── api/ → FastAPI server
└── backup/ → backup otomatis ke Supabase tiap 60 menit
Topologi Jaringan:
NAT/Internet
│
[main-router] MikroTik CHR — ether3=192.168.99.1 (management)
│
[router-kantor] MikroTik CHR — ether4=192.168.99.3 (management)
│ │
[Switch-1] [openWRT] — eth2=192.168.99.4 (management)
PC Kantor x3 │
[Switch-3]
PC Tamu x3
[Switch-2] — Management Network (192.168.99.0/24)
│
[monitoring-server] Debian — enp0s3=192.168.99.2
| Library | Versi | Fungsi |
|---|---|---|
| Python | 3.11+ | Runtime |
| FastAPI | latest | REST API server |
| uvicorn | latest | ASGI server untuk FastAPI |
| SQLAlchemy | 2.x | ORM database |
| PyMySQL | latest | MySQL/MariaDB connector |
| easysnmp | latest | SNMP polling |
| icmplib | latest | Ping/ICMP monitoring |
| paramiko | latest | SSH untuk reboot device |
| schedule | latest | Job scheduler |
| supabase | latest | Backup ke Supabase |
| python-dotenv | latest | Manajemen environment variables |
- OS: Debian/Ubuntu Linux
- Python: 3.11 ke atas
- Database: MariaDB atau MySQL
- Akun: Supabase (untuk backup)
- Network: Akses ke management network perangkat (192.168.99.0/24)
monitoring/
├── .env # konfigurasi (tidak di-commit)
├── .env.example # template environment variables
├── .gitignore
├── main.py # entry point scheduler utama
├── menu.py # CLI menu + manajemen device
├── migrate.py # setup schema & seed device dari .env
├── test_api.py # test semua endpoint API via CLI
├── requirements.txt
├── api/
│ ├── __init__.py
│ ├── main_api.py # FastAPI endpoints
│ └── run_api.py # entry point API server
├── backup/
│ ├── __init__.py
│ └── supabase_backup.py # backup offset-based & cleanup
├── collectors/
│ ├── __init__.py
│ ├── bandwidth.py # traffic per interface via SNMP
│ ├── ping_monitor.py # status, latency, packet_loss
│ └── snmp_collector.py # SNMP metrics + macAddress + monitoringInterval
├── models/
│ ├── __init__.py
│ └── database.py # schema, koneksi DB, get_active_devices()
├── utils/
│ ├── __init__.py
│ └── logger.py # logging ke file & terminal
├── logs/ # tidak di-commit
└── exports/ # hasil export CSV, tidak di-commit
git clone https://github.com/username/monitoring.git
cd monitoringsudo apt update && sudo apt upgrade -y
sudo apt install -y python3-pip python3-venv python3-dev
sudo apt install -y mariadb-server mariadb-client
sudo apt install -y libsnmp-dev snmp snmp-mibs-downloader
sudo apt install -y gcc build-essentialsudo nano /etc/sysctl.d/no-apipa.confIsi dengan:
net.ipv4.conf.all.autoconf = 0
net.ipv4.conf.default.autoconf = 0
sudo sysctl --systemsudo mysql_secure_installation
sudo mysql -u root -pCREATE DATABASE monitoring_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'monitor_user'@'localhost' IDENTIFIED BY 'ganti_password_ini';
GRANT ALL PRIVILEGES ON monitoring_db.* TO 'monitor_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;cd ~/monitoring
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtsudo setcap cap_net_raw+ep ~/monitoring/venv/bin/python3
# Verifikasi
getcap ~/monitoring/venv/bin/python3
# Output: .../python3 cap_net_raw+epPenting: Ulangi
setcapjika Python di-upgrade atau venv di-recreate.
Masuk ke Supabase Dashboard → SQL Editor, jalankan:
-- Backup tables
CREATE TABLE device_status_backup (
id BIGSERIAL PRIMARY KEY, device VARCHAR(50) NOT NULL,
ip_address VARCHAR(20) NOT NULL, status VARCHAR(10) NOT NULL,
latency_ms FLOAT, checked_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE snmp_metrics_backup (
id BIGSERIAL PRIMARY KEY, device VARCHAR(50) NOT NULL,
ip_address VARCHAR(20) NOT NULL, metric_name VARCHAR(100) NOT NULL,
metric_value TEXT, collected_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE interface_traffic_backup (
id BIGSERIAL PRIMARY KEY, device VARCHAR(50) NOT NULL,
ip_address VARCHAR(20) NOT NULL, interface_name VARCHAR(50) NOT NULL,
bytes_in BIGINT DEFAULT 0, bytes_out BIGINT DEFAULT 0,
packets_in BIGINT DEFAULT 0, packets_out BIGINT DEFAULT 0,
collected_at TIMESTAMPTZ NOT NULL, created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Archive tables (data device yang dihapus)
CREATE TABLE deleted_device_status (
id BIGSERIAL PRIMARY KEY, device VARCHAR(50) NOT NULL,
ip_address VARCHAR(20) NOT NULL, status VARCHAR(10) NOT NULL,
latency_ms FLOAT, checked_at TIMESTAMPTZ NOT NULL,
deleted_at TIMESTAMPTZ DEFAULT NOW(), device_info JSONB
);
CREATE TABLE deleted_snmp_metrics (
id BIGSERIAL PRIMARY KEY, device VARCHAR(50) NOT NULL,
ip_address VARCHAR(20) NOT NULL, metric_name VARCHAR(100) NOT NULL,
metric_value TEXT, collected_at TIMESTAMPTZ NOT NULL,
deleted_at TIMESTAMPTZ DEFAULT NOW(), device_info JSONB
);
CREATE TABLE deleted_interface_traffic (
id BIGSERIAL PRIMARY KEY, device VARCHAR(50) NOT NULL,
ip_address VARCHAR(20) NOT NULL, interface_name VARCHAR(50) NOT NULL,
bytes_in BIGINT DEFAULT 0, bytes_out BIGINT DEFAULT 0,
packets_in BIGINT DEFAULT 0, packets_out BIGINT DEFAULT 0,
collected_at TIMESTAMPTZ NOT NULL,
deleted_at TIMESTAMPTZ DEFAULT NOW(), device_info JSONB
);
-- Indexes
CREATE INDEX idx_ds ON device_status_backup (device, checked_at DESC);
CREATE INDEX idx_sm ON snmp_metrics_backup (device, collected_at DESC);
CREATE INDEX idx_it ON interface_traffic_backup (device, collected_at DESC);
CREATE INDEX idx_dds ON deleted_device_status (device, checked_at DESC);
CREATE INDEX idx_dsm ON deleted_snmp_metrics (device, collected_at DESC);
CREATE INDEX idx_dit ON deleted_interface_traffic(device, collected_at DESC);cp .env.example .env
nano .env# Database
DB_HOST=localhost
DB_PORT=3306
DB_NAME=monitoring_db
DB_USER=monitor_user
DB_PASS=ganti_password_ini
# SNMP
SNMP_COMMUNITY=public
SNMP_PORT=161
# Polling Interval (detik)
PING_INTERVAL=30
SNMP_INTERVAL=60
BANDWIDTH_INTERVAL=60
# Supabase Backup
SUPABASE_URL=https://xxxxxxxxxxxxxxxx.supabase.co
SUPABASE_KEY=your_service_role_key
# FastAPI
API_HOST=0.0.0.0
API_PORT=8000Catatan: IP dan SSH credentials device tidak lagi disimpan di
.env. Semua data device dikelola dari database via menu CLI atau API.
Jalankan sekali setelah setup untuk membuat semua tabel dan seed device awal:
cd ~/monitoring
source venv/bin/activate
python3 migrate.pyOutput normal:
==================================================
DATABASE MIGRATION
==================================================
[1/3] Membuat tabel... [DB] Tabel berhasil dibuat/diverifikasi
[2/3] Verifikasi tabel devices... Tabel 'devices' sudah ada.
[3/3] Cek data device... Sudah ada 4 device, skip seed.
==================================================
MIGRASI SELESAI
==================================================
[1] main-router 192.168.99.1 mikrotik AKTIF
[2] router-kantor 192.168.99.3 mikrotik AKTIF
[3] openwrt 192.168.99.4 openwrt AKTIF
[4] router-test 192.168.99.5 mikrotik AKTIF
| Kolom | Tipe | Keterangan |
|---|---|---|
| id | INT AUTO_INCREMENT | Primary key |
| name | VARCHAR(50) UNIQUE | Nama unik device |
| ip_address | VARCHAR(20) | IP management network |
| type | VARCHAR(20) | mikrotik / openwrt / linux |
| ssh_user | VARCHAR(50) | Username SSH |
| ssh_pass | VARCHAR(100) | Password SSH |
| snmp_community | VARCHAR(20) | SNMP community string |
| is_active | INT | 1=aktif, 0=nonaktif |
| description | VARCHAR(100) | Deskripsi opsional |
| created_at | DATETIME | Waktu dibuat |
| updated_at | DATETIME | Waktu terakhir diubah |
Jalankan dari
~/monitoringdengan venv aktif. Masing-masing program di terminal terpisah.
# Terminal 1 — Monitoring scheduler
source venv/bin/activate && python3 main.py
# Terminal 2 — API server
source venv/bin/activate && python3 api/run_api.py
# Terminal 3 — Menu CLI (opsional)
source venv/bin/activate && python3 menu.py
# Terminal 4 — Test API (opsional)
source venv/bin/activate && python3 test_api.pySwagger UI: https://monitoring.domain.com/docs
Base URL: https://monitoring.domain.com
| Method | Endpoint | Deskripsi |
|---|---|---|
| GET | / |
Health check |
| GET | /status |
Status terbaru semua device |
| GET | /status/{device} |
Status terbaru satu device |
| POST | /ping |
Ping manual → simpan ke DB |
| POST | /reboot |
Reboot device via SSH |
| Method | Endpoint | Deskripsi |
|---|---|---|
| GET | /api/devices |
List semua device |
| POST | /api/devices |
Tambah device baru |
| PUT | /api/devices/{id} |
Update detail device |
| PATCH | /api/devices/{id}/toggle |
Toggle aktif/nonaktif |
| DELETE | /api/devices/{id} |
Hapus + archive ke Supabase |
| DELETE | /api/devices/cleanup/orphan |
Bersihkan data device orphan |
| Method | Endpoint | Deskripsi |
|---|---|---|
| POST | /api/backup/manual |
Trigger backup ke Supabase (background) |
POST /api/devices — Tambah device
curl -X POST https://monitoring.domain.com/api/devices \
-H "Content-Type: application/json" \
-d '{
"name": "router-baru",
"ip_address": "192.168.99.10",
"type": "mikrotik",
"ssh_user": "admin",
"ssh_pass": "",
"snmp_community": "public",
"description": "Router lantai 2"
}'{ "status": "ok", "message": "Device 'router-baru' berhasil ditambahkan", "id": 5 }DELETE /api/devices/{id} — Hapus + archive
curl -X DELETE https://monitoring.domain.com/api/devices/5{
"status": "ok",
"message": "Device 'router-baru' berhasil dihapus beserta semua datanya",
"archived": { "device_status": 48, "snmp_metrics": 288, "interface_traffic": 192 },
"deleted_from_local": { "device_status": 48, "snmp_metrics": 288, "interface_traffic": 192 }
}POST /api/backup/manual — Trigger backup
curl -X POST https://monitoring.domain.com/api/backup/manual{
"status": "accepted",
"message": "Backup akan dilakukan di background. Cek log untuk hasilnya.",
"timestamp": "2026-05-04T12:00:00"
}Endpoint backup return
acceptedlangsung — proses berjalan di background. Cek progress:tail -f ~/monitoring/logs/api.log | grep backup
$base = config('services.monitoring.url');
// Tambah device
Http::post("{$base}/api/devices", [
'name' => 'router-baru', 'ip_address' => '192.168.99.10',
'type' => 'mikrotik', 'ssh_user' => 'admin',
'ssh_pass' => '', 'snmp_community' => 'public',
])->json();
// Hapus device (archive otomatis ke Supabase)
Http::delete("{$base}/api/devices/5")->json();
// Trigger backup manual
Http::post("{$base}/api/backup/manual")->json();====================================================
NETWORK MONITORING — MENU UTAMA
====================================================
[1] Status perangkat (realtime)
[2] Lihat log lokal
[3] Lihat backup Supabase
[4] Backup manual ke Supabase
[5] Statistik database
[6] Export data ke CSV
[7] Manajemen device
[0] Keluar
====================================================
[1] Lihat semua device
[2] Tambah device baru
[3] Toggle aktif / nonaktif
[4] Hapus device permanen (archive ke Supabase)
Perubahan device langsung efektif pada siklus berikutnya — tidak perlu restart main.py.
Data dihapus berdasarkan jumlah row, bukan waktu. Mencegah data hilang saat device down lama.
| Tabel | Retain Lokal | Coverage |
|---|---|---|
| device_status | 1200 row | ~3 jam |
| snmp_metrics | 3500 row | ~3 jam |
| interface_traffic | 2500 row | ~3 jam |
Saat device dihapus, semua data monitoring otomatis di-archive ke tabel
deleted_device_status, deleted_snmp_metrics, deleted_interface_traffic
di Supabase sebelum dihapus dari database lokal.
Setiap record menyimpan device_info (JSON) sebagai referensi historis.
| Periode | Storage |
|---|---|
| Per hari | ~8 MB |
| Per bulan | ~240 MB |
| Free tier (500 MB) | ~2 bulan |
icmplib: permission denied
sudo setcap cap_net_raw+ep ~/monitoring/venv/bin/python3ModuleNotFoundError saat pakai sudo
sudo ~/monitoring/venv/bin/python3 main.pySNMP timeout ke openWRT
/etc/init.d/snmpd restart
snmpwalk -v2c -c public 192.168.99.4 1.3.6.1.2.1.1SSH Authentication Failed
ssh admin@192.168.99.1 # test koneksi manual
# Cek credentials di tabel devices via menu CLI [7][1]Device tidak termonitor setelah ditambah
Normal — device baru mulai dimonitor pada siklus berikutnya (maks 30-60 detik). Tidak perlu restart main.py.
Data orphan setelah hapus device lama
curl -X DELETE https://monitoring.domain.com/api/devices/cleanup/orphanBackup tidak merespons
tail -f ~/monitoring/logs/api.log | grep backup- OpenWRT 25.12 menggunakan
apk(bukanopkg) dannftables(bukaniptables) setcapperlu diulang jika Python di-upgrade atau venv di-recreatemenu.pydanapi/run_api.pydijalankan di terminal terpisah darimain.py
This project is licensed under the MIT License. See the LICENSE file for details.