#!/usr/bin/env bash ## ------------------------------------------------------------------- ## --- All Configurations ill be done in /etc/check_net/check_net.conf ## ------------------------------------------------------------------- ## - Load Configuration ## - source /etc/check_net/check_net.conf ## ------------------ ## --- Some functions ## ------------------ ## - Check if a given array (parameter 2) contains a given string (parameter 1) ## - fatal(){ echo "" echo -e "[ \033[31m\033[1mError\033[m ]: $*" echo "" echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m" echo "" echo "" >> $log_file echo "[ Error ]: $*" >> $log_file echo "" >> $log_file echo " Script is canceled." >> $log_file echo "" >> $log_file exit 1 } containsElement () { local e for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done return 1 } set_ping_addresses () { if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Try to set IP-Address(es) for ping test. This may take some time.." >> $log_file fi ping_test_ip="" unset ping_ip_arr declare -i i=0 for _host in $PING_TEST_HOSTS ; do while [ $i -lt 2 ]; do if dig +short $_host > /dev/null 2>&1 ; then ping_test_ip=`dig +short $_host | head -1` if ping -q -c2 $ping_test_ip >/dev/null 2>&1 ; then ping_ip_arr+=("$ping_test_ip") let i++ break fi fi break done done if [ ${#ping_ip_arr[@]} -lt 1 ]; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Error ] Setting IP-Address(es) for ping test FAILED!" >> $log_file if [[ ${#previous_ping_ip_arr[@]} -gt 0 ]] ; then ping_ip_arr=("${previous_ping_ip_arr[@]}") echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] Take IP-Address(es) from previous ping test:" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] ${ping_ip_arr[@]}" >> $log_file else ping_ip_arr+=("$BACKUP_PING_ADDR") fi else if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] IP-Addresses for ping tests set to ${ping_ip_arr[@]}" >> $log_file fi unset previous_ping_ip_arr previous_ping_ip_arr=("${ping_ip_arr[@]}") fi } usage() { echo [ -n "$1" ] && echo -e "[ \033[1;31mError\033[m ] : $1\n" echo -e " Usage:" echo -e " \033[1m`basename $0` [OPTIONS] ..\033[m" echo "" echo -e " This script checks the status (online/offline) of the given devices. Afterward another" echo " script called \033[1m`basename $netconfig_script`\033[m will be triggered to configure" echo " the routing depending on the status of the devices." echo "" echo -e " It is strongly recommend to put \033[1mall devices, which should have a connection to" echo -e " the internet\033[m, on the command line." echo "" echo -e " \033[1mNotice\033[m" echo -e " On static line devices \033[1mappend \":\"\033[m. This is very important," echo -e " otherwise this script will \033[1mNOT work as expected\033[m." echo -e " Example:" echo -e " \033[1m`basename $0` -l \"eth0 ppp-light\" eth0:172.16.0.1 ppp-light\033[m" echo "" echo -e " The declaration of the device(s) is mandatory." echo "" echo -e " Options:" echo "" echo -e " \033[1m-h\033[m" echo -e " Prints this help\033[m" echo "" echo -e " \033[1m-l \033[m" echo -e " List of all (internet) devices known as online." echo "" exit 1 } if [[ $EUID -ne 0 ]]; then fatal "This script must be run as root" 1>&2 fi if [[ ! -f "$netconfig_script" ]]; then fatal "Netconfig script \"$netconfig_script\" not found!" fi ## ------------------------------------------------- ## --- If script is already running, stop execution ## ------------------------------------------------- LOCK_DIR=/tmp/`basename $0`.LOCK if mkdir "$LOCK_DIR" 2> /dev/null ; then ## - Remove lockdir when the script finishes, or when it receives a signal trap 'rm -rf "$LOCK_DIR"' 0 2 15 else datum=`date +"%d.%m.%Y"` msg="[ Error ]: A previous instance of script \"`basename $0`\" seems already be running.\n\n Exiting now.." echo -e "To:${admin_email}\n${content_type}\nSubject:DSL Script Error $company -- $datum\n\n${msg}\n" | /usr/sbin/sendmail -F "DSL Monitoring $company" -f $from_address $admin_email 2> /dev/null if $LOGGING_CONSOLE ; then echo "" echo "[ Error ]: A previous instance script \"`basename $0`\" seems already be running." echo "" echo " Exiting now.." echo "" fi exit 1 fi ## ------------- ## --- Configure ## ------------- while getopts l:h opt ; do case $opt in h) usage ;; l) ONLINE_DEVICE_LIST=$OPTARG ;; esac done shift `expr $OPTIND - 1` INITIAL_DEVICE_LIST="$@" if [[ -z "$INITIAL_DEVICE_LIST" ]]; then INITIAL_DEVICE_LIST=$_INITIAL_DEVICE_LIST fi [[ -z "$INITIAL_DEVICE_LIST" ]] && usage "No device list given" ## - Define (non associative) array ## - declare -a inet_devices_arr declare -a dsl_devices_arr declare -a static_devices_arr declare -a online_devices_arr declare -A static_gw_arr declare -A dsl_gw_available_arr for _device in $INITIAL_DEVICE_LIST ; do if [[ "$_device" =~ : ]]; then static_gateway="${_device##*:}" _device="${_device%:*}" static_gw_arr[$_device]="$static_gateway" static_devices_arr+=("$_device") else dsl_devices_arr+=("$_device") fi inet_devices_arr+=("$_device") done for _online_device in $ONLINE_DEVICE_LIST ; do online_devices_arr+=("$_online_device") done ## - Define associative array ## - declare -A remote_gw_arr declare -A filetime_PID_arr for inet_device in "${online_devices_arr[@]}" ; do if containsElement "$inet_device" "${dsl_devices_arr[@]}" ; then remote_gw_address="$(ip addr show $inet_device 2> /dev/null | grep peer | awk '{print$4}' | cut -d "/" -f1)" else remote_gw_address=${static_gw_arr[$inet_device]} fi remote_gw_arr[$inet_device]=$remote_gw_address _pid_file=/var/run/${inet_device}.pid if [ -f $_pid_file ]; then filetime_PID_arr[$inet_device]=`stat -c %Y /var/run/${inet_device}.pid` else filetime_PID_arr[$inet_device]="NOT FOUND" fi done declare -a ping_ip_arr; declare -a previous_ping_ip_arr; #echo "--" #for _key in "${!filetime_PID_arr[@]}" ; do # echo "filetime_PID_arr[$_key]: ${filetime_PID_arr[$_key]}" #done # #for _key in "${!remote_gw_arr[@]}" ; do # echo "remote_gw_arr[$_key]: ${remote_gw_arr[$_key]}" #done # #for _device in ${online_devices_arr[@]} ; do # echo "$_device is online" #done #echo "--" #exit echo "" >> $log_file echo "" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] ## ---" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] ## --- Starting script `basename $0`" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] ## ---" >> $log_file echo "" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Devices configured..: ${inet_devices_arr[@]}" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Devices Online......: ${online_devices_arr[@]}" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] DSL Devices.........: ${dsl_devices_arr[@]}" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Static Devices......: ${static_devices_arr[@]}" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Remote Gateways.....: ${remote_gw_arr[@]}" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] ## ---" >> $log_file echo "" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] ## --- Initial Setup:" >> $log_file ## - Initial: get ping addresses ## - set_ping_addresses echo "" >> $log_file while true ; do changed=false for inet_device in "${inet_devices_arr[@]}" ; do if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] ## --- Device $inet_device" >> $log_file fi ## - Set interface name, routing tables name and, if available, remote gateway. ## - if containsElement "$inet_device" "${dsl_devices_arr[@]}" ; then ## - Is remote a remote gateway for this device knpn? ## - remote_gw_address="$(ip addr show $inet_device 2> /dev/null | grep peer | awk '{print$4}' | cut -d "/" -f1)" iface_name="dsl-`echo $inet_device | cut -d '-' -f2`" rt_name="dsl_`echo $inet_device | cut -d '-' -f2`" if [[ -n "$remote_gw_address" ]]; then if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] Add $remote_gw_address to array dsl_gw_available_arr for DSL line $inet_device" >> $log_file fi dsl_gw_available_arr[$inet_device]=$remote_gw_address else if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] No remote gateway found for DSL line $inet_device" >> $log_file fi if [[ ${dsl_gw_available_arr[$inet_device]+_} ]]; then if $DEBUG; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] Unset dsl_gw_available_arr for DSL line $inet_device" >> $log_file fi unset ${dsl_gw_available_arr[$inet_device]} fi fi else remote_gw_address=${static_gw_arr[$inet_device]} iface_name=$inet_device rt_name="static_`echo $inet_device | cut -d '-' -f1`" fi ## --- ## --- Check if routing through this connection works ## --- ## - Notice: ## - if no remote gateway is known (remote_gw_address is empty), then we don't ## - need to test here. ## - device_is_online=false if [[ -n "$remote_gw_address" ]]; then ## - Check if routing through this dsl connection realy works ## - if [ ${#ping_ip_arr[@]} -lt 1 ]; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] No ip-address for ping-test is set. Skipping test.." >> $log_file else failed=true for _key in ${!ping_ip_arr[@]} ; do if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] route add -net ${ping_ip_arr[$_key]} netmask 255.255.255.255 gw $remote_gw_address" >> $log_file fi #/sbin/ip rule add to ${ping_ip_arr[$_key]} table $rt_name route add -net ${ping_ip_arr[$_key]} netmask 255.255.255.255 gw $remote_gw_address if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] ping4 -I $inet_device -q -c2 ${ping_ip_arr[$_key]}" >> $log_file fi #if ping -q -c2 ${ping_ip_arr[$_key]} >/dev/null 2>&1 ; then if ping4 -I $inet_device -q -c2 ${ping_ip_arr[$_key]} >/dev/null 2>&1 ; then if $DEBUG ; then _local_gw=`curl -4 https://meine-ip.oopen.de 2> /dev/null` if [ -n "$_local_gw" ]; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] Using local gateway \"$_local_gw\" for ping test" >> $log_file fi echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] Ping test (to ${ping_ip_arr[$_key]}) for device \"${inet_device}\" was successful." >> $log_file fi if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] route del -net ${ping_ip_arr[$_key]} netmask 255.255.255.255 gw $remote_gw_address" >> $log_file fi #/sbin/ip rule del to ${ping_ip_arr[$_key]} table $rt_name route del -net ${ping_ip_arr[$_key]} netmask 255.255.255.255 gw $remote_gw_address failed=false device_is_online=true break fi if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] route del -net ${ping_ip_arr[$_key]} netmask 255.255.255.255 gw $remote_gw_address" >> $log_file fi #/sbin/ip rule del to ${ping_ip_arr[$_key]} table $rt_name route del -net ${ping_ip_arr[$_key]} netmask 255.255.255.255 gw $remote_gw_address done if $failed ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Error ] Ping test for device \"${inet_device}\" failed" >> $log_file #echo "`date +'%Y-%m-%d %H:%M:%S'` [ Error ] Status Devices \"$inet_device\" changed" >> $log_file #echo "`date +'%Y-%m-%d %H:%M:%S'` [ Error ] Devices \"$inet_device\" is now OFFLINE" >> $log_file ## - Remote gateway is not reachable. So empty variable "remote_gw_address" #remote_gw_address= fi # End: if $failed fi # End: if [ ${#ping_ip_arr[@]} -lt 1 ]; then fi # End: if [[ -n "$remote_gw_address" ]] ## --- ## --- Now check, if something has changed ## --- if $device_is_online; then if containsElement "$inet_device" ${online_devices_arr[@]} ; then ## - ## - ## - online online if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] $inet_device is still online" >> $log_file fi ## - Check if remote gateway has changed ## - if [ "$remote_gw_address" != "${remote_gw_arr[$inet_device]}" ]; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ WARN ] Remote Gateway on device \"$inet_device\" has changed: ${remote_gw_arr[$inet_device]} --> $remote_gw_address" >> $log_file remote_gw_arr[$inet_device]=$remote_gw_address _pid_file=/var/run/${inet_device}.pid if [ -f $_pid_file ]; then filetime_PID_arr[$inet_device]=`stat -c %Y $_pid_file` fi changed=true else if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] Remote Gateway on device \"$inet_device\": still ${remote_gw_arr[$inet_device]}" >> $log_file fi ## - Test if pid-file's modify time hs changed ## - ## - Notice: that happens if your provider forces a reconnect (mostly one time a day ## - or in other words after 1440 minutes) ## - _pid_file=/var/run/${inet_device}.pid if [ -f $_pid_file ]; then if [ "`stat -c %Y $_pid_file`" != "${filetime_PID_arr[$inet_device]}" ]; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] Modify time for pid-file \"${inet_device}.pid\" has changed" >> $log_file filetime_PID_arr[$inet_device]=`stat -c %Y $_pid_file` changed=true fi fi fi else ## - ## - ## - offline online echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] Status Devices \"$inet_device\" changed" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] Devices \"$inet_device\" is now online" >> $log_file ## - Add device to array online_devices_arr ## - online_devices_arr+=("$inet_device") ## - Add device to array remote_gw_arr ## - remote_gw_arr[$inet_device]=$remote_gw_address _pid=/var/run/${inet_device}.pid if [ -f "$_pid" ]; then filetime_PID_arr[$inet_device]=`stat -c %Y /var/run/${inet_device}.pid` fi changed=true fi # END: if containsElement "$inet_device" ${online_devices_arr[@]} else # ELSE: if $device_is_online; then if containsElement "$inet_device" ${online_devices_arr[@]} ; then ## - ## - ## - online offline echo "`date +'%Y-%m-%d %H:%M:%S'` [ Error ] Status Devices \"$inet_device\" changed" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Error ] Devices \"$inet_device\" is now OFFLINE" >> $log_file ## - In case of DSL Device, have a look at the ppp deamon ## - if containsElement "$inet_device" "${dsl_devices_arr[@]}" ; then if ps -x | grep -E "/usr/sbin/pppd\s+call\s+$iface_name" > /dev/null 2>&1 ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] pppd for \"$iface_name\" is running: Waiting another period" >> $log_file else echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Trying to start pppd for \"$inet_device\".." >> $log_file /usr/sbin/pppd call $iface_name > /dev/null 2>&1 fi fi ## - Remove device from array online_devices_arr ## - for _index in ${!online_devices_arr[@]} ; do if [ "${online_devices_arr[$_index]}" = "$inet_device" ]; then unset online_devices_arr[$_index] break fi done ## - Also remove device from remote_gw_arr ## - unset remote_gw_arr[$inet_device] ## - In case of DSL Device, kill the concerning the ppp deamon ## - if containsElement "$inet_device" "${dsl_devices_arr[@]}" ; then _pid=`ps -ax | grep -e "pppd call $iface_name" | grep -v grep | awk '{print$1}'` echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Kill ppp-daemon for $iface_name (pid $_pid)" >> $log_file kill -9 $_pid fi changed=true else ## - ## - ## - offline offline if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] $inet_device is still offline" >> $log_file fi ## - In case of DSL Device, have a look at the ppp deamon ## - if containsElement "$inet_device" "${dsl_devices_arr[@]}" ; then if ps -x | grep -E "/usr/sbin/pppd\s+call\s+$iface_name" > /dev/null 2>&1 ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] pppd for \"$iface_name\" is running: Waiting another period" >> $log_file else echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Trying to start pppd for \"$inet_device\".." >> $log_file /usr/sbin/pppd call $iface_name > /dev/null 2>&1 fi fi fi # END: if containsElement "$inet_device" ${online_devices_arr[@]} fi # END: if $device_is_online; then done # End: for inet_device in "${inet_devices_arr[@]}" if $changed ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] Status Online Devices changed" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] Online Devices: ${online_devices_arr[@]}" >> $log_file echo "`date +'%Y-%m-%d %H:%M:%S'` [ Warn ] Reconfigure Routing: invoking script \"$netconfig_script\".." >> $log_file if [[ -z "${online_devices_arr[@]}" ]]; then if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] $netconfig_script $INITIAL_DEVICE_LIST" >> $log_file fi $netconfig_script $INITIAL_DEVICE_LIST > /dev/null 2>&1 else _LIST= for _device in ${online_devices_arr[@]} ; do _LIST="$_LIST $_device" done _LIST=`echo "${_LIST}" | sed -e 's/^[ \t]*//'` if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Debug ] $netconfig_script -l \"$_LIST\" $INITIAL_DEVICE_LIST" >> $log_file fi $netconfig_script -l "$_LIST" $INITIAL_DEVICE_LIST > /dev/null 2>&1 fi datum=`date +"%d.%m.%Y"` msg="[ `date +\"%H:%M:%S\"` ]: Status Online Devices changed..\n Online Devices: ${online_devices_arr[@]}\n\n Script \"$netconfig_script\" was invoked to reconfigure routing." echo -e "To:${admin_email}\n${content_type}\nSubject:DSL Status changed $company -- $datum\n\n${msg}\n" | /usr/sbin/sendmail -F "DSL Monitoring $company" -f $from_address $admin_email 2> /dev/null fi # END if $changed ## - Set IP-adresses for Ping-Test at next run ## - if [[ ${#online_devices_arr[@]} -gt 0 ]]; then ## - Try to set IP-Addresses for ping test ## - set_ping_addresses elif [[ ${#dsl_gw_available_arr[@]} -gt 0 ]]; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Try to set default gateway to an existing DSL line .." >> $log_file __set_default_gatway=false default_gw_deleted=false for _device in "${dsl_devices_arr[@]}" ; do if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Device: $_device - Gateway: ${dsl_gw_available_arr[$_device]}" >> $log_file fi if [[ -n "${dsl_gw_available_arr[$_device]}" ]]; then ## - Delete old default route ## - if ! $default_gw_deleted ; then if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] /sbin/ip route delete default" >> $log_file /sbin/ip route delete default >> $log_file 2>&1 else /sbin/ip route delete default > /dev/null 2>&1 fi default_gw_deleted=true fi ## - Try to set default gateway to this DSL connection ## - if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] /sbin/ip route add default via ${dsl_gw_available_arr[$_device]} dev $_device" >> $log_file /sbin/ip route add default via ${dsl_gw_available_arr[$_device]} dev $_device >> $log_file 2>&1 else /sbin/ip route add default via ${dsl_gw_available_arr[$_device]} dev $_device > /dev/null 2>&1 fi if [[ "$?" == "0" ]]; then __set_default_gatway=true break fi fi done # END: for _device in "${inet_devices_arr[@]}" if ! $__set_default_gatway ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Error ] No default gateway (for DSL Device ${_device}) is set!" >> $log_file else echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Default gateway on DSL Device $_device is set to ${inet_devices_arr[$_device]}" >> $log_file ## - Try to set IP-Addresses for ping test ## - set_ping_addresses fi elif [[ ${#static_devices_arr[@]} -gt 0 ]]; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Try to set default gateway to an existing static line .." >> $log_file __set_default_gatway=false default_gw_deleted=false for _device in "${static_devices_arr[@]}" ; do ## - Delete old default route ## - if ! $default_gw_deleted ; then if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] /sbin/ip route delete default" >> $log_file /sbin/ip route delete default >> $log_file 2>&1 else /sbin/ip route delete default > /dev/null 2>&1 fi default_gw_deleted=true fi ## - Set new default route ## - if $DEBUG ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] /sbin/ip route add default via ${static_gw_arr[$_device]} dev $_device" >> $log_file /sbin/ip route add default via ${static_gw_arr[$_device]} dev $_device >> $log_file 2>&1 else /sbin/ip route add default via ${static_gw_arr[$_device]} dev $_device > /dev/null 2>&1 fi if [[ "$?" == 0 ]] ; then __set_default_gatway=true break fi done if ! $__set_default_gatway ; then echo "`date +'%Y-%m-%d %H:%M:%S'` [ Error ] No default gateway is set!" >> $log_file else echo "`date +'%Y-%m-%d %H:%M:%S'` [ Info ] Default gateway is set to ${static_gw_arr[$_device]}" >> $log_file ## - Try to set IP-Addresses for ping test ## - set_ping_addresses fi fi # if [[ ${#online_devices_arr[@]} -gt 0 ]] sleep 30 done exit 0