Can mgetty count rings and do something useful with it? Success!!

Tom Diehl (tdiehl@pil.net)
Sat, 19 Jun 1999 18:43:52 -0400


On Tue, 8 Jun 1999, Gert Doering wrote:

Hi all,

> On Sun, Jun 06, 1999 at 05:27:10PM -0400, Tom Diehl wrote:
> > I was just wondering if there was a way to have mgetty count the number 
> > of rings it received say greater than 4 but less than 7 
> > for instance and if it received 2 calls within 2 minutes that contained 
> > more than 4 rings but less than 7 it would launch a diald or something 
> > session. 
> 
> No, but you could do something similar either by looking at the log file
> (do a 'tail -f mgetty.log | grep "phone stopped ringing" | perl ...'), or
> by using the ring-back feature.

Thanks for the suggestion. Below I have attached a shell and awk script 
that I wrote to bring up my ppp link to my ISP with diald. So far it works
great. Feel free to use and abuse it but if you break it you get to keep 
the pieces.  I did this with awk since I have never written anything with 
perl. Oh well someday maybe I will learn perl, but why. sigh :-)

Gert, if you think this would be useful to others feel free to include it 
in your contrib section.

I am running mgetty 1.1.14, redhat 5.2 and diald 0.16-2. It should not be 
version specific unless the logfile format changes.

If anyone improves on this I would be interested in the improvements.

Enjoy!!

......Tom			"Do not meddle in the affairs of wizards,
tdiehl@pil.net			 for you are crunchy and good with ketchup."

	 Unix IS user friendly. It's just selective about who its friends are.

################### Start script ############################################

#! /bin/sh
# This script watches the /var/log/mgetty.log.ttyS0 logfile to see if the phone
# rang a few times and quit. If this happens twice within timeout minutes
# we bring up the ppp link with diald. You can set timeout to suite 
# your tastes. If your mgetty uses another log file then you must change 
# that also. Mgetty must be set for debug level 4 or higher to get the ring
# count into the log and to answer the phone after 3 or 4 rings to give this
# script time to do it's job. I modified the diald rc script to start this 
# script at boot time. Also if you use the redhat logrotate program to rotate 
# the mgetty logs, you need to modify the logrotate script for the mgetty 
# logfile so that this script gets restarted every night.
#
# Thanks to Gert Doring for mgetty and the idea to parse the log to do this.
# mtd 11 Jun 99

tail -f /var/log/mgetty.log.ttyS0 |\
gawk 'BEGIN { 
	# defines for the 2 calls the must be set to -1 initally. If you change
	# this you break the script.
	call1 = -1 
	call2 = -1 

	# Set timeout for the max number of minutes you want between calls
	# before we start over again. The shorter you keep this the less likely
	# we are to get false triggers.
	timeout = 3
	}

# When mgetty puts out the string "phone stopped ringing" to the 
# log, we go to work.
/phone stopped ringing /{
	# Turn the time in the log entry into a decimal number so we can 
	# add and subtract it.
	min = substr($2,4,2)
	sec = substr($2,7,2)
	time = min"."sec

	if ( call1 != -1 )
		call2 = time
	else
		call1 = time
	
	# If we went past the top of the hour add 60 so we can still do the 
	# math.
	if (call2 < call1 && call2 != -1)
		call2 = call2 + 60

	# Check to see if we bring up the link or not.
	if ((call2 - call1) <= timeout && call2 != -1)
		{
		# Put in here whatever you want to use to bring up the ppp
		# link.

		# Force the link up for 5 minutes. That way I have 
		# time to login.
		print "force" > "/etc/diald/diald.ctl"

		# Sleep for 5 minutes b4 unforcing diald. This can be adjusted
		# to suite your personal preferance.
		system("sleep 300")

		# I had to use the system(echo ... command because for 
		# some reason diald was ignoring the print unforce...
		# command string.
		system("echo unforce > /etc/diald/diald.ctl")

		# Reset call1 and call2 to -1
		call1 = -1
		call2 = -1
		}
	else
	# The timeout was exceeded. Reset call1 = call2 and call2 = -1
	# Since the latest call might be the 1st call of a valid attempt
	# to bring up the link.
	if (call2 != -1)
		{
		call1 = call2
		call2 = -1
		}

}'

########################## End of script ####################################

########################## Begin script #####################################
#! /bin/sh

# This script finds the pids of the mgetty_ring_det script. It then kills
# them and restarts the script. We need to do this so that the logs can
# be rotated with the redhat logrotate program. mtd 19 Jun 99

# Find the pid of the tail-f running from mgetty_ring_det and kill it.
kill `ps | grep -v grep | grep "tail -f /var/log/mgetty.log.ttyS0" | \
	cut -d " " -f1`

# Restart the mgetty_ring_det script.
/usr/local/bin/mgetty_ring_det &

########################## End of script ####################################