Bonjour,
Voici ici un script permettant la notification via un serveur de voip, je m'en suis servi pour des notification nagios en l'occurance.
Dans mon propre cas lorsque une alerte est levé dans nagios le script notify by phone est appellé:
notify-by-phone
#!/bin/bash
channel=$1
alarmtext=$2
alarmid=$3
espeak -v mb-fr4 "$2" | mbrola -e -l 8000 -v 5.0 -t 1.0 /opt/mbrola/fr4/fr4 - /tmp/alert$3.wav
scp -p /tmp/alert$3.wav root@IPSERVERASTERISK:/var/lib/pf-xivo/sounds/playback/
ssh root@IPSERVERASTERISK "chmod 777 /var/lib/pf-xivo/sounds/playback/alert$3.wav"
/usr/lib/nagios/plugins/asterisk_notify $1 alert$3
Dans ce code grace a espeak et mbrola je genere un fichier .wav contenant les infos de l'alerte nagios, ce .wav est envoyer au serveur asterisk ensuite et mis dans un repertoire dedié au enregistrement vocaux.
ensuite l'ont appel le script permettant de nous contacter auquel on lui passe comme argument le nom du .wav
le script appellé:
asterisk_notify
#!/usr/bin/perl
use strict; use warnings;
#
# CONFIG
#
# Recuperation des parametres
my ($TELEPHONE, $PathFichier) = @ARGV;
#my $TELEPHONE = $ENV{'NAGIOS_CONTACTPAGER'};
#my $TELEPHONE = 'num de tel';
# opsview makes us put +XX
$TELEPHONE =~ s/^\+[0-9]{2}// if (defined $TELEPHONE);
# These are the credentials for the Asterisk Manager. please assure you
# have a permit= entry for the nagios server in your config file.
my $ASTMAN_HOST = 'ASTERISK SERVER IP'; my $ASTMAN_USERNAME = 'USERNAME'; my
$ASTMAN_SECRET = 'PASSWORD'; my $LOCK_DIR = '/tmp/'; my $LOCK_PREFIX =
'nagios-asterisk-';
# When resolving if the telephone should be called or not, CALLED_KEY
# will be used. If established to "$ENV{'NAGIOS_CONTACTPAGER'}", we will
# not call the phone if the contact's phones are the same before
# DONTCALL_BEFORE. If established to
# "$ENV{'NAGIOS_CONTACTPAGER'}-$ENV{'NAGIOS_HOSTNAME'}" the number same
# number will be phoned for different hostnames even if the number has
# been called earlier than DONTCALL_BEFORE Default: 'nagios-phone': Do
# not call before DONTCALL_BEFORE secs for ANY notification
#
# Be careful. File names are formed with $CALLER_KEY, so don't include r
my $CALLER_KEY = 'nagios-phone';
#my $CALLER_KEY = $ENV{'NAGIOS_CONTACTPAGER'};
# if we have called CALLED_KEY before this time, don't call
my $DONTCALL_BEFORE = 0*60; #15 Mins
# Command to send to asterisk
my %ASTERISK_COMMAND = (
Action => 'Originate',
Channel => 'Local/0'.$TELEPHONE,
Timeout => 3600000,
Context => 'alarm',
Exten => 252763,
Priority => 1,
Callerid => 'NAGIOS',
Variable => "SUPPORT_GROUP_NUMS=0$TELEPHONE#|VAR_SERVEUR=$PathFichier" );
my $LOG_FILE = '/var/log/asterisk_notify.log';
# -----------------------------------------------------
#
# DON'T TOUCH ANYTHING PAST THIS POINT!!!
#
use Asterisk::Manager; use Storable; use File::NFSLock; use
Data::Dumper; use POSIX 'setsid'; open STDIN, '/dev/null' or die "Can't
read /dev/null: $!"; open LOG, ">> $LOG_FILE"; if (not defined
$TELEPHONE) {
print LOG "Aborting notification: called without \$TELEPHONE \n";
exit 1;
}
open(STDERR, ">&LOG") or die "Can't write to /dev/null: $!"; defined(my
$pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die
"Can't start a new session: $!"; my $now = time; _log("Notification
started " . scalar(localtime) . " time: $now "); _log("Going to call
$TELEPHONE "); my $call_db = $LOCK_DIR . $LOCK_PREFIX . $CALLER_KEY;
#retrieval and writing of the timestamp for the key have to be atomic.
my $lock = new File::NFSLock {
file => $call_db,
lock_type => 'EXCLUSIVE',
# if we've been waiting 1 hour for reading the db,
# we'll abandon.
blocking_timeout => 60*60,
# if some process got killed holding the lock, we
# won't wait much more for the lock to be
# considered stale, and we will take it
stale_lock_timeout => 20*60 }; if (not defined
$lock){
_log("Couldn't get lock for $CALLER_KEY for 1 hour. ");
_log("Something went quite wrong... and I'm cowardly going to make the
call ");
do_asterisk_call();
exit 0;
}
my $contacts_phoned; eval {
$contacts_phoned = Storable::lock_retrieve($call_db);
};
# if there was an exception (db doesn't exist), consider it unreadable.
$contacts_phoned = {} if ($@); my $last_phoned_time =
$contacts_phoned->{ $CALLER_KEY }; my $call = 1; if (defined
$last_phoned_time){
#we've called CALLER_KEY in the past. Let's see when...
if ($now > ($last_phoned_time + $DONTCALL_BEFORE)){
# now we have past $last_phoned_time + don't call before so call
_log ("Will call: $CALLER_KEY was last called $last_phoned_time and
more than $DONTCALL_BEFORE has passed ");
} else {
# don't call - just log that we didn't call...
_log ("Aborting call. I already called $CALLER_KEY at
$last_phoned_time and $DONTCALL_BEFORE hasn't passed ");
$call = 0;
}
} else {
#never phoned that CALLER_KEY
_log ("Never called $CALLER_KEY ");
}
if ($call == 1){
#this function call can take a LONG time. While
if (do_asterisk_call()){
# store the attempt only if the call succeded... that way, the next
# alert can have the possibility of working, without waiting for
# $DONTCALL_BEFORE
$contacts_phoned->{ $CALLER_KEY } = $now;
eval {
Storable::lock_nstore($contacts_phoned, $call_db);
};
if ($@){
_log("DBWrite Error $@ ");
}
}
# release LOCK
$lock->unlock();
} else {
# release LOCK
$lock->unlock();
}
close LOG; sub do_asterisk_call {
# connect to asterisk manager
my $astman = new Asterisk::Manager;
# $astman->debug(1);
$astman->user($ASTMAN_USERNAME);
$astman->secret($ASTMAN_SECRET);
$astman->host($ASTMAN_HOST);
if (not $astman->connect) {
_log("Could not connect to asterisk $ASTMAN_HOST ");
return 0;
}
my %return = $astman->sendcommand(%ASTERISK_COMMAND);
$astman->disconnect;
if ((defined $return{'Result'}) and ($return{'Result'} eq 'Error')){
_log("Asterisk returned an error: ", Dumper(\%return));
return 0;
} else {
return 1;
}
#$astman->setcallback('DEFAULT', sub {
# print STDERR (join ' ', @_) .
# "\n";
# });
#$astman->eventloop();
}
sub _log {
my (@message) = @_;
#print STDERR (join ' ', @message), "\n";
print LOG (join ' ', @message), "\n";
}