Script Examples/Monitoring a Webstar 2100 Cable Modem

From DD-WRT Wiki

Jump to: navigation, search

[edit] WAN testing script

This script is intended to be run from cron every minute, and requires that you have set up a static route to your Webstar cable modem on 192.168.100.1. It fetches the cable modem's status page and looks for a status of 'Operational'; anything else means that the cable modem is down. The minute-by-minute status is logged (as a string of 1s and 0s) to /tmp/cablemodem.log.

When we confirm an outage, we try to reboot the cable modem via the web interface. Edit the script to feed it your usual downstream frequency. If that doesn't work, and we still detect an outage, we can trigger other actions, such as failover routing via a MiFi device, by running other scripts. Similarly, we can detect when the connection comes back up and stays back up for two or three minutes.

#!/bin/sh
# Get the Webstar cable modem's status from 192.168.100.1 and write it to a log
# Roll the log over either on midnight or the log is at 1440 characters

if [ "`cat /tmp/cablemodem.log |wc -c`" -ge 1440 ] || [ "`date +%H%M`" -eq "0000" ]; 
 then 
	mv /tmp/cablemodem.log /tmp/cablemodem.log.0;
fi;

# Now get the Webstar cable modem's status from the ststua page at 192.168.100.1 and write it to a log

if [ ! -z "`wget -q -O- http://192.168.100.1/system.asp | grep -o Operational`" ];
 then 
	echo -n "1" >> /tmp/cablemodem.log;
 else 
	echo -n "0" >> /tmp/cablemodem.log;
fi;

# Now fetch a string representing the last four minutes results from the logs.

STATUS="1111";
STATUS=`cat /tmp/cablemodem.log.0 /tmp/cablemodem.log 2>/dev/null | tail -c 4`;

if  [ "$STATUS" -eq "1111" ]; 
 	then
	# Everything is fine, the modem is operational and has been for last four samples.
	exit 0;
fi

if  [ "$STATUS" -eq "0000" ]; 
 	then
	# The modem is down, but it's not news. Nothing for us to do, then.
	exit 1;
fi

if  [ "$STATUS" -eq "1100" ]; 
 	then
	# We've detected a new outage and it has persisted for at least one full minute.
	# Attempt to restart the cable modem with a POST to /goform/gscan
	RESET=`wget -q -O- http://192.168.100.1/goform/gscan --post-data SADownStartingFrequency=291000000`;
	exit 1;
fi

if  [ "$STATUS" -eq "1000" ]; 
 	then
	# We've detected a new outage and it has persisted for at least two full minutes. Run scripts to change route, if possible.
	exit 1;
fi

if  [ "$STATUS" -eq "0011" ]; 
 	then
	# We've been back up for two minutes after an outage. Restore the primary route.
	exit 1;
fi

[edit] Without JFFS

The following is designed to be run as a startup script on routers without JFFS, such as the Virgin Media-supplied D-link DIR-615. It writes the WAN testing script into /tmp/wantest.sh, replaces backticks (which wouldn't work by escaping them), makes the script executable and adds a cron job to run it as root.

#!/bin/sh
# Install the WAN test script into /tmp/wantest.sh and crontab it, for non-JFFS routers.
cat > /tmp/wantest.sh << EOF
#!/bin/sh
# Get the Webstar cable modem's status from 192.168.100.1 and write it to a log
# Roll the log over either on midnight or the log is at 1440 characters

if [ "%bcat /tmp/cablemodem.log |wc -c%b" -ge 1440 ] || [ "%bdate +%H%M%b" -eq "0000" ]; 
 then 
	mv /tmp/cablemodem.log /tmp/cablemodem.log.0;
fi;

# Now get the Webstar cable modem's status from the ststua page at 192.168.100.1 and write it to a log

if [ ! -z "%bwget -q -O- http://192.168.100.1/system.asp | grep -o Operational%b" ];
 then 
	echo -n "1" >> /tmp/cablemodem.log;
 else 
	echo -n "0" >> /tmp/cablemodem.log;
fi;

# Now fetch a string representing the last four minutes results from the logs.

STATUS="1111";
STATUS=%bcat /tmp/cablemodem.log.0 /tmp/cablemodem.log 2>/dev/null | tail -c 4%b;

if  [ "\$STATUS" -eq "1111" ]; 
 	then
	# Everything is fine, the modem is operational and has been for last four samples.
	exit 0;
fi

if  [ "\$STATUS" -eq "0000" ]; 
 	then
	# The modem is down, but it's not news. Nothing for us to do, then.
	exit 1;
fi

if  [ "\$STATUS" -eq "1100" ]; 
 	then
	# We've detected a new outage and it has persisted for at least one full minute.
	# Attempt to restart the cable modem with a POST to /goform/gscan
	RESET=%bwget -q -O- http://192.168.100.1/goform/gscan --post-data SADownStartingFrequency=291000000%b;
	exit 1;
fi

if  [ "\$STATUS" -eq "1000" ]; 
 	then
	# We've detected a new outage and it has persisted for at least two full minutes. Run scripts to change route, if possible.
	exit 1;
fi

if  [ "\$STATUS" -eq "0011" ]; 
 	then
	# We've been back up for two minutes after an outage. Restore the primary route.
	exit 1;
fi
EOF
sed -i  s/%b/\`/g /tmp/wantest.sh
chmod 777 /tmp/wantest.sh
echo "* * * * * root /tmp/wantest.sh >/dev/null 2>&1" > /tmp/cron.d/wantest
stopservice cron && startservice cron