mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-10-31 20:15:13 +08:00
💋 Sync 2026-10-04 04:16:09
This commit is contained in:
1 parent
f6b2df73ec
commit
f62992fd80
745 files changed
+212416
-1173
No files matched your search
@@ -0,0 +1,45 @@
|
||||
#!/bin/sh
|
||||
# Master switch for the quickactions failover system.
|
||||
# quickactions-failover on|off|status|test
|
||||
|
||||
LOCK=/tmp/quickactions_wan_down.lock
|
||||
FAILS=/tmp/quickactions_wan_fails.count
|
||||
OKS=/tmp/quickactions_wan_oks.count
|
||||
HP=/etc/hotplug.d/iface/99-quickactions-conntrack-flush
|
||||
CRON_LINE="* * * * * /usr/bin/wan-watchdog.sh"
|
||||
|
||||
case "$1" in
|
||||
on)
|
||||
uci -q set quickactions.failover.enabled=1
|
||||
uci -q commit quickactions
|
||||
[ -f "$HP" ] && chmod 755 "$HP"
|
||||
(crontab -l 2>/dev/null | grep -v "wan-watchdog.sh"; echo "$CRON_LINE") | crontab -
|
||||
/etc/init.d/cron restart
|
||||
echo "ENABLED"
|
||||
logger -t quickactions-failover "master switch: enabled"
|
||||
;;
|
||||
off)
|
||||
uci -q set quickactions.failover.enabled=0
|
||||
uci -q commit quickactions
|
||||
[ -f "$HP" ] && chmod 644 "$HP"
|
||||
crontab -l 2>/dev/null | grep -v "wan-watchdog.sh" | crontab -
|
||||
rm -f "$LOCK" "$FAILS" "$OKS"
|
||||
/etc/init.d/cron restart
|
||||
echo "DISABLED"
|
||||
logger -t quickactions-failover "master switch: disabled"
|
||||
;;
|
||||
status)
|
||||
en=$(uci -q get quickactions.failover.enabled)
|
||||
[ -z "$en" ] && en=1
|
||||
[ "$en" = "1" ] && echo "STATUS: [ON]" || echo "STATUS: [OFF]"
|
||||
[ -f "$LOCK" ] && echo "MODE: [BACKUP]" || echo "MODE: [PRIMARY]"
|
||||
;;
|
||||
test)
|
||||
/usr/bin/wan-watchdog.sh
|
||||
echo "manual run complete"
|
||||
;;
|
||||
*)
|
||||
echo "Usage: quickactions-failover [on|off|status|test]"
|
||||
;;
|
||||
esac
|
||||
exit 0
|
||||
@@ -0,0 +1,126 @@
|
||||
#!/bin/sh
|
||||
# Merged from luci-app-taskplan — quickactions edition
|
||||
|
||||
# Use bash if available, else fall back to busybox sh
|
||||
if command -v bash >/dev/null 2>&1; then
|
||||
SHELL_BIN=/bin/bash
|
||||
else
|
||||
SHELL_BIN=/bin/sh
|
||||
fi
|
||||
|
||||
# Author: sirpdboy (original) / quickactions merge
|
||||
|
||||
TMP_T=/var/quickactions-taskplan/taskplan.tmp
|
||||
LOG=/etc/quickactions-taskplan/taskplan.log
|
||||
CUSTOM_DIR=/etc/quickactions-taskplan
|
||||
|
||||
log(){
|
||||
echo "$(date +'%Y-%m-%d %H:%M:%S') $*" >> $LOG
|
||||
}
|
||||
|
||||
limit_log() {
|
||||
local logf=$1
|
||||
[ ! -f "$logf" ] && return
|
||||
local sc=100
|
||||
[ -n "$2" ] && sc=$2
|
||||
local count=$(grep -c "" $logf 2>/dev/null || echo 0)
|
||||
if [ $count -gt $sc ]; then
|
||||
let count=count-$sc
|
||||
sed -i "1,$count d" $logf 2>/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
remwan3() {
|
||||
while true; do
|
||||
status=$(/usr/sbin/mwan3 status 2>/dev/null | grep -i error)
|
||||
if [ -z "$status" ]; then
|
||||
log "Mwan3 is OK"
|
||||
exit 0
|
||||
else
|
||||
log "mwan3 restarting ..."
|
||||
/usr/sbin/mwan3 restart
|
||||
fi
|
||||
sleep 60
|
||||
done
|
||||
}
|
||||
|
||||
disreconnwan() {
|
||||
i=0
|
||||
PingA=114.114.114.114
|
||||
PingB=223.5.5.5
|
||||
PingErr=1
|
||||
|
||||
while [ $i -lt 5 ]; do
|
||||
if ping -c 1 -W 3 $PingA >/dev/null 2>&1; then
|
||||
PingErr=0
|
||||
break
|
||||
elif ping -c 1 -W 3 $PingB >/dev/null 2>&1; then
|
||||
PingErr=0
|
||||
break
|
||||
else
|
||||
i=$((i + 1))
|
||||
sleep 10
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
trigger() {
|
||||
case "$1" in
|
||||
reboot) reboot ;;
|
||||
poweroff) poweroff ;;
|
||||
network) /etc/init.d/network restart ;;
|
||||
clearmem) sync && echo 3 > /proc/sys/vm/drop_caches ;;
|
||||
sysfree)
|
||||
cd /overlay
|
||||
rm -rf $(ls | egrep -v '(upper|.fs_state)') 2>/dev/null
|
||||
cd upper 2>/dev/null
|
||||
rm -rf $(ls | egrep -v '(etc)') 2>/dev/null
|
||||
cd etc 2>/dev/null
|
||||
rm -rf $(ls | egrep -v '(config|ssr|bench.log|shadow)') 2>/dev/null
|
||||
rm -rf /var/luci-modulecache 2>/dev/null
|
||||
rm -rf /var/luci-indexcache 2>/dev/null
|
||||
rm -rf /tmp/cache/* 2>/dev/null
|
||||
reboot ;;
|
||||
restartsamba)
|
||||
/etc/init.d/ksmdb restart 2>/dev/null
|
||||
/etc/init.d/samba restart 2>/dev/null
|
||||
/etc/init.d/samba4 restart 2>/dev/null
|
||||
log "Samba restarted" ;;
|
||||
restartlan) ifdown lan && ifup lan; log "restart lan succeeded!" ;;
|
||||
restartwan) ifup wan; log "restart wan succeeded!" ;;
|
||||
closewan) ifdown wan ;;
|
||||
disreconn)
|
||||
disreconnwan
|
||||
if [ $PingErr = 1 ]; then ifup wan; log "Redial succeeded!"; fi ;;
|
||||
disrereboot)
|
||||
disreconnwan
|
||||
if [ $PingErr = 1 ]; then log "Reboot succeeded!"; reboot; fi ;;
|
||||
restartmwan3) [ -x /usr/sbin/mwan3 ] && remwan3 ;;
|
||||
customscript)
|
||||
if [ -s $CUSTOM_DIR/taskplancustomscript ]; then
|
||||
$SHELL_BIN $CUSTOM_DIR/taskplancustomscript
|
||||
log "CustomScript succeeded!"
|
||||
else log "CustomScript run Error!"; fi ;;
|
||||
customscript2)
|
||||
if [ -s $CUSTOM_DIR/taskplancustomscript2 ]; then
|
||||
$SHELL_BIN $CUSTOM_DIR/taskplancustomscript2
|
||||
log "CustomScript2 succeeded!"
|
||||
else log "CustomScript2 run Error!"; fi ;;
|
||||
upwifi)
|
||||
ifconfig wlan0 up 2>/dev/null
|
||||
uci set wireless.radio0.disabled=0 2>/dev/null
|
||||
uci set wireless.radio1.disabled=0 2>/dev/null
|
||||
uci commit wireless 2>/dev/null
|
||||
wifi up ;;
|
||||
downwifi)
|
||||
ifconfig wlan0 down 2>/dev/null
|
||||
uci set wireless.radio0.disabled=1 2>/dev/null
|
||||
uci set wireless.radio1.disabled=1 2>/dev/null
|
||||
uci commit wireless 2>/dev/null
|
||||
wifi down ;;
|
||||
esac
|
||||
}
|
||||
|
||||
limit_log $LOG 500
|
||||
PingErr=0
|
||||
log "Scheduled operation $2: $1" && trigger $1
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
# Quick Actions — toggle all WiFi radios on/off
|
||||
if wifi status 2>/dev/null | grep -qE '"up":[[:space:]]*true'; then
|
||||
wifi down
|
||||
logger -t quickactions "WiFi turned OFF"
|
||||
echo "WiFi turned OFF"
|
||||
else
|
||||
wifi up
|
||||
logger -t quickactions "WiFi turned ON"
|
||||
echo "WiFi turned ON"
|
||||
fi
|
||||
exit 0
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
#!/bin/sh
|
||||
# quickactions WAN failover watchdog.
|
||||
# Runs from cron every N seconds. Reads config from
|
||||
# /etc/config/quickactions section 'failover'. Zero background RAM when idle.
|
||||
|
||||
. /lib/functions.sh
|
||||
|
||||
config_load quickactions
|
||||
|
||||
cfg() {
|
||||
local v
|
||||
config_get v failover "$1" "$2"
|
||||
echo "$v"
|
||||
}
|
||||
|
||||
ENABLED=$(cfg enabled 1)
|
||||
[ "$ENABLED" = "0" ] && exit 0
|
||||
|
||||
PRIMARY=$(cfg primary "")
|
||||
[ -z "$PRIMARY" ] && exit 0
|
||||
|
||||
CHECK_IPS=$(cfg check_ips "1.1.1.1 8.8.8.8")
|
||||
PING_TIMEOUT=$(cfg ping_timeout 5)
|
||||
FAIL_AT=$(cfg fail_threshold 2)
|
||||
REC_AT=$(cfg recover_threshold 1)
|
||||
TAILSCALE_RESTART=$(cfg tailscale_restart 0)
|
||||
FIREWALL_RESET=$(cfg firewall_reset 1)
|
||||
LOG_HEALTHY=$(cfg log_healthy 0)
|
||||
|
||||
LOCK=/tmp/quickactions_wan_down.lock
|
||||
FAILS=/tmp/quickactions_wan_fails.count
|
||||
OKS=/tmp/quickactions_wan_oks.count
|
||||
|
||||
# Logical interface name -> kernel device name
|
||||
phys_iface() {
|
||||
local log="$1" dev
|
||||
# pppoe-* style kernel interface
|
||||
if [ -e "/sys/class/net/pppoe-$log" ]; then echo "pppoe-$log"; return; fi
|
||||
# interface with pppoe in the name
|
||||
case "$log" in
|
||||
*pppoe*) [ -e "/sys/class/net/$log" ] && { echo "$log"; return; } ;;
|
||||
esac
|
||||
# read device from UCI network
|
||||
dev=$(uci -q get network.$log.device)
|
||||
[ -n "$dev" ] && [ -e "/sys/class/net/$dev" ] && { echo "$dev"; return; }
|
||||
# fallback
|
||||
echo "$log"
|
||||
}
|
||||
|
||||
ping_ok() {
|
||||
local iface="$1" ip
|
||||
for ip in $CHECK_IPS; do
|
||||
ping -I "$iface" -c 1 -W "$PING_TIMEOUT" "$ip" >/dev/null 2>&1 && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
flush_ct() {
|
||||
if [ -e /proc/net/nf_conntrack ]; then
|
||||
echo f > /proc/net/nf_conntrack
|
||||
elif [ -e /proc/net/ip_conntrack ]; then
|
||||
echo f > /proc/net/ip_conntrack
|
||||
fi
|
||||
}
|
||||
|
||||
PRIMARY_DEV=$(phys_iface "$PRIMARY")
|
||||
|
||||
# Recovery mode: we believe primary is down; check if it's back
|
||||
if [ -f "$LOCK" ]; then
|
||||
OKS=0
|
||||
[ -f "$OKS" ] && OKS=$(cat "$OKS" 2>/dev/null)
|
||||
if ping_ok "$PRIMARY_DEV"; then
|
||||
OKS=$((OKS + 1))
|
||||
if [ "$OKS" -ge "$REC_AT" ]; then
|
||||
rm -f "$LOCK" "$FAILS" "$OKS"
|
||||
flush_ct
|
||||
logger -t quickactions-failover "PRIMARY $PRIMARY recovered; conntrack flushed"
|
||||
[ "$TAILSCALE_RESTART" = "1" ] && /etc/init.d/tailscale restart 2>/dev/null
|
||||
else
|
||||
echo "$OKS" > "$OKS"
|
||||
fi
|
||||
else
|
||||
echo 0 > "$OKS"
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Normal mode: is primary healthy?
|
||||
if ping_ok "$PRIMARY_DEV"; then
|
||||
rm -f "$FAILS"
|
||||
[ "$LOG_HEALTHY" = "1" ] && logger -t quickactions-failover "PRIMARY $PRIMARY HEALTHY"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Primary failed once
|
||||
FAILS=0
|
||||
[ -f "$FAILS" ] && FAILS=$(cat "$FAILS" 2>/dev/null)
|
||||
FAILS=$((FAILS + 1))
|
||||
echo "$FAILS" > "$FAILS"
|
||||
|
||||
if [ "$FAILS" -ge "$FAIL_AT" ]; then
|
||||
touch "$LOCK"
|
||||
rm -f "$FAILS"
|
||||
flush_ct
|
||||
logger -t quickactions-failover "PRIMARY $PRIMARY DOWN after $FAIL_AT failures; failover engaged"
|
||||
if [ "$FIREWALL_RESET" = "1" ]; then
|
||||
/etc/init.d/firewall restart 2>/dev/null
|
||||
fi
|
||||
if [ "$TAILSCALE_RESTART" = "1" ]; then
|
||||
sleep 30
|
||||
/etc/init.d/tailscale restart 2>/dev/null
|
||||
fi
|
||||
fi
|
||||
|
||||
exit 0
|
||||
Reference in new issue
Block a user