使用RouterOS进行线路分流,需要监测旁路由的线路状态。通过检查Google Generate 204状态进行判断。此服务是谷歌提供的用于检查网络状态的API,正常情况下返回204状态码。谷歌的众多服务都提供此API,为了检测准确性,我们可以添加多个检测地址。 ### HAProxy状态检测配置 旁路以OpenWrt为例,首先需要安装HAProxy。 ``` opkg update opkg install haproxy ``` HAProxy配置文件位于`/etc/haproxy.cfg`。添加用于检测的前端和后端。监听所有接口上的99端口(由于RouterOS的NetWatch不支持URL检测,所以这里只能用根目录),访问http://0.0.0.0:99/,正常情况下会返回200状态码,如果后端异常将返回503状态码。状态码的判断由monitor fail指定的条件决定,if语句判断后端可用服务数量,如果为0则返回ture,即触发monitor fail。我这里只添加了两个服务端,只要其中一个可用,那么整体就认为服务可达。 ``` frontend passwall_status bind :99 mode http monitor-uri / monitor fail if { nbsrv(gererate204) eq 0 } backend gererate204 balance roundrobin default-server check inter 2000 rise 1 fall 1 maxconn 20 option httpchk HEAD /generate_204 server server01 clients1.google.com:80 server server02 clients1.google.com:80 ``` 如果希望通过HAProxy页面查看监控状态,可以添加如下配置,通过浏览器访问`192.168.88.2:81/stats`即可查看。 ``` frontend haproxy_status bind :81 mode http stats enable stats hide-version stats uri /stats stats realm HA_Stats stats auth admin:123456 ``` ### NetWatch配置 我们先通过CURL测试一下HAProxy是否工作正常。 ``` xueye@MacBook ~ % curl -I http://192.168.88.2:99/ HTTP/1.1 200 OK content-length: 58 cache-control: no-cache content-type: text/html ``` 可以尝试关闭旁路由上的某些服务,然后再次观察测试结果。 ``` xueye@MacBook ~ % curl -I http://192.168.88.2:99/ HTTP/1.1 503 Service Unavailable content-length: 107 cache-control: no-cache content-type: text/html ``` 状态检测正常,最后就可以在RouterOS中配置NwtWatch,通过up-script和down-script在状态切换时执行脚本,实现更加复杂的功能。 ``` /tool netwatch add host=192.168.88.2 port=99 interval=10s \ up-script=switch_gfw_dns down-script=switch_cn_dns ``` ``` /system script add dont-require-permissions=yes name=switch_gfw_dns owner=admin policy=read,write source=":fo\ reach i in=[/ip firewall mangle find comment=\"breakwall\"] do={\ \n /ip firewall mangle enable \$i\ \n}\ \n\ \n/ip dns set server=192.168.88.2\ \n/ip dns cache flush" ``` ``` /system script add dont-require-permissions=yes name=switch_cn_dns owner=admin policy=read,write source=":for\ each i in=[/ip firewall mangle find comment=\"breakwall\"] do={\ \n /ip firewall mangle disable \$i\ \n}\ \n\ \n/ip dns set server=218.2.2.2,218.4.4.4\ \n/ip dns cache flush" ``` ### 一些问题 有人会好奇,为什么不在RouterOS直接检测Google Generate 204?一方面,由于切换机制的存在,在旁路宕机的时候,RouterOS会自动切换网关策略路由等,在这种情况下,检测结果永远为false,所以无法自动恢复(也不是绝对的,可以通过脚本尝试恢复并判断,但是显然会对网络产生影响)。另一方面,NetWatch不支持URL,尽管也可以通过脚本实现,但是怎么也比不过内置功能来的高效快捷,所以通过HAProxy来做一个转化。 检测的灵敏度一方面来自于HAProxy中的check,另一方面来自于NetWatch的interval,根据需求选择合适的区间就可以了。 https://www.haproxy.com/documentation/hapee/latest/observability/monitoring/monitor-uri/ https://stackoverflow.com/questions/1989214/google-com-and-clients1-google-com-generate-204 Loading... 使用RouterOS进行线路分流,需要监测旁路由的线路状态。通过检查Google Generate 204状态进行判断。此服务是谷歌提供的用于检查网络状态的API,正常情况下返回204状态码。谷歌的众多服务都提供此API,为了检测准确性,我们可以添加多个检测地址。 ### HAProxy状态检测配置 旁路以OpenWrt为例,首先需要安装HAProxy。 ``` opkg update opkg install haproxy ``` HAProxy配置文件位于`/etc/haproxy.cfg`。添加用于检测的前端和后端。监听所有接口上的99端口(由于RouterOS的NetWatch不支持URL检测,所以这里只能用根目录),访问http://0.0.0.0:99/,正常情况下会返回200状态码,如果后端异常将返回503状态码。状态码的判断由monitor fail指定的条件决定,if语句判断后端可用服务数量,如果为0则返回ture,即触发monitor fail。我这里只添加了两个服务端,只要其中一个可用,那么整体就认为服务可达。 ``` frontend passwall_status bind :99 mode http monitor-uri / monitor fail if { nbsrv(gererate204) eq 0 } backend gererate204 balance roundrobin default-server check inter 2000 rise 1 fall 1 maxconn 20 option httpchk HEAD /generate_204 server server01 clients1.google.com:80 server server02 clients1.google.com:80 ``` 如果希望通过HAProxy页面查看监控状态,可以添加如下配置,通过浏览器访问`192.168.88.2:81/stats`即可查看。 ``` frontend haproxy_status bind :81 mode http stats enable stats hide-version stats uri /stats stats realm HA_Stats stats auth admin:123456 ``` ### NetWatch配置 我们先通过CURL测试一下HAProxy是否工作正常。 ``` xueye@MacBook ~ % curl -I http://192.168.88.2:99/ HTTP/1.1 200 OK content-length: 58 cache-control: no-cache content-type: text/html ``` 可以尝试关闭旁路由上的某些服务,然后再次观察测试结果。 ``` xueye@MacBook ~ % curl -I http://192.168.88.2:99/ HTTP/1.1 503 Service Unavailable content-length: 107 cache-control: no-cache content-type: text/html ``` 状态检测正常,最后就可以在RouterOS中配置NwtWatch,通过up-script和down-script在状态切换时执行脚本,实现更加复杂的功能。 ``` /tool netwatch add host=192.168.88.2 port=99 interval=10s \ up-script=switch_gfw_dns down-script=switch_cn_dns ``` ``` /system script add dont-require-permissions=yes name=switch_gfw_dns owner=admin policy=read,write source=":fo\ reach i in=[/ip firewall mangle find comment=\"breakwall\"] do={\ \n /ip firewall mangle enable \$i\ \n}\ \n\ \n/ip dns set server=192.168.88.2\ \n/ip dns cache flush" ``` ``` /system script add dont-require-permissions=yes name=switch_cn_dns owner=admin policy=read,write source=":for\ each i in=[/ip firewall mangle find comment=\"breakwall\"] do={\ \n /ip firewall mangle disable \$i\ \n}\ \n\ \n/ip dns set server=218.2.2.2,218.4.4.4\ \n/ip dns cache flush" ``` ### 一些问题 有人会好奇,为什么不在RouterOS直接检测Google Generate 204?一方面,由于切换机制的存在,在旁路宕机的时候,RouterOS会自动切换网关策略路由等,在这种情况下,检测结果永远为false,所以无法自动恢复(也不是绝对的,可以通过脚本尝试恢复并判断,但是显然会对网络产生影响)。另一方面,NetWatch不支持URL,尽管也可以通过脚本实现,但是怎么也比不过内置功能来的高效快捷,所以通过HAProxy来做一个转化。 检测的灵敏度一方面来自于HAProxy中的check,另一方面来自于NetWatch的interval,根据需求选择合适的区间就可以了。 https://www.haproxy.com/documentation/hapee/latest/observability/monitoring/monitor-uri/ https://stackoverflow.com/questions/1989214/google-com-and-clients1-google-com-generate-204 最后修改:2024 年 09 月 23 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 3 如果觉得我的文章对你有用,请随意赞赏