Search This Blog

Friday, April 06, 2012

Cyclic paging script

Let's say that we have an emergency voicemail box where our clients can leave us messages if they have a contract for extended hours or a limited response time contract. Let's also suppose that we have a shitty mobile phone provider, whose signal does no goes higher that 3000ft and is not available too far in wilderness, but we still have those old pagers, able to receive the signal even on top of mount Everest. And let's say that we have a bunch of unfortunate enough bastards on call that are supposed to respond to those calls. Now, every week it's some other poor bastard turn to be on call, but just in case he's not available, we might be force to page the other unfortunates, even when it is not their week of duty. And we page them in order, one after another, until one of them login the web interface of our asterisk server or call the emergency mailbox and acknowledge the message.
This is the script that have to be run from cron every */10 minutes on the asterisk server:

# A script that checks for new messages on emergency voicemail and send messages to pagers
# (c) 2012 under the terms of GNU GPL
# 2012/06/23 v0.5 neXt ( - Add names to email
# 2012/06/09 v0.4 neXt ( - Don't send pages during the night
# 2012/05/10 v0.3 neXt ( - Monday morning warn the user on duty
# 2012/04/09 v0.2 neXt ( - Verify if the cycle has been done today
# 2012/04/06 v0.1 neXt ( - Initial release

# Voicemail location

# Paging provider

# Working directory
if [ ! -d $working ] ; then
 mkdir -p $working

# Location of the file containing the pager numbers
if [ ! -f $pagers ] ; then
 # If the pager file does not exist define the defaults - We need a NEW-LINE at the end of the file!
 echo "5140000000 John Doe
5140000001 Jane Doe
5140000002 Jack Doe
" > $pagers

# A temporary file containing a volatile data
if [ ! -f $runvars ] ; then
 # If the variables file does not exist define some sane defaults
 echo "current=1" > $runvars
 echo "oldday=0" > $runvars

# Cycle pager numbers every Monday, the first number will become the last
today=`date +%w`
day=`date +%d%m`
# read the variables
. $runvars 1>&2>/dev/null
# If today is Monday (weekday 1) and the numbers were not already cycled
if [ $today = 1 ] && [ ! $day = $oldday ]; then
 # copy pager numbers to a new file, deleting the first line of this file
 sed -n "2,\$ p;/^ *$/d" $pagers > $
 # read the first line of the old file and put that line at the end of the new file
 head -1 $pagers >> $
 # replace the old file with the new one
 mv -f $ $pagers
 # send an email with the name and number of the current guy on duty
 guy=`head -n1 $pagers | tail -n1
 recipients=`cat $pagers | sed 's/[^@]* \([a-zA-Z0-9.]*@[^ ]*\).*/\1/' | sed ':a;N;$!ba;s/\n/ /g'`
 echo "This week the main pager is $guy" | mail -s "Pager $guy" $recipients
 # page this new guy with a test message, but not in the middle of the night, 07:30AM seems like OK-ish...
 guynr=`echo "$guy" | cut -f1 -d" "`
 wrn="echo '00 10-86 00' | mail -s911 $guynr@$provider"
 echo $wrn | at 07:30
 # keep the today's date into variable oldday - used to see if we've already processed the cycle today
 sed -i "/oldday/d" $runvars
 echo "oldday=$day" >> $runvars

# We DO NOT send paging messages between 10PM and 7AM - there are no contracts between those hours
if [ $((10#$(date +%H%M))) -gt 700 ] && [ $((10#$(date +%H%M))) -lt 2200 ]; then

 # Let's see if there are new messages in the emergency voicemail
 messages=`ls -l $voicemail | wc -l`
 if [ $messages -gt 1 ]; then
  # There is something in the voicemail, read the already defined variables to see to whom we have to send the page
. $runvars 1>&2>/dev/null
   # If we are aleady at the end of the paging list, we have to restart from the beginning and annoy everybody again
   max_current=`wc -l $pagers | cut -f1 -d" "`
   if [ $current -gt $max_current ]; then
   # Hell, let's send a page to the sucker that is on duty today. Extract only the phone number from file.
   current_page=`head -n$current $pagers | tail -n1 | cut -f1 -d" "`
#  echo "msg=$messages max=$max_current cn=$current cur=$current_page" # This line is here for debugging
   echo "10-78 911" | mail -s911 $current_page@$provider
   # Now remember, we've already sent a page to this unfortunate, if he's not available, we'll have to move to the next in line
   current=`expr $current + 1`
   sed -i "/current/d" $runvars
   echo "current=$current" >> $runvars
   # Cleanup the variables, some poor bastard took the message, we reset the counter
   sed -i "/current/d" $runvars
   echo "current=1" >> $runvars

---------------------------------------------------------------------------------------------  Note: This is work in progress. I'll update changes during time.

No comments:

Post a Comment

Blog Archive