ff_olsr_test_gw.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/sh
  2. # Copyright 2013 Manuel Munz <freifunk at somakoma dot de>
  3. # Licensed under the GNU General Public License (GPL) v3
  4. # This script monitors the local internet gateway
  5. . /lib/functions.sh
  6. . /lib/functions/network.sh
  7. . /usr/share/libubox/jshn.sh
  8. # exit if dyngw_plain is not enabled or RtTable is not (254 or unset)
  9. config_load olsrd
  10. check_dyngw_plain()
  11. {
  12. local cfg="$1"
  13. config_get library "$cfg" library
  14. if [ "${library#olsrd_dyn_gw_plain}" != "$library" ]; then
  15. config_get ignore "$cfg" ignore
  16. config_get RtTable "$cfg" RtTable
  17. if [ "$ignore" != "1" ] && [ -z "$RtTable" -o "$RtTable" = "254" ]; then
  18. exit=0
  19. fi
  20. fi
  21. }
  22. exit=1
  23. config_foreach check_dyngw_plain LoadPlugin
  24. [ "$exit" = "1" ] && exit 1
  25. #Exit if this script is already running
  26. pid="$(pidof ff_olsr_test_gw.sh)"
  27. if [ ${#pid} -gt 5 ]; then
  28. logger -p debug -t gwcheck "Gateway check script is already running, exit now"
  29. exit 1
  30. fi
  31. # exit if there is no defaultroute with metric=0 in main or gw-check table.
  32. defroutemain="$(ip route show |grep default |grep -v metric)"
  33. defroutegwcheck="$(ip route show table gw-check |grep default |grep -v metric)"
  34. if [ -z "$defroutegwcheck" -a -z "$defroutemain" ]; then
  35. exit 1
  36. fi
  37. # get and shuffle list of testservers
  38. testserver="$(uci -q get freifunk-gwcheck.hosts.host)"
  39. [ -z "$testserver" ] && echo "No testservers found, exit" && exit
  40. testserver="$(for t in $testserver; do echo $t; done | awk 'BEGIN {
  41. srand();
  42. }
  43. {
  44. l[NR] = $0;
  45. }
  46. END {
  47. for (i = 1; i <= NR; i++) {
  48. n = int(rand() * (NR - i + 1)) + i;
  49. print l[n];
  50. l[n] = l[i];
  51. }
  52. }')"
  53. check_internet() {
  54. for t in $testserver; do
  55. local test
  56. test=$(wget -q http://$t/conntest.html -O -| grep "Internet_works")
  57. if [ "$test" == "Internet_works" ]; then
  58. echo 0
  59. break
  60. else
  61. logger -p debug -t gw-check "Could not fetch http://$t/conntest.html"
  62. fi
  63. done
  64. }
  65. resolve() {
  66. echo "$(nslookup $1 2>/dev/null |grep 'Address' |grep -v '127.0.0.1' |awk '{ print $3 }')"
  67. }
  68. get_dnsservers() {
  69. # this gets all dns servers for the interface which has the default route
  70. dns=""
  71. if [ ! -x /bin/ubus ]; then
  72. # ubus not present (versions before Attitude): fallback to get these from /var/state/network.
  73. # We always assume that wan is the default route interface here
  74. dns="$(grep network.wan.resolv_dns /var/state/network | cut -d "=" -f 2)"
  75. else
  76. network_find_wan wan
  77. network_get_dnsserver dns $wan
  78. fi
  79. }
  80. iw=$(check_internet)
  81. if [ "$iw" == 0 ]; then
  82. # Internet available again, restore default route and remove ip rules
  83. if [ -n "$defroutegwcheck" ]; then
  84. ip route add $defroutegwcheck
  85. ip route del $defroutegwcheck table gw-check
  86. for host in $testserver; do
  87. ips="$(resolve $host)"
  88. for ip in $ips; do
  89. [ -n "$(ip rule show | grep "to $ip lookup gw-check")" ] && ip rule del to $ip table gw-check
  90. done
  91. done
  92. get_dnsservers
  93. for d in $dns; do
  94. [ -n "$(ip rule show | grep "to $d lookup gw-check")" ] && ip rule del to $d table gw-check
  95. done
  96. logger -p err -t gw-check "Internet is available again, default route restored ( $defroutegwcheck)"
  97. fi
  98. else
  99. # Check failed. Move default route to table gw-check and setup ip rules.
  100. if [ -z "$(ip rule show | grep gw-check)" -a -n "$defroutemain" ]; then
  101. ip route add $defroutemain table gw-check
  102. ip route del $defroutemain
  103. logger -p err -t gw-check "Internet is not available, default route deactivated ( $defroutemain)"
  104. fi
  105. for host in $testserver; do
  106. ips="$(resolve $host)"
  107. for ip in $ips; do
  108. [ -z "$(ip rule show | grep "to $ip lookup gw-check")" ] && ip rule add to $ip table gw-check
  109. done
  110. done
  111. get_dnsservers
  112. for d in $dns; do
  113. [ -z "$(ip rule show | grep "to $d lookup gw-check")" ] && ip rule add to $d table gw-check
  114. done
  115. logger -p err -t gw-check "Check your internet connection!"
  116. fi