diff --git a/package/ctcgfw/luci-app-serverchan/Makefile b/package/ctcgfw/luci-app-serverchan/Makefile index be7e650b9c..8dc07e15b0 100644 --- a/package/ctcgfw/luci-app-serverchan/Makefile +++ b/package/ctcgfw/luci-app-serverchan/Makefile @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-serverchan -PKG_VERSION:=1.52 +PKG_VERSION:=1.63 PKG_RELEASE:=7 include $(INCLUDE_DIR)/package.mk diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan.lua b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan.lua index 4e57df7c62..75588ebd0f 100644 --- a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan.lua +++ b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan.lua @@ -18,11 +18,12 @@ s:tab("tab_basic", translate("基本设置")) s:tab("tab_basic2", translate("设备状态")) s:tab("tab_basic3", translate("定时推送")) s:tab("tab_basic4", translate("免打扰")) +s:tab("tab_basic5", translate("高级设置")) s:tab("log",translate("客户端日志")) s.addremove = false s.anonymous = true ---Base Setting +--基本设置 a=s:taboption("tab_basic", Flag,"serverchan_enable",translate("启用")) a.default=0 a.rmempty=true @@ -50,31 +51,7 @@ device_aliases.rmempty = true device_aliases.optional = true device_aliases.description = translate("
请输入设备 MAC 和设备别名,用“-”隔开,如:
XX:XX:XX:XX:XX:XX-我的手机") -local logfile = "/tmp/serverchan/serverchan.log" - -e=s:taboption("log",TextValue,"log") -e:depends({debuglevel="1"}) -e:depends({debuglevel="2"}) -e.rows=26 -e.wrap="off" -e.readonly=true -e.cfgvalue=function(s,s) -return fs.readfile(logfile)or"" -end -e.write=function(e,e,e) -end - -e=s:taboption("log", Button,translate("")) -e:depends({debuglevel="1"}) -e:depends({debuglevel="2"}) -e.inputtitle=translate("清理日志") -e.inputstyle = "clean_log" -function e.write(self, section) - -luci.sys.call("cbi.clean_log") - fs.writefile(logfile, "") -end - +--设备状态 a=s:taboption("tab_basic2", ListValue,"serverchan_ipv4",translate("ipv4 变动通知")) a.default="disable" a:value("0",translate("关闭")) @@ -147,59 +124,8 @@ a.default = "80" a.datatype="uinteger" a:depends({temperature_enable="1"}) a.description = translate("
设备报警只会在连续五分钟超过设定值时才会推送
而且一个小时内不会再提醒第二次") -a=s:taboption("tab_basic2", Flag,"err_enable",translate("无人值守任务")) -a.default=0 -a.rmempty=true -a.description = translate("请确认脚本可以正常运行!!
否则可能造成频繁重启等错误!") -a=s:taboption("tab_basic2", Flag,"err_sheep_enable",translate("仅在免打扰时段重拨")) -a.default=0 -a.rmempty=true -a.description = translate("避免白天重拨 ddns 域名等待解析
此功能不影响断网检测") -a:depends({err_enable="1"}) -device_aliases= s:taboption("tab_basic2", DynamicList, "err_device_aliases", translate("关注列表")) -device_aliases.rmempty = true -device_aliases.optional = true -device_aliases.description = translate("只会在列表中设备都不在线时才会执行
免打扰时段一小时后,关注设备十分钟低流量(约10kb/m)将视为离线") -nt.mac_hints(function(mac, name) device_aliases :value(mac, "%s (%s)" %{ mac, name }) end) -device_aliases:depends({err_enable="1"}) -a=s:taboption("tab_basic2", ListValue,"network_err_event",translate("网络断开时")) -a.default="" -a:depends({err_enable="1"}) -a:value("",translate("无操作")) -a:value("1",translate("重启路由器")) -a:value("2",translate("重启网络接口")) -a:value("3",translate("修改相关设置项,尝试自动修复网络")) -a.description = translate("选项 1 选项 2 不会修改设置,并最多尝试 2 次。
选项 3 会将设置项备份于 /usr/bin/serverchan/configbak 目录,并在失败后还原。
自用于远程误操作无法连接,人在塔在请自行修复。
【!!因为我无法保证兼容性!!】不熟悉系统设置项,不会救砖请勿使用") -a=s:taboption("tab_basic2", ListValue,"system_time_event",translate("定时重启")) -a.default="" -a:depends({err_enable="1"}) -a:value("",translate("无操作")) -a:value("1",translate("重启路由器")) -a:value("2",translate("重启网络接口")) -a= s:taboption("tab_basic2", Value, "autoreboot_time", "系统运行时间大于") -a.rmempty = true -a.default = "24" -a.datatype="uinteger" -a:depends({system_time_event="1"}) -a.description = translate("单位为小时") -a= s:taboption("tab_basic2", Value, "network_restart_time", "网络在线时间大于") -a.rmempty = true -a.default = "24" -a.datatype="uinteger" -a:depends({system_time_event="2"}) -a.description = translate("单位为小时") - -a=s:taboption("tab_basic2", Flag,"public_ip_event",translate("重拨尝试获取公网 ip")) -a.default=0 -a.rmempty=true -a:depends({err_enable="1"}) -a.description = translate("重拨时不会推送 ip 变动通知,并会导致你的域名无法及时更新 ip 地址
请确认你可以通过重拨获取公网 ip,否则这不仅徒劳无功还会引起频繁断网
移动等大内网你就别挣扎了!!") -a= s:taboption("tab_basic2", Value, "public_ip_retry_count", "当天最大重试次数") -a.rmempty = true -a.default = "10" -a.datatype="uinteger" -a:depends({public_ip_event="1"}) +--定时推送 e=s:taboption("tab_basic3", ListValue,"send_mode",translate("定时任务设定")) e.default="" e:value("",translate("关闭")) @@ -275,15 +201,7 @@ luci.sys.call("cbi.apply") luci.sys.call("/usr/bin/serverchan/serverchan send &") end -up_timeout=s:taboption("tab_basic4", Value,"up_timeout",translate('设备上线检测超时')) -up_timeout.default = "2" -up_timeout.datatype="uinteger" - -down_timeout=s:taboption("tab_basic4", Value,"down_timeout",translate('设备离线检测超时')) -down_timeout.default = "10" -down_timeout.datatype="uinteger" -down_timeout.description = translate("如果遇到设备 wifi 休眠,频繁推送离线,可以把超时时间设置长一些") - +--免打扰 sheep=s:taboption("tab_basic4", ListValue,"serverchan_sheep",translate("免打扰时段设置"),translate("在指定整点时间段内,暂停推送消息
免打扰时间中,定时推送也会被阻止。")) sheep:value("0",translate("关闭")) sheep:value("1",translate("模式一:脚本挂起,延迟发送")) @@ -339,6 +257,114 @@ for _, iface in ipairs(ifaces) do end end +--高级设置 +a=s:taboption("tab_basic5", Value,"up_timeout",translate('设备上线检测超时')) +a.default = "2" +a.datatype="uinteger" +a=s:taboption("tab_basic5", Value,"down_timeout",translate('设备离线检测超时')) +a.default = "5" +a.datatype="uinteger" +a.description = translate("如果遇到设备 wifi 休眠,频繁推送离线,可以把超时时间设置长一些") + +a=s:taboption("tab_basic5", Value, "soc_code", "自定义温度读取命令") +a.rmempty = true +a:value("",translate("默认")) +a:value("sensors",translate("sensors命令")) +a.description = translate("请尽量避免使用特殊符号,如双引号、$、!等,执行结果需为数字,用于温度对比") + +a=s:taboption("tab_basic5", Button,"soc",translate("测试温度命令")) +a.inputtitle = translate("输出信息") +a.write = function() + luci.sys.call("/usr/bin/serverchan/serverchan soc") + luci.http.redirect(luci.dispatcher.build_url("admin","services","serverchan")) +end + +if nixio.fs.access("/tmp/serverchan/soc_tmp") then +e=s:taboption("tab_basic5",TextValue,"soc_tmp") +e.rows=2 +e.readonly=true +e.cfgvalue = function() + return luci.sys.exec("cat /tmp/serverchan/soc_tmp && rm -f /tmp/serverchan/soc_tmp") +end +end +a=s:taboption("tab_basic5", Flag,"err_enable",translate("无人值守任务")) +a.default=0 +a.rmempty=true +a.description = translate("请确认脚本可以正常运行,否则可能造成频繁重启等错误!
如果你不了解这些选项的含义,请不要修改") +a=s:taboption("tab_basic5", Flag,"err_sheep_enable",translate("仅在免打扰时段重拨")) +a.default=0 +a.rmempty=true +a.description = translate("避免白天重拨 ddns 域名等待解析,此功能不影响断网检测
因夜间跑流量问题,该功能可能不稳定") +a:depends({err_enable="1"}) +device_aliases= s:taboption("tab_basic5", DynamicList, "err_device_aliases", translate("关注列表")) +device_aliases.rmempty = true +device_aliases.optional = true +device_aliases.description = translate("只会在列表中设备都不在线时才会执行
免打扰时段一小时后,关注设备五分钟低流量(约10kb/m)将视为离线") +nt.mac_hints(function(mac, name) device_aliases :value(mac, "%s (%s)" %{ mac, name }) end) +device_aliases:depends({err_enable="1"}) +a=s:taboption("tab_basic5", ListValue,"network_err_event",translate("网络断开时")) +a.default="" +a:depends({err_enable="1"}) +a:value("",translate("无操作")) +a:value("1",translate("重启路由器")) +a:value("2",translate("重启网络接口")) +a:value("3",translate("修改相关设置项,尝试自动修复网络")) +a.description = translate("选项 1 选项 2 不会修改设置,并最多尝试 2 次。
选项 3 会将设置项备份于 /usr/bin/serverchan/configbak 目录,并在失败后还原。
【!!无法保证兼容性!!】不熟悉系统设置项,不会救砖请勿使用") +a=s:taboption("tab_basic5", ListValue,"system_time_event",translate("定时重启")) +a.default="" +a:depends({err_enable="1"}) +a:value("",translate("无操作")) +a:value("1",translate("重启路由器")) +a:value("2",translate("重启网络接口")) +a= s:taboption("tab_basic5", Value, "autoreboot_time", "系统运行时间大于") +a.rmempty = true +a.default = "24" +a.datatype="uinteger" +a:depends({system_time_event="1"}) +a.description = translate("单位为小时") +a=s:taboption("tab_basic5", Value, "network_restart_time", "网络在线时间大于") +a.rmempty = true +a.default = "24" +a.datatype="uinteger" +a:depends({system_time_event="2"}) +a.description = translate("单位为小时") + +a=s:taboption("tab_basic5", Flag,"public_ip_event",translate("重拨尝试获取公网 ip")) +a.default=0 +a.rmempty=true +a:depends({err_enable="1"}) +a.description = translate("重拨时不会推送 ip 变动通知,并会导致你的域名无法及时更新 ip 地址
请确认你可以通过重拨获取公网 ip,否则这不仅徒劳无功还会引起频繁断网
移动等大内网你就别挣扎了!!") +a= s:taboption("tab_basic5", Value, "public_ip_retry_count", "当天最大重试次数") +a.rmempty = true +a.default = "10" +a.datatype="uinteger" +a:depends({public_ip_event="1"}) + +--客户端日志 +local logfile = "/tmp/serverchan/serverchan.log" +e=s:taboption("log",TextValue,"log") +e:depends({debuglevel="1"}) +e:depends({debuglevel="2"}) +e.rows=26 +e.wrap="off" +e.readonly=true +e.cfgvalue=function(s,s) +return fs.readfile(logfile)or"" +end +e.write=function(e,e,e) +end + +e=s:taboption("log", Button,translate("")) +e:depends({debuglevel="1"}) +e:depends({debuglevel="2"}) +e.inputtitle=translate("清理日志") +e.inputstyle = "clean_log" +function e.write(self, section) + +luci.sys.call("cbi.clean_log") + fs.writefile(logfile, "") +end + local apply = luci.http.formvalue("cbi.apply") if apply then io.popen("/etc/init.d/serverchan start &") diff --git a/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan b/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan index 89daa3028a..2564eecfe3 100755 --- a/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan +++ b/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan @@ -1,7 +1,7 @@ #!/bin/sh # 等待 3s,确保 luci 设置已保存 -sleep 3 +if [ ! "$1" ] ;then sleep 3;fi # 读取设置文件 function get_config(){ @@ -10,26 +10,120 @@ function get_config(){ fi } +# 初始化设置信息 +function read_config(){ + serverchan_enable=`get_config serverchan_enable` + serverchan_sckey=`get_config sckey` + serverchan_ipv4=`get_config serverchan_ipv4` + ipv4_interface=`get_config ipv4_interface` + ipv4_URL=`get_config ipv4_URL` + serverchan_ipv6=`get_config serverchan_ipv6` + ipv6_interface=`get_config ipv6_interface` + ipv6_URL=`get_config ipv6_URL` + serverchan_up=`get_config serverchan_up` + serverchan_down=`get_config serverchan_down` + serverchan_sheep=`get_config serverchan_sheep` + serverchan_whitelist=`get_config serverchan_whitelist` + serverchan_blacklist=`get_config serverchan_blacklist` + serverchan_interface=`get_config serverchan_interface` + device_aliases=`get_config device_aliases | sed 's/ /\n/g' | sed 's/-/ /g'` 2>/dev/null + starttime=`get_config starttime` + endtime=`get_config endtime` + sleeptime=`get_config sleeptime` && if [ -z "$sleeptime" ] ; then sleeptime="60";fi + cpuload_enable=`get_config cpuload_enable` + cpuload=`get_config cpuload` + temperature_enable=`get_config temperature_enable` + temperature=`get_config temperature` + device_name=`get_config device_name` + dir="/tmp/serverchan/" && mkdir -p ${dir} + debuglevel=`get_config debuglevel` && if [ -z "$debuglevel" ] ; then logfile="/dev/null";else logfile="${dir}serverchan.log";fi + up_timeout=`get_config up_timeout` && if [ -z "$up_timeout" ] ; then up_timeout="2";fi + down_timeout=`get_config down_timeout` && if [ -z "$down_timeout" ] ; then down_timeout="10";fi + err_enable=`get_config err_enable` + network_err_event=`get_config network_err_event` + err_device_aliases=`get_config err_device_aliases | sed 's/ /\n/g'` 2>/dev/null + err_sheep_enable=`get_config err_sheep_enable` + system_time_event=`get_config system_time_event` + autoreboot_time=`get_config autoreboot_time` + network_restart_time=`get_config network_restart_time` + wlan_interface=`iw dev | grep Interface | awk '{print $2}'` 2>/dev/null + public_ip_event=`get_config public_ip_event` + public_ip_retry_count=`get_config public_ip_retry_count` + wrtbwmon_version=`opkg list-installed |grep -w ^wrtbwmon|awk '{print $3}'` + arping_version=`opkg list-installed |grep -w ^iputils-arping|awk '{print $3}'` + curl_version=`opkg list-installed |grep -w ^curl|awk '{print $3}'` + soc_code=`get_config soc_code` + enable_detection + markdown_linefeed="%0D%0A%0D%0A" #换行符 + markdown_tab=" " #块区 + markdown_space=" " #空格 "%26ensp%3b" +} + +# 初始化 +function serverchan_init(){ + #rm -f ${dir}usage.db >/dev/null 2>&1 + rm -f ${dir}fd1 >/dev/null 2>&1 + rm -f ${dir}sheep_usage >/dev/null 2>&1 + rm -f ${dir}old_sheep_usage >/dev/null 2>&1 + deltemp + public_ip_today=`date +"%d"` + public_ip_count=0 + if [ -f "/usr/bin/serverchan/errlog" ] ; then + cat /usr/bin/serverchan/errlog > ${logfile} + echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】载入上次重启前日志" >> ${logfile} + echo "--------------------------------------------------------" >> ${logfile} + rm -f /usr/bin/serverchan/errlog >/dev/null 2>&1 + fi + if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then `/etc/init.d/serverchan stop`;fi + echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】载入配置文件" >> ${logfile} + if [ -z "$serverchan_sckey" ] || [ "${#serverchan_sckey}" -lt "50" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】请填入正确的 SCKEY " >> ${logfile} && exit;fi + if [ -z "$arping_version" ] || [ -z "$curl_version" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】依赖未正确安装,程序即将推出 " >> ${logfile} && exit;fi + if [ -z "$serverchan_ipv4" ] || [ "$serverchan_ipv4" -ne "0" ] && [ "$serverchan_ipv4" -ne "1" ] && [ "$serverchan_ipv4" -ne "2" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv4 变化通知 开关" >> ${logfile};fi + if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "1" ] && [ -z "$ipv4_interface" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv4 IP 接口信息" >> ${logfile};fi + if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "2" ] && [ -z "$ipv4_URL" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv4 IP URL信息" >> ${logfile};fi + if [ -z "$serverchan_ipv6" ] || [ "$serverchan_ipv6" -ne "0" ] && [ "$serverchan_ipv6" -ne "1" ] && [ "$serverchan_ipv6" -ne "2" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv6 变化通知 开关" >> ${logfile};fi + if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "1" ] && [ -z "$ipv6_interface" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv6 IP 接口信息" >> ${logfile};fi + if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "2" ] && [ -z "$ipv6_URL" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv6 IP URL信息" >> ${logfile};fi + if [ -z "$serverchan_up" ] || [ "$serverchan_up" -ne "0" ] && [ "$serverchan_up" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 设备上线提醒 开关" >> ${logfile};fi + if [ -z "$serverchan_down" ] || [ "$serverchan_down" -ne "0" ] && [ "$serverchan_down" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 设备下线提醒 开关" >> ${logfile};fi + if [ ! -f "/usr/sbin/wrtbwmon" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】未安装 wrtbwmon ,流量统计不可用" >> ${logfile};fi +} + +# 检测程序开关 +function enable_detection(){ + if [ "$1" ] ;then + for i in `seq 1 $1` + do + serverchan_enable=`get_config serverchan_enable` + if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then `/etc/init.d/serverchan stop`;fi + sleep 1 + done + else + serverchan_enable=`get_config serverchan_enable` + if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then `/etc/init.d/serverchan stop`;fi + fi +} + # 获取 ip function getip(){ if [ "$1" ] ;then if [ $1 = "wanipv4" ] ;then - if [ ! -z "$ipv4_interface" ] ; then local wanIP=$(/sbin/ifconfig $ipv4_interface | awk '/inet addr/ {print $2}' | awk -F: '{print $2}'| grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}');fi + if [ ! -z "$ipv4_interface" ] ; then local wanIP=$(/sbin/ifconfig ${ipv4_interface} | awk '/inet addr/ {print $2}' | awk -F: '{print $2}'| grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}');fi if [ -z "$ipv4_interface" ] ; then local 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}');fi echo "$wanIP" elif [ $1 = "hostipv4" ] ;then if [ -z "$ipv4_URL" ] ; then local ipv4_URL="members.3322.org/dyndns/getip";fi - if [ ! -z "$ipv4_interface" ] ; then local hostIP=$(curl -k -s -4 --interface $ipv4_interface $ipv4_URL);fi - if [ -z "$ipv4_interface" ] ; then local hostIP=$(curl -k -s -4 $ipv4_URL);fi + if [ ! -z "$ipv4_interface" ] ; then local hostIP=$(curl -k -s -4 --interface ${ipv4_interface} ${ipv4_URL});fi + if [ -z "$ipv4_interface" ] ; then local hostIP=$(curl -k -s -4 ${ipv4_URL});fi echo "$hostIP" elif [ $1 = "wanipv6" ] ;then - if [ ! -z "$ipv6_interface" ] ; then local wanIPv6=$(ip addr show $ipv6_interface |grep -v deprecated|grep -A1 'inet6 [^f:]'|sed -nr ':a;N;s#^ +inet6 ([a-f0-9:]+)/.+? scope global .*? valid_lft ([0-9]+sec) .*#\2 \1#p;ta'|sort -nr|head -n1|awk '{print $2}');fi + if [ ! -z "$ipv6_interface" ] ; then local wanIPv6=$(ip addr show ${ipv6_interface} |grep -v deprecated|grep -A1 'inet6 [^f:]'|sed -nr ':a;N;s#^ +inet6 ([a-f0-9:]+)/.+? scope global .*? valid_lft ([0-9]+sec) .*#\2 \1#p;ta'|sort -nr|head -n1|awk '{print $2}');fi if [ -z "$ipv6_interface" ] ; then local wanIPv6=$(ip addr show|grep -v deprecated|grep -A1 'inet6 [^f:]'|sed -nr ':a;N;s#^ +inet6 ([a-f0-9:]+)/.+? scope global .*? valid_lft ([0-9]+sec) .*#\2 \1#p;ta'|sort -nr|head -n1|awk '{print $2}');fi echo "$wanIPv6" elif [ $1 = "hostipv6" ] ;then if [ -z "$ipv6_URL" ] ; then local ipv6_URL="v6.ip.zxinc.org/getip";fi - if [ ! -z "$ipv6_interface" ] ; then local hostIPv6=$(curl -k -s -6 --interface $ipv6_interface $ipv6_URL);fi - if [ -z "$ipv6_interface" ] ; then local hostIPv6=$(curl -k -s -6 $ipv6_URL);fi + if [ ! -z "$ipv6_interface" ] ; then local hostIPv6=$(curl -k -s -6 --interface ${ipv6_interface} ${ipv6_URL});fi + if [ -z "$ipv6_interface" ] ; then local hostIPv6=$(curl -k -s -6 ${ipv6_URL});fi echo "$hostIPv6" fi fi @@ -37,23 +131,29 @@ fi # 查询 mac 地址 function getmac(){ - if [ -z "$ip_mac" ] ; then local ip_mac=`cat /var/dhcp.leases | grep -w $1 |awk '{print $2}' | grep -v "^$" | sort -u`;fi - if [ -f "${dir}ipAddress" ] ; then local ip_mac=`cat ${dir}ipAddress | grep -w $1 |awk '{print $2}' | grep -v "^$" | sort -u`;fi - if [ -f "${dir}tmp_downlist" ] && [ -z "$ip_mac" ] ; then local ip_mac=`cat ${dir}tmp_downlist | grep -w $1 |awk '{print $2}' | grep -v "^$" | sort -u`;fi - if ( echo "$ip_mac" | grep -q "unknown" ) || [ -z "$ip_mac" ] ; then local ip_mac=`cat /proc/net/arp | grep "0x2\|0x6" | grep -w $1 | awk '{print $4}' | grep -v "^$" | sort -u`;fi - if [ -z "$ip_mac" ] ; then local ip_mac="unknown";fi - echo "$ip_mac" + if [ -z "$tmp_mac" ] ; then local tmp_mac=`cat /var/dhcp.leases | grep -w $1 |awk '{print $2}' | grep -v "^$" | sort -u` 2>/dev/null;fi + if [ -f "${dir}ipAddress" ] ; then local tmp_mac=`cat ${dir}ipAddress | grep -w $1 |awk '{print $2}' | grep -v "^$" | sort -u`;fi + if [ -f "${dir}tmp_downlist" ] && [ -z "$tmp_mac" ] ; then local tmp_mac=`cat ${dir}tmp_downlist | grep -w $1 |awk '{print $2}' | grep -v "^$" | sort -u`;fi + if [ -z "$tmp_mac" ] || ( echo "$tmp_mac" | grep -q "unknown" ) ; then local tmp_mac=`cat /proc/net/arp | grep "0x2\|0x6" | grep -w $1 | awk '{print $4}' | grep -v "^$" | sort -u`;fi + if [ -z "$tmp_mac" ] ; then local tmp_mac="unknown";fi + echo "$tmp_mac" } # 查询主机名 function getname(){ - local ip_name=`echo "$device_aliases" | grep -i $2 |awk '{print $2}' | grep -v "^$" | sort -u` - if [ -z "$ip_name" ] ; then local ip_name=`cat /var/dhcp.leases | grep -w $1 |awk '{print $4}' | grep -v "^$" | sort -u`;fi - if [ -z "$ip_name" ] && [ -f "${dir}ipAddress" ] ; then local ip_name=`cat ${dir}ipAddress | grep -w $1 |awk '{print $3}' | grep -v "^$" | sort -u`;fi - if [ -z "$ip_name" ] && [ -f "${dir}tmp_downlist" ] ; then local ip_name=`cat ${dir}tmp_downlist | grep -w $1 |awk '{print $3}' | grep -v "^$" | sort -u`;fi - if ( echo "$ip_name" | grep -q "unknown" ) || [ -z "$ip_name" ] ; then local ip_name=$(uci get dhcp.`uci show dhcp | grep -i ^dhcp.@host.*mac=.$2 | sed -nr 's#^dhcp.(.*).mac.*#\1#gp'`.name 2>/dev/null);fi - if [ -z "$ip_name" ] ; then local ip_name="unknown";fi - echo "$ip_name" + if [ -z "$tmp_name" ] && [ -f "${dir}ipAddress" ] ; then local tmp_name=`cat ${dir}ipAddress | grep -w $1 |awk '{print $3}' | grep -v "^$" | sort -u`;fi + if [ -z "$tmp_name" ] && [ -f "${dir}tmp_downlist" ] ; then local tmp_name=`cat ${dir}tmp_downlist | grep -w $1 |awk '{print $3}' | grep -v "^$" | sort -u`;fi + if [ -z "$tmp_name" ] || ( echo "$tmp_name" | grep -q "unknown" ) ; then local tmp_name=`echo "$device_aliases" | grep -i $2 |awk '{print $2}' | grep -v "^$" | sort -u`;fi + if [ -z "$tmp_name" ] || ( echo "$tmp_name" | grep -q "unknown" ) ; then + local dhcp_config=`uci show dhcp` + local tmp_name=$(uci get dhcp.`echo "$dhcp_config" | grep -i ^dhcp.@host.*mac=.${2} | sed -nr 's#^dhcp.(.*).mac.*#\1#gp'`.name) 2>/dev/null + if [ -z "$tmp_name" ] ; then local tmp_name=$(uci get dhcp.`echo "$dhcp_config" | grep -i ^dhcp.@host.*ip=.${1} | sed -nr 's#^dhcp.(.*).ip.*#\1#gp'`.name) 2>/dev/null;fi + if [ -z "$tmp_name" ] ; then local tmp_name=$(uci get dhcp.`echo "$dhcp_config" | grep -i ^dhcp.@domain.*mac=.${2} | sed -nr 's#^dhcp.(.*).mac.*#\1#gp'`.name) 2>/dev/null;fi + if [ -z "$tmp_name" ] ; then local tmp_name=$(uci get dhcp.`echo "$dhcp_config" | grep -i ^dhcp.@domain.*ip=.${1} | sed -nr 's#^dhcp.(.*).ip.*#\1#gp'`.name) 2>/dev/null;fi + fi + if [ -z "$tmp_name" ] || ( echo "$tmp_name" | grep -q "unknown" ) ; then local tmp_name=`cat /var/dhcp.leases | grep -w $1 |awk '{print $4}' | grep -v "^$" | sort -u` 2>/dev/null;fi + if [ -z "$tmp_name" ] ; then local tmp_name="unknown";fi + echo "$tmp_name" } # 查询设备接口 @@ -62,7 +162,7 @@ function getinterface(){ if [ -f "${dir}tmp_downlist" ] && [ -z "$ip_interface" ] ; then local ip_interface=`cat ${dir}tmp_downlist | grep -w $1 |awk '{print $5}' | grep -v "^$" | sort -u`;fi if [ ! -z "$wlan_interface" ] ; then for interface in $wlan_interface; do - if [ -z "$ip_interface" ] ; then local ip_interface=`iw dev $interface station dump | grep Station | grep -i -w $1 | sed -nr 's#^.*on (.*))#\1#gp' 2>/dev/null`;fi + if [ -z "$ip_interface" ] ; then local ip_interface=`iw dev $interface station dump | grep Station | grep -i -w $1 | sed -nr 's#^.*on (.*))#\1#gp'` 2>/dev/null;fi done fi if [ -z "$ip_interface" ] ; then local ip_interface=`cat /proc/net/arp | grep "0x2" | grep -i -w $1 | awk '{print $6}' | grep -v "^$" | sort -u`;fi @@ -79,7 +179,12 @@ function getcpu(){ # 获取SOC温度 (取所有传感器温度最大值) function soc_temp(){ - cat /sys/class/thermal/thermal_zone*/temp | sort | sed '$!d' | cut -c-2 2>/dev/null + if [ -z "$soc_code" ] ;then + soc_code="cat /sys/class/thermal/thermal_zone*/temp|sort -nr|head -n1|cut -c-2" + elif [ "$soc_code" = "sensors" ] ;then + soc_code="sensors|grep °C|sed -nr 's#^Core.*:.*\+(.*)°C .*#\1#gp'|sort -nr|head -n1" + fi + echo ${soc_code}|awk '{run=$0;system(run)}' 2>/dev/null } # 流量数据 @@ -98,12 +203,10 @@ if [ "$1" ] ; then fi elif [ -f "/usr/sbin/wrtbwmon" ] ; then wrtbwmon update ${dir}usage.db - else - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】未安装 wrtbwmon ,流量统计不可用" >> ${logfile};fi fi # 读取数据 elif [ $1 = "get" ] ;then - if [ "$2" ] ; then local ip_total=`cat ${dir}usage.db | grep -w $2 | sed 's/,/ /g' | awk '{print $6}' 2>/dev/null`;fi + if [ "$2" ] ; then local ip_total=`cat ${dir}usage.db | grep -w $2 | sed 's/,/ /g' | awk '{print $6}'` 2>/dev/null;fi if [ -z "$ip_total" ] ; then local ip_total="0";fi echo `bytes_for_humans $ip_total` # 剔除ip @@ -158,92 +261,16 @@ function time_for_humans { fi } -# 初始化设置信息 -function read_config(){ - serverchan_enable=`get_config serverchan_enable` - serverchan_sckey=`get_config sckey` - serverchan_ipv4=`get_config serverchan_ipv4` - ipv4_interface=`get_config ipv4_interface` - ipv4_URL=`get_config ipv4_URL` - serverchan_ipv6=`get_config serverchan_ipv6` - ipv6_interface=`get_config ipv6_interface` - ipv6_URL=`get_config ipv6_URL` - serverchan_up=`get_config serverchan_up` - serverchan_down=`get_config serverchan_down` - serverchan_sheep=`get_config serverchan_sheep` - serverchan_whitelist=`get_config serverchan_whitelist` - serverchan_blacklist=`get_config serverchan_blacklist` - serverchan_interface=`get_config serverchan_interface` - device_aliases=`get_config device_aliases | sed 's/ /\n/g' | sed 's/-/ /g' 2>/dev/null` - starttime=`get_config starttime` - endtime=`get_config endtime` - sleeptime=`get_config sleeptime` && if [ -z "$sleeptime" ] ; then sleeptime="60";fi - cpuload_enable=`get_config cpuload_enable` - cpuload=`get_config cpuload` - temperature_enable=`get_config temperature_enable` - temperature=`get_config temperature` - device_name=`get_config device_name` - dir="/tmp/serverchan/" && mkdir -p ${dir} - debuglevel=`get_config debuglevel` && if [ -z "$debuglevel" ] ; then logfile="/dev/null";else logfile="${dir}serverchan.log";fi - up_timeout=`get_config up_timeout` && if [ -z "$up_timeout" ] ; then up_timeout="2";fi - down_timeout=`get_config down_timeout` && if [ -z "$down_timeout" ] ; then down_timeout="10";fi - err_enable=`get_config err_enable` - network_err_event=`get_config network_err_event` - err_device_aliases=`get_config err_device_aliases | sed 's/ /\n/g' 2>/dev/null` - err_sheep_enable=`get_config err_sheep_enable` - system_time_event=`get_config system_time_event` - autoreboot_time=`get_config autoreboot_time` - network_restart_time=`get_config network_restart_time` - wlan_interface=`iw dev | grep Interface | awk '{print $2}'` - public_ip_event=`get_config public_ip_event` - public_ip_retry_count=`get_config public_ip_retry_count` - wrtbwmon_version=`opkg list-installed |grep -w ^wrtbwmon|awk '{print $3}'` - arping_version=`opkg list-installed |grep -w ^iputils-arping|awk '{print $3}'` - curl_version=`opkg list-installed |grep -w ^curl|awk '{print $3}'` - enable_detection +# 计算字符真实长度 +function length_str { +if [ "$1" ] ;then + local length_zh=`echo "$1" | awk '{print gensub(/[\u4e00-\u9fa5]/,"","g",$0)}' | awk -F "" '{print NF}'` + local length_en=`echo "$1" | awk '{print gensub(/[^\u4e00-\u9fa5]/,"","g",$0)}' | awk -F "" '{print NF}'` + echo `expr $length_zh / 3 \* 2 + $length_en` +fi } -# 初始化 -function serverchan_init(){ - #rm -f ${dir}usage.db >/dev/null 2>&1 - rm -f ${dir}fd1 >/dev/null 2>&1 - rm -f ${dir}sheep_usage >/dev/null 2>&1 - rm -f ${dir}old_sheep_usage >/dev/null 2>&1 - deltemp - public_ip_today=`date +"%d"` - public_ip_retry_count=0 - if [ -f "/usr/bin/serverchan/errlog" ] ; then - cat /usr/bin/serverchan/errlog > ${logfile} - echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】载入上次重启前日志" >> ${logfile} - echo "--------------------------------------------------------" >> ${logfile} - rm -f /usr/bin/serverchan/errlog >/dev/null 2>&1 - fi - if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then `/etc/init.d/serverchan stop`;fi - echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】载入配置文件" >> ${logfile} - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】工作目录为 $dir " >> ${logfile};fi - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】log 目录为 $logfile " >> ${logfile};fi - if [ -z "$serverchan_sckey" ] || [ "${#serverchan_sckey}" -lt "50" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】请填入正确的 SCKEY " >> ${logfile} && exit;fi - if [ -z "$arping_version" ] || [ -z "$curl_version" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】依赖未正确安装,程序即将推出 " >> ${logfile} && exit;fi - if [ -z "$serverchan_ipv4" ] || [ "$serverchan_ipv4" -ne "0" ] && [ "$serverchan_ipv4" -ne "1" ] && [ "$serverchan_ipv4" -ne "2" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv4 变化通知 开关" >> ${logfile};fi - if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "1" ] && [ -z "$ipv4_interface" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv4 IP 接口信息" >> ${logfile};fi - if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "2" ] && [ -z "$ipv4_URL" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv4 IP URL信息" >> ${logfile};fi - if [ -z "$serverchan_ipv6" ] || [ "$serverchan_ipv6" -ne "0" ] && [ "$serverchan_ipv6" -ne "1" ] && [ "$serverchan_ipv6" -ne "2" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv6 变化通知 开关" >> ${logfile};fi - if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "1" ] && [ -z "$ipv6_interface" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv6 IP 接口信息" >> ${logfile};fi - if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "2" ] && [ -z "$ipv6_URL" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 ipv6 IP URL信息" >> ${logfile};fi - if [ -z "$serverchan_up" ] || [ "$serverchan_up" -ne "0" ] && [ "$serverchan_up" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 设备上线提醒 开关" >> ${logfile};fi - if [ -z "$serverchan_down" ] || [ "$serverchan_down" -ne "0" ] && [ "$serverchan_down" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取 设备下线提醒 开关" >> ${logfile};fi - if [ -z "$serverchan_whitelist" ] && [ -z "$serverchan_blacklist" ] && [ -z "$serverchan_interface" ]; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】未启用 MAC 过滤" >> ${logfile};fi - elif [ ! -z "$serverchan_whitelist" ] ; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】忽略列表内设备" >> ${logfile};fi - elif [ ! -z "$serverchan_blacklist" ] ; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】仅关注列表内设备" >> ${logfile};fi - elif [ ! -z "$serverchan_interface" ] ; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】仅关注接口内设备" >> ${logfile};fi - fi -} - -# 初始化在线设备列表 +# 在线设备列表 function serverchan_first(){ # 从 ipAddress 文件中读取上次检测到的在线列表 if [ ! -f "${dir}ipAddress" ] ; then > ${dir}ipAddress;fi @@ -257,7 +284,7 @@ function serverchan_first(){ }& fi done - unset ip && unset IPLIST + unset ip IPLIST local IPLIST=$( cat /proc/net/arp | grep "0x2\|0x6" | awk '{print $1}' | grep -v "^$" | sort -u ) for ip in $IPLIST; do # 如果 “ip 列表不为空” @@ -272,21 +299,6 @@ function serverchan_first(){ wait } -# 检测程序开关 -function enable_detection(){ - if [ "$1" ] ;then - for i in `seq 1 $1` - do - serverchan_enable=`get_config serverchan_enable` - if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then `/etc/init.d/serverchan stop`;fi - sleep 1 - done - else - serverchan_enable=`get_config serverchan_enable` - if [ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] ; then `/etc/init.d/serverchan stop`;fi - fi -} - # 创建计划任务 function serverchan_cron(){ local send_mode=`get_config send_mode` @@ -340,15 +352,13 @@ function serverchan_disturb(){ 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 local enable=1;else local enable=0;fi if [ "$enable" -eq "0" ] ;then - if [ -z "$sheep_starttime" ] ;then sheep_starttime=`date +%s`;fi + if [ -z "$sheep_starttime" ] ;then + echo "`date "+%Y-%m-%d %H:%M:%S"` 【免打扰】夜深了,该休息了" >> ${logfile} + sheep_starttime=`date +%s` + fi if [ "$serverchan_sheep" -eq "1" ] ;then - local sheep_str=0 while [ $enable -eq "0" ] ; do - if [ "$sheep_str" -eq "0" ] ; then - echo "`date "+%Y-%m-%d %H:%M:%S"` 【免打扰】夜深了,该休息了" >> ${logfile} - local sheep_str=1 - fi 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 local enable=1 else @@ -362,11 +372,13 @@ function serverchan_disturb(){ continue done elif [ "$serverchan_sheep" -eq "2" ] ;then + disturb_text="【免打扰】" return 0 fi fi fi unset sheep_starttime + disturb_text="【微信推送】" return 1 } @@ -393,48 +405,43 @@ function LockFile(){ # 清理临时文件 function deltemp(){ - unset title - unset content - rm -f ${dir}title >/dev/null 2>&1 - rm -f ${dir}content >/dev/null 2>&1 - rm -f ${dir}top >/dev/null 2>&1 - rm -f ${dir}tmp_downlist >/dev/null 2>&1 - rm -f ${dir}send_enable.lock >/dev/null 2>&1 - LockFile unlock - if [ -f ${logfile} ] ; then - local logrow=$(grep -c "" ${logfile}) - if [ $logrow -gt 500 ] ;then - sed -i '1,100d' ${logfile} - echo "`date "+%Y-%m-%d %H:%M:%S"` 【清理】日志超出上限,删除前 100 条" >> ${logfile} - fi +unset title content +rm -f ${dir}title >/dev/null 2>&1 +rm -f ${dir}content >/dev/null 2>&1 +rm -f ${dir}top >/dev/null 2>&1 +rm -f ${dir}tmp_downlist >/dev/null 2>&1 +rm -f ${dir}send_enable.lock >/dev/null 2>&1 +LockFile unlock +if [ -f ${logfile} ] ; then + local logrow=$(grep -c "" ${logfile}) + if [ $logrow -gt 500 ] ;then + sed -i '1,100d' ${logfile} + echo "`date "+%Y-%m-%d %H:%M:%S"` 【清理】日志超出上限,删除前 100 条" >> ${logfile} fi +fi } # 检测黑白名单 function blackwhitelist(){ - if [ "$1" ] ;then - # 如果 “忽略列表 为空” 并且 “关注列表为空” - if [ -z "$serverchan_whitelist" ] && [ -z "$serverchan_blacklist" ] && [ -z "$serverchan_interface" ]; then +if [ "$1" ] ;then + # 如果 “忽略列表 为空” 并且 “关注列表为空” + if [ -z "$serverchan_whitelist" ] && [ -z "$serverchan_blacklist" ] && [ -z "$serverchan_interface" ]; then + echo 1 + # 如果 “忽略列表不为空” + elif [ ! -z "$serverchan_whitelist" ] && ( ! echo $serverchan_whitelist | grep -q -i -w $1); then + echo 1 + # 如果 “关注列表不为空” + elif [ ! -z "$serverchan_blacklist" ] && ( echo $serverchan_blacklist | grep -q -i -w $1); then + echo 1 + # 如果 “仅关注接口选项打开” + elif [ ! -z "$serverchan_interface" ] ; then + local ip=`iw dev $serverchan_interface station dump | grep -i -w $1 | grep Station | grep -v "^$"` 2>/dev/null + if [ -z "$ip" ];then local ip=`getinterface $1 | grep -i -w $serverchan_interface | grep -v "^$"`;fi + if [ ! -z "$ip" ];then echo 1 - # 如果 “忽略列表不为空” - elif [ ! -z "$serverchan_whitelist" ] ; then - if ( ! echo $serverchan_whitelist | grep -q -i -w $1); then - echo 1 - fi - # 如果 “关注列表不为空” - elif [ ! -z "$serverchan_blacklist" ] ; then - if ( echo $serverchan_blacklist | grep -q -i -w $1); then - echo 1 - fi - # 如果 “仅关注接口选项打开” - elif [ ! -z "$serverchan_interface" ] ; then - local ip=`iw dev $serverchan_interface station dump | grep -i -w $1 | grep Station | grep -v "^$" 2>/dev/null` - if [ -z "$ip" ];then local ip=`getinterface $1 | grep -i -w $serverchan_interface | grep -v "^$"`;fi - if [ ! -z "$ip" ];then - echo 1 - fi fi fi +fi } # 重启网络服务 @@ -445,11 +452,11 @@ cat>${dir}network_restart</dev/null 2>&1 & /etc/init.d/dnsmasq restart >/dev/null 2>&1 & EOF - chmod 0755 ${dir}network_restart && ${dir}network_restart - rm -f ${dir}network_restart >/dev/null 2>&1 - if [ "$1" ] ;then - sleep $1 - fi +chmod 0755 ${dir}network_restart && ${dir}network_restart +rm -f ${dir}network_restart >/dev/null 2>&1 +if [ "$1" ] ;then + sleep $1 +fi } # 查看无人值守任务设备是否在线 @@ -461,27 +468,26 @@ if [ ! -z "$err_device_aliases" ] && [ -f ${dir}ipAddress ] ; then local err_mac=$( echo "$err_device_aliases" | grep -i $mac | grep -v "^$" | sort -u ) fi done - # 进入免打扰时间已经超过一小时 if [ ! -z "$sheep_starttime" ] && [ "$((`date +%s`-$sheep_starttime))" -ge "3600" ] ; then > ${dir}sheep_usage local MACLIST=$( echo "$err_device_aliases" | grep -v "^$" | sort -u ) - if [ -z "$total_n" ] ; then total_n=`cat ${dir}usage.db 2>/dev/null | head -n1 | grep "total" | sed 's/,/\n/g' | awk '/total/{print NR}'`;fi + if [ -z "$total_n" ] ; then total_n=`cat ${dir}usage.db | head -n1 | grep "total" | sed 's/,/\n/g' | awk '/total/{print NR}'` 2>/dev/null;fi if [ -z "$total_n" ] ; then total_n="6";fi for mac in $MACLIST; do - if [ ! -z "$mac" ] ; then local tmptotal=`cat ${dir}usage.db 2>/dev/null | sed 's/,/ /g' | grep -i $mac | awk "{print "'$'$total_n"}" | grep -v "^$" | sort -u`;fi + if [ ! -z "$mac" ] ; then local tmptotal=`cat ${dir}usage.db | sed 's/,/ /g' | grep -i $mac | awk "{print "'$'$total_n"}" | grep -v "^$" | sort -u` 2>/dev/null;fi if [ ! -z "$tmptotal" ] ; then awk 'BEGIN{printf "%.0f\n",'$tmptotal'/'20480'}' 2>/dev/null >> ${dir}sheep_usage;fi done - old_sheep_usage=`cat ${dir}old_sheep_usage 2>/dev/null` - sheep_usage=`cat ${dir}sheep_usage 2>/dev/null` + old_sheep_usage=`cat ${dir}old_sheep_usage` 2>/dev/null + sheep_usage=`cat ${dir}sheep_usage` 2>/dev/null if [ "$old_sheep_usage" == "$sheep_usage" ] ; then if [ -z "$sheep_nousage_starttime" ] ;then sheep_nousage_starttime=`date +%s`;fi else unset sheep_nousage_starttime cat ${dir}sheep_usage 2>/dev/null > ${dir}old_sheep_usage fi - # 如果关注设备连续十分钟低流量(每次低于10kb),视为不在线 - if [ ! -z "$sheep_nousage_starttime" ] && [ "$((`date +%s`-$sheep_nousage_starttime))" -ge "600" ] ; then unset err_mac;fi + # 如果关注设备连续五分钟低流量(约10kb/m),视为不在线 + if [ ! -z "$sheep_nousage_starttime" ] && [ "$((`date +%s`-$sheep_nousage_starttime))" -ge "300" ] ; then unset err_mac;fi fi if [ -z "$err_mac" ] ; then return 0;else return 1;fi fi @@ -490,12 +496,10 @@ fi # 无人值守任务 function unattended(){ if [ -z "$err_enable" ] || [ "$err_enable" -ne "1" ] ; then return;fi -if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】检测客户端在线情况" >> ${logfile};fi geterrdevicealiases if [ $? -eq "1" ] ; then err_mac="1";fi # 如果打开仅在免打扰时段重拨 if [ ! -z "$err_sheep_enable" ] && [ "$err_sheep_enable" -eq "1" ] && [ -z "$sheep_starttime" ]; then err_mac="1";fi -if [ -z "$err_mac" ] && [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ]; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】关注设备不在线" >> ${logfile};elif [ ! -z "$err_mac" ] && [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ]; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】关注设备在线,跳过" >> ${logfile};fi # 关注设备不在线,执行任务 if [ -z "$err_mac" ] && [ ! -z "$system_time_event" ] ; then if [ ! -z "$autoreboot_time" ] && [ `cat /proc/uptime | awk -F. '{run_hour=$1/3600;printf("%d",run_hour)}'` -ge "$autoreboot_time" ] && [ "$system_time_event" -eq "1" ] ; then @@ -512,12 +516,12 @@ if [ -z "$err_mac" ] && [ ! -z "$system_time_event" ] ; then fi # 重拨尝试获取公网 -if [ -z "$err_mac" ] && [ ! -z "$public_ip_event" ] && [ ! -z "$public_ip_retry_count" ] ; then +if [ $public_ip_today -eq `date +"%d"` ] ;then public_ip_count=`expr $public_ip_count + 1`;else public_ip_today=`date +"%d"` && public_ip_count=1;fi +if [ -z "$err_mac" ] && [ ! -z "$public_ip_event" ] && [ ! -z "$public_ip_retry_count" ] && [ "$public_ip_count" -le "$public_ip_retry_count" ]; then local wanIP=`getip wanipv4` local hostIP=`getip hostipv4` if [ ! -z ${wanIP} ] && [ ! -z ${hostIP} ] && ( ! echo ${wanIP} | grep -q -w ${hostIP} );then - if [ $today -eq `date +"%d"` ] ;then public_ip_retry_count=`expr $public_ip_retry_count + 1`;else public_ip_today=`date +"%d"` && public_ip_retry_count=1;fi - echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】重拨尝试获取公网 ip,当前第 $public_ip_retry_count 次 " >> ${logfile} + echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】重拨尝试获取公网 ip,当前第 $public_ip_count 次 " >> ${logfile} # 等待 60 秒后重新获取 ip 并写入 ifup wan >/dev/null 2>&1 sleep 60 @@ -678,85 +682,69 @@ function rand_geturl(){ # 检测 ip 状况 function ip_changes(){ - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【外网IP】正在检测外网IP变化" >> ${logfile};fi - if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "1" ] ; then local IPv4=`getip wanipv4`;fi - if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "2" ] ; then local IPv4=`getip hostipv4`;fi - if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "1" ] ; then local IPv6=`getip wanipv6`;fi - if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "2" ] ; then local IPv6=`getip hostipv6`;fi +if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【外网IP】正在检测外网IP变化" >> ${logfile};fi +if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "1" ] ; then local IPv4=`getip wanipv4`;fi +if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "2" ] ; then local IPv4=`getip hostipv4`;fi +if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "1" ] ; then local IPv6=`getip wanipv6`;fi +if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "2" ] ; then local IPv6=`getip hostipv6`;fi - if [ -f ${dir}ip ] ; then - local last_IPv4=$(cat "${dir}ip" | grep IPv4 | awk '{print $2}' | grep -v "^$" | sort -u) - local last_IPv6=$(cat "${dir}ip" | grep IPv6 | awk '{print $2}' | grep -v "^$" | sort -u) - - if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] ;then - if [ ! -z "$IPv4" ] && ( echo ${IPv4} | grep -w -q ${last_IPv4} );then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` IP 无变化,当前 ip 为 $last_IPv4" >> ${logfile};fi - elif [ ! -z "$IPv4" ] ; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 当前 IP: ${IPv4}" >> ${logfile};fi - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 上次 IP: ${last_IPv4}" >> ${logfile};fi - echo "`date "+%Y-%m-%d %H:%M:%S"` 【微信推送】当前IP:${IPv4}" >> ${logfile} - echo IPv4 $IPv4 > ${dir}ip - echo -e IPv6 $last_IPv6 >> ${dir}ip - - if [ -f "${dir}title" ] ; then local title=`cat ${dir}title` ;fi - if [ -f "${dir}content" ] ; then local content=`cat ${dir}content` ;fi - if ( echo ${title} | grep -q "IP 地址变化" ) || [ -z "$title" ] ; then - local title="IP 地址变化" - else - local title="设备状态变化" - fi - - local content="%0D%0A%0D%0A---%0D%0A%0D%0A""####IP 地址变化%0D%0A%0D%0A>当前 IP:${IPv4}" - echo $title >${dir}title - echo -n $content >>${dir}content - else - echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】获取 IPv4 地址失败" >> ${logfile} - fi +if [ -f ${dir}ip ] ; then + local last_IPv4=$(cat "${dir}ip" | grep IPv4 | awk '{print $2}' | grep -v "^$" | sort -u) + local last_IPv6=$(cat "${dir}ip" | grep IPv6 | awk '{print $2}' | grep -v "^$" | sort -u) + # 获取 IPv4 地址 + if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] ;then + if [ ! -z "$IPv4" ] && ( echo ${IPv4} | grep -w -q ${last_IPv4} );then + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` IP 无变化,当前 ip 为 ${last_IPv4}" >> ${logfile};fi + elif [ ! -z "$IPv4" ] ; then + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 当前 IP: ${IPv4}" >> ${logfile};fi + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 上次 IP: ${last_IPv4}" >> ${logfile};fi + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}当前IP:${IPv4}" >> ${logfile} + echo IPv4 $IPv4 > ${dir}ip + echo -e IPv6 $last_IPv6 >> ${dir}ip + title="IP 地址变化" + content="${content}${markdown_linefeed}---${markdown_linefeed}####IP 地址变化${markdown_linefeed}${markdown_tab}当前 IP:${IPv4}" + else + echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】获取 IPv4 地址失败" >> ${logfile} fi - - if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] ;then - if [ ! -z "$IPv6" ] && ( echo ${IPv6} | grep -w -q ${last_IPv6} );then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` IPv6 无变化,当前 ipv6 为 $last_IPv6" >> ${logfile};fi - elif [ ! -z "$IPv6" ] ; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 当前 IPv6: ${IPv6}" >> ${logfile};fi - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 上次 IPv6: ${last_IPv6}" >> ${logfile};fi - echo "`date "+%Y-%m-%d %H:%M:%S"` 【微信推送】当前IPv6:${IPv6}" >> ${logfile} - echo IPv4 $IPv4 > ${dir}ip - echo -e IPv6 $IPv6 >> ${dir}ip - - if [ -f "${dir}title" ] ; then local title=`cat ${dir}title` ;fi - if [ -f "${dir}content" ] ; then local content=`cat ${dir}content` ;fi - if ( echo ${title} | grep -q "IPv6 地址变化" ) || [ -z "$title" ] ; then - local title="IPv6 地址变化" - else - local title="设备状态变化" - fi - - local content="%0D%0A%0D%0A---%0D%0A%0D%0A""####IPv6 地址变化%0D%0A%0D%0A>当前 IPv6:${IPv6}" - echo $title >${dir}title - echo -n $content >>${dir}content - else - echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】获取 IPv6 地址失败" >> ${logfile} - fi - fi - else - echo "`date "+%Y-%m-%d %H:%M:%S"` 【微信推送】路由器已经重启!" >> ${logfile} - if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] ;then echo "`date "+%Y-%m-%d %H:%M:%S"` 当前IP: ${IPv4}" >> ${logfile};fi - if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] ;then echo "`date "+%Y-%m-%d %H:%M:%S"` 当前IPv6: ${IPv6}" >> ${logfile};fi - echo IPv4 $IPv4 > ${dir}ip - echo -e IPv6 $IPv6 >> ${dir}ip - if [ -f "${dir}title" ] ; then local title=`cat ${dir}title` ;fi - if [ -f "${dir}content" ] ; then local content=`cat ${dir}content` ;fi - local title="路由器重新启动" - local content="%0D%0A%0D%0A---%0D%0A%0D%0A""####路由器重新启动" - if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] ;then local content=${content}"%0D%0A%0D%0A>当前IP:${IPv4}";fi - if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] ;then local content=${content}"%0D%0A%0D%0A>当前IPv6:${IPv6}";fi - echo $title >${dir}title - echo -n $content >>${dir}content fi + # 获取 IPv6 地址 + if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] ;then + if [ ! -z "$IPv6" ] && ( echo ${IPv6} | grep -w -q ${last_IPv6} );then + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` IPv6 无变化,当前 ipv6 为 $last_IPv6" >> ${logfile};fi + elif [ ! -z "$IPv6" ] ; then + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 当前 IPv6: ${IPv6}" >> ${logfile};fi + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 上次 IPv6: ${last_IPv6}" >> ${logfile};fi + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}当前IPv6:${IPv6}" >> ${logfile} + echo IPv4 $IPv4 > ${dir}ip + echo -e IPv6 $IPv6 >> ${dir}ip + if [ -z "$title" ] ; then + local title="IPv6 地址变化" + else + local title="IP 地址变化" + fi + content="${content}${markdown_linefeed}---${markdown_linefeed}####IPv6 地址变化${markdown_linefeed}${markdown_tab}当前 IPv6:${IPv6}" + else + echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】获取 IPv6 地址失败" >> ${logfile} + fi + fi +# 路由器已经重启 +else + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}路由器已经重启!" >> ${logfile} + if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] ;then echo "`date "+%Y-%m-%d %H:%M:%S"` 当前IP: ${IPv4}" >> ${logfile};fi + if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] ;then echo "`date "+%Y-%m-%d %H:%M:%S"` 当前IPv6: ${IPv6}" >> ${logfile};fi + echo IPv4 $IPv4 > ${dir}ip + echo -e IPv6 $IPv6 >> ${dir}ip + title="路由器重新启动" + content="${content}${markdown_linefeed}---${markdown_linefeed}####路由器重新启动" + if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] ;then content="${content}${markdown_linefeed}${markdown_tab}当前IP:${IPv4}";fi + if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] ;then content="${content}${markdown_linefeed}${markdown_tab}当前IPv6:${IPv6}";fi +fi +# 如果启用了ddns,启动它 +if [ ! -z "$content" ] ;then ddns_enabled=$(uci show ddns | grep "enabled" | grep "1") if [ -z "$ddns_enabled" ] ; then ddns_logrow=0 ; else ddns_logrow=$(echo "$ddns_enabled" | wc -l);fi - if [ $ddns_logrow -ge 1 ] ;then /etc/init.d/ddns start;fi + if [ $ddns_logrow -ge 1 ] ;then /etc/init.d/ddns start >/dev/null 2>&1;fi +fi } # 检测设备上线 @@ -765,22 +753,17 @@ if [ "$1" ] ;then # 如果 ipAddress 文件中存在此ip,则跳过,待断线检测 local ip=`cat ${dir}ipAddress | grep -w $1` if [ -z "$ip" ] ; then - local ip_mac=`getmac $1` - local ip_name=`getname $1 $ip_mac` - - local ip_ms=`echo $( arping -I $( cat /proc/net/arp | grep -w $1 | awk '{print $6}' | grep -v "^$" | sort -u ) -c 10 -f -w 1 $1)` + local ip_ms=`arping -I $( cat /proc/net/arp | grep -w $1 | awk '{print $6}' | grep -v "^$" | sort -u ) -c 10 -f -w 1 $1` 2>/dev/null if ( ! echo ${ip_ms} | grep -q ms );then - local ip_ms=`ping -c 10 -w 1 $1` + local ip_ms=`ping -c 10 -w 1 $1 | grep -v '100% packet loss'` 2>/dev/null if ( ! echo ${ip_ms} | grep -q ms );then enable_detection 1 - local ip_ms=`echo $( arping -I $( cat /proc/net/arp | grep -w $1 | awk '{print $6}' | grep -v "^$" | sort -u ) -c 10 -f -w $up_timeout $1)` + local ip_ms=`arping -I $( cat /proc/net/arp | grep -w $1 | awk '{print $6}' | grep -v "^$" | sort -u ) -c 10 -f -w $up_timeout $1` 2>/dev/null fi fi - # 如果连接成功 if ( echo ${ip_ms} | grep -q ms ); then LockFile lock - local ip_interface=`getinterface $ip_mac` # 刚刚掉线又重连了 if [ -f "${dir}tmp_downlist" ];then local tmp_downip=`cat ${dir}tmp_downlist | grep -w $1 | awk '{print $2}' | grep -v "^$" | sort -u`;fi if [ ! -z "$tmp_downip" ] ; then @@ -789,43 +772,39 @@ if [ "$1" ] ;then # 真·刚上线 else usage down $1 - echo "$1 $ip_mac $ip_name `date +%s` $ip_interface" >> ${dir}ipAddress - # 如果定时任务没有运行 - if [ ! -f "${dir}send_enable.lock" ] ; then - if [ ! -z "$serverchan_up" ] && [ "$serverchan_up" -eq "1" ] ; then - # 如果通过黑白名单验证 - local ip_blackwhite=`blackwhitelist $ip_mac` - if [ ! -z "$ip_blackwhite" ] && [ "$ip_blackwhite" -eq 1 ] ; then - if [ -f "${dir}title" ] ; then local title=`cat ${dir}title` ;fi - if [ -f "${dir}content" ] ; then local content=`cat ${dir}content` ;fi - if [ -z "$title" ] ; then - local title="$ip_name 连接了你的路由器" - local content="%0D%0A%0D%0A---%0D%0A%0D%0A####新设备连接%0D%0A%0D%0A客户端名:%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip_name%0D%0A%0D%0A客户端 IP: %26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$1%0D%0A%0D%0A客户端 MAC:%26ensp%3b%26ensp%3b$ip_mac" - elif ( echo ${title} | grep -q "连接了你的路由器" ) ; then - local title="$ip_name 连接了你的路由器" - local content="%0D%0A%0D%0A---%0D%0A%0D%0A客户端名:%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip_name%0D%0A%0D%0A客户端 IP:%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$1%0D%0A%0D%0A客户端 MAC:%26ensp%3b%26ensp%3b$ip_mac" - else - local title="设备状态变化" - local content="%0D%0A%0D%0A---%0D%0A%0D%0A####新设备连接%0D%0A%0D%0A客户端名:%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip_name%0D%0A%0D%0A客户端 IP:%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$1%0D%0A%0D%0A客户端 MAC:%26ensp%3b%26ensp%3b$ip_mac" - fi - echo "`date "+%Y-%m-%d %H:%M:%S"` 【微信推送】新设备 $ip_name $1 连接了">> ${logfile} - - if [ ! -z "$serverchan_blacklist" ] ; then - local title="你偷偷关注的设备上线了" - fi - - echo $title >${dir}title - echo -n $content >>${dir}content - fi + local ip_mac=`getmac $1` + local ip_name=`getname $1 ${ip_mac}` + local ip_interface=`getinterface ${ip_mac}` + local ip_blackwhite=`blackwhitelist ${ip_mac}` + echo "$1 ${ip_mac} ${ip_name} `date +%s` ${ip_interface}" >> ${dir}ipAddress + # 如果定时任务没有运行 && 如果通过黑白名单验证 + if [ ! -f "${dir}send_enable.lock" ] && [ ! -z "$serverchan_up" ] && [ "$serverchan_up" -eq "1" ] && [ ! -z "$ip_blackwhite" ] && [ "$ip_blackwhite" -eq 1 ] ; then + if [ -f "${dir}title" ] ; then local title=`cat ${dir}title` ;fi + if [ -f "${dir}content" ] ; then local content=`cat ${dir}content` ;fi + if [ -z "$title" ] ; then + local title="$ip_name 连接了你的路由器" + local content="${markdown_linefeed}---${markdown_linefeed}####新设备连接${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${1}${markdown_linefeed}${markdown_tab}客户端MAC:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}" + elif ( echo ${title} | grep -q "连接了你的路由器" ) ; then + local title="${ip_name} ${title}" + local content="${markdown_linefeed}---${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${1}${markdown_linefeed}${markdown_tab}客户端MAC:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}" + else + local title="设备状态变化" + local content="${markdown_linefeed}---${markdown_linefeed}####新设备连接${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${1}${markdown_linefeed}${markdown_tab}客户端MAC:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}" fi - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 新设备 $ip_name $1 连接了" >> ${logfile};fi + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}新设备 ${ip_name} ${1} 连接了">> ${logfile} + if [ ! -z "$serverchan_blacklist" ] ; then + local title="你偷偷关注的设备上线了" + fi + if [ ! -z "$title" ] ; then echo "$title" >${dir}title;fi + if [ ! -z "$content" ] ; then echo -n "$content" >>${dir}content;fi fi fi - LockFile unlock - else - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 设备 $ip_name $1 连接失败" >> ${logfile};fi + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 新设备 ${ip_name} ${1} 连接了" >> ${logfile};fi fi fi + LockFile unlock +else + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 设备 ${ip_name} ${1} 连接失败" >> ${logfile};fi fi } @@ -834,30 +813,28 @@ function down(){ if [ ! -f "${dir}ipAddress" ] ; then return;fi if [ "$1" ] ;then local ip_mac=`getmac $1` - local ip_name=`getname $1 $ip_mac` - local ip_interface=`getinterface $ip_mac` - - local wlan_online=`iw dev $ip_interface station dump | grep -i -w $ip_mac | grep Station 2>/dev/null` - local ip_ms=`echo $( arping -I $( cat /proc/net/arp | grep -w $1 | awk '{print $6}' | grep -v "^$" | sort -u ) -c 20 -f -w 2 $1)` + local ip_name=`getname $1 ${ip_mac}` + local ip_interface=`getinterface ${ip_mac}` + local wlan_online=`iw dev ${ip_interface} station dump | grep -i -w ${ip_mac} | grep Station` 2>/dev/null + local ip_ms=`arping -I $( cat /proc/net/arp | grep -w $1 | awk '{print $6}' | grep -v "^$" | sort -u ) -c 20 -f -w 2 $1` 2>/dev/null if [ -z "$wlan_online" ] && ( ! echo ${ip_ms} | grep -q ms );then - local ip_ms=`ping -c 5 -w 3 $1` + local ip_ms=`ping -c 5 -w 3 $1 | grep -v '100% packet loss'` 2>/dev/null if ( ! echo ${ip_ms} | grep -q ms );then enable_detection 1 - local ip_ms=`echo $( arping -I $( cat /proc/net/arp | grep -w $1 | awk '{print $6}' | grep -v "^$" | sort -u ) -c 20 -f -w $down_timeout $1)` + local ip_ms=`arping -I $( cat /proc/net/arp | grep -w $1 | awk '{print $6}' | grep -v "^$" | sort -u ) -c 20 -f -w $down_timeout $1` 2>/dev/null fi fi - # 如果连接成功 if [ ! -z "$wlan_online" ] || ( echo ${ip_ms} | grep -q ms ); then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 设备 $ip_name $1 依然坚挺" >> ${logfile};fi - # 如果连接失败 + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 设备 ${ip_name} ${1} 依然坚挺" >> ${logfile};fi + # 如果连接失败 else LockFile lock usage down $1 # 如果定时任务没有运行 if [ ! -f "${dir}send_enable.lock" ] && [ ! -z "$ip_ms" ] ; then # 先放到缓存区 - echo "$1 $ip_mac $ip_name `cat ${dir}ipAddress | grep -w $1 |awk '{print $4}' | grep -v "^$" | sort -u` $ip_interface" >> ${dir}tmp_downlist + echo "$1 ${ip_mac} ${ip_name} `cat ${dir}ipAddress | grep -w $1 |awk '{print $4}' | grep -v "^$" | sort -u` ${ip_interface}" >> ${dir}tmp_downlist fi sed -i "/$1/d" ${dir}ipAddress LockFile unlock @@ -870,39 +847,27 @@ function down_send(){ if [ ! -f "${dir}tmp_downlist" ] ; then return;fi local IPLIST=$( cat ${dir}tmp_downlist | awk '{print $1}' ) for ip in $IPLIST; do - if [ ! -z "$ip" ] ; then - local ip_mac=$( cat ${dir}tmp_downlist | grep -w $ip |awk '{print $2}' | grep -v "^$" | sort -u ) - local ip_name=$( cat ${dir}tmp_downlist | grep -w $ip |awk '{print $3}' | grep -v "^$" | sort -u ) - local ip_uptime=$( cat ${dir}tmp_downlist | grep -w $ip |awk '{print $4}' | grep -v "^$" | sort -u ) - local ip_interface=$( cat ${dir}tmp_downlist | grep -w $ip |awk '{print $5}' | grep -v "^$" | sort -u ) - - if [ ! -z "$serverchan_down" ] && [ "$serverchan_down" -eq "1" ] ; then - # 如果通过黑白名单验证 - local ip_blackwhite=`blackwhitelist $ip_mac` - if [ ! -z "$ip_blackwhite" ] && [ "$ip_blackwhite" -eq 1 ] ; then - if [ -f "${dir}title" ] ; then local title=`cat ${dir}title` ;fi - if [ -f "${dir}content" ] ; then local content=`cat ${dir}content` ;fi - local ip_total=`usage get $ip` && if [ ! -z "$ip_total" ] ; then local ip_total="%0D%0A%0D%0A总计流量: %26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip_total";fi - local time_down=`date +%s` - local time1=$(($time_down-$ip_uptime)) - local time1=`time_for_humans $time1` - if [ -z "$title" ] ; then - local title="$ip_name 断开连接" - local content="%0D%0A%0D%0A---%0D%0A%0D%0A####设备断开连接%0D%0A%0D%0A客户端名:%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip_name%0D%0A%0D%0A客户端 IP: %26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip%0D%0A%0D%0A客户端 MAC:%26ensp%3b%26ensp%3b$ip_mac$ip_total%0D%0A%0D%在线时间: %26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$time1" - elif ( echo ${title} | grep -q "断开连接" ) ; then - local title="$ip_name 断开连接" - local content="%0D%0A%0D%0A---%0D%0A%0D%0A客户端名:%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip_name%0D%0A%0D%0A客户端 IP: %26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip%0D%0A%0D%0A客户端 MAC:%26ensp%3b%26ensp%3b$ip_mac$ip_total%0D%0A%0D%0A在线时间: %26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$time1" - else - local title="设备状态变化" - local content="%0D%0A%0D%0A---%0D%0A%0D%0A####设备断开连接%0D%0A%0D%0A客户端名:%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip_name%0D%0A%0D%0A客户端 IP: %26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip%0D%0A%0D%0A客户端 MAC:%26ensp%3b%26ensp%3b$ip_mac$ip_total%0D%0A%0D%0A在线时间: %26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$time1" - fi - - echo "`date "+%Y-%m-%d %H:%M:%S"` 【微信推送】设备 $ip_name $ip 断开连接 " >> ${logfile} - echo $title >${dir}title - echo -n $content >>${dir}content - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 设备 $ip_name $ip 断开连接 " >> ${logfile};fi - fi + local ip_mac=$( cat ${dir}tmp_downlist | grep -w ${ip} |awk '{print $2}' | grep -v "^$" | sort -u ) + local ip_blackwhite=`blackwhitelist ${ip_mac}` + if [ ! -z "$ip" ] && [ ! -z "$serverchan_down" ] && [ "$serverchan_down" -eq "1" ] && [ ! -z "$ip_blackwhite" ] && [ "$ip_blackwhite" -eq 1 ] ; then + local ip_name=$( cat ${dir}tmp_downlist | grep -w ${ip} |awk '{print $3}' | grep -v "^$" | sort -u ) + local ip_uptime=$( cat ${dir}tmp_downlist | grep -w ${ip} |awk '{print $4}' | grep -v "^$" | sort -u ) + local ip_total=`usage get $ip` && if [ ! -z "$ip_total" ] ; then local ip_total="${markdown_linefeed}${markdown_tab}总计流量: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_total}";fi + local time_down=`date +%s` + local time1=$(($time_down-$ip_uptime)) + local time1=`time_for_humans ${time1}` + if [ -z "$title" ] ; then + title="${ip_name} 断开连接" + content="${content}${markdown_linefeed}---${markdown_linefeed}####设备断开连接${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip}${markdown_linefeed}${markdown_tab}客户端MAC:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}$ip_total${markdown_linefeed}${markdown_tab}在线时间: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${time1}" + elif ( echo ${title} | grep -q "断开连接" ) ; then + title="${ip_name} ${title}" + content="${content}${markdown_linefeed}---${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip}${markdown_linefeed}${markdown_tab}客户端MAC:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}$ip_total${markdown_linefeed}${markdown_tab}在线时间: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${time1}" + else + title="设备状态变化" + content="${content}${markdown_linefeed}---${markdown_linefeed}####设备断开连接${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip}${markdown_linefeed}${markdown_tab}客户端MAC:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}$ip_total${markdown_linefeed}${markdown_tab}在线时间: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${time1}" fi + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}设备 ${ip_name} ${ip} 断开连接 " >> ${logfile} + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 设备 ${ip_name} ${ip} 断开连接 " >> ${logfile};fi fi done rm -f ${dir}tmp_downlist >/dev/null 2>&1 @@ -910,44 +875,45 @@ rm -f ${dir}tmp_downlist >/dev/null 2>&1 # 当前设备列表 function current_device(){ - local content=`cat ${dir}content` - if [ -f ${dir}usage.db ] ; then local ip_total_db="总计流量%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b";fi - content="%0D%0A%0D%0A---%0D%0A%0D%0A####现有在线设备 `grep -c "" ${dir}ipAddress` 台,具体如下%0D%0A%0D%0A#####IP 地址%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b%26ensp%3b$ip_total_db客户端名" + if [ -f ${dir}usage.db ] ; then local ip_total_db="总计流量${markdown_space}${markdown_space}${markdown_space}${markdown_space}";fi + local ipAddress_grepc=`grep -c "" ${dir}ipAddress` + content="${content}${markdown_linefeed}---${markdown_linefeed}####现有在线设备 ${ipAddress_grepc} 台,具体如下${markdown_linefeed}${markdown_tab}IP 地址${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_total_db}客户端名" local IPLIST=$( cat ${dir}ipAddress | awk '{print $1}' ) for ip in $IPLIST; do - local ip_total=`usage get $ip` - local ip_mac=`getmac $ip` - local ip_name=`getname $ip $ip_mac` - if [ "${#ip_name}" -gt "18" ] ; then local ip_name=`echo ${ip_name:0:18}"*"`;fi - n=`expr 16 - ${#ip}` - for i in `seq 1 $n` - do - local ip=$ip"%26ensp%3b" - done - unset i && unset n - - if [ ! -z "$ip_total" ] ; then - n=`expr 12 - ${#ip_total}` + local ip_total=`usage get ${ip}` + local ip_mac=`getmac ${ip}` + local ip_name=`getname ${ip} ${ip_mac}` + if [ `length_str "$ip_name"` -gt "15" ] ; then + local ip_name=`echo ${ip_name:0:15}"*"` + fi + if [ "${#ip}" -lt "15" ] ; then + n=`expr 15 - ${#ip}` for i in `seq 1 $n` do - local ip_total=$ip_total"%26ensp%3b" + local ip="${ip}${markdown_space}" + done + unset i n + fi + if [ ! -z "$ip_total" ] ; then + n=`expr 11 - ${#ip_total}` + for i in `seq 1 $n` + do + local ip_total="${ip_total}${markdown_space}" done fi - unset i && unset n - - local content=${content}"%0D%0A%0D%0A$ip$ip_total$ip_name" + content="${content}${markdown_linefeed}${markdown_tab}${ip}${ip_total}${ip_name}" + unset i n ip_total ip_mac ip_name done - echo -n $content >>${dir}content } # 检测 cpu 状态 function cpu_load(){ - local cpu_Alert=0 + local cpu_alert=0 # 负载状态报警 if [ ! -z "$cpuload_enable" ] && [ "$cpuload_enable" -eq "1" ] ; then if [ -z "$cpuload_time" ] ; then cpuload_time=`date +%s`;fi if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【负载】正在检测 CPU 负载" >> ${logfile};fi - local cpu_fuzai=`cat /proc/loadavg | awk '{print $1}' 2>/dev/null` + local cpu_fuzai=`cat /proc/loadavg | awk '{print $1}'` 2>/dev/null local cpuload=`get_config cpuload` if [ ! -z "$cpu_fuzai" ] && [ ! -z "$cpuload" ] && [ `expr $cpu_fuzai \> $cpuload` -eq "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!警报!!】 CPU 负载过高: ${cpu_fuzai}" >> ${logfile} @@ -955,27 +921,22 @@ function cpu_load(){ echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取设备负载,请检查命令" >> ${logfile} # 一小时 CD 中 elif [ ! -z "$cpucd_time" ] ; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` CPU 负载正常:$cpu_fuzai" >> ${logfile};fi + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` CPU 负载正常:${cpu_fuzai}" >> ${logfile};fi else cpuload_time=`date +%s` - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` CPU 负载正常:$cpu_fuzai" >> ${logfile};fi + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` CPU 负载正常:${cpu_fuzai}" >> ${logfile};fi fi if [ "$((`date +%s`-$cpuload_time))" -ge "300" ] && [ -z "$cpucd_time" ] ; then - if [ -f "${dir}title" ] ; then local title=`cat ${dir}title` ;fi - if [ -f "${dir}content" ] ; then local content=`cat ${dir}content` ;fi - local title="CPU 负载过高!" - local cpu_Alert=1 + title="CPU 负载过高!" + local cpu_alert=1 if [ -z "$cpucd_time" ] ; then cpucd_time=1;fi - echo "`date "+%Y-%m-%d %H:%M:%S"` 【微信推送】 CPU 负 载过高: ${cpu_fuzai}" >> ${logfile} - local content="%0D%0A%0D%0A---%0D%0A%0D%0A""####CPU 负载过高%0D%0A%0D%0A>CPU 负载已连续五分钟超过预设%0D%0A%0D%0A>接下来一小时不再提示%0D%0A%0D%0A>当前负载:${cpu_fuzai}" - echo $title >${dir}title - echo -n $content >>${dir}content + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text} CPU 负 载过高: ${cpu_fuzai}" >> ${logfile} + content="${content}${markdown_linefeed}---${markdown_linefeed}####CPU 负载过高${markdown_linefeed}${markdown_tab}CPU 负载已连续五分钟超过预设${markdown_linefeed}${markdown_tab}接下来一小时不再提示${markdown_linefeed}${markdown_tab}当前负载:${cpu_fuzai}" elif [ "$((`date +%s`-$cpuload_time))" -ge "3300" ] ;then unset cpucd_time fi fi - # 温度状态报警 if [ ! -z "$temperature_enable" ] && [ "$temperature_enable" -eq "1" ] ; then if [ -z "$temperature_time" ] ; then temperature_time=`date +%s`;fi @@ -988,53 +949,54 @@ function cpu_load(){ echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取设备温度,请检查命令" >> ${logfile} # 一小时 CD 中 elif [ ! -z "$temperaturecd_time" ] ; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` CPU 温度正常:$cpu_wendu" >> ${logfile};fi + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` CPU 温度正常:${cpu_wendu}" >> ${logfile};fi else temperature_time=`date +%s` - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` CPU 温度正常:$cpu_wendu" >> ${logfile};fi + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` CPU 温度正常:${cpu_wendu}" >> ${logfile};fi fi if [ "$((`date +%s`-$temperature_time))" -ge "300" ] && [ -z "$temperaturecd_time" ] ; then - if [ -f "${dir}title" ] ; then local title=`cat ${dir}title` ;fi - if [ -f "${dir}content" ] ; then local content=`cat ${dir}content` ;fi if [ ! -z "$title" ] && ( echo ${title} | grep -q "过高" ) ; then - local title="设备报警!" + title="设备报警!" else - local title="CPU 温度过高!" + title="CPU 温度过高!" fi - local cpu_Alert=1 + local cpu_alert=1 if [ -z "$temperaturecd_time" ] ; then temperaturecd_time=1;fi - echo "`date "+%Y-%m-%d %H:%M:%S"` 【微信推送】 CPU 温 度过高: ${cpu_wendu}" >> ${logfile} - local content="%0D%0A%0D%0A---%0D%0A%0D%0A""####CPU 温度过高%0D%0A%0D%0A>CPU 温度已连续五分钟超过预设%0D%0A%0D%0A>接下来一小时不再提示%0D%0A%0D%0A>当前温度:${cpu_wendu}" - echo $title >${dir}title - echo -n $content >>${dir}content + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text} CPU 温 度过高: ${cpu_wendu}" >> ${logfile} + content="${content}${markdown_linefeed}---${markdown_linefeed}####CPU 温度过高${markdown_linefeed}${markdown_tab}CPU 温度已连续五分钟超过预设${markdown_linefeed}${markdown_tab}接下来一小时不再提示${markdown_linefeed}${markdown_tab}当前温度:${cpu_wendu}" elif [ "$((`date +%s`-$temperature_time))" -ge "3300" ] ;then unset temperaturecd_time fi fi - - if [ ! -z "$cpu_Alert" ] && [ "$cpu_Alert" -eq "1" ] ; then + # 占用前三的进程 + if [ ! -z "$cpu_alert" ] && [ "$cpu_alert" -eq "1" ] ; then top -bn 1 >${dir}top - local content="%0D%0A%0D%0A---%0D%0A%0D%0A""####当前 CPU 占用前三的进程" + content="${content}${markdown_linefeed}---${markdown_linefeed}####当前 CPU 占用前三的进程" local i=1 && local top_i=5 while [ $i -le 3 ] do if ( cat ${dir}top | awk 'NR=='$top_i | grep -q "top -bn 1" ) ; then echo "" >/dev/null else - local content=${content}"%0D%0A%0D%0A>`cat ${dir}top | awk 'NR=='$top_i | awk '{print $8" "$7}'`" + local top_name=`cat ${dir}top | awk 'NR=='$top_i | awk '{print $8}'` + if [ "$top_name" == "/bin/sh" ] || [ "$top_name" == "/bin/bash" ] ; then local top_name=`cat ${dir}top | awk 'NR=='$top_i | awk '{print $9}'`;fi + local top_load=`cat ${dir}top | awk 'NR=='$top_i | awk '{print $7}'` + local temp_top="$top_name $top_load" + content="${content}${markdown_linefeed}${markdown_tab}${temp_top}" local i=`expr $i + 1` fi local top_i=`expr $top_i + 1` done - echo -n $content >>${dir}content rm -f ${dir}top >/dev/null 2>&1 fi } # 发送定时数据 function send(){ - echo "`date "+%Y-%m-%d %H:%M:%S"` 【定时数据】创建定时任务" >> ${logfile} + echo "`date "+%Y-%m-%d %H:%M:%S"` 【定时数据】创建定时任务" >> ${logfile} + serverchan_disturb + disturb=$? local send_title=`get_config send_title` local router_status=`get_config router_status` local client_list=`get_config client_list` @@ -1046,69 +1008,75 @@ function send(){ local send_hostIPv6=`getip hostipv6` if [ -z "$send_title" ] ; then local send_title="路由状态:";fi if [ ! -z "$client_list" ] && [ "$client_list" -eq "1" ] ; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【定时数据】检测客户端列表" >> ${logfile};fi > ${dir}send_enable.lock serverchan_first & fi - if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ] ; then local 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 local 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 local send_content=${send_content}"%0D%0A%0D%0A"" CPU占用:`getcpu`";fi - if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ] ; then local 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 local 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 - if [ ! -z "$router_temp" ] && [ "$router_temp" -eq "1" ] ; then local send_content=${send_content}"%0D%0A%0D%0A""---%0D%0A%0D%0A#### 设备温度%0D%0A%0D%0A CPU:`soc_temp`%e2%84%83";fi + # 系统运行状态 + if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ] ; then + local systemload=`cat /proc/loadavg | awk '{print $1" "$2" "$3}'` + local cpuload=`getcpu` + local ramload=`free -m | sed -n '2p' | awk '{print""($3/$2)*100"%"}'` + local systemstatustime=`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)}'` + local send_content="${send_content}${markdown_linefeed}---${markdown_linefeed}#### 系统运行状态" + local send_content="${send_content}${markdown_linefeed}${markdown_tab}平均负载:${systemload}" + local send_content="${send_content}${markdown_linefeed}${markdown_tab}CPU占用:${cpuload}" + local send_content="${send_content}${markdown_linefeed}${markdown_tab}内存占用:${ramload}" + local send_content="${send_content}${markdown_linefeed}${markdown_tab}${systemstatustime}" + fi + # 设备温度 + if [ ! -z "$router_temp" ] && [ "$router_temp" -eq "1" ] ; then + local cputemp=`soc_temp` + local send_content="${send_content}${markdown_linefeed}---${markdown_linefeed}#### 设备温度${markdown_linefeed}${markdown_tab}CPU:${cputemp}%e2%84%83" #%e2%84%83 ℃ + fi + # WAN 口信息 if [ ! -z "$router_wan" ] && [ "$router_wan" -eq "1" ] ; then - local send_content=${send_content}"%0D%0A%0D%0A""---%0D%0A%0D%0A#### WAN 口信息%0D%0A%0D%0A 接口ip:$send_wanIP" - local send_content=${send_content}"%0D%0A%0D%0A"" 外网ip:$send_hostIP" + local send_content="${send_content}${markdown_linefeed}---${markdown_linefeed}#### WAN 口信息${markdown_linefeed}${markdown_tab}接口ip:${send_wanIP}" + local send_content="${send_content}${markdown_linefeed}${markdown_tab}外网ip:${send_hostIP}" if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] ; then - local send_content=${send_content}"%0D%0A%0D%0A"" ipv6 :$send_wanIPv6" - local send_content=${send_content}"%0D%0A%0D%0A"" 外网v6:$send_hostIPv6" + local send_content="${send_content}${markdown_linefeed}${markdown_tab}ipv6 :${send_wanIPv6}" + local send_content="${send_content}${markdown_linefeed}${markdown_tab}外网v6:${send_hostIPv6}" fi if [ ! -z "$send_content" ] && [ ! -z "$router_wan" ] ; then - if ( ! echo ${send_wanIP} | grep -q -w ${send_hostIP} );then local send_content=${send_content}"%0D%0A%0D%0A"" 外网 ip 与接口 ip 不一致,你的 ip 不是公网 ip";fi + if ( ! echo ${send_wanIP} | grep -q -w ${send_hostIP} );then local send_content="${send_content}${markdown_linefeed}${markdown_tab}外网 ip 与接口 ip 不一致,你的 ip 不是公网 ip";fi else - local send_content=${send_content}"%0D%0A%0D%0A"" ip 获取失败" + local send_content="${send_content}${markdown_linefeed}${markdown_tab}ip 获取失败" fi - local 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)}'`" + local wanstatustime=`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)}'` + local send_content="${send_content}${markdown_linefeed}${markdown_tab}${wanstatustime}" fi + # 在线设备 if [ ! -z "$client_list" ] && [ "$client_list" -eq "1" ] ; then wait - #if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【定时数据】检测客户端列表" >> ${logfile};fi - #serverchan_first - local send_content=${send_content}"%0D%0A%0D%0A---%0D%0A%0D%0A#### 在线设备%0D%0A%0D%0A"; + local send_content="${send_content}${markdown_linefeed}---${markdown_linefeed}#### 在线设备" local IPLIST=$( cat ${dir}ipAddress | awk '{print $1}' ) for ip in $IPLIST; do - local ip_total=`usage get $ip` && if [ ! -z "$ip_total" ] ; then local ip_total="总计流量:$ip_total ";fi + local ip_total=`usage get ${ip}` && if [ ! -z "$ip_total" ] ; then local ip_total="总计流量:${ip_total} ";fi local time_down=`date +%s` - local time_up=$( cat ${dir}ipAddress | grep -w $ip |awk '{print $4}' | grep -v "^$" | sort -u ) + local time_up=$( cat ${dir}ipAddress | grep -w ${ip} |awk '{print $4}' | grep -v "^$" | sort -u ) local time1=$(($time_down-$time_up)) - local time1=`time_for_humans $time1` - local ip_mac=`getmac $ip` - local ip_name=`getname $ip $ip_mac` - if [ "${#ip_name}" -gt "20" ] ; then local ip_name=`echo ${ip_name:0:20}"*"`;fi - local send_content=${send_content}"%0D%0A%0D%0A $ip_name $ip%0D%0A%0D%0A $ip_total在线 $time1%0D%0A%0D%0A ----" + local time1=`time_for_humans ${time1}` + local ip_mac=`getmac ${ip}` + local ip_name=`getname ${ip} ${ip_mac}` + if [ `length_str "$ip_name"` -gt "20" ] ; then local ip_name=`echo ${ip_name:0:20}"*"`;fi + local send_content="${send_content}${markdown_linefeed}${markdown_tab}【${ip_name}】 ${ip}${markdown_linefeed}${markdown_tab}${ip_total}在线 ${time1}" + unset ip_total time_down time_up time1 ip_mac ip_name done - unset ip && unset IPLIST fi - - if [ ! -z "$device_name" ] ; then local send_title="【$device_name】$send_title" ;fi - local send_title=$( echo $send_title | sed $'s/\ /%20/g'| sed $'s/\"/%22/g'| sed $'s/\#/%23/g'| sed $'s/\&/%26/g'| sed $'s/\,/%2C/g'| sed $'s/\//%2F/g'| sed $'s/\:/%3A/g'| sed $'s/\;/%3B/g'| sed $'s/\=/%3D/g'| sed $'s/\@/%40/g') - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【定时数据】进行 URL 编码,标题:$send_title" >> ${logfile};fi + if [ ! -z "$device_name" ] ; then local send_title="【$device_name】${send_title}" ;fi + local send_title=$( echo ${send_title} | sed $'s/\ /%20/g'| sed $'s/\"/%22/g'| sed $'s/\#/%23/g'| sed $'s/\&/%26/g'| sed $'s/\,/%2C/g'| sed $'s/\//%2F/g'| sed $'s/\:/%3A/g'| sed $'s/\;/%3B/g'| sed $'s/\=/%3D/g'| sed $'s/\@/%40/g') + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【定时数据】进行 URL 编码,标题:${send_title}" >> ${logfile};fi if [ -z "$send_content" ] ; then - local send_content="%0D%0A%0D%0A---%0D%0A%0D%0A#### 我遇到了一个难题%0D%0A%0D%0A>你没有选择需要发送的项目,该怎么办呢%0D%0A%0D%0A---"; - echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】定时发送选项错误,请检查你的设置" >> ${logfile} + local send_content="${markdown_linefeed}---${markdown_linefeed}#### 我遇到了一个难题${markdown_linefeed}${markdown_tab}定时发送选项错误,你没有选择需要发送的项目,该怎么办呢${markdown_linefeed}---${markdown_linefeed}"; fi - local nowtime=`date "+%Y-%m-%d %H:%M:%S"` - serverchan_disturb - if [ $? -eq "0" ] ;then - echo "`date "+%Y-%m-%d %H:%M:%S"` 【免打扰】当前为免打扰时间,消息不发送" >> ${logfile} - else - curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=${send_title}" -d "&desp=${nowtime}%0D%0A%0D%0A${send_content}" >/dev/null - echo "`date "+%Y-%m-%d %H:%M:%S"` 【定时数据】微信推送" >> ${logfile} + if [ "$disturb" -eq "1" ] ;then + curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=${send_title}" -d "&desp=${nowtime}${markdown_linefeed}${send_content}" >/dev/null + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}定时推送任务完成" >> ${logfile} fi deltemp } +# 初始化 read_config deltemp serverchan_cron @@ -1126,12 +1094,11 @@ unset i # 启动参数 if [ "$1" ] ;then - if [ $1 = "send" ] ;then send;fi - exit + if [ $1 = "send" ] ;then send && exit;fi + if [ $1 = "soc" ] ;then echo `soc_temp` > /tmp/serverchan/soc_tmp && exit;fi fi -# 初始化 - +# 载入在线设备 serverchan_init echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】载入在线设备" >> ${logfile} > ${dir}send_enable.lock && serverchan_first && deltemp @@ -1142,8 +1109,9 @@ while [ "$serverchan_enable" -eq "1" ] ; do deltemp usage update - #read_config - + serverchan_disturb + disturb=$? + # 外网IP变化检测 if [ ! -z "$serverchan_ipv4" ] && [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv4" -ne "0" ] || [ "$serverchan_ipv6" -ne "0" ] ; then rand_geturl @@ -1152,12 +1120,20 @@ do # 设备列表 if [ ! -f "${dir}send_enable.lock" ] ; then + if [ ! -z "$title" ] ; then echo "$title" > ${dir}title;fi + if [ ! -z "$content" ] ; then echo "$content" > ${dir}content;fi serverchan_first + if [ -f "${dir}title" ] ; then title=`cat ${dir}title` && rm -f ${dir}title >/dev/null 2>&1;fi + if [ -f "${dir}content" ] ; then content=`cat ${dir}content` && rm -f ${dir}content >/dev/null 2>&1;fi + fi + + # 离线缓存区推送 + if [ ! -f "${dir}send_enable.lock" ] ; then down_send fi # 当前设备列表 - if [ -f "${dir}content" ] && [ ! -f "${dir}send_enable.lock" ] ; then + if [ ! -z "$content" ] && [ ! -f "${dir}send_enable.lock" ] ; then current_device fi @@ -1170,20 +1146,14 @@ do if [ ! -f "${dir}send_enable.lock" ] ; then cpu_load fi - - serverchan_disturb - disturb=$? - if [ ! -f "${dir}send_enable.lock" ] && [ -f "${dir}title" ] && [ -f "${dir}content" ] ; then - title=`cat ${dir}title` - content=`cat ${dir}content` + + if [ ! -f "${dir}send_enable.lock" ] && [ ! -z "$title" ] && [ ! -z "$content" ] ; then nowtime=`date "+%Y-%m-%d %H:%M:%S"` if [ ! -z "$device_name" ] ; then title="【$device_name】$title" ;fi - title=$( echo $title | sed $'s/\ /%20/g'| sed $'s/\"/%22/g'| sed $'s/\#/%23/g'| sed $'s/\&/%26/g'| sed $'s/\,/%2C/g'| sed $'s/\//%2F/g'| sed $'s/\:/%3A/g'| sed $'s/\;/%3B/g'| sed $'s/\=/%3D/g'| sed $'s/\@/%40/g') - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 进行 URL 编码,标题=$title" >> ${logfile};fi - if [ $disturb -eq "0" ] ;then - echo "`date "+%Y-%m-%d %H:%M:%S"` 【免打扰】当前为免打扰时间,消息不发送" >> ${logfile} - else - curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=${title}" -d "&desp=${nowtime}%0D%0A%0D%0A${content}" >/dev/null + title=$( echo "$title" | sed $'s/\ /%20/g'| sed $'s/\"/%22/g'| sed $'s/\#/%23/g'| sed $'s/\&/%26/g'| sed $'s/\,/%2C/g'| sed $'s/\//%2F/g'| sed $'s/\:/%3A/g'| sed $'s/\;/%3B/g'| sed $'s/\=/%3D/g'| sed $'s/\@/%40/g') + if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 进行 URL 编码,标题=${title}" >> ${logfile};fi + if [ $disturb -eq "1" ] ;then + curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=${title}" -d "desp=${nowtime}${markdown_linefeed}${content}" >/dev/null fi fi @@ -1192,9 +1162,9 @@ do while [ -f "${dir}send_enable.lock" ] ; do if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] && [ "$send_str" -eq "0" ] ; then - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "-----------------------------------------------------" >> ${logfile};fi - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 发送定时数据,程序暂停中" >> ${logfile};fi - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo ""-----------------------------------------------------"" >> ${logfile};fi + echo "-----------------------------------------------------" >> ${logfile} + echo "`date "+%Y-%m-%d %H:%M:%S"` 发送定时数据,程序暂停中" >> ${logfile} + echo ""-----------------------------------------------------"" >> ${logfile} send_str=1 fi sleep $sleeptime @@ -1202,7 +1172,6 @@ do done continue fi - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【等待】我累了,$sleeptime 秒后再回来" >> ${logfile};fi sleep $sleeptime continue done \ No newline at end of file