luci-app-serverchan: sync with upstream source

This commit is contained in:
CN_SZTL 2020-01-03 13:19:14 +08:00
parent 061bf20c61
commit bbd0c763c9
No known key found for this signature in database
GPG Key ID: 6850B6345C862176

View File

@ -2,56 +2,60 @@
# 读取设置文件
function get_config(){
serverchan_enable=`uci get serverchan.serverchan.serverchan_enable`>/dev/null 2>&1
serverchan_sckey=`uci get serverchan.serverchan.sckey`>/dev/null 2>&1
serverchan_ip=`uci get serverchan.serverchan.serverchan_ip`>/dev/null 2>&1
serverchan_up=`uci get serverchan.serverchan.serverchan_up`>/dev/null 2>&1
serverchan_down=`uci get serverchan.serverchan.serverchan_down`>/dev/null 2>&1
serverchan_sheep=`uci get serverchan.serverchan.serverchan_sheep`>/dev/null 2>&1
if [ -z "$serverchan_sheep" ] ; then serverchan_sheep="0";fi
serverchan_whitelist=`uci get serverchan.serverchan.serverchan_whitelist` >/dev/null 2>&1
serverchan_blacklist=`uci get serverchan.serverchan.serverchan_blacklist` >/dev/null 2>&1
serverchan_interface=`uci get serverchan.serverchan.serverchan_interface` >/dev/null 2>&1
device_aliases=`uci get serverchan.serverchan.device_aliases` >/dev/null 2>&1
device_aliases=`echo $device_aliases | sed 's/ /\n/g' | sed 's/-/ /g'`
starttime=`uci get serverchan.serverchan.starttime` >/dev/null 2>&1
endtime=`uci get serverchan.serverchan.endtime` >/dev/null 2>&1
#sleeptime=`uci get serverchan.serverchan.serverchan_sleeptime` >/dev/null 2>&1
#if [ -z "$sleeptime" ] ; then sleeptime="60";fi
sleeptime=60
cpuload_enable=`uci get serverchan.serverchan.cpuload_enable` >/dev/null 2>&1
cpuload=`uci get serverchan.serverchan.cpuload` >/dev/null 2>&1
temperature_enable=`uci get serverchan.serverchan.temperature_enable` >/dev/null 2>&1
temperature=`uci get serverchan.serverchan.temperature` >/dev/null 2>&1
dir="/tmp/serverchan/"
debuglevel=`uci get serverchan.serverchan.debuglevel` >/dev/null 2>&1
#debuglevel="5"
if [ -z "$debuglevel" ] ; then logfile="/dev/null";else logfile="/tmp/serverchan/server_chan.log";fi
if [ -z "$serverchan_enable" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取程序开关 " >> ${logfile};fi
#if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then echo "`date "+%H:%M:%S"` 【!!!】程序未启用,即将关闭 " >> ${logfile};fi
if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then exit;fi
if [ -z "$serverchan_sckey" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取 SCKEY" >> ${logfile};fi
if [ -z "$serverchan_sckey" ] ; then exit;fi
if [ -z "$serverchan_sckey" ] && [ "${#serverchan_sckey}" -lt "50" ] ; then echo "`date "+%H:%M:%S"` 【!!!】请填入正确的 SCKEY " >> ${logfile};fi
if [ -z "$serverchan_ip" ] || [ "$serverchan_ip" -ne "0" ] && [ "$serverchan_ip" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取 ip 变化通知 开关" >> ${logfile};fi
if [ -z "$serverchan_up" ] || [ "$serverchan_up" -ne "0" ] && [ "$serverchan_up" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取 设备上线提醒 开关" >> ${logfile};fi
if [ -z "$serverchan_down" ] || [ "$serverchan_down" -ne "0" ] && [ "$serverchan_down" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取 设备下线提醒 开关" >> ${logfile};fi
if [ "$1" ] ;then
serverchan_config=`uci show serverchan`
if ( echo $serverchan_config | grep -q -i $1 ); then echo `uci get serverchan.serverchan.$1`;else echo "";fi
fi
}
function read_config(){
serverchan_enable=`get_config serverchan_enable`
serverchan_sckey=`get_config sckey`
serverchan_ip=`get_config serverchan_ip`
serverchan_up=`get_config serverchan_up`
serverchan_down=`get_config serverchan_down`
serverchan_sheep=`get_config serverchan_sheep`
if [ -z "$serverchan_sheep" ] ; then serverchan_sheep="0";fi
serverchan_whitelist=`get_config serverchan_whitelist`
serverchan_blacklist=`get_config serverchan_blacklist`
serverchan_interface=`get_config serverchan_interface`
device_aliases=`get_config device_aliases`
if [ -z "$device_aliases" ] ; then device_aliases=`echo $device_aliases | sed 's/ /\n/g' | sed 's/-/ /g'`;fi
starttime=`get_config starttime`
endtime=`get_config endtime`
sleeptime=60
cpuload_enable=`get_config cpuload_enable`
cpuload=`get_config cpuload`
temperature_enable=`get_config temperature_enable`
temperature=`get_config temperature`
dir="/tmp/serverchan/"
debuglevel=`get_config debuglevel`
if [ -z "$debuglevel" ] ; then logfile="/dev/null";else logfile="/tmp/serverchan/server_chan.log";fi
if [ -z "$serverchan_enable" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取程序开关 " >> ${logfile};fi
if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then exit;fi
if [ -z "$serverchan_sckey" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取 SCKEY" >> ${logfile};fi
if [ -z "$serverchan_sckey" ] ; then exit;fi
if [ -z "$serverchan_sckey" ] && [ "${#serverchan_sckey}" -lt "50" ] ; then echo "`date "+%H:%M:%S"` 【!!!】请填入正确的 SCKEY " >> ${logfile};fi
if [ -z "$serverchan_ip" ] || [ "$serverchan_ip" -ne "0" ] && [ "$serverchan_ip" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取 ip 变化通知 开关" >> ${logfile};fi
if [ -z "$serverchan_up" ] || [ "$serverchan_up" -ne "0" ] && [ "$serverchan_up" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取 设备上线提醒 开关" >> ${logfile};fi
if [ -z "$serverchan_down" ] || [ "$serverchan_down" -ne "0" ] && [ "$serverchan_down" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【!!!】无法读取 设备下线提醒 开关" >> ${logfile};fi
}
#等待 5s确保 luci 设置已保存
sleep 5
get_config
read_config
# 检测程序开关
function enable_detection(){
if [ "$1" ] ;then
for i in `seq 1 $1`
do
serverchan_enable=`uci get serverchan.serverchan.serverchan_enable`>/dev/null 2>&1
serverchan_enable=`get_config serverchan_enable`
if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then kill -9 `pgrep -f serverchan`;fi
sleep 1
done
else
serverchan_enable=`uci get serverchan.serverchan.serverchan_enable`>/dev/null 2>&1
serverchan_enable=`get_config serverchan_enable`
if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then kill -9 `pgrep -f serverchan`;fi
fi
}
@ -92,10 +96,10 @@ fi
#创建计划任务
function serverchan_cron(){
send_mode=`uci get serverchan.serverchan.send_mode`>/dev/null 2>&1
send_mode=`get_config send_mode`
if [ ! -z "$send_mode" ] ; then
if [ "$send_mode" -eq "1" ] ; then
regular_time=`uci get serverchan.serverchan.regular_time`>/dev/null 2>&1
regular_time=`get_config regular_time`
if ( echo `crontab -l` | grep "serverchan" | grep -q " $regular_time " ); then
echo "" >/dev/null
elif ( echo `crontab -l` | grep -q "serverchan" ); then
@ -109,7 +113,7 @@ if [ ! -z "$send_mode" ] ; then
/etc/init.d/cron start
fi
elif [ "$send_mode" -eq "2" ] ; then
interval_time=`uci get serverchan.serverchan.interval_time`>/dev/null 2>&1
interval_time=`get_config interval_time`
if ( echo `crontab -l` | grep "serverchan" | grep -q "*/$interval_time " ); then
echo "" >/dev/null
elif ( echo `crontab -l` | grep -q "serverchan" ); then
@ -146,7 +150,7 @@ if [ ! -z "$serverchan_sheep" ] && [ "$serverchan_sheep" -eq "1" ] && [ ! -z "$s
echo "`date "+%H:%M:%S"` 【免打扰】夜深了,该休息了" >> ${logfile}
sheep_str=1
fi
get_config
read_config
if [ `date +%H` -ge $endtime -a $starttime -lt $endtime ] || [ `date +%H` -lt $starttime -a $starttime -lt $endtime ] || [ `date +%H` -lt $starttime -a `date +%H` -ge $endtime -a $starttime -gt $endtime ] ; then
enable=1
else
@ -194,7 +198,7 @@ fi
function up(){
if [ ! -f "${dir}ipAddress" ] ; then > ${dir}ipAddress;fi
if [ "$1" ] ;then
send_enable=`uci get serverchan.serverchan.send_enable`>/dev/null 2>&1
send_enable=`get_config send_enable`
local temp_send_enable="$send_enable"
grep -w -q $1 ${dir}ipAddress
if [ $? -ne 0 ] ; then
@ -222,7 +226,7 @@ if [ "$1" ] ;then
fi
fi
send_enable=`uci get serverchan.serverchan.send_enable`>/dev/null 2>&1
send_enable=`get_config send_enable`
if [ "$temp_send_enable" -ne "$send_enable" ] ; then return;fi
if ( echo ${ip_ms} | grep -q ms ); then
@ -301,7 +305,7 @@ fi
function down(){
if [ ! -f "${dir}ipAddress" ] ; then return;fi
if [ "$1" ] ;then
send_enable=`uci get serverchan.serverchan.send_enable`>/dev/null 2>&1
send_enable=`get_config send_enable`
local temp_send_enable="$send_enable"
ip_mac=$( cat ${dir}ipAddress | grep -w $1 |awk '{print $2}' | grep -v "^$" | sort -u )
if [ -z "$ip_mac" ] ; then ip_mac=$( cat /proc/net/arp | grep "0x2" | grep -w $1 | awk '{print $4}' | grep -v "^$" | sort -u );fi
@ -331,7 +335,7 @@ if [ "$1" ] ;then
fi
fi
send_enable=`uci get serverchan.serverchan.send_enable`>/dev/null 2>&1
send_enable=`get_config send_enable`
if [ "$temp_send_enable" -ne "$send_enable" ] ; then return;fi
if ( echo ${ip_ms} | grep -q ms ); then
@ -382,17 +386,17 @@ function send(){
unset content
send_content=""
serverchan_cron
send_title=`uci get serverchan.serverchan.send_title` >/dev/null 2>&1
send_title=`get_config send_title`
if [ -z "$send_title" ] ; then send_title="OpenWrt By tty228 路由状态:";fi
router_status=`uci get serverchan.serverchan.router_status` >/dev/null 2>&1
router_status=`get_config router_status`
if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ] ; then send_content=${send_content}"%0D%0A%0D%0A---%0D%0A%0D%0A#### 系统运行状态%0D%0A%0D%0A";fi
#if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ] ; then send_content=${send_content}"%0D%0A%0D%0A"" CPU占用`top -n 1 |grep -i Cpu:| awk '{print $3":"$2" "$5":"$4}'`";fi
if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ] ; then send_content=${send_content}"%0D%0A%0D%0A"" 平均负载:`cat /proc/loadavg | awk '{print $1" "$2" "$3}'`";fi
if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ] ; then send_content=${send_content}"%0D%0A%0D%0A"" 内存占用:`free -m | sed -n '2p' | awk '{print""($3/$2)*100"%"}'`";fi
if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ] ; then send_content=${send_content}"%0D%0A%0D%0A""`cat /proc/uptime| awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf(" 运行时间:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'`";fi
router_temp=`uci get serverchan.serverchan.router_temp` >/dev/null 2>&1
router_temp=`get_config router_temp`
if [ ! -z "$router_temp" ] && [ "$router_temp" -eq "1" ] ; then send_content=${send_content}"%0D%0A%0D%0A""---%0D%0A%0D%0A#### 设备温度%0D%0A%0D%0A CPU`cut -c1-2 /sys/class/thermal/thermal_zone0/temp`%e2%84%83";fi
router_wan=`uci get serverchan.serverchan.router_wan` >/dev/null 2>&1
router_wan=`get_config router_wan`
send_wanIP=$(ubus call network.interface.wan status | grep '\"address\"' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
send_hostIP=$(curl -k -s "http://members.3322.org/dyndns/getip")
if [ ! -z "$router_wan" ] && [ "$router_wan" -eq "1" ] ; then
@ -405,7 +409,7 @@ function send(){
fi
send_content=${send_content}"%0D%0A%0D%0A""`ubus call network.interface.wan status | grep \"uptime\" | sed $'s/\"uptime": //g'| sed $'s/\,//g'| awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf(" 在线时间:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'`"
fi
client_list=`uci get serverchan.serverchan.client_list` >/dev/null 2>&1
client_list=`get_config client_list`
if [ ! -z "$client_list" ] && [ "$client_list" -eq "1" ] ; then
if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 检测客户端列表" >> ${logfile};fi
@ -538,7 +542,7 @@ serverchan_first
while [ "$serverchan_enable" -eq "1" ] ;
do
if [ -f ${logfile} ] ; then clean_log;fi
get_config
read_config
function rand_geturl(){
urllist="https://www.163.com https://www.qq.com https://www.baidu.com https://www.qidian.com https://www.douban.com"
@ -607,12 +611,12 @@ function rand_geturl(){
if [ ! -z "$serverchan_down" ] && [ "$serverchan_down" -eq "1" ] ; then
if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【离线】开始进行设备下线检测" >> ${logfile};fi
fi
get_config
read_config
IPLIST=$( cat ${dir}ipAddress | awk '{print $1}' | grep -v "^$" | sort -u )
#从 ipAddress 文件中读取上次检测到的在线列表
for ip in $IPLIST; do
# 如果 ip 列表不为空
send_enable=`uci get serverchan.serverchan.send_enable`>/dev/null 2>&1
send_enable=`get_config send_enable`
if [ ! -z "$ip" ] && [ "$send_enable" -ne 3 ] ; then
ip_mac=$( cat /var/dhcp.leases | grep -w $ip |awk '{print $2}' | grep -v "^$" | sort -u )
if [ -z "$ip_mac" ] ; then ip_mac=$( cat /proc/net/arp | grep "0x2" | grep -w $ip | awk '{print $4}' | grep -v "^$" | sort -u );fi
@ -669,13 +673,13 @@ function rand_geturl(){
#新设备接入
if [ ! -z "$serverchan_up" ] && [ "$serverchan_up" -eq "1" ] ; then
get_config
read_config
if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【连接】开始进行设备接入检测" >> ${logfile};fi
#从 arp 缓存中读取 0x2 在线列表
IPLIST=$( cat /proc/net/arp | grep "0x2" | awk '{print $1}' | grep -v "^$" | sort -u )
for ip in $IPLIST; do
# 如果 “ip 列表不为空”
send_enable=`uci get serverchan.serverchan.send_enable`>/dev/null 2>&1
send_enable=`get_config send_enable`
if [ ! -z "$ip" ] && [ "$send_enable" -ne 3 ] ; then
ip_mac=$( cat /var/dhcp.leases | grep -w $ip |awk '{print $2}' | grep -v "^$" | sort -u )
@ -736,8 +740,7 @@ function rand_geturl(){
if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【负载】正在检测 CPU 负载" >> ${logfile};fi
cpu_fuzai=`cat /proc/loadavg | awk '{print $1}'`
cpuload=`uci get serverchan.serverchan.cpuload`>/dev/null 2>&1
cpuload=`get_config cpuload`
if [ ! -z "$cpuload" ] && [ `expr $cpu_fuzai \> $cpuload` -eq "1" ] ; then
cpuload_n=$(($cpuload_n+1))
echo "`date "+%H:%M:%S"` 【!!警报!!】当前 CPU 负载过高: ${cpu_fuzai}" >> ${logfile}
@ -763,7 +766,7 @@ function rand_geturl(){
if [ ! -z "$temperature_enable" ] && [ "$temperature_enable" -eq "1" ] ; then
if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%H:%M:%S"` 【温度】正在检测 CPU 温度" >> ${logfile};fi
cpu_wendu=`cut -c1-2 /sys/class/thermal/thermal_zone0/temp`
temperature=`uci get serverchan.serverchan.temperature`>/dev/null 2>&1
temperature=`get_config temperature`
if [ ! -z "$temperature" ] && [ "$cpu_wendu" -gt "$temperature" ] ; then
Temperature_n=$(($Temperature_n+1))
echo "`date "+%H:%M:%S"` 【!!警报!!】当前 CPU 温度过高: ${cpu_wendu}" >> ${logfile}
@ -797,7 +800,7 @@ function rand_geturl(){
continue
fi
send_enable=`uci get serverchan.serverchan.send_enable`>/dev/null 2>&1
send_enable=`get_config send_enable`
if [ ! -z "$send_enable" ] && [ "$send_enable" -eq "3" ] ;then
send_str=0
while [ $send_enable -eq "3" ] ;
@ -809,7 +812,7 @@ function rand_geturl(){
send_str=1
fi
enable_detection 60
send_enable=`uci get serverchan.serverchan.send_enable`>/dev/null 2>&1
send_enable=`get_config send_enable`
continue
done
continue