| 1 | #!/bin/sh
|
|---|
| 2 | #
|
|---|
| 3 | # Internet Connection Wrapper From Cron
|
|---|
| 4 | #
|
|---|
| 5 | # a) Disable lvrouted if the internet is down.
|
|---|
| 6 | # b) Re-enable lvrouted if the internet is back up.
|
|---|
| 7 | #
|
|---|
| 8 | # XXX: Do we need build an fail-save for flapping states?
|
|---|
| 9 | # XXX: Do we need to manage state, like DHCP here?
|
|---|
| 10 | # XXX: Check if page output is actually the output expected and not some weird captive portal somewhere.
|
|---|
| 11 | # XXX: For effiently reasons this should be combined with the nagios check_inet check
|
|---|
| 12 | #
|
|---|
| 13 | # Rick van der Zwet <rick@wirelessleiden.nl>
|
|---|
| 14 | #
|
|---|
| 15 |
|
|---|
| 16 | TAG=`basename $0`
|
|---|
| 17 | logit() {
|
|---|
| 18 | logger -t "$TAG" $*
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | # Check if we need to check inet at all
|
|---|
| 22 | . /etc/rc.subr
|
|---|
| 23 |
|
|---|
| 24 | load_rc_config "lvrouted"
|
|---|
| 25 | load_rc_config "tinyproxy"
|
|---|
| 26 |
|
|---|
| 27 | : ${lvouted_enable="NO"}
|
|---|
| 28 | : ${tinyproxy_enable="NO"}
|
|---|
| 29 | : ${service_ileiden="NO"}
|
|---|
| 30 | : ${service_proxy="NO"}
|
|---|
| 31 |
|
|---|
| 32 | control_lvrouted=false
|
|---|
| 33 | control_tinyproxy=false
|
|---|
| 34 | checkyesno service_proxy_ileiden && checkyesno lvouted_enable control_lvrouted=true
|
|---|
| 35 | checkyesno service_proxy_normal && checkyesno tinyproxy_enable || control_tinyproxy=false
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | # Get current state of the daemons
|
|---|
| 39 | lvouted_status="stopped"
|
|---|
| 40 | tinyproxy_status="stopped"
|
|---|
| 41 | service lvrouted onestatus > /dev/null && lvouted_status="running"
|
|---|
| 42 | service tinyproxy onestatus > /dev/null && tinyproxy_status="running"
|
|---|
| 43 |
|
|---|
| 44 | check_http() {
|
|---|
| 45 | fetch -o /dev/null -q $* 2>/dev/null
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | # Get connection stats for internet direct and via proxy
|
|---|
| 49 | inet_status=up
|
|---|
| 50 | proxy_status=up
|
|---|
| 51 | check_http http://proxy-test.wirelessleiden.nl || check_http http://ams-ix.net || inet_status=down
|
|---|
| 52 | export HTTP_PROXY=${HTTP_PROXY-:http://proxy.wleiden.net:3128}
|
|---|
| 53 | check_http http://tinyproxy.stats || check_http http://ams-ix.net || proxy_status=down
|
|---|
| 54 |
|
|---|
| 55 | # Log Network Status
|
|---|
| 56 | cat <<EOF > /tmp/network.status
|
|---|
| 57 | internet=$inet_status
|
|---|
| 58 | proxy=$proxy_status
|
|---|
| 59 | EOF
|
|---|
| 60 |
|
|---|
| 61 | # Control connections
|
|---|
| 62 | if $control_lvrouted; then
|
|---|
| 63 | if [ $lvrouted_status = "stopped" ] && [ $inet_status = "up" ]; then
|
|---|
| 64 | service lvrouted start | logit
|
|---|
| 65 | elif [ $lvrouted_status = "running" ] && [ $inet_status = "down" ]; then
|
|---|
| 66 | service lvrouted stop | logit
|
|---|
| 67 | fi
|
|---|
| 68 | fi
|
|---|
| 69 |
|
|---|
| 70 | if $control_tinyproxy; then
|
|---|
| 71 | if [ $tinyproxy_status = "stopped" ] && [ $inet_status = "up" ]; then
|
|---|
| 72 | service tinyproxy start | logit
|
|---|
| 73 | elif [ $tinyproxy_status = "running" ] && [ $inet_status = "down" ]; then
|
|---|
| 74 | service tinyproxy stop | logit
|
|---|
| 75 | fi
|
|---|
| 76 | fi
|
|---|