#!/bin/sh PPPMON_LOCK=/var/run/ppmon.lock PPP_ON_BOOT=/etc/ppp/ppp_on_boot CALL_LIST=$@ LOG_DIR=/tmp PING_CHECK_TIMEOUT=120 #Ping check frequency (s) PING_CONFIRM_CHECK_TIMEOUT=20 #Ping timeout before confirming ping PMON_LOOP_TIMEOUT=1 #E1 check #Restart wanrouter if all mlppp #go down to indicate to other side #that all ports went down and #IPCP will be restarted #Options: 0: disable # 1: enable WANROUTER_RESTART_ON_ALL_DOWN=1 #Wanrouter restart timeout #before starting up the wanpipe ports #This option works in conjunction with #above option. WANROUTER_DOWN_TIMEOUT=10 pidcnt=0 unset WANPIPE_STATUS unset PPPD_ISP_NAME ping_was_ok=0 function pmon_log { logger -p daemon.info "pppmon[$$]: $1"; echo "pppmon[$$]: $1"; } function load_ppp_modules { eval "modprobe ppp_generic 2> /dev/null" eval "modprobe ppp_synctty 2> /dev/null" eval "cat /proc/ksyms | grep ppp_register_channel 2> /dev/null > /dev/null" if [ $? -ne 0 ]; then pmon_log "Error: Kernel has no PPP support" return 1 fi return 0; } function stop_all_pppd { timeout=15; pppdown=0; pmon_log "Stopping all PPPD Services" eval "killall pppd 2> /dev/null" while [ $timeout -gt 0 ]; do timeout=$((timeout -1)) eval "killall pppd 2> /dev/null" if [ $? -eq 0 ]; then pmon_log "Waiting for pppd to go down" sleep 1 else pppdown=1; break; fi done if [ $pppdown -eq 0 ]; then pmon_log "Forcing PPPD Kill" eval "killall -9 pppd 2> /dev/null" sleep 1 else pmon_log "PPPD is down" fi } function pmon_hangup_pppd { isp_dev=$1 pidtmp=$(ps fax | grep "[p]ppd.*$isp_dev") pid2=${pidtmp/# /} pid=$(echo $pid2 | cut -d" " -f 1) eval "kill -s HUP $pid" if [ $? -ne 0 ]; then pmon_log "Error: Failed to hangup on $isp_dev" return 1 fi return 0 } function pmon_find_call_idx { local callname=$1 for ((i=1;i<=$pidcnt;i+=1)); do if [ "${PPPD_ISP_NAME[$i]}" = "$callname" ]; then return $i fi done return 255 } function pmon_check_wanpipe_ports { local idx local calllist=$1 for call in $calllist do wanpipe_dev=${call#isp_} rstatus=$(cat /proc/net/wanrouter/status | grep $wanpipe_dev | cut -d'|' -f 4) status=${rstatus// /} #pmon_log "Dev $wanpipe_dev New Status -$status-" pmon_find_call_idx $call idx=$? #pmon_log "IDX of $call is $idx Old Status ${WANPIPE_STATUS[$idx]} " if [ ${WANPIPE_STATUS[$idx]} != $status ]; then if [ $status != "Connected" ]; then if [ ${WANPIPE_STATUS[$idx]} == "Connected" ]; then pmon_log "Error: Device $wanpipe_dev is $status - Hanging up $call" pmon_hangup_pppd $call fi fi WANPIPE_STATUS[$idx]=$status; fi done } function pmon_ping_check { local rip=$(cat /etc/ppp/peers/isp_wanpipe1 | grep "^\w.*:" |cut -f2 -d :) #if ppp interface is not up no point of pinging eval "ifconfig | grep "$rip" 2> /dev/null > /dev/null" if [ $? -ne 0 ]; then if [ $ping_was_ok -eq 1 ]; then pmon_log "Error: Failed MLPPP ppp interface is down... restarting" ping_was_ok=0; return 1 fi #No point of pinging interface never came up return 0 fi #pmon_log "Starting MLPPP Ping Check on $rip" eval "ping -c5 $rip 2> /dev/null > /dev/null" if [ $? -ne 0 ]; then #Very first time do not try to timeout #because link was never up if [ $ping_was_ok -eq 1 ]; then pmon_log "Error: Failed MLPPP ping check on $rip... confirming" #Confirm that this failure is not due to heavy traffic for ((i=0;i<$PING_CONFIRM_CHECK_TIMEOUT;i++)) do eval "ifconfig | grep "$rip" 2> /dev/null > /dev/null" if [ $? -ne 0 ]; then pmon_log "Error: Failed MLPPP ppp interface is down... restarting" return 1; fi pmon_check_wanpipe_ports "$CALL_LIST" sleep 1 done eval "ping -c5 $rip 2> /dev/null > /dev/null" if [ $? -ne 0 ]; then pmon_log "Error: Failed MLPPP ping check on $rip... restarting" ping_was_ok=0; return 1 else pmon_log "Note: Passed MLPPP ping check on $rip... continuing" fi fi else ping_was_ok=1; fi return 0 } #ps ax | grep "pppmon" #pppmoncnt=$(ps aux | grep [p]ppmon | grep [p]ppmon -c) #pmon_log "PSMONCNT $pppmoncnt Rc=$? $$" #if [ $pppmoncnt -gt 3 ]; then # pmon_log "Error: PPPMON is alread running: $pppmoncnt" # exit 1 #fi #exit 1 case "$1" in -*) echo " Usage: pppmon If pppmon is invoked without arguments, $PPP_ON_BOOT file will be run, presuming it exists and is executable. Otherwise, a PPP connection will be started using settings from /etc/ppp/peers/provider. If you specify one argument, a PPP connection will be started using settings from the appropriate file in the /etc/ppp/peers/ directory, and any additional arguments supplied will be passed as extra arguments to pppd. " exit 0 ;; esac if [ -z "$1" -a -x "$PPP_ON_BOOT" ]; then exec "$PPP_ON_BOOT" fi load_ppp_modules if [ $? -ne 0 ]; then exit 1; fi stop_all_pppd while true do rm -f /tmp/PPPD_STOPPED 2> /dev/null > /dev/null pidcnt=0 for callfile in $CALL_LIST do pmon_log "Starting MLPPP on $callfile" if [ ! -f "/etc/ppp/peers/$callfile" ]; then pmon_log "Error: Call file /etc/ppp/peers/$callfile does not exist!" exit 1 fi eval "pppd call $callfile >> "$LOG_DIR/"$callfile".log" 2>> "$LOG_DIR/"$callfile".log" &" if [ $? -ne 0 ]; then pmon_log "Failed to start PPPD for $callfile! (err=$?)" pmon_log "Exiting...." sleep 5 exit 1 fi pidcnt=$((pidcnt+1)) #pmon_log "ISP Name = $callfile IDX=$pidcnt"; PPPD_ISP_NAME[$pidcnt]=$callfile WANPIPE_STATUS[$pidcnt]="Disconnected" done lcnt=0; while true do runcnt=$(ps -C pppd | grep pppd -c) if [ $runcnt -ne $pidcnt ]; then stop_all_pppd if [ $WANROUTER_RESTART_ON_ALL_DOWN -gt 0 ]; then pmon_log "All MLPPP links down! Restarting wanpipe...." eval "wanrouter stop 2> /dev/null > /dev/null" sleep $WANROUTER_DOWN_TIMEOUT eval "wanrouter start 2> /dev/null > /dev/null" pmon_log "All MLPPP links down! Wanpipe Restarted" fi break; fi pmon_check_wanpipe_ports "$CALL_LIST" lcnt=$((lcnt+1)) if [ $lcnt -gt $PING_CHECK_TIMEOUT ]; then lcnt=0; pmon_ping_check if [ $? -ne 0 ]; then stop_all_pppd break; fi fi sleep $PMON_LOOP_TIMEOUT done done& exit 0