From 1ef7c438dd73923a2f476caa6829e0970a894d6c Mon Sep 17 00:00:00 2001 From: wwartana Date: Sat, 7 Feb 2026 20:42:15 +0800 Subject: [PATCH] Backup OpenWrt NUT Configuration & Scripts --- Langkah_Kerja.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++ notify_mikrotik.sh | 27 ++++++++++++++++++ nut-monitor | 21 ++++++++++++++ nut-server | 23 +++++++++++++++ nut.conf | 1 + ups.conf | 6 ++++ upsd.conf | 1 + upsd.users | 5 ++++ upsmon.conf | 6 ++++ 9 files changed, 160 insertions(+) create mode 100644 Langkah_Kerja.md create mode 100644 notify_mikrotik.sh create mode 100644 nut-monitor create mode 100644 nut-server create mode 100644 nut.conf create mode 100644 ups.conf create mode 100644 upsd.conf create mode 100644 upsd.users create mode 100644 upsmon.conf diff --git a/Langkah_Kerja.md b/Langkah_Kerja.md new file mode 100644 index 0000000..6df9ec2 --- /dev/null +++ b/Langkah_Kerja.md @@ -0,0 +1,70 @@ +# Panduan Instalasi OpenWrt NUT & Notifikasi MikroTik + +Gunakan panduan ini untuk mengkonfigurasi router OpenWrt baru agar sama persis dengan setingan saat ini. + +## 1. Persiapan Paket +Pastikan router terhubung internet, lalu install paket yang dibutuhkan via Terminal (SSH): +```bash +opkg update +opkg install nut usbutils curl ca-bundle ca-certificates +``` +*Catatan: Paket `nut` biasanya sudah mencakup driver dan tools dasar.* + +## 2. Restore File Konfigurasi +Upload file-file berikut dari folder backup ini ke router OpenWrt (gunakan SCP atau WinSCP): + +1. **Folder `/etc/nut/`**: + - `ups.conf` (Setingan Driver UPS) + - `upsd.conf` (Setingan Server Listen Port) + - `upsd.users` (User Monitor) + - `upsmon.conf` (Setingan Monitor & Notifikasi) + - `nut.conf` (Mode Netserver) + - `notify_mikrotik.sh` (Script Notifikasi Utama) + +2. **Folder `/etc/init.d/`**: + - `nut-server` (Script Startup Server) + - `nut-monitor` (Script Startup Monitor) + +## 3. Set Permission (Hak Akses) +Setelah file di-upload, jalankan perintah ini di terminal OpenWrt agar script bisa dijalankan: +```bash +chmod +x /etc/nut/notify_mikrotik.sh +chmod +x /etc/init.d/nut-server +chmod +x /etc/init.d/nut-monitor +``` + +## 4. Aktifkan Service +Nyalakan service agar jalan otomatis saat router nyala: +```bash +/etc/init.d/nut-server enable +/etc/init.d/nut-monitor enable +/etc/init.d/nut-server start +/etc/init.d/nut-monitor start +``` + +## 5. Konfigurasi Kunci SSH (PENTING!) +Agar OpenWrt bisa kirim perintah ke MikroTik tanpa password, Anda harus membuat kunci rahasia (SSH Key) baru di OpenWrt. + +1. **Buat Key di OpenWrt**: + ```bash + ssh-keygen + ``` + *(Tekan Enter terus sampai selesai, biarkan default)* + +2. **Lihat Public Key**: + ```bash + cat /root/.ssh/id_rsa.pub + ``` + *(Copy tulisan yang muncul, biasanya diawali `ssh-rsa ...`)* + +3. **Import ke MikroTik**: + - Login ke Winbox MikroTik (192.168.7.1 & 103.125.50.7). + - Masuk ke menu **System -> Users -> SSH Keys**. + - Import key tersebut untuk user `upsuser`. + +## 6. Test Sistem +Coba test apakah notifikasi berjalan: +```bash +/etc/nut/notify_mikrotik.sh "TEST_INSTALL" +``` +Jika sukses, log akan muncul di MikroTik dan pesan masuk ke Telegram. diff --git a/notify_mikrotik.sh b/notify_mikrotik.sh new file mode 100644 index 0000000..38dde52 --- /dev/null +++ b/notify_mikrotik.sh @@ -0,0 +1,27 @@ +#!/bin/sh +# $1 = event type (ONLINE, ONBATT, LOWBATT, FSD, etc.) + +# Configuration +MIKROTIK_TARGETS="192.168.7.1 103.125.50.7" +TELEGRAM_BOT_TOKEN="6770207225:AAG3yNPepyhhGXqNh8BYjLYUMctc1DSfrH4" +TELEGRAM_CHAT_ID="6796478523" + +# Message for Telegram +DATE=$(date "+%Y-%m-%d %H:%M:%S") +MESSAGE="UPS Notification: $1 at $DATE" + +# 1. Send to Telegram using CURL (Secure) +curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" -d chat_id="$TELEGRAM_CHAT_ID" -d text="$MESSAGE" > /dev/null 2>&1 & + +# 2. Send to MikroTik Routers +for IP in $MIKROTIK_TARGETS; do + # Log event + ssh -i /root/.ssh/id_rsa -y upsuser@$IP "/log warning \"UPS Notification: $1\"" & + + # Shutdown on Critical + if [ "$1" = "LOWBATT" ] || [ "$1" = "FSD" ]; then + ssh -i /root/.ssh/id_rsa -y upsuser@$IP "/system shutdown" & + fi +done + +wait diff --git a/nut-monitor b/nut-monitor new file mode 100644 index 0000000..f06e565 --- /dev/null +++ b/nut-monitor @@ -0,0 +1,21 @@ +#!/bin/sh /etc/rc.common + +START=60 +USE_PROCD=1 + +restart() { + stop_service + start_service +} + +start_service() { + upsmon -p +} + +stop_service() { + upsmon -c stop +} + +reload_service() { + upsmon -c reload +} diff --git a/nut-server b/nut-server new file mode 100644 index 0000000..ce1bdb0 --- /dev/null +++ b/nut-server @@ -0,0 +1,23 @@ +#!/bin/sh /etc/rc.common + +START=50 +USE_PROCD=1 + +restart() { + stop_service + start_service +} + +start_service() { + upsdrvctl start + upsd +} + +stop_service() { + upsd -c stop + upsdrvctl stop +} + +reload_service() { + upsd -c reload +} diff --git a/nut.conf b/nut.conf new file mode 100644 index 0000000..e141f91 --- /dev/null +++ b/nut.conf @@ -0,0 +1 @@ +MODE=netserver diff --git a/ups.conf b/ups.conf new file mode 100644 index 0000000..e307c45 --- /dev/null +++ b/ups.conf @@ -0,0 +1,6 @@ +[myups] + driver = blazer_usb + port = auto + vendorid = 0665 + productid = 5161 + desc = "UPS 0665:5161" diff --git a/upsd.conf b/upsd.conf new file mode 100644 index 0000000..61c76fc --- /dev/null +++ b/upsd.conf @@ -0,0 +1 @@ +LISTEN 0.0.0.0 3493 diff --git a/upsd.users b/upsd.users new file mode 100644 index 0000000..a0e8790 --- /dev/null +++ b/upsd.users @@ -0,0 +1,5 @@ +[monuser] + password = secret + upsmon master + actions = SET + instcmds = ALL diff --git a/upsmon.conf b/upsmon.conf new file mode 100644 index 0000000..d665c71 --- /dev/null +++ b/upsmon.conf @@ -0,0 +1,6 @@ +MONITOR myups@localhost 1 monuser secret master + +NOTIFYCMD /etc/nut/notify_mikrotik.sh +NOTIFYFLAG ONLINE SYSLOG+EXEC +NOTIFYFLAG ONBATT SYSLOG+EXEC +NOTIFYFLAG LOWBATT SYSLOG+EXEC