#!/usr/bin/perl
#----------------------------------------------------------------------#
# This script acts as a voice shell and must be used as a parameter to #
# vm.  For example: `vm shell -l ttyS1 -S buster_alert.pl`.  This      #
# script uses vgetty and an analog voice modem to call the support     #
# center and play a recording of buster telling them that nagios has   #
# detected unacknowledged alarms.                                      #
#----------------------------------------------------------------------#
use Modem::Vgetty;
$|=1;

my $phone   = "123456789";
my $message = "/usr/local/nagios/libexec/TAS/buster.rmd";
my $voice   = new Modem::Vgetty;

$voice->device('DIALUP_LINE');
$voice->add_handler('BUSY_TONE', 'finish',
                sub { my $self=shift; $self->stop; $self->shutdown; exit 0; 
});
$voice->add_handler('DIAL_TONE', 'finish',
                sub { my $self=shift; $self->stop; $self->shutdown; exit 0; 
});

$voice->enable_events;

local $SIG{ALRM} = sub { $voice->stop; $voice->shutdown; exit 0; };
alarm(300);

$voice->dial($phone);
$voice->waitfor('READY');

#----------------------------------------------------------------------#
# Since I can't enable voice detection on the voice modem, I just loop #
# the recording for two  minutes.                                      #
#----------------------------------------------------------------------#
for ($i = 1; $i <= 10; $i++)
{
  $voice->play_and_wait($message);
  sleep (1);
}
$voice->shutdown;

exit 0;
