From 80d18a0f7ea161b93f64fbd47c535eee93c4690d Mon Sep 17 00:00:00 2001 From: CN_SZTL Date: Sun, 19 Apr 2020 20:03:12 +0800 Subject: [PATCH] luci-app-serverchan: bump to v1.77 --- package/ctcgfw/luci-app-serverchan/Makefile | 8 +- .../luasrc/controller/serverchan.lua | 38 +- .../luasrc/model/cbi/serverchan.lua | 373 ----- .../luasrc/model/cbi/serverchan/advanced.lua | 112 ++ .../luasrc/model/cbi/serverchan/log.lua | 5 + .../luasrc/model/cbi/serverchan/setting.lua | 297 ++++ .../luasrc/view/serverchan/log.htm | 32 + .../{serverchan_status.htm => status.htm} | 0 .../root/etc/init.d/serverchan | 14 +- .../root/etc/uci-defaults/luci-serverchan | 11 + .../root/usr/bin/serverchan/serverchan | 1336 +++++++---------- 11 files changed, 1007 insertions(+), 1219 deletions(-) delete mode 100644 package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan.lua create mode 100644 package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/advanced.lua create mode 100644 package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/log.lua create mode 100644 package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/setting.lua create mode 100644 package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/log.htm rename package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/{serverchan_status.htm => status.htm} (100%) create mode 100644 package/ctcgfw/luci-app-serverchan/root/etc/uci-defaults/luci-serverchan diff --git a/package/ctcgfw/luci-app-serverchan/Makefile b/package/ctcgfw/luci-app-serverchan/Makefile index 8dc07e15b0..1af9b9d8d7 100644 --- a/package/ctcgfw/luci-app-serverchan/Makefile +++ b/package/ctcgfw/luci-app-serverchan/Makefile @@ -6,8 +6,8 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-serverchan -PKG_VERSION:=1.63 -PKG_RELEASE:=7 +PKG_VERSION:=1.77 +PKG_RELEASE:=8 include $(INCLUDE_DIR)/package.mk @@ -28,12 +28,12 @@ define Package/$(PKG_NAME)/conffiles endef define Package/$(PKG_NAME)/install - $(INSTALL_DIR) $(1)/etc/init.d $(1)/usr/bin/serverchan $(1)/etc/config $(1)/usr/lib/lua/luci + $(INSTALL_DIR) $(1)/etc/init.d $(1)/usr/bin/serverchan $(1)/etc/config $(1)/usr/lib/lua/luci $(1)/etc/uci-defaults $(CP) ./luasrc/* $(1)/usr/lib/lua/luci $(INSTALL_CONF) ./root/etc/config/serverchan $(1)/etc/config $(INSTALL_BIN) ./root/etc/init.d/serverchan $(1)/etc/init.d + $(INSTALL_BIN) ./root/etc/uci-defaults/luci-serverchan $(1)/etc/uci-defaults/luci-serverchan $(INSTALL_BIN) ./root/usr/bin/serverchan/serverchan $(1)/usr/bin/serverchan endef $(eval $(call BuildPackage,$(PKG_NAME))) - diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/controller/serverchan.lua b/package/ctcgfw/luci-app-serverchan/luasrc/controller/serverchan.lua index 36cf8258dc..77dc85bc90 100644 --- a/package/ctcgfw/luci-app-serverchan/luasrc/controller/serverchan.lua +++ b/package/ctcgfw/luci-app-serverchan/luasrc/controller/serverchan.lua @@ -1,15 +1,33 @@ module("luci.controller.serverchan",package.seeall) + function index() -if not nixio.fs.access("/etc/config/serverchan")then -return -end -entry({"admin","services","serverchan"},cbi("serverchan"),_("微信推送"),30).dependent=true -entry({"admin","services","serverchan","status"},call("act_status")).leaf=true + + if not nixio.fs.access("/etc/config/serverchan")then + return + end + + entry({"admin", "services", "serverchan"}, alias("admin", "services", "serverchan", "setting"),_("微信推送"), 30).dependent = true + entry({"admin","services","serverchan","status"},call("act_status")).leaf=true + entry({"admin", "services", "serverchan", "setting"}, cbi("serverchan/setting"),_("配置"), 40).leaf = true + entry({"admin", "services", "serverchan", "advanced"}, cbi("serverchan/advanced"),_("高级设置"), 90).leaf = true + entry({"admin", "services", "serverchan", "log"}, form("serverchan/log"),_("日志"), 99).leaf = true + + entry({"admin", "services", "serverchan", "get_log"}, call("get_log")).leaf = true + entry({"admin", "services", "serverchan", "clear_log"}, call("clear_log")).leaf = true end function act_status() - local e={} - e.running=luci.sys.call("pgrep -f serverchan/serverchan >/dev/null")==0 - luci.http.prepare_content("application/json") - luci.http.write_json(e) -end \ No newline at end of file + local e={} + e.running=luci.sys.call("pgrep -f serverchan/serverchan >/dev/null")==0 + luci.http.prepare_content("application/json") + luci.http.write_json(e) +end + +function get_log() + luci.http.write(luci.sys.exec( + "[ -f '/tmp/serverchan/serverchan.log' ] && cat /tmp/serverchan/serverchan.log")) +end + +function clear_log() + luci.sys.call("echo '' > /tmp/serverchan/serverchan.log") +end diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan.lua b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan.lua deleted file mode 100644 index 75588ebd0f..0000000000 --- a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan.lua +++ /dev/null @@ -1,373 +0,0 @@ -local nt = require "luci.sys".net -local fs=require"nixio.fs" -local e=luci.model.uci.cursor() -local net = require "luci.model.network".init() -local sys = require "luci.sys" -local ifaces = sys.net:devices() - -m=Map("serverchan",translate("ServerChan"), -translate("「Server酱」,英文名「ServerChan」,是一款从服务器推送报警信息和日志到微信的工具。

如果你在使用中遇到问题,请到这里提交:") -.. [[]] -.. translate("github 项目地址") -.. [[]] -) - -m:section(SimpleSection).template = "serverchan/serverchan_status" -s=m:section(NamedSection,"serverchan","serverchan",translate("Server酱设置")) -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 - ---基本设置 -a=s:taboption("tab_basic", Flag,"serverchan_enable",translate("启用")) -a.default=0 -a.rmempty=true - -a=s:taboption("tab_basic", Value,"sckey",translate('SCKEY'), translate("Serverchan Sckey").."
调用代码获取点击这里

") -a.rmempty=true - -device_name=s:taboption("tab_basic", Value,"device_name",translate('本设备名称')) -device_name.rmempty=true -device_name.description = translate("在推送信息标题中会标识本设备名称,用于区分推送信息的来源设备") - -sleeptime=s:taboption("tab_basic", Value,"sleeptime",translate('检测时间间隔')) -sleeptime.default = "60" -sleeptime.description = translate("越短的时间时间响应越及时,但会占用更多的系统资源") - -debuglevel=s:taboption("tab_basic", ListValue,"debuglevel",translate("日志调试等级")) -debuglevel:value("",translate("关闭")) -debuglevel:value("1",translate("简单")) -debuglevel:value("2",translate("调试")) -debuglevel.rmempty = true -debuglevel.optional = true - -device_aliases= s:taboption("tab_basic", DynamicList, "device_aliases", translate("设备别名")) -device_aliases.rmempty = true -device_aliases.optional = true -device_aliases.description = translate("
请输入设备 MAC 和设备别名,用“-”隔开,如:
XX:XX:XX:XX:XX:XX-我的手机") - ---设备状态 -a=s:taboption("tab_basic2", ListValue,"serverchan_ipv4",translate("ipv4 变动通知")) -a.default="disable" -a:value("0",translate("关闭")) -a:value("1",translate("通过接口获取")) -a:value("2",translate("通过URL获取")) -a = s:taboption("tab_basic2", ListValue, "ipv4_interface", translate("接口名称")) -a:depends({serverchan_ipv4="1"}) -for _, iface in ipairs(ifaces) do - if not (iface == "lo" or iface:match("^ifb.*")) then - local nets = net:get_interface(iface) - nets = nets and nets:get_networks() or {} - for k, v in pairs(nets) do - nets[k] = nets[k].sid - end - nets = table.concat(nets, ",") - a:value(iface, ((#nets > 0) and "%s (%s)" % {iface, nets} or iface)) - end -end -a.description = translate("
一般选择 wan 接口,多拨环境请自行选择") -a= s:taboption("tab_basic2", Value, "ipv4_URL", "URL 地址") -a.rmempty = true -a.default = "members.3322.org/dyndns/getip" -a:depends({serverchan_ipv4="2"}) -a.description = translate("
会因服务器稳定性/连接频繁等原因导致获取失败,一般不推荐") - -a=s:taboption("tab_basic2", ListValue,"serverchan_ipv6",translate("ipv6 变动通知")) -a.default="disable" -a:value("0",translate("关闭")) -a:value("1",translate("通过接口获取")) -a:value("2",translate("通过URL获取")) -a = s:taboption("tab_basic2", ListValue, "ipv6_interface", translate("接口名称")) -a:depends({serverchan_ipv6="1"}) -for _, iface in ipairs(ifaces) do - if not (iface == "lo" or iface:match("^ifb.*")) then - local nets = net:get_interface(iface) - nets = nets and nets:get_networks() or {} - for k, v in pairs(nets) do - nets[k] = nets[k].sid - end - nets = table.concat(nets, ",") - a:value(iface, ((#nets > 0) and "%s (%s)" % {iface, nets} or iface)) - end -end -a.description = translate("
一般选择 wan 接口,多拨环境请自行选择") -a= s:taboption("tab_basic2", Value, "ipv6_URL", "URL 地址") -a.rmempty = true -a.default = "v6.ip.zxinc.org/getip" -a:depends({serverchan_ipv6="2"}) -a.description = translate("
会因服务器稳定性/连接频繁等原因导致获取失败,一般不推荐") - -a=s:taboption("tab_basic2", Flag,"serverchan_up",translate("设备上线通知")) -a.default=0 -a.rmempty=true -a=s:taboption("tab_basic2", Flag,"serverchan_down",translate("设备下线通知")) -a.default=0 -a.rmempty=true -a=s:taboption("tab_basic2", Flag,"cpuload_enable",translate("CPU 负载报警")) -a.default=0 -a.rmempty=true -a= s:taboption("tab_basic2", Value, "cpuload", "负载报警阈值") -a.default = "3.0" -a.rmempty = true -a:depends({cpuload_enable="1"}) -a=s:taboption("tab_basic2", Flag,"temperature_enable",translate("CPU 温度报警")) -a.default=0 -a.rmempty=true -a= s:taboption("tab_basic2", Value, "temperature", "温度报警阈值") -a.rmempty = true -a.default = "80" -a.datatype="uinteger" -a:depends({temperature_enable="1"}) -a.description = translate("
设备报警只会在连续五分钟超过设定值时才会推送
而且一个小时内不会再提醒第二次") - ---定时推送 -e=s:taboption("tab_basic3", ListValue,"send_mode",translate("定时任务设定")) -e.default="" -e:value("",translate("关闭")) -e:value("1",translate("定时发送")) -e:value("2",translate("间隔发送")) - -e=s:taboption("tab_basic3", ListValue,"regular_time",translate("发送时间")) -for t=0,23 do -e:value(t,translate("每天"..t.."点")) -end -e.default=8 -e.datatype=uinteger -e:depends("send_mode","1") - -e=s:taboption("tab_basic3", ListValue,"regular_time_2",translate("发送时间")) -e:value("",translate("关闭")) -for t=0,23 do -e:value(t,translate("每天"..t.."点")) -end -e.default="关闭" -e.datatype=uinteger -e:depends("send_mode","1") - -e=s:taboption("tab_basic3", ListValue,"regular_time_3",translate("发送时间")) -e:value("",translate("关闭")) -for t=0,23 do -e:value(t,translate("每天"..t.."点")) -end -e.default="关闭" -e.datatype=uinteger -e:depends("send_mode","1") - -e=s:taboption("tab_basic3", ListValue,"interval_time",translate("发送间隔")) -for t=1,23 do -e:value(t,translate(t.."小时")) -end -e.default=6 -e.datatype=uinteger -e:depends("send_mode","2") -e.description = translate("
从 00:00 开始,每 * 小时发送一次") - -title= s:taboption("tab_basic3", Value, "send_title", translate("微信推送标题")) -title:depends("send_mode","1") -title:depends("send_mode","2") -title.placeholder = "OpenWrt By tty228 路由状态:" -title.optional = true -title.description = translate("
使用特殊符号可能会造成发送失败") - -router_status=s:taboption("tab_basic3", Flag,"router_status",translate("系统运行情况")) -router_status:depends("send_mode","1") -router_status:depends("send_mode","2") - -router_temp=s:taboption("tab_basic3", Flag,"router_temp",translate("设备温度")) -router_temp:depends("send_mode","1") -router_temp:depends("send_mode","2") - -router_wan=s:taboption("tab_basic3", Flag,"router_wan",translate("WAN信息")) -router_wan:depends("send_mode","1") -router_wan:depends("send_mode","2") - -client_list=s:taboption("tab_basic3", Flag,"client_list",translate("客户端列表")) -client_list:depends("send_mode","1") -client_list:depends("send_mode","2") - -e=s:taboption("tab_basic3", Button,"_add",translate("手动发送")) -e.inputtitle=translate("发送") -e:depends("send_mode","1") -e:depends("send_mode","2") -e.inputstyle = "apply" -function e.write(self, section) - -luci.sys.call("cbi.apply") - luci.sys.call("/usr/bin/serverchan/serverchan send &") -end - ---免打扰 -sheep=s:taboption("tab_basic4", ListValue,"serverchan_sheep",translate("免打扰时段设置"),translate("在指定整点时间段内,暂停推送消息
免打扰时间中,定时推送也会被阻止。")) -sheep:value("0",translate("关闭")) -sheep:value("1",translate("模式一:脚本挂起,延迟发送")) -sheep:value("2",translate("模式二:静默模式,不发送任何信息")) -sheep.rmempty = true -sheep.optional = true -sheep=s:taboption("tab_basic4", ListValue,"starttime",translate("免打扰开始时间")) -for t=0,23 do -sheep:value(t,translate("每天"..t.."点")) -end -sheep.default=0 -sheep.datatype=uinteger -sheep:depends({serverchan_sheep="1"}) -sheep:depends({serverchan_sheep="2"}) -sheep=s:taboption("tab_basic4", ListValue,"endtime",translate("免打扰结束时间")) -for t=0,23 do -sheep:value(t,translate("每天"..t.."点")) -end -sheep.default=8 -sheep.datatype=uinteger -sheep:depends({serverchan_sheep="1"}) -sheep:depends({serverchan_sheep="2"}) - -mac=s:taboption("tab_basic4", ListValue,"macmechanism",translate("MAC过滤")) -mac:value("",translate("disable")) -mac:value("allow",translate("忽略列表内设备")) -mac:value("block",translate("仅通知列表内设备")) -mac:value("interface",translate("仅通知此接口设备")) - -allowedmac = s:taboption("tab_basic4", DynamicList, "serverchan_whitelist", translate("忽略列表")) -nt.mac_hints(function(mac, name) allowedmac :value(mac, "%s (%s)" %{ mac, name }) end) -allowedmac.rmempty = true -allowedmac.optional = true -allowedmac:depends({macmechanism="allow"}) - -blockedmac = s:taboption("tab_basic4", DynamicList, "serverchan_blacklist", translate("关注列表")) -nt.mac_hints(function(mac, name) blockedmac:value(mac, "%s (%s)" %{ mac, name }) end) -blockedmac.rmempty = true -blockedmac.optional = true -blockedmac:depends({macmechanism="block"}) - -n = s:taboption("tab_basic4", ListValue, "serverchan_interface", translate("接口名称")) -n:depends({macmechanism="interface"}) -for _, iface in ipairs(ifaces) do - if not (iface == "lo" or iface:match("^ifb.*")) then - local nets = net:get_interface(iface) - nets = nets and nets:get_networks() or {} - for k, v in pairs(nets) do - nets[k] = nets[k].sid - end - nets = table.concat(nets, ",") - n:value(iface, ((#nets > 0) and "%s (%s)" % {iface, nets} or iface)) - 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 &") -end - -return m diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/advanced.lua b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/advanced.lua new file mode 100644 index 0000000000..89bc28e784 --- /dev/null +++ b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/advanced.lua @@ -0,0 +1,112 @@ +local nt = require "luci.sys".net +local fs=require"nixio.fs" + +m=Map("serverchan",translate("提示:"), +translate("如果你不了解这些选项的含义,请不要修改这些选项")) + +s = m:section(TypedSection, "serverchan", "高级设置") +s.anonymous = true +s.addremove = false + +a=s:option(Value,"up_timeout",translate('设备上线检测超时')) +a.default = "2" +a.optional=false +a.datatype="uinteger" + +a=s:option(Value,"down_timeout",translate('设备离线检测超时')) +a.default = "20" +a.optional=false +a.datatype="uinteger" + +a=s:option(Value,"timeout_retry_count",translate('离线检测次数')) +a.default = "2" +a.optional=false +a.datatype="uinteger" +a.description = translate("若无二级路由设备,信号强度良好,可以减少以上数值
因夜间 wifi 休眠较为玄学,遇到设备频繁推送断开,烦请自行调整参数
..╮(╯_╰)╭..") + +a=s:option(Value,"thread_num",translate('最大并发进程数')) +a.default = "3" +a.datatype="uinteger" + +a=s:option(Value, "soc_code", "自定义温度读取命令") +a.rmempty = true +a:value("",translate("默认")) +a:value("sensors",translate("sensors命令")) +a.description = translate("请尽量避免使用特殊符号,如双引号、$、!等,执行结果需为数字,用于温度对比") + +a=s:option(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","advanced")) +end + +if nixio.fs.access("/tmp/serverchan/soc_tmp") then +e=s:option(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:option(Flag,"err_enable",translate("无人值守任务")) +a.default=0 +a.rmempty=true +a.description = translate("请确认脚本可以正常运行,否则可能造成频繁重启等错误!") + +a=s:option(Flag,"err_sheep_enable",translate("仅在免打扰时段重拨")) +a.default=0 +a.rmempty=true +a.description = translate("避免白天重拨 ddns 域名等待解析,此功能不影响断网检测
因夜间跑流量问题,该功能可能不稳定") +a:depends({err_enable="1"}) + +a= s:option(DynamicList, "err_device_aliases", translate("关注列表")) +a.rmempty = true +a.description = translate("只会在列表中设备都不在线时才会执行
免打扰时段一小时后,关注设备五分钟低流量(约10kb/m)将视为离线") +nt.mac_hints(function(mac, name) a :value(mac, "%s (%s)" %{ mac, name }) end) +a:depends({err_enable="1"}) + +a=s:option(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:option(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:option(Value, "autoreboot_time", "系统运行时间大于") +a.rmempty = true +a.default = "24" +a.datatype="uinteger" +a:depends({system_time_event="1"}) +a.description = translate("单位为小时") + +a=s:option(Value, "network_restart_time", "网络在线时间大于") +a.rmempty = true +a.default = "24" +a.datatype="uinteger" +a:depends({system_time_event="2"}) +a.description = translate("单位为小时") + +a=s:option(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:option(Value, "public_ip_retry_count", "当天最大重试次数") +a.rmempty = true +a.default = "10" +a.datatype="uinteger" +a:depends({public_ip_event="1"}) + +return m \ No newline at end of file diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/log.lua b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/log.lua new file mode 100644 index 0000000000..9063c55c22 --- /dev/null +++ b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/log.lua @@ -0,0 +1,5 @@ +f = SimpleForm("serverchan") +f.reset = false +f.submit = false +f:append(Template("serverchan/log")) +return f diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/setting.lua b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/setting.lua new file mode 100644 index 0000000000..495325422a --- /dev/null +++ b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/setting.lua @@ -0,0 +1,297 @@ + +local nt = require "luci.sys".net +local fs=require"nixio.fs" +local e=luci.model.uci.cursor() +local net = require "luci.model.network".init() +local sys = require "luci.sys" +local ifaces = sys.net:devices() + +m=Map("serverchan",translate("ServerChan"), +translate("「Server酱」,英文名「ServerChan」,是一款从服务器推送报警信息和日志到微信的工具。

如果你在使用中遇到问题,请到这里提交:") +.. [[]] +.. translate("github 项目地址") +.. [[]] +) + +m:section(SimpleSection).template = "serverchan/status" + +s=m:section(NamedSection,"serverchan","serverchan",translate("")) +s:tab("basic", translate("基本设置")) +s:tab("content", translate("推送内容")) +s:tab("crontab", translate("定时推送")) +s:tab("disturb", translate("免打扰")) +s.addremove = false +s.anonymous = true + +--基本设置 +a=s:taboption("basic", Flag,"serverchan_enable",translate("启用")) +a.default=0 +a.rmempty = true + +a=s:taboption("basic", ListValue,"send_tg",translate("推送模式")) +a.default="" +a.rmempty = true +a:value("",translate("微信")) +a:value("1",translate("Telegram")) + +a=s:taboption("basic", Value,"sckey",translate('SCKEY'), translate("Serverchan Sckey").."
调用代码获取点击这里

") +a.rmempty = true +a:depends("send_tg","") + +a=s:taboption("basic", Value, "tgtoken", translate("tg推送链接"),translate("").."
获取机器人点击这里

") +a.rmempty = true +a:depends("send_tg","1") +a.placeholder="https://tgbot.lbyczf.com/sendMessage/:Token" + +a=s:taboption("basic", Value,"device_name",translate('本设备名称')) +a.rmempty = true +a.description = translate("在推送信息标题中会标识本设备名称,用于区分推送信息的来源设备") + +a=s:taboption("basic", Value,"sleeptime",translate('检测时间间隔')) +a.rmempty = true +a.optional = false +a.default = "60" +a.description = translate("越短的时间时间响应越及时,但会占用更多的系统资源") + +a=s:taboption("basic", Flag,"debuglevel",translate("开启日志")) +a.rmempty = true + +a= s:taboption("basic", DynamicList, "device_aliases", translate("设备别名")) +a.rmempty = true +a.description = translate("
请输入设备 MAC 和设备别名,用“-”隔开,如:
XX:XX:XX:XX:XX:XX-我的手机") + +--设备状态 +a=s:taboption("content", ListValue,"serverchan_ipv4",translate("ipv4 变动通知")) +a.rmempty = true +a.default="" +a:value("",translate("关闭")) +a:value("1",translate("通过接口获取")) +a:value("2",translate("通过URL获取")) + +a = s:taboption("content", ListValue, "ipv4_interface", translate("接口名称")) +a.rmempty = true +a:depends({serverchan_ipv4="1"}) +for _, iface in ipairs(ifaces) do + if not (iface == "lo" or iface:match("^ifb.*")) then + local nets = net:get_interface(iface) + nets = nets and nets:get_networks() or {} + for k, v in pairs(nets) do + nets[k] = nets[k].sid + end + nets = table.concat(nets, ",") + a:value(iface, ((#nets > 0) and "%s (%s)" % {iface, nets} or iface)) + end +end +a.description = translate("
一般选择 wan 接口,多拨环境请自行选择") + +a= s:taboption("content", Value, "ipv4_URL", "URL 地址") +a.rmempty = true +a.default = "members.3322.org/dyndns/getip" +a:depends({serverchan_ipv4="2"}) +a.description = translate("
会因服务器稳定性/连接频繁等原因导致获取失败") + +a=s:taboption("content", ListValue,"serverchan_ipv6",translate("ipv6 变动通知")) +a.rmempty = true +a.default="disable" +a:value("0",translate("关闭")) +a:value("1",translate("通过接口获取")) +a:value("2",translate("通过URL获取")) + +a = s:taboption("content", ListValue, "ipv6_interface", translate("接口名称")) +a.rmempty = true +a:depends({serverchan_ipv6="1"}) +for _, iface in ipairs(ifaces) do + if not (iface == "lo" or iface:match("^ifb.*")) then + local nets = net:get_interface(iface) + nets = nets and nets:get_networks() or {} + for k, v in pairs(nets) do + nets[k] = nets[k].sid + end + nets = table.concat(nets, ",") + a:value(iface, ((#nets > 0) and "%s (%s)" % {iface, nets} or iface)) + end +end +a.description = translate("
一般选择 wan 接口,多拨环境请自行选择") + +a= s:taboption("content", Value, "ipv6_URL", "URL 地址") +a.rmempty = true +a.default = "v6.ip.zxinc.org/getip" +a:depends({serverchan_ipv6="2"}) +a.description = translate("
会因服务器稳定性/连接频繁等原因导致获取失败") + +a=s:taboption("content", Flag,"serverchan_up",translate("设备上线通知")) +a.default=1 +a.rmempty = true + +a=s:taboption("content", Flag,"serverchan_down",translate("设备下线通知")) +a.default=1 +a.rmempty = true + +a=s:taboption("content", Flag,"cpuload_enable",translate("CPU 负载报警")) +a.default=1 +a.rmempty = true + +a= s:taboption("content", Value, "cpuload", "负载报警阈值") +a.default = 2 +a.rmempty = true + +a:depends({cpuload_enable="1"}) +a=s:taboption("content", Flag,"temperature_enable",translate("CPU 温度报警")) +a.default=70 +a.rmempty = true + +a= s:taboption("content", Value, "temperature", "温度报警阈值") +a.rmempty = true +a.default = "80" +a.datatype="uinteger" +a:depends({temperature_enable="1"}) +a.description = translate("
设备报警只会在连续五分钟超过设定值时才会推送
而且一个小时内不会再提醒第二次") + +--定时推送 +a=s:taboption("crontab", ListValue,"send_mode",translate("定时任务设定")) +a.rmempty = true +a.default="" +a:value("",translate("关闭")) +a:value("1",translate("定时发送")) +a:value("2",translate("间隔发送")) + +a=s:taboption("crontab", ListValue,"regular_time",translate("发送时间")) +a.rmempty = true +for t=0,23 do +a:value(t,translate("每天"..t.."点")) +end +a.default=8 +a.datatype=uinteger +a:depends("send_mode","1") + +a=s:taboption("crontab", ListValue,"regular_time_2",translate("发送时间")) +a.rmempty = true +a:value("",translate("关闭")) +for t=0,23 do +a:value(t,translate("每天"..t.."点")) +end +a.default="关闭" +a.datatype=uinteger +a:depends("send_mode","1") + +a=s:taboption("crontab", ListValue,"regular_time_3",translate("发送时间")) +a.rmempty = true + +a:value("",translate("关闭")) +for t=0,23 do +a:value(t,translate("每天"..t.."点")) +end +a.default="关闭" +a.datatype=uinteger +a:depends("send_mode","1") + +a=s:taboption("crontab", ListValue,"interval_time",translate("发送间隔")) +a.rmempty = true +for t=1,23 do +a:value(t,translate(t.."小时")) +end +a.default=6 +a.datatype=uinteger +a:depends("send_mode","2") +a.description = translate("
从 00:00 开始,每 * 小时发送一次") + +a= s:taboption("crontab", Value, "send_title", translate("微信推送标题")) +a:depends("send_mode","1") +a:depends("send_mode","2") +a.placeholder = "OpenWrt By tty228 路由状态:" +a.description = translate("
使用特殊符号可能会造成发送失败") + +a=s:taboption("crontab", Flag,"router_status",translate("系统运行情况")) +a.default=1 +a:depends("send_mode","1") +a:depends("send_mode","2") + +a=s:taboption("crontab", Flag,"router_temp",translate("设备温度")) +a.default=1 +a:depends("send_mode","1") +a:depends("send_mode","2") + +a=s:taboption("crontab", Flag,"router_wan",translate("WAN信息")) +a.default=1 +a:depends("send_mode","1") +a:depends("send_mode","2") + +a=s:taboption("crontab", Flag,"client_list",translate("客户端列表")) +a.default=1 +a:depends("send_mode","1") +a:depends("send_mode","2") + +e=s:taboption("crontab", Button,"_add",translate("手动发送")) +e.inputtitle=translate("发送") +e:depends("send_mode","1") +e:depends("send_mode","2") +e.inputstyle = "apply" +function e.write(self, section) +luci.sys.call("cbi.apply") + luci.sys.call("/usr/bin/serverchan/serverchan send &") +end + +--免打扰 +a=s:taboption("disturb", ListValue,"serverchan_sheep",translate("免打扰时段设置"),translate("在指定整点时间段内,暂停推送消息
免打扰时间中,定时推送也会被阻止。")) +a.rmempty = true + +a:value("",translate("关闭")) +a:value("1",translate("模式一:脚本挂起")) +a:value("2",translate("模式二:静默模式")) +a.description = translate("模式一停止一切检测,包括无人值守。") +a=s:taboption("disturb", ListValue,"starttime",translate("免打扰开始时间")) +a.rmempty = true + +for t=0,23 do +a:value(t,translate("每天"..t.."点")) +end +a.default=0 +a.datatype=uinteger +a:depends({serverchan_sheep="1"}) +a:depends({serverchan_sheep="2"}) +a=s:taboption("disturb", ListValue,"endtime",translate("免打扰结束时间")) +a.rmempty = true + +for t=0,23 do +a:value(t,translate("每天"..t.."点")) +end +a.default=8 +a.datatype=uinteger +a:depends({serverchan_sheep="1"}) +a:depends({serverchan_sheep="2"}) + +a=s:taboption("disturb", ListValue,"macmechanism",translate("MAC过滤")) +a:value("",translate("disable")) +a:value("allow",translate("忽略列表内设备")) +a:value("block",translate("仅通知列表内设备")) +a:value("interface",translate("仅通知此接口设备")) +a.rmempty = true + + +a = s:taboption("disturb", DynamicList, "serverchan_whitelist", translate("忽略列表")) +nt.mac_hints(function(mac, name) a :value(mac, "%s (%s)" %{ mac, name }) end) +a.rmempty = true +a:depends({macmechanism="allow"}) + +a = s:taboption("disturb", DynamicList, "serverchan_blacklist", translate("关注列表")) +nt.mac_hints(function(mac, name) a:value(mac, "%s (%s)" %{ mac, name }) end) +a.rmempty = true +a:depends({macmechanism="block"}) + +a = s:taboption("disturb", ListValue, "serverchan_interface", translate("接口名称")) +a:depends({macmechanism="interface"}) +a.rmempty = true + +for _, iface in ipairs(ifaces) do + if not (iface == "lo" or iface:match("^ifb.*")) then + local nets = net:get_interface(iface) + nets = nets and nets:get_networks() or {} + for k, v in pairs(nets) do + nets[k] = nets[k].sid + end + nets = table.concat(nets, ",") + a:value(iface, ((#nets > 0) and "%s (%s)" % {iface, nets} or iface)) + end +end + +return m \ No newline at end of file diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/log.htm b/package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/log.htm new file mode 100644 index 0000000000..0802053c28 --- /dev/null +++ b/package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/log.htm @@ -0,0 +1,32 @@ +<% +local dsp = require "luci.dispatcher" +-%> + + +
+ + +
diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/serverchan_status.htm b/package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/status.htm similarity index 100% rename from package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/serverchan_status.htm rename to package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/status.htm diff --git a/package/ctcgfw/luci-app-serverchan/root/etc/init.d/serverchan b/package/ctcgfw/luci-app-serverchan/root/etc/init.d/serverchan index e500500522..1cbd0a9d86 100755 --- a/package/ctcgfw/luci-app-serverchan/root/etc/init.d/serverchan +++ b/package/ctcgfw/luci-app-serverchan/root/etc/init.d/serverchan @@ -4,17 +4,23 @@ START=99 STOP=10 start() { - kill -9 `pgrep -f "/usr/bin/serverchan/serverchan"` 2>/dev/null - /usr/bin/serverchan/serverchan & + state=`pgrep -f "/usr/bin/serverchan/serverchan"` + if [ ! -z "$state" ]; then + restart + else + /usr/bin/serverchan/serverchan & + fi echo "serverchan is starting now ..." } stop() { kill -9 `pgrep -f "/usr/bin/serverchan/serverchan"` 2>/dev/null - kill -9 `pgrep -f "/etc/init.d/serverchan"` 2>/dev/null echo "serverchan exit ..." } restart(){ + stop + sleep 2 start -} + echo "restarted." +} \ No newline at end of file diff --git a/package/ctcgfw/luci-app-serverchan/root/etc/uci-defaults/luci-serverchan b/package/ctcgfw/luci-app-serverchan/root/etc/uci-defaults/luci-serverchan new file mode 100644 index 0000000000..4b4c9d43e2 --- /dev/null +++ b/package/ctcgfw/luci-app-serverchan/root/etc/uci-defaults/luci-serverchan @@ -0,0 +1,11 @@ +#!/bin/sh + +uci -q batch <<-EOF >/dev/null + delete ucitrack.@serverchan[-1] + add ucitrack serverchan + set ucitrack.@serverchan[-1].init=serverchan + commit ucitrack +EOF + +rm -rf /tmp/luci-* +exit 0 \ No newline at end of file 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 543293db44..bfaa8a7e98 100755 --- a/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan +++ b/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan @@ -1,447 +1,316 @@ #!/bin/sh # 等待 3s,确保 luci 设置已保存 -if [ ! "$1" ] ;then sleep 3;fi +[ ! "$1" ] && sleep 3 # 读取设置文件 function get_config(){ - if [ "$1" ] ;then - uci get serverchan.serverchan.$1 2>/dev/null - fi + while [[ "$*" != "" ]]; do + eval ${1}='`uci get serverchan.serverchan.$1`' 2>/dev/null + shift + done } # 初始化设置信息 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` + get_config "serverchan_enable" "sckey" "serverchan_ipv4" "ipv4_interface" "ipv4_URL" "serverchan_ipv6" "ipv6_interface" "ipv6_URL" "serverchan_up" "serverchan_down" "serverchan_sheep" "serverchan_whitelist" "serverchan_blacklist" "serverchan_interface" "starttime" "endtime" "cpuload_enable" "cpuload" "temperature_enable" "temperature" "device_name" "err_enable" "network_err_event" "err_sheep_enable" "system_time_event" "autoreboot_time" "network_restart_time" "public_ip_event" "public_ip_retry_count" "soc_code" "sleeptime" "up_timeout" "down_timeout" "device_aliases" "debuglevel" "cpuload" "temperature" "send_mode" "regular_time" "regular_time_2" "regular_time_3" "interval_time" "thread_num" "timeout_retry_count" "send_tg" "tgtoken" + for str_version in "wrtbwmon" "iputils-arping" "curl" "iw"; do + eval `echo ${str_version:0:2}"_version"`=`opkg list-installed|grep -w ^${str_version}|awk '{print $3}'` 2>/dev/null + done 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` + debuglevel=`echo "$debuglevel"` && [ -z "$debuglevel" ] && logfile="/dev/null" || logfile="${dir}serverchan.log" + device_aliases=`echo "$device_aliases"|sed 's/ /\n/g'|sed 's/-/ /g'` 2>/dev/null + err_device_aliases=`echo "$err_device_aliases"|sed 's/ /\n/g'` 2>/dev/null + [ "$iw_version" ] && wlan_interface=`iw dev|grep Interface|awk '{print $2}'` >/dev/null 2>&1 + [ -z "$up_timeout" ] || [ "$up_timeout" -eq "0" ] && up_timeout="2" + [ -z "$down_timeout" ] || [ "$down_timeout" -eq "0" ] && down_timeout="20";down_timeout=`expr ${down_timeout} / 2 + 1` + [ -z "$timeout_retry_count" ] && timeout_retry_count="2";[ "$timeout_retry_count" -eq "0" ] && timeout_retry_count="1" + markdown_splitline="%0D%0A%0D%0A---%0D%0A%0D%0A";markdown_linefeed="%0D%0A%0D%0A";markdown_tab=" ";markdown_space=" " + [ ! -z "$send_tg" ] && [ "$send_tg" -eq "1" ] && markdown_splitline="%0D%0A%0D%0A%0D%0A%0D%0A" && markdown_tab="" 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 + 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 + deltemp + rm -f ${dir}fd1 ${dir}sheep_usage ${dir}old_sheep_usage /usr/bin/serverchan/errlog >/dev/null 2>&1 + [ ! -f "/usr/sbin/wrtbwmon" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】未安装 wrtbwmon ,流量统计不可用" >> ${logfile} + for read_str in "ip_version" "cu_version"; do + [ -z `eval echo '$'$read_str` ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】依赖项未安装" >> ${logfile} && return 1 + done + [ -z "$sckey" ] && [ -z "$tgtoken" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】请填写正确的 sckey " >> ${logfile} && return 1 + return 0 +} + +# 清理临时文件 +function deltemp(){ + unset title content + rm -f ${dir}title ${dir}content ${dir}top ${dir}tmp_downlist ${dir}send_enable.lock >/dev/null 2>&1 + LockFile unlock + [ -f ${logfile} ] && local logrow=$(grep -c "" ${logfile}) || local logrow="0" + [ $logrow -gt 500 ] && sed -i '1,100d' ${logfile} && echo "`date "+%Y-%m-%d %H:%M:%S"` 【清理】日志超出上限,删除前 100 条" >> ${logfile} } # 检测程序开关 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 + [ ! "$1" ] && local time_n=1 + for i in `seq 1 $time_n`; do + get_config serverchan_enable;[ -z "$serverchan_enable" ] || [ "$serverchan_enable" -eq "0" ] && `/etc/init.d/serverchan stop` || sleep 1 + done } # 获取 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=$(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 + [ ! "$1" ] && return + if [ $1 == "wanipv4" ] ;then + [ ! -z "$ipv4_interface" ] && 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}') || 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}') 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 + elif [ $1 == "hostipv4" ] ;then + [ -z "$ipv4_URL" ] && local ipv4_URL="members.3322.org/dyndns/getip" + [ ! -z "$ipv4_interface" ] && local hostIP=$(curl -k -s -4 --interface ${ipv4_interface} ${ipv4_URL}) || local hostIP=$(curl -k -s -4 ${ipv4_URL}) 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|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 + elif [ $1 == "wanipv6" ] ;then + [ ! -z "$ipv6_interface" ] && 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}') || 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}') 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 + elif [ $1 == "hostipv6" ] ;then + [ -z "$ipv6_URL" ] && local ipv6_URL="v6.ip.zxinc.org/getip" + [ ! -z "$ipv6_interface" ] && local hostIPv6=$(curl -k -s -6 --interface ${ipv6_interface} ${ipv6_URL}) || local hostIPv6=$(curl -k -s -6 ${ipv6_URL}) echo "$hostIPv6" fi -fi } # 查询 mac 地址 function getmac(){ - 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"|grep -q "unknown" ) && unset tmp_mac # 为unknown时重新读取 + [ -f "${dir}ipAddress" ] && [ -z "$tmp_mac" ] && local tmp_mac=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u` + [ -f "${dir}tmp_downlist" ] && [ -z "$tmp_mac" ] && local tmp_mac=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u` + [ -f "/var/dhcp.leases" ] && [ -z "$tmp_mac" ] && local tmp_mac=`cat /var/dhcp.leases|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u` + [ -z "$tmp_mac" ] && local tmp_mac=`cat /proc/net/arp|grep "0x2\|0x6"|grep -w ${1}|awk '{print $4}'|grep -v "^$"|sort -u` + [ -z "$tmp_mac" ] && local tmp_mac="unknown" echo "$tmp_mac" } # 查询主机名 function getname(){ - 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 -w ^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 -w ^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"|grep -q "unknown" ) && unset tmp_name # 为unknown时重新读取 + [ -f "${dir}ipAddress" ] && [ -z "$tmp_name" ] && local tmp_name=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $3}'|grep -v "^$"|sort -u` + [ -f "${dir}tmp_downlist" ] && [ -z "$tmp_name" ] && local tmp_name=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $3}'|grep -v "^$"|sort -u` + [ -z "$tmp_name" ] && local tmp_name=`echo "$device_aliases"|grep -i $2|awk '{print $2}'|grep -v "^$"|sort -u` + [ ! -z "$tmp_name" ] && echo "$tmp_name" && return + [ -z "$dhcp_config" ] && dhcp_config=`uci show dhcp|grep "ip\|mac\|name"` + for dhcp_config_str in "host" "domain"; do + local dhcp_ip_n=`echo "$dhcp_config"|grep -w ^dhcp.@${dhcp_config_str}.*ip=.${1}|sed -nr 's#^dhcp.(.*).ip.*#\1#gp'` 2>/dev/null + [ ! -z "$dhcp_ip_n" ] && [ -z "$tmp_name" ] && local tmp_name=`uci get dhcp.${dhcp_ip_n}.name` 2>/dev/null + local dhcp_mac_n=`echo "$dhcp_config"|grep -i ^dhcp.@${dhcp_config_str}.*mac=.${2}|sed -nr 's#^dhcp.(.*).mac.*#\1#gp'` 2>/dev/null + [ ! -z "$dhcp_mac_n" ] && [ -z "$tmp_name" ] && local tmp_name=`uci get dhcp.${dhcp_ip_n}.name` 2>/dev/null + [ ! -z "$tmp_name" ] && break + done + [ -f "/var/dhcp.leases" ] && [ -z "$tmp_name" ] && local tmp_name=`cat /var/dhcp.leases|grep -w ${1}|awk '{print $4}'|grep -v "^$"|sort -u` + [ -z "$tmp_name" ] && local tmp_name="unknown" echo "$tmp_name" } # 查询设备接口 function getinterface(){ - if [ -f "${dir}ipAddress" ] ; then local ip_interface=`cat ${dir}ipAddress | grep -w $1 |awk '{print $5}' | grep -v "^$" | sort -u`;fi - 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 + [ -f "${dir}ipAddress" ] && local ip_interface=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $5}'|grep -v "^$"|sort -u` + [ -f "${dir}tmp_downlist" ] && [ -z "$ip_interface" ] && local ip_interface=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $5}'|grep -v "^$"|sort -u` + if [ -z "$ip_interface" ] && [ ! -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 + local ip_interface=`iw dev $interface station dump 2>/dev/null|grep Station|grep -i -w ${1}|sed -nr 's#^.*on (.*))#\1#gp'` >/dev/null 2>&1 + [ ! -z "$ip_interface" ] && echo "$ip_interface" && return 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 - echo $ip_interface + [ -z "$ip_interface" ] && local ip_interface=`cat /proc/net/arp|grep "0x2\|0x6"|grep -i -w ${1}|awk '{print $6}'|grep -v "^$"|sort -u` + echo "$ip_interface" +} + +# ping +function getping(){ + for i in `seq 1 ${3}`; do + ( ! echo "$ip_ms"|grep -q "ms" ) && 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 + ( ! echo "$ip_ms"|grep -q "ms" ) && local ip_ms=`ping -c 5 -w ${2} ${1}|grep -v '100% packet loss'` 2>/dev/null + ( ! echo "$ip_ms"|grep -q "ms" ) && sleep 1 + done + ( echo "$ip_ms"|grep -q "ms" ) } # CPU 占用率 function getcpu(){ - local AT=$(cat /proc/stat | grep "^cpu " | awk '{print $2+$3+$4+$5+$6+$7+$8 " " $2+$3+$4+$7+$8}') + local AT=$(cat /proc/stat|grep "^cpu "|awk '{print $2+$3+$4+$5+$6+$7+$8 " " $2+$3+$4+$7+$8}') sleep 3 - local BT=$(cat /proc/stat | grep "^cpu " | awk '{print $2+$3+$4+$5+$6+$7+$8 " " $2+$3+$4+$7+$8}') - printf "%.01f%%" $(echo $AT $BT | awk '{print (($4-$2)/($3-$1))*100}') + local BT=$(cat /proc/stat|grep "^cpu "|awk '{print $2+$3+$4+$5+$6+$7+$8 " " $2+$3+$4+$7+$8}') + printf "%.01f%%" $(echo ${AT} ${BT}|awk '{print (($4-$2)/($3-$1))*100}') } # 获取SOC温度 (取所有传感器温度最大值) function soc_temp(){ - 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 + [ -z "$soc_code" ] && soc_code="cat /sys/class/thermal/thermal_zone*/temp|sort -nr|head -n1|cut -c-2" + [ "$soc_code" == "sensors" ] && soc_code="sensors|grep °C|sed -nr 's#^Core.*:.*\+(.*)°C .*#\1#gp'|sort -nr|head -n1" + echo "$soc_code"|awk '{run=$0;system(run)}' 2>/dev/null } # 流量数据 function usage(){ -if [ ! -f "/usr/sbin/wrtbwmon" ] ; then return;fi -if [ "$1" ] ; then - # 更新数据 - if [ $1 = "update" ] ;then - function version_le() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" == "$1"; } - function version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; } - if [ ! -z "$wrtbwmon_version" ] ; then - if version_ge $wrtbwmon_version "1.2.0"; then - wrtbwmon -f ${dir}usage.db - elif version_le $wrtbwmon_version "1.0.0"; then - wrtbwmon update ${dir}usage.db - fi - elif [ -f "/usr/sbin/wrtbwmon" ] ; then - wrtbwmon update ${dir}usage.db - 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 [ -z "$ip_total" ] ; then local ip_total="0";fi - echo `bytes_for_humans $ip_total` - # 剔除ip - elif [ $1 = "down" ] ;then - if [ "$2" ] ; then sed -i "/$2/d" ${dir}usage.db 2>/dev/null;fi + [ ! -f "/usr/sbin/wrtbwmon" ] || [ ! "$1" ] && return + if [ $1 == "update" ] ;then + function version_le() { test "$(echo "$@"|tr " " "\n"|sort -V|head -n 1)" == "$1"; } + function version_ge() { test "$(echo "$@"|tr " " "\n"|sort -rV|head -n 1)" == "$1"; } + [ ! -z "$wr_version" ] && ( version_ge "${wr_version}" "1.2.0" ) && wrtbwmon -f ${dir}usage.db 2>/dev/null && return + [ ! -z "$wr_version" ] && ( version_le "${wr_version}" "1.0.0" ) || [ -z "$wr_version" ] && wrtbwmon update ${dir}usage.db 2>/dev/null && return + elif [ $1 == "get" ] ;then + [ ! -f "${dir}usage.db" ] && echo `bytes_for_humans 0` && return + [ -z "$total_n" ] && total_n=`cat ${dir}usage.db|head -n1|grep "total"|sed 's/,/\n/g'|awk '/total/{print NR}'` 2>/dev/null + [ -z "$total_n" ] && total_n="6" + [ "$2" ] && local tmptotal=`cat ${dir}usage.db|sed 's/,/ /g'|grep -i -w ${2}|awk "{print "'$'$total_n"}"|grep -v "^$"|sort -u` 2>/dev/null + [ -z "$tmptotal" ] && local tmptotal="0" + echo `bytes_for_humans ${tmptotal}` + elif [ $1 == "down" ] ;then + [ "$2" ] && sed -i "/${2}/d" ${dir}usage.db 2>/dev/null fi -fi } # 流量数据单位换算 function bytes_for_humans { - if [ "$1" ] ;then - if [[ "$1" -lt 1024 ]]; then - echo "$1" bytes - elif [[ "$1" -lt 1048576 ]]; then - echo `awk 'BEGIN{printf "%.2f\n",'$1'/'1024'}'` KB - elif [[ "$1" -lt 1073741824 ]]; then - echo `awk 'BEGIN{printf "%.2f\n",'$1'/'1048576'}'` MB - elif [[ "$1" -gt 1073741824 ]]; then - echo `awk 'BEGIN{printf "%.2f\n",'$1'/'1073741824'}'` GB - fi - else - echo 0 bytes - fi + [ ! "$1" ] && return + [ "$1" -gt 1073741824 ] && echo "`awk 'BEGIN{printf "%.2f\n",'$1'/'1073741824'}'` GB" && return + [ "$1" -lt 1073741824 ] && echo "`awk 'BEGIN{printf "%.2f\n",'$1'/'1048576'}'` MB" && return + [ "$1" -lt 1048576 ] && echo "`awk 'BEGIN{printf "%.2f\n",'$1'/'1024'}'` KB" && return + [ "$1" -lt 1024 ] && echo "${1} bytes" } # 时间单位换算 function time_for_humans { - if [ "$1" ] ;then - if [[ $1 -lt 60 ]]; then - echo $1 秒 - elif [[ $1 -lt 3600 ]]; then - local usetime_min=`expr $1 / 60` - local usetime_sec=`expr $usetime_min \* 60` - local usetime_sec=`expr $1 - ${usetime_sec}` - echo $usetime_min 分 $usetime_sec 秒 - elif [[ $1 -lt 216000 ]]; then - local usetime_hour=`expr $1 / 3600` - local usetime_min=`expr $usetime_hour \* 3600` - local usetime_min=`expr $1 - ${usetime_min}` - local usetime_min=`expr ${usetime_min} / 60` - echo $usetime_hour 小时 $usetime_min 分 - else - local usetime_day=`expr $1 / 86400` - local usetime_hour=`expr $usetime_day \* 86400` - local usetime_hour=`expr $1 - ${usetime_hour}` - local usetime_hour=`expr ${usetime_hour} / 3600` - echo $usetime_day 天 $usetime_hour 小时 - fi + [ ! "$1" ] && return + if [ "$1" -lt 60 ]; then + echo "${1} 秒" + elif [ "$1" -lt 3600 ]; then + local usetime_min=`expr $1 / 60` + local usetime_sec=`expr $usetime_min \* 60` + local usetime_sec=`expr $1 - $usetime_sec` + echo "${usetime_min} 分 ${usetime_sec} 秒" + elif [ "$1" -lt 86400 ]; then + local usetime_hour=`expr $1 / 3600` + local usetime_min=`expr $usetime_hour \* 3600` + local usetime_min=`expr $1 - $usetime_min` + local usetime_min=`expr $usetime_min / 60` + echo "${usetime_hour} 小时 ${usetime_min} 分" else - echo 0 秒 - fi + local usetime_day=`expr $1 / 86400` + local usetime_hour=`expr $usetime_day \* 86400` + local usetime_hour=`expr $1 - $usetime_hour` + local usetime_hour=`expr $usetime_hour / 3600` + echo "${usetime_day} 天 ${usetime_hour} 小时" + fi } # 计算字符真实长度 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}'` + [ ! "$1" ] && return + 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_first(){ - # 从 ipAddress 文件中读取上次检测到的在线列表 - if [ ! -f "${dir}ipAddress" ] ; then > ${dir}ipAddress;fi - local IPLIST=$( cat ${dir}ipAddress | awk '{print $1}' | grep -v "^$" | sort -u ) + [ -f "${dir}ipAddress" ] && local IPLIST=`cat ${dir}ipAddress|awk '{print $1}'|grep -v "^$"|sort -u` for ip in $IPLIST; do - if [ ! -z "$ip" ] ; then - read -u 5 - { + read -u 5 + { down $ip echo "" >&5 - }& - fi + }& done + wait unset ip IPLIST - local IPLIST=$( cat /proc/net/arp | grep "0x2\|0x6" | awk '{print $1}' | grep -v "^$" | sort -u ) + local IPLIST=`cat /proc/net/arp|grep "0x2\|0x6"|awk '{print $1}'|grep -v "^$"|sort -u` for ip in $IPLIST; do - # 如果 “ip 列表不为空” - if [ ! -z "$ip" ] ; then - read -u 5 - { + read -u 5 + { up $ip echo "" >&5 - }& - fi + }& done wait } # 创建计划任务 function serverchan_cron(){ - local send_mode=`get_config send_mode` - if [ ! -z "$send_mode" ] ; then - local regular_time=`get_config regular_time` - local regular_time_2=`get_config regular_time_2` - local regular_time_3=`get_config regular_time_3` - if [ ! -z "$regular_time_2" ] ; then local regular_time_2=,${regular_time_2};fi - if [ ! -z "$regular_time_3" ] ; then local regular_time_3=,${regular_time_3};fi - local interval_time=`get_config interval_time` - - # 定时发送 - if [ ! -z "$regular_time" ] || [ ! -z "$regular_time_2" ] || [ ! -z "$regular_time_3" ] ; then - if ( echo `crontab -l` | grep "serverchan" | grep -q " $regular_time$regular_time_2$regular_time_3 " ); then - return - elif ( echo `crontab -l` | grep -q "serverchan" ); then - crontab -l > conf && sed -i "/serverchan/d" conf && crontab conf && rm -f conf >/dev/null 2>&1 - crontab -l > conf && echo -e "0 $regular_time$regular_time_2$regular_time_3 * * * /usr/bin/serverchan/serverchan send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1 - else - crontab -l > conf && echo -e "0 $regular_time$regular_time_2$regular_time_3 * * * /usr/bin/serverchan/serverchan send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1 - fi - # 间隔发送 - elif [ ! -z "$interval_time" ] ; then - if ( echo `crontab -l` | grep "serverchan" | grep -q " */$interval_time " ); then - return - elif ( echo `crontab -l` | grep -q "serverchan" ); then - crontab -l > conf && sed -i "/serverchan/d" conf && crontab conf && rm -f conf >/dev/null 2>&1 - crontab -l > conf && echo -e "0 */$interval_time * * * /usr/bin/serverchan/serverchan send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1 - else - crontab -l > conf && echo -e "0 */$interval_time * * * /usr/bin/serverchan/serverchan send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1 - fi - fi - elif [ -z "$send_mode" ] ; then - if ( echo `crontab -l` | grep -q "serverchan" ); then - crontab -l > conf && sed -i "/serverchan/d" conf && crontab conf && rm -f conf >/dev/null 2>&1 - fi + function del_cron(){ + ( echo `crontab -l`|grep -q "serverchan" ) && crontab -l > conf && sed -i "/serverchan/d" conf && crontab conf && rm -f conf >/dev/null 2>&1 + } + function re_cron(){ + /etc/init.d/cron stop + /etc/init.d/cron start + } + if [ -z "$send_mode" ] || [ -z "$serverchan_enable" ]; then + del_cron + re_cron + return fi - serverchan_enable=`get_config serverchan_enable` - if [ -z "$serverchan_enable" ] ; then - if ( echo `crontab -l` | grep -q "serverchan" ); then - crontab -l > conf && sed -i "/serverchan/d" conf && crontab conf && rm -f conf >/dev/null 2>&1 - fi + [ ! -z "$regular_time_2" ] && local regular_time_2=",${regular_time_2}" + [ ! -z "$regular_time_3" ] && local regular_time_3=",${regular_time_3}" + # 定时发送 + if [ ! -z "$regular_time" ] || [ ! -z "$regular_time_2" ] || [ ! -z "$regular_time_3" ]; then + ( echo `crontab -l`|grep "serverchan"|grep -q " $regular_time$regular_time_2$regular_time_3 " ) && return + del_cron + crontab -l > conf && echo -e "0 $regular_time$regular_time_2$regular_time_3 * * * /usr/bin/serverchan/serverchan send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1 + # 间隔发送 + elif [ ! -z "$interval_time" ]; then + ( echo `crontab -l`|grep "serverchan"|grep -q " */$interval_time " ) && return + del_cron + crontab -l > conf && echo -e "0 */$interval_time * * * /usr/bin/serverchan/serverchan send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1 fi - /etc/init.d/cron stop - /etc/init.d/cron start + re_cron } # 免打扰检测 function serverchan_disturb(){ - if [ ! -z "$serverchan_sheep" ] && [ ! -z "$starttime" ] && [ ! -z "$endtime" ] ;then - 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 - echo "`date "+%Y-%m-%d %H:%M:%S"` 【免打扰】夜深了,该休息了" >> ${logfile} - sheep_starttime=`date +%s` - fi - if [ "$serverchan_sheep" -eq "1" ] ;then - while [ $enable -eq "0" ] ; - do - 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 - enable_detection - sleep $sleeptime - usage update - > ${dir}send_enable.lock && serverchan_first && deltemp - unattended - fi - continue - done - elif [ "$serverchan_sheep" -eq "2" ] ;then - disturb_text="【免打扰】" - return 0 - fi + [ -z "$serverchan_sheep" ] || [ -z "$starttime" ] || [ -z "$endtime" ] && return 0 + 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 + unset sheep_starttime + rm -f ${dir}sheep_usage ${dir}old_sheep_usage 2>/dev/null + disturb_text="【微信推送】" + [ ! -z "$send_tg" ] && [ "$send_tg" -eq "1" ] && disturb_text="【Telegram推送】" + return 0 + else + [ -z "$sheep_starttime" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【免打扰】夜深了,该休息了" >> ${logfile} && sheep_starttime=`date +%s` + if [ "$serverchan_sheep" -eq "1" ] ;then + while [ `date +%H` -lt "$endtime" ]; do + enable_detection + sleep $sleeptime + done + elif [ "$serverchan_sheep" -eq "2" ] ;then + disturb_text="【免打扰】" + return 1 fi fi - unset sheep_starttime - disturb_text="【微信推送】" - return 1 } # 文件锁 function LockFile(){ - if [ "$1" ] ;then - if [ $1 = "lock" ] ;then - if [ ! -f "${dir}serverchan.lock" ] ; then - > ${dir}serverchan.lock - else - while [ -f "${dir}serverchan.lock" ] ; - do - enable_detection 1 - continue - done - LockFile lock - fi - fi - if [ $1 = "unlock" ] ;then - rm -f ${dir}serverchan.lock >/dev/null 2>&1 - fi + if [ $1 = "lock" ] ;then + [ ! -f "${dir}serverchan.lock" ] && > ${dir}serverchan.lock && return + while [ -f "${dir}serverchan.lock" ]; do + enable_detection 1 + done + LockFile lock fi -} - -# 清理临时文件 -function deltemp(){ -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 + [ $1 = "unlock" ] && rm -f ${dir}serverchan.lock >/dev/null 2>&1 } # 检测黑白名单 function blackwhitelist(){ -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 - fi - fi -fi + [ ! "$1" ] && return 1 + [ -z "$serverchan_whitelist" ] && [ -z "$serverchan_blacklist" ] && [ -z "$serverchan_interface" ] && return + [ ! -z "$serverchan_whitelist" ] && ( ! echo $serverchan_whitelist|grep -q -i -w $1) && return + [ ! -z "$serverchan_blacklist" ] && ( echo $serverchan_blacklist|grep -q -i -w $1) && return + [ ! -z "$serverchan_interface" ] && ( echo `getinterface ${1}`|grep -q -i -w $serverchan_interface ) && return } # 重启网络服务 @@ -452,228 +321,164 @@ 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 } # 查看无人值守任务设备是否在线 function geterrdevicealiases(){ -if [ ! -z "$err_device_aliases" ] && [ -f ${dir}ipAddress ] ; then - local MACLIST=$( cat ${dir}ipAddress | awk '{print $2}' | grep -v "^$" | sort -u ) + [ -z "$err_device_aliases" ] && return + [ -f ${dir}ipAddress ] && local logrow=$(grep -c "" ${dir}ipAddress) || local logrow="0";[ $logrow -eq "0" ] && return + local MACLIST=`cat ${dir}ipAddress|awk '{print $2}'|grep -v "^$"|sort -u` for mac in $MACLIST; do - if [ -z "$err_mac" ] && [ ! -z "$mac" ] ; then - local err_mac=$( echo "$err_device_aliases" | grep -i $mac | grep -v "^$" | sort -u ) - fi + [ -z "$err_mac" ] && [ ! -z "$mac" ] && local err_mac=`echo "$err_device_aliases"|grep -i $mac|grep -v "^$"|sort -u` done # 进入免打扰时间已经超过一小时 - if [ ! -z "$sheep_starttime" ] && [ "$((`date +%s`-$sheep_starttime))" -ge "3600" ] ; then + 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 | 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 + local MACLIST=`echo "$err_device_aliases"|grep -v "^$"|sort -u` for mac in $MACLIST; do - 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 + [ ! -z "$mac" ] && local tmptotal=`usage get ${mac}` + [ ! -z "$tmptotal" ] && awk 'BEGIN{printf "%.0f\n",'$tmptotal'/'20480'}' 2>/dev/null >> ${dir}sheep_usage done 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/m),视为不在线 - if [ ! -z "$sheep_nousage_starttime" ] && [ "$((`date +%s`-$sheep_nousage_starttime))" -ge "300" ] ; then unset err_mac;fi + [ "$old_sheep_usage" == "$sheep_usage" ] && [ -z "$sheep_nousage_starttime" ] && sheep_nousage_starttime=`date +%s` + [ "$old_sheep_usage" != "$sheep_usage" ] && unset sheep_nousage_starttime && cat ${dir}sheep_usage 2>/dev/null > ${dir}old_sheep_usage + [ ! -z "$sheep_nousage_starttime" ] && [ "$((`date +%s`-$sheep_nousage_starttime))" -ge "300" ] && unset err_mac fi - if [ -z "$err_mac" ] ; then return 0;else return 1;fi -fi + [ -z "$err_mac" ] } # 无人值守任务 function unattended(){ -if [ -z "$err_enable" ] || [ "$err_enable" -ne "1" ] ; then return;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 "$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 - echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】重启路由器咯" >> ${logfile} - cat ${logfile} > /usr/bin/serverchan/errlog - sleep 2 - reboot - exit - elif [ ! -z "$network_restart_time" ] && [ `ubus call network.interface.wan status | grep \"uptime\" | sed $'s/\"uptime": //g'| sed $'s/\,//g' | awk -F. '{run_hour=$1/3600;printf("%d",run_hour)}'` -ge "$network_restart_time" ] && [ "$system_time_event" -eq "2" ] ; then - echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】重新拨号咯" >> ${logfile} - ifup wan >/dev/null 2>&1 - sleep 60 - fi -fi - -# 重拨尝试获取公网 -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 - echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】重拨尝试获取公网 ip,当前第 $public_ip_count 次 " >> ${logfile} - # 等待 60 秒后重新获取 ip 并写入 - ifup wan >/dev/null 2>&1 - sleep 60 - local wanIP=`getip wanipv4` - local hostIP=`getip hostipv4` - if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "1" ] ; then local IPv4=${wanIP};fi - if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "2" ] ; then local IPv4=${hostIP};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 ${wanIP} ] && [ ! -z ${hostIP} ] && ( ! echo ${wanIP} | grep -q -w ${hostIP} );then - echo IPv4 $IPv4 > ${dir}ip - echo -e IPv6 $last_IPv6 >> ${dir}ip + [ -z "$err_enable" ] || [ "$err_enable" -ne "1" ] && return + [ ! -z "$err_sheep_enable" ] && [ "$err_sheep_enable" -eq "1" ] && [ -z "$sheep_starttime" ] && return + geterrdevicealiases;[ $? -eq "1" ] && local err_mac="1" + [ -z "$err_mac" ] && return + + if [ ! -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 + echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】重启路由器咯" >> ${logfile} + cat ${logfile} > /usr/bin/serverchan/errlog + sleep 2 && reboot && exit + elif [ ! -z "$network_restart_time" ] && [ `ubus call network.interface.wan status|grep \"uptime\"|sed $'s/\"uptime": //g'|sed $'s/\,//g'|awk -F. '{run_hour=$1/3600;printf("%d",run_hour)}'` -ge "$network_restart_time" ] && [ "$system_time_event" -eq "2" ]; then + echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】重新拨号咯" >> ${logfile} + ifup wan >/dev/null 2>&1 + sleep 60 + fi + fi + + [ -z "$public_ip_today" ] && public_ip_today=`date +"%d"` + [ -z "$public_ip_count" ] && public_ip_count="0" + [ $public_ip_today -ne `date +"%d"` ] && public_ip_today=`date +"%d"` && public_ip_count=1 + if [ ! -z "$public_ip_event" ] && [ ! -z "$public_ip_retry_count" ] && [ "$public_ip_count" -le "$public_ip_retry_count" ]; then + public_ip_count=`expr $public_ip_count + 1` + local wanIP=`getip wanipv4` + local hostIP=`getip hostipv4` + if [ ! -z "$wanIP" ] && [ ! -z "$hostIP" ] && ( ! echo "$wanIP"|grep -q -w ${hostIP} );then + echo "`date "+%Y-%m-%d %H:%M:%S"` 【无人值守任务】重拨尝试获取公网 ip,当前第 $public_ip_count 次 " >> ${logfile} + ifup wan >/dev/null 2>&1 + sleep 60 + local wanIP=`getip wanipv4` && local hostIP=`getip hostipv4` + [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "1" ] && local IPv4=${wanIP} + [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "2" ] && local IPv4=${hostIP} + [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "1" ] && local IPv6=`getip wanipv6` + [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "2" ] && local IPv6=`getip hostipv6` + [ ! -z "$wanIP" ] && [ ! -z "$hostIP" ] && ( ! echo "$wanIP"|grep -q -w ${hostIP} ) && echo IPv4 $IPv4 > ${dir}ip && echo -e IPv6 $last_IPv6 >> ${dir}ip fi fi -fi } # 检测网络状态 function rand_geturl(){ local urllist="https://www.163.com https://www.qq.com https://www.baidu.com https://www.qidian.com https://www.douban.com" - local url_str=$( echo "$urllist" | awk -v i="`awk 'BEGIN{srand();sum=rand()*5+ 1;printf("%d",sum)}'`" '{print $i}' ) - local check=`curl -k -s -w "%{http_code}" $url_str -A "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11" -o /dev/null` - local retry_count=0 - - while [ -z "$check" ] || [ "$check" -ne "200" ]; - do - local check=`curl -k -s -w "%{http_code}" $url_str -A "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11" -o /dev/null` - if [ ! -z "$check" ] && [ "$check" -eq "200" ] ; then - if [ ! -z "$network_enable" ] && [ "$network_enable" -eq "404" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【网络状态】网络恢复正常.." >> ${logfile};fi + local url_str=$( echo "$urllist"|awk -v i="`awk 'BEGIN{srand();sum=rand()*5+ 1;printf("%d",sum)}'`" '{print $i}' ) + function getcheck(){ + echo `curl -k -s -w "%{http_code}" ${url_str} -A "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11" -o /dev/null` + } + local check=`getcheck` + while [ -z "$check" ] || [ "$check" -ne "200" ]; do + local check=`getcheck` + if [ ! -z "$check" ] && [ "$check" -eq "200" ]; then + [ ! -z "$network_enable" ] && [ "$network_enable" -eq "404" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【网络状态】网络恢复正常.." >> ${logfile} local network_enable="200" else - if [ -z "$network_enable" ] || [ "$network_enable" -eq "200" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】当前网络不通!停止检测! " >> ${logfile};fi + [ -z "$network_enable" ] || [ "$network_enable" -eq "200" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】当前网络不通!停止检测! " >> ${logfile} local network_enable="404" - - if [ -z "$network_err_time" ] ; then local network_err_time=`date +%s`;fi - # 网络断开超过十分钟,检测是否执行网络重置 - if [ ! -z "$network_err_event" ] && [ "$((`date +%s`-$network_err_time))" -ge "600" ] ; then - # 查找列表 MAC 是否在线 + [ -z "$network_err_time" ] && network_err_time=`date +%s` + if [ ! -z "$network_err_event" ] && [ "$((`date +%s`-$network_err_time))" -ge "600" ]; then > ${dir}send_enable.lock && serverchan_first && deltemp - geterrdevicealiases - if [ $? -eq "1" ] ; then err_mac="1";fi - - # 关注设备不在线,尝试修复网络 - if [ -z "$err_mac" ] ; then - if [ -f /usr/bin/serverchan/autoreboot_count ] ; then - retry_count=`cat /usr/bin/serverchan/autoreboot_count` - rm -f /usr/bin/serverchan/autoreboot_count >/dev/null 2>&1 - fi - retry_count=`expr $retry_count + 1` - - # 修复方式为重启路由器 + geterrdevicealiases;[ $? -eq "1" ] && local err_mac="1" + if [ -z "$err_mac" ]; then + [ -f /usr/bin/serverchan/autoreboot_count ] && retry_count=`cat /usr/bin/serverchan/autoreboot_count` && rm -f /usr/bin/serverchan/autoreboot_count >/dev/null 2>&1 + [ ! -z ${retry_count} ] && retry_count=0;retry_count=`expr $retry_count + 1` if [ "$network_err_event" -eq "1" ] ;then if [ "$retry_count" -lt "3" ] ;then echo "$retry_count" > /usr/bin/serverchan/autoreboot_count - echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试重启网络,当前第 $retry_count 次 " >> ${logfile} + echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试重启路由,当前第 $retry_count 次 " >> ${logfile} cat ${logfile} > /usr/bin/serverchan/errlog - sleep 2 - reboot - exit - elif [ "$retry_count" -eq "3" ] ;then - echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】已经重启路由2次,修复失败,请主人自行修复哦" >> ${logfile} + sleep 2 && reboot && exit fi - fi - - # 修复方式为重启网络接口 - if [ "$network_err_event" -eq "2" ] ;then - if [ "$retry_count" -lt "3" ] ;then - echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试重启网络,当前第 $retry_count 次 " >> ${logfile} - network_restart 60 - elif [ "$retry_count" -eq "3" ] ;then - echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】已经重启网络2次,修复失败,请主人自行修复哦 " >> ${logfile} - fi - fi - - # 修复方式为修改相关设置项,尝试修复网络 - if [ "$network_err_event" -eq "3" ] ;then - # 重启网络 + [ "$retry_count" -eq "3" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】已经重启路由2次,修复失败,请主人自行修复哦" >> ${logfile} + elif [ "$network_err_event" -eq "2" ] ;then + [ "$retry_count" -lt "3" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试重启网络,当前第 $retry_count 次 " >> ${logfile} && ifup wan >/dev/null 2>&1 + [ "$retry_count" -eq "3" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】已经重启网络2次,修复失败,请主人自行修复哦 " >> ${logfile} + elif [ "$network_err_event" -eq "3" ] ;then if [ "$retry_count" -eq "1" ] ;then - echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试修复网络,当前第 1 次,重启网络服务中 " >> ${logfile} - network_restart 60 + echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试修复网络,当前第 1 次,重启网络服务中 " >> ${logfile} && network_restart elif [ "$retry_count" -eq "2" ] ;then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试修复网络,当前第 2 次,关闭可能造成网络断开的软件" >> ${logfile} - # 备份设置项 - if [ `uci get koolproxy.@global[0].enabled 2>/dev/null` -eq "1" ] && [ `uci get koolproxy.@global[0].koolproxy_mode 2>/dev/null` -eq "1" ] ;then /etc/init.d/koolproxy stop;fi - if [ `uci get adbyby.@adbyby[0].enable 2>/dev/null` -eq "1" ] && [ `uci get adbyby.@adbyby[0].wan_mode 2>/dev/null` -eq "0" ] ;then /etc/init.d/adbyby stop;fi - if [ `uci get passwall.@global[0].enabled 2>/dev/null` -eq "1" ] && [ `uci get passwall.@global[0].proxy_mode 2>/dev/null | grep global` ];then /etc/init.d/koolproxy stop;fi - shadowsocksr_enabled=`uci get shadowsocksr.@global[0].global_server 2>/dev/null | grep nil` - shadowsocksr_run_mode=`uci get shadowsocksr.@global[0].run_mode 2>/dev/null | grep all` - if [ -z "$shadowsocksr_enabled" ] && [ ! -z "$shadowsocksr_run_mode" ];then /etc/init.d/shadowsocksr stop;fi - network_restart 60 + [ `uci get koolproxy.@global[0].enabled 2>/dev/null` -eq "1" ] && [ `uci get koolproxy.@global[0].koolproxy_mode 2>/dev/null` -eq "1" ] && /etc/init.d/koolproxy stop >/dev/null 2>&1 + [ `uci get adbyby.@adbyby[0].enable 2>/dev/null` -eq "1" ] && [ `uci get adbyby.@adbyby[0].wan_mode 2>/dev/null` -eq "0" ] && /etc/init.d/adbyby stop >/dev/null 2>&1 + [ `uci get passwall.@global[0].enabled 2>/dev/null` -eq "1" ] && [ `uci get passwall.@global[0].proxy_mode 2>/dev/null|grep global` ] && /etc/init.d/koolproxy stop >/dev/null 2>&1 + local shadowsocksr_enabled=`uci get shadowsocksr.@global[0].global_server 2>/dev/null|grep nil` + local shadowsocksr_run_mode=`uci get shadowsocksr.@global[0].run_mode 2>/dev/null|grep all` + [ -z "$shadowsocksr_enabled" ] && [ ! -z "$shadowsocksr_run_mode" ] && /etc/init.d/shadowsocksr stop >/dev/null 2>&1 + sleep 60 && network_restart elif [ "$retry_count" -eq "3" ] ;then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试修复网络,当前第 3 次,备份设置项,并修改相关设置" >> ${logfile} - # 备份设置项 mkdir -p /usr/bin/serverchan/configbak cp -p -f /etc/config/network /usr/bin/serverchan/configbak/network cp -p -f /etc/config/dhcp /usr/bin/serverchan/configbak/dhcp cp -p -f /etc/config/firewall /usr/bin/serverchan/configbak/firewall cp -p -f /etc/firewall.user /usr/bin/serverchan/configbak/firewall.user - # 设置 dns 为自动获取 - #uci set network.wan.peerdns='1' - #uci delete network.wan.dns - # 设置 dns 为阿里+腾讯 uci set network.wan.peerdns='0' uci delete network.wan.dns uci add_list network.wan.dns='223.5.5.5' uci add_list network.wan.dns='119.29.29.29' - # 还原 MTU 为默认获取 uci delete network.wan.mtu uci commit network - # 还原 dns 重定向 uci set dhcp.@dnsmasq[0].port='53' uci set dhcp.@dnsmasq[0].resolvfile='/tmp/resolv.conf.auto' uci delete dhcp.@dnsmasq[0].server uci delete dhcp.@dnsmasq[0].noresolv uci commit dhcp - # 删除静态租约 - #uci delete dhcp.host - # 删除防火墙转发规则 uci delete firewall.redirect - # 删除防火墙自定义规则 >/etc/firewall.user uci commit firewall - network_restart 60 + sleep 60 && network_restart elif [ "$retry_count" -eq "4" ] ;then echo "$retry_count" > /usr/bin/serverchan/autoreboot_count cat ${logfile} > /usr/bin/serverchan/errlog - sleep 2 - reboot + sleep 2 && reboot && exit elif [ "$retry_count" -eq "5" ] ;then echo "$retry_count" > /usr/bin/serverchan/autoreboot_count echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】修复失败,还原设置中,请自行检查网络设置" >> ${logfile} - # 还原设置项 cp -p -f /usr/bin/serverchan/configbak/network /etc/config/network cp -p -f /usr/bin/serverchan/configbak/dhcp /etc/config/dhcp cp -p -f /usr/bin/serverchan/configbak/firewall /etc/config/firewall cp -p -f /usr/bin/serverchan/configbak/firewall.user /etc/firewall.user - #rm -rf /usr/bin/serverchan/configbak >/dev/null 2>&1 - # 如果 LAN 口误设置为 pppoe 则改为静态分配,不还原,点错懒得捅菊花就拔网线等十分钟 - lan_proto=`uci get network.lan.proto 2>/dev/null | grep pppoe` - if [ ! -z "$lan_proto" ];then uci set network.lan.proto='static' && uci set network.lan.ipaddr='192.168.1.1' && uci set network.lan.netmask='255.255.255.0' && uci commit network;fi cat ${logfile} > /usr/bin/serverchan/errlog - sleep 2 - reboot + sleep 2 && reboot && exit fi fi fi - elif [ -f /usr/bin/serverchan/autoreboot_count ] ; then - network_err_time=`expr $network_err_time - 600` - sleep 60 + elif [ -f /usr/bin/serverchan/autoreboot_count ]; then + network_err_time=`expr $network_err_time - 600` && sleep 60 fi enable_detection - sleep $sleeptime + sleep 60 fi continue done @@ -682,222 +487,158 @@ 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 + [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "1" ] && local IPv4=`getip wanipv4` + [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -eq "2" ] && local IPv4=`getip hostipv4` + [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "1" ] && local IPv6=`getip wanipv6` + [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "2" ] && local IPv6=`getip hostipv6` -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 + 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" ] && [ ! -z "$IPv4" ] && ( ! echo ${IPv4}|grep -w -q ${last_IPv4} ); then 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 + 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 + content="${content}${markdown_splitline}####IP 地址变化${markdown_linefeed}${markdown_tab}当前 IP:${IPv4}" + elif [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] && [ -z "$IPv4" ]; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】获取 IPv4 地址失败" >> ${logfile} fi - 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 + + if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] && [ ! -z "$IPv6" ] && ( ! echo "$IPv6"|grep -w -q ${last_IPv6} ); then 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 IPv4 $IPv4 > ${dir}ip && echo -e IPv6 $IPv6 >> ${dir}ip + [ -z "$title" ] && title="IPv6 地址变化" + [ ! -z "$title" ] && title="IP 地址变化" + content="${content}${markdown_splitline}####IPv6 地址变化${markdown_linefeed}${markdown_tab}当前 IPv6:${IPv6}" + elif [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] && [ -z "$IPv6" ]; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】获取 IPv6 地址失败" >> ${logfile} fi + + else + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}路由器已经重启!" >> ${logfile} + [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 当前IP: ${IPv4}" >> ${logfile} + [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 当前IPv6: ${IPv6}" >> ${logfile} + echo IPv4 $IPv4 > ${dir}ip && echo -e IPv6 $IPv6 >> ${dir}ip + title="路由器重新启动" + content="${content}${markdown_splitline}####路由器重新启动" + [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] && content="${content}${markdown_linefeed}${markdown_tab}当前IP:${IPv4}" + [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ] && content="${content}${markdown_linefeed}${markdown_tab}当前IPv6:${IPv6}" 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 >/dev/null 2>&1;fi -fi -} + + if [ ! -z "$content" ] ;then + [ -z "$ddns_enabled" ] && ddns_enabled=$(uci show ddns|grep "enabled"|grep "1") + [ -z "$ddns_enabled" ] && ddns_logrow=0 || ddns_logrow=$(echo "$ddns_enabled"|wc -l) + if [ $ddns_logrow -ge 1 ]; then + /etc/init.d/ddns stop >/dev/null 2>&1 + sleep 10 + /etc/init.d/ddns start >/dev/null 2>&1 + fi + fi +} # 检测设备上线 function up(){ -if [ "$1" ] ;then - # 如果 ipAddress 文件中存在此ip,则跳过,待断线检测 - local ip=`cat ${dir}ipAddress | grep -w $1` - if [ -z "$ip" ] ; then - 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 | grep -v '100% packet loss'` 2>/dev/null - if ( ! echo ${ip_ms} | grep -q ms );then - enable_detection 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 - # 刚刚掉线又重连了 - 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 - cat ${dir}tmp_downlist | grep -w $1 | grep -v "^$" | sort -u >> ${dir}ipAddress - sed -i "/$1/d" ${dir}tmp_downlist - # 真·刚上线 + [ -f ${dir}ipAddress ] && ( cat ${dir}ipAddress|grep -q -w $1 ) && return + local ip_mac=`getmac $1` + local ip_interface=`getinterface ${ip_mac}` + [ "$iw_version" ] && local wlan_online=`iw dev ${ip_interface} station dump|grep -i -w ${ip_mac}|grep Station` >/dev/null 2>&1 + getping ${1} ${up_timeout} "1";local ping_online=$? + if [ ! -z "$wlan_online" ] || [ "$ping_online" -eq "0" ]; then + LockFile lock + [ -f "${dir}tmp_downlist" ] && local tmp_downip=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u` + if [ ! -z "$tmp_downip" ]; then + cat ${dir}tmp_downlist|grep -w ${1}|grep -v "^$"|sort -u >> ${dir}ipAddress + sed -i "/$1/d" ${dir}tmp_downlist + else + usage down $1 + local ip_name=`getname $1 ${ip_mac}` + blackwhitelist ${ip_mac};local ip_blackwhite=$? + echo "$1 ${ip_mac} ${ip_name} `date +%s` ${ip_interface}" >> ${dir}ipAddress + [ -f "${dir}send_enable.lock" ] || [ -z "$serverchan_up" ] || [ "$serverchan_up" -ne "1" ] || [ -z "$ip_blackwhite" ] || [ "$ip_blackwhite" -ne 0 ] && LockFile unlock && return + [ -f "${dir}title" ] && local title=`cat ${dir}title` + [ -f "${dir}content" ] && local content=`cat ${dir}content` + if [ -z "$title" ]; then + local title="$ip_name 连接了你的路由器" + local content="${markdown_splitline}####新设备连接${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_splitline}${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 - usage down $1 - 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 - 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 - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 新设备 ${ip_name} ${1} 连接了" >> ${logfile};fi + local title="设备状态变化" + local content="${markdown_splitline}####新设备连接${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 + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}新设备 ${ip_name} ${1} 连接了">> ${logfile} + [ ! -z "$serverchan_blacklist" ] && local title="你偷偷关注的设备上线了" + [ ! -z "$title" ] && echo "$title" >${dir}title + [ ! -z "$content" ] && echo -n "$content" >>${dir}content 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 } # 检测设备离线 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=`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 | grep -v '100% packet loss'` 2>/dev/null - if ( ! echo ${ip_ms} | grep -q ms );then - enable_detection 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 - # 如果连接失败 - else + [ "$iw_version" ] && local wlan_down=`iw dev ${ip_interface} station dump|grep -i -w ${ip_mac}|grep Station` >/dev/null 2>&1 + getping ${1} ${down_timeout} ${timeout_retry_count};local ping_online=$? + if [ -z "$wlan_down" ] && [ "$ping_online" -eq "1" ]; then 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 - fi + [ ! -f "${dir}send_enable.lock" ] && cat ${dir}ipAddress|grep -w ${1}|grep -v "^$"|sort -u >> ${dir}tmp_downlist sed -i "/$1/d" ${dir}ipAddress LockFile unlock fi -fi } # 设备离线通知 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 - 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 + [ ! -f "${dir}tmp_downlist" ] && return + local IPLIST=`cat ${dir}tmp_downlist|awk '{print $1}'` + for ip in $IPLIST; do + local ip_mac=`getmac ${ip}` + blackwhitelist ${ip_mac};local ip_blackwhite=$? + [ -z "$serverchan_down" ] || [ "$serverchan_down" -ne "1" ] || [ -z "$ip_blackwhite" ] || [ "$ip_blackwhite" -ne 0 ] && continue + local ip_name=`getname ${ip} ${ip_mac}` + local time_up=`cat ${dir}tmp_downlist|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u` + local ip_total=`usage get $ip` && [ ! -z "$ip_total" ] && local ip_total="${markdown_linefeed}${markdown_tab}总计流量: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_total}" + local time1=`date +%s` + local time1=$(time_for_humans `expr ${time1} - ${time_up}`) + 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 + content="${content}${markdown_splitline}####设备断开连接${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}" + content="${content}${markdown_splitline}${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}" + content="${content}${markdown_splitline}####设备断开连接${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 + done + rm -f ${dir}tmp_downlist >/dev/null 2>&1 } # 当前设备列表 function current_device(){ - 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}' ) + [ -f ${dir}ipAddress ] && local logrow=$(grep -c "" ${dir}ipAddress) || local logrow="0";[ $logrow -eq "0" ] && return + [ -f ${dir}usage.db ] && local ip_total_db="总计流量${markdown_space}${markdown_space}${markdown_space}${markdown_space}" + content="${content}${markdown_splitline}####现有在线设备 ${logrow} 台,具体如下${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 [ `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 + [ `length_str "$ip_name"` -gt "15" ] && local ip_name=`echo ${ip_name:0:15}"*"` + if [ "${#ip}" -lt "15" ]; then + local n=`expr 15 - ${#ip}` + for i in `seq 1 $n`; do 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 + if [ ! -z "$ip_total" ]; then + local n=`expr 11 - ${#ip_total}` + for i in `seq 1 $n`; do local ip_total="${ip_total}${markdown_space}" done fi @@ -908,172 +649,136 @@ function current_device(){ # 检测 cpu 状态 function cpu_load(){ - 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 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} - elif [ -z "$cpu_fuzai" ] ; then - 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 - 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 - fi + if [ ! -z "$temperature_enable" ] && [ "$temperature_enable" -eq "1" ] && [ ! -z "$temperature" ]; then + [ -z "$temperature_time" ] && temperature_time=`date +%s` + local cpu_wendu=`soc_temp`; + [ -z "$cpu_wendu" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取设备温度,请检查命令" >> ${logfile} - if [ "$((`date +%s`-$cpuload_time))" -ge "300" ] && [ -z "$cpucd_time" ] ; then - title="CPU 负载过高!" - local cpu_alert=1 - if [ -z "$cpucd_time" ] ; then cpucd_time=1;fi - 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 - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] ; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【温度】正在检测 CPU 温度" >> ${logfile};fi - local cpu_wendu=`soc_temp` - local temperature=`get_config temperature` - if [ ! -z "$cpu_wendu" ] && [ ! -z "$temperature" ] && [ "$cpu_wendu" -gt "$temperature" ] ; then + if [ "$cpu_wendu" -gt "$temperature" ]; then echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!警报!!】 CPU 温度过高: ${cpu_wendu}" >> ${logfile} - elif [ -z "$cpu_wendu" ] ; then - 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 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 fi - - if [ "$((`date +%s`-$temperature_time))" -ge "300" ] && [ -z "$temperaturecd_time" ] ; then - if [ ! -z "$title" ] && ( echo ${title} | grep -q "过高" ) ; then - title="设备报警!" - else - title="CPU 温度过高!" - fi - local cpu_alert=1 - if [ -z "$temperaturecd_time" ] ; then temperaturecd_time=1;fi + + if [ "$((`date +%s`-$temperature_time))" -ge "300" ] && [ -z "$temperaturecd_time" ]; then + title="CPU 温度过高!" + temperaturecd_time=`date +%s` 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 + content="${content}${markdown_splitline}####CPU 温度过高${markdown_linefeed}${markdown_tab}CPU 温度已连续五分钟超过预设${markdown_linefeed}${markdown_tab}接下来一小时不再提示${markdown_linefeed}${markdown_tab}当前温度:${cpu_wendu}" + elif [ ! -z "$temperaturecd_time" ] && [ "$((`date +%s`-$temperaturecd_time))" -ge "3300" ] ;then unset temperaturecd_time fi fi - # 占用前三的进程 - if [ ! -z "$cpu_alert" ] && [ "$cpu_alert" -eq "1" ] ; then - top -bn 1 >${dir}top - 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 + + if [ ! -z "$cpuload_enable" ] && [ "$cpuload_enable" -eq "1" ] && [ ! -z "$cpuload" ]; then + [ -z "$cpuload_time" ] && cpuload_time=`date +%s` + local cpu_fuzai=`cat /proc/loadavg|awk '{print $1}'` 2>/dev/null + [ -z "$cpu_fuzai" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取设备负载,请检查命令" >> ${logfile} + + if [ `expr $cpu_fuzai \> $cpuload` -eq "1" ]; then + echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!警报!!】 CPU 负载过高: ${cpu_fuzai}" >> ${logfile} + cputop log + else + cpuload_time=`date +%s` + fi + + if [ "$((`date +%s`-$cpuload_time))" -ge "300" ] && [ -z "$cpucd_time" ]; then + unset getlogtop + if [ ! -z "$title" ] && ( echo "$title"|grep -q "过高" ); then + title="设备报警!" else - 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` + title="CPU 负载过高!" fi - local top_i=`expr $top_i + 1` - done - rm -f ${dir}top >/dev/null 2>&1 + cpucd_time=`date +%s` + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text} CPU 负 载过高: ${cpu_fuzai}" >> ${logfile} + content="${content}${markdown_splitline}####CPU 负载过高${markdown_linefeed}${markdown_tab}CPU 负载已连续五分钟超过预设${markdown_linefeed}${markdown_tab}接下来一小时不再提示${markdown_linefeed}${markdown_tab}当前负载:${cpu_fuzai}" + cputop + elif [ ! -z "$cpucd_time" ] && [ "$((`date +%s`-$cpucd_time))" -ge "3300" ] ;then + unset cpucd_time + fi fi } +function cputop(){ + [ -z "$1" ] && content="${content}${markdown_splitline}####当前 CPU 占用前三的进程" + local i=1 && local top_i=5 && `top -bn 1 > ${dir}top` >/dev/null 2>&1 + while [ $i -le 3 ]; do + if ( ! cat ${dir}top|awk 'NR=='${top_i}|grep -q "top -bn 1" ); then + local top_name=`cat ${dir}top|awk 'NR=='${top_i}|awk '{print $8}'`;[ "$top_name" == "/bin/sh" ] || [ "$top_name" == "/bin/bash" ] && local top_name=`cat ${dir}top|awk 'NR=='${top_i}|awk '{print $9}'` + local top_load=`cat ${dir}top|awk 'NR=='${top_i}|awk '{print $7}'` + local temp_top="${top_name} ${top_load}" + [ ! -z "$1" ] && local logtop="$logtop $temp_top" + [ -z "$1" ] && content="${content}${markdown_linefeed}${markdown_tab}${temp_top}25" + local i=`expr ${i} + 1` + fi + local top_i=`expr ${top_i} + 1` + done + [ ! -z "$1" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!警报!!】 CPU 占用前三: ${logtop}" >> ${logfile} + rm -f ${dir}top >/dev/null 2>&1 +} + # 发送定时数据 function send(){ 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` - local router_temp=`get_config router_temp` - local router_wan=`get_config router_wan` - local send_wanIP=`getip wanipv4` - local send_hostIP=`getip hostipv4` - local send_wanIPv6=`getip wanipv6` - local send_hostIPv6=`getip hostipv6` - if [ -z "$send_title" ] ; then local send_title="路由状态:";fi - if [ ! -z "$client_list" ] && [ "$client_list" -eq "1" ] ; then - > ${dir}send_enable.lock - serverchan_first & - fi - # 系统运行状态 - if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ] ; then - local systemload=`cat /proc/loadavg | awk '{print $1" "$2" "$3}'` + serverchan_disturb;local send_disturb=$? + get_config "send_title" "router_status" "client_list" "router_temp" "router_wan" + + [ -z "$send_title" ] && local send_title="路由状态:" + [ ! -z "$client_list" ] && [ "$client_list" -eq "1" ] && > ${dir}send_enable.lock && serverchan_first & + + 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 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)}'`;unset run_days run_hour run_minute run_second + local send_content="${send_content}${markdown_splitline}#### 系统运行状态" 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}CPU占用:${cpuload}25" + local send_content="${send_content}${markdown_linefeed}${markdown_tab}内存占用:${ramload}25" 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}${markdown_linefeed}---${markdown_linefeed}#### WAN 口信息${markdown_linefeed}${markdown_tab}接口ip:${send_wanIP}" + + [ ! -z "$router_temp" ] && [ "$router_temp" -eq "1" ] && local cputemp=`soc_temp` && local send_content="${send_content}${markdown_splitline}#### 设备温度${markdown_linefeed}${markdown_tab}CPU:${cputemp}%e2%84%83" #%e2%84%83="℃" + + if [ ! -z "$router_wan" ] && [ "$router_wan" -eq "1" ]; then + local send_wanIP=`getip wanipv4`;local send_hostIP=`getip hostipv4` + local send_content="${send_content}${markdown_splitline}#### 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 + if [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -ne "0" ]; then + local send_wanIPv6=`getip wanipv6`;local send_hostIPv6=`getip 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}${markdown_linefeed}${markdown_tab}外网 ip 与接口 ip 不一致,你的 ip 不是公网 ip";fi - else - local send_content="${send_content}${markdown_linefeed}${markdown_tab}ip 获取失败" - fi - 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)}'` + ( ! echo "$send_wanIP"|grep -q -w ${send_hostIP} ) && local send_content="${send_content}${markdown_linefeed}${markdown_tab}外网 ip 与接口 ip 不一致,你的 ip 不是公网 ip" + 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)}'`;unset 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 + + if [ ! -z "$client_list" ] && [ "$client_list" -eq "1" ]; then wait - local send_content="${send_content}${markdown_linefeed}---${markdown_linefeed}#### 在线设备" - local IPLIST=$( cat ${dir}ipAddress | awk '{print $1}' ) + + local IPLIST=`cat ${dir}ipAddress 2>/dev/null|awk '{print $1}'` + [ -z "$IPLIST" ] && local send_content="${send_content}${markdown_splitline}#### 当前无在线设备" || local send_content="${send_content}${markdown_splitline}#### 在线设备" for ip in $IPLIST; do - 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 time1=$(($time_down-$time_up)) - local time1=`time_for_humans ${time1}` + local ip_total=`usage get ${ip}`;[ ! -z "$ip_total" ] && local ip_total="总计流量:${ip_total} " + local time_up=`cat ${dir}ipAddress|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u` + local time1=`date +%s` + local time1=$(time_for_humans `expr ${time1} - ${time_up}`) 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 + [ `length_str "$ip_name"` -gt "18" ] && local ip_name=`echo ${ip_name:0:18}"*"` 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 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="${markdown_linefeed}---${markdown_linefeed}#### 我遇到了一个难题${markdown_linefeed}${markdown_tab}定时发送选项错误,你没有选择需要发送的项目,该怎么办呢${markdown_linefeed}---${markdown_linefeed}"; - fi + [ ! -z "$device_name" ] && local send_title="【$device_name】${send_title}" + 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'` + [ -z "$send_content" ] && local send_content="${markdown_splitline}#### 我遇到了一个难题${markdown_linefeed}${markdown_tab}定时发送选项错误,你没有选择需要发送的项目,该怎么办呢${markdown_splitline}" local nowtime=`date "+%Y-%m-%d %H:%M:%S"` - 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 + [ "$send_disturb" -eq "0" ] && [ -z "$send_tg" ] && curl -s "http://sc.ftqq.com/${sckey}.send?text=${send_title}" -d "&desp=${nowtime}${markdown_linefeed}${send_content}" >/dev/null 2>&1 + [ "$send_disturb" -eq "0" ] && [ ! -z "$send_tg" ] && [ "$send_tg" -eq "1" ] && curl -d "text=${send_title}${markdown_linefeed}${nowtime}${markdown_linefeed}${send_content}" -X POST "${tgtoken}" >/dev/null 2>&1 deltemp + echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}定时推送任务完成" >> ${logfile} } # 初始化 @@ -1082,96 +787,71 @@ deltemp serverchan_cron # 限制并发进程 -thread_num=4 +[ -z "$thread_num" ] || [ "$thread_num" -eq "0" ] && thread_num=5 [ -e ${dir}fd1 ] || mkfifo ${dir}fd1 exec 5<>${dir}fd1 rm -f ${dir}fd1 >/dev/null 2>&1 -for i in `seq 1 $thread_num` -do +for i in `seq 1 $thread_num`; do echo >&5 done unset i # 启动参数 if [ "$1" ] ;then - if [ $1 = "send" ] ;then send && exit;fi - if [ $1 = "soc" ] ;then echo `soc_temp` > /tmp/serverchan/soc_tmp && exit;fi + [ $1 == "send" ] && send + [ $1 == "soc" ] && echo `soc_temp` > /tmp/serverchan/soc_tmp + exit fi # 载入在线设备 -serverchan_init +serverchan_init;[ $? -eq 1 ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】读取设置出错,请检查设置项 " >> ${logfile} && exit echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】载入在线设备" >> ${logfile} > ${dir}send_enable.lock && serverchan_first && deltemp echo "`date "+%Y-%m-%d %H:%M:%S"` 【初始化】初始化完成" >> ${logfile} # 循环 -while [ "$serverchan_enable" -eq "1" ] ; -do +while [ "$serverchan_enable" -eq "1" ]; do deltemp usage update - serverchan_disturb - disturb=$? + serverchan_disturb;disturb=$? # 外网IP变化检测 - if [ ! -z "$serverchan_ipv4" ] && [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv4" -ne "0" ] || [ "$serverchan_ipv6" -ne "0" ] ; then + if [ ! -z "$serverchan_ipv4" ] && [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv4" -ne "0" ] || [ "$serverchan_ipv6" -ne "0" ]; then rand_geturl ip_changes fi # 设备列表 - 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 + if [ ! -f "${dir}send_enable.lock" ]; then + [ ! -z "$title" ] && echo "$title" > ${dir}title + [ ! -z "$content" ] && echo "$content" > ${dir}content 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 + [ -f "${dir}title" ] && title=`cat ${dir}title` && rm -f ${dir}title >/dev/null 2>&1 + [ -f "${dir}content" ] && content=`cat ${dir}content` && rm -f ${dir}content >/dev/null 2>&1 fi # 离线缓存区推送 - if [ ! -f "${dir}send_enable.lock" ] ; then - down_send - fi + [ ! -f "${dir}send_enable.lock" ] && down_send # 当前设备列表 - if [ ! -z "$content" ] && [ ! -f "${dir}send_enable.lock" ] ; then - current_device - fi + [ ! -z "$content" ] && [ ! -f "${dir}send_enable.lock" ] && current_device # 无人值守任务 - if [ ! -f "${dir}send_enable.lock" ] ; then - unattended - fi + [ ! -f "${dir}send_enable.lock" ] && unattended # CPU 检测 - if [ ! -f "${dir}send_enable.lock" ] ; then - cpu_load - fi + [ ! -f "${dir}send_enable.lock" ] && cpu_load - if [ ! -f "${dir}send_enable.lock" ] && [ ! -z "$title" ] && [ ! -z "$content" ] ; then + 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 "1" ] ;then - curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=${title}" -d "desp=${nowtime}${markdown_linefeed}${content}" >/dev/null - fi + [ ! -z "$device_name" ] && title="【$device_name】$title" + 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'` + [ "$disturb" -eq "0" ] && [ -z "$send_tg" ] && curl -s "http://sc.ftqq.com/${sckey}.send?text=${title}" -d "desp=${nowtime}${markdown_linefeed}${content}" >/dev/null 2>&1 + [ "$disturb" -eq "0" ] && [ ! -z "$send_tg" ] && [ "$send_tg" -eq "1" ] && curl -d "text=${title}${markdown_linefeed}${nowtime}${markdown_linefeed}${content}" -X POST "${tgtoken}" >/dev/null 2>&1 fi - if [ -f "${dir}send_enable.lock" ] ;then - send_str=0 - while [ -f "${dir}send_enable.lock" ] ; - do - if [ ! -z "$debuglevel" ] && [ "$debuglevel" -ne "1" ] && [ "$send_str" -eq "0" ] ; then - echo "-----------------------------------------------------" >> ${logfile} - echo "`date "+%Y-%m-%d %H:%M:%S"` 发送定时数据,程序暂停中" >> ${logfile} - echo ""-----------------------------------------------------"" >> ${logfile} - send_str=1 - fi + while [ -f "${dir}send_enable.lock" ]; do sleep $sleeptime - continue - done - continue - fi + done sleep $sleeptime -continue -done +done \ No newline at end of file