Surveillance des Trunks SIP et IAX
Imprimer cet article
Sylvain Thérien | 22 août 2010
Il est possible d’utiliser via FreePBX un script afin de surveiller l’état d’un Trunk en utilisant la fonction « Monitoring problèmes de trunk » et en fournissant le nom d’un script AGI personnalisé qui sera appelé pour reporter et prendre toute action sur les problèmes d’un Trunk.
Cependant avec cette méthode, vous ne serez averti que lorsqu’un appel sera fait sur le Trunk. Si vous avez plutôt besoin de savoir s’il y a un problème avec un Trunk avant que l’appel soit fait, par exemple lorsqu’un Trunk n’est plus enregistré, alors suivez plutôt la méthode décrite ci-dessous.
Voici un script perl que l’on nommera « trunkalerts.pl ». Créez un fichier /usr/sbin/trunkalerts.pl en lui donnant le mode d’accès exécution approprié « chmod +x », puis copiez le contenu suivant dans ce fichier :
#!/usr/bin/perl -w
use strict;
use warnings;
#no warnings; #Remark out to suppress ALL warnings, also add remark to above line.
#### email address must have \@ as perl needs to escape the @ symbol. Obviously you’ll need to edit these email addresses
my $alertemail = « YOURNAME\@YOURDOMAIN.COM » ;
my $pbxdomain = « YOURDOMAIN.COM » ;
#### comment out whichever of the following two lines are not relevant for your server
checktrunk(« sip »);
checktrunk(« iax2″);
##################################################
##################################################
####
#### Trunk Alerts script written by Jim Hribnak Oct 7th 2007
#### if there is any questions please feel free to drop me an email at jimh at domain nucleus.com
####
##################################################
##################################################
####
#### Script modified by Charles Pippin Feb 21st 2009 – Added counters for loop retry.
#### if there is any questions please feel free to drop me an email at cwpippin at domain gmail.com
####
##################################################
##################################################
####
#### Script modified by Stephen Wilkey Mar 18th 2010
#### – moved email addressing details to the top of the script so that they are easier to customise
#### – original script assumed both iax2 and sip trunks and would fail if you didn’t have both – now the one you don’t want can be easily commented out at the top
#### – improved the documentation below about what files to create and what to include in those files in order to customise best to individual environments
#### – simplified the code to use the same code for both SIP and IAX2. PLEASE NOTE: the iax trunk alert file has now changed to having iax2 in the name. If you are upgrading from an older version you will need to rename the original iax trunk alert file.
##################################################
##################################################
####
#### Create the following files in /etc/asterisk (if you want to only check sip trunks don’t create the other file etc)
#### – /etc/asterisk/trunkalerts_sip.conf
#### – /etc/asterisk/trunkalerts_iax2.conf
####
#### in the files below add the any unique entry from asterisk -rx « sip show registry » and
#### from asterisk -rx « iax2 show registry ». The original specification was to use
#### the hostname, however if you have multiple trunks from the same supplier it is more
#### likely that the username might be unique. You don’t need to be consistent either
#### – you can use hostname for some and username for others – as long as each
#### line of the show registry result is specified uniquely once by something
#### unique in that line for this script to grep on.
####
##################################################
##################################################
####
sub checktrunk {
# the next line gets the trunktype parameter passed to the subroutine
my $trunktype=shift;
####
print « ============================================= \n »;
print « $trunktype Trunk information\n »;
print « ============================================= \n »;
open(TRUNKLIST, »</etc/asterisk/trunkalerts_$trunktype.conf »);
while (<TRUNKLIST>) {
chomp;
my $recheckcounter = 1;
my $upcounter = 5;
my $dncounter = 5;
until ($upcounter == 0) {
my $trunks = `/usr/sbin/asterisk -rx « $trunktype show registry » | grep \ »$_\ » | awk ‘{
print \$4
}
‘`;
#comment next line if you don’t want to see the output
#print « $trunktype trunks = $trunks\n »; #Remove remark to see output from « sip show registry » command
if ($trunks =~ « Registered ») {
$upcounter = $upcounter – 1;
#print $upcounter, »\n »; #Remove remark to see counter decrement
print « $_ is up\n » ; #Remove remark to see notification of trunk being « UP »
}
else {
$dncounter = $dncounter – 1;
#print $dncounter, »\n »; #Remove remark to see counter decrement once trunk is found to be down
print « $_ is down\n » ; #Remove remark to see notification of trunk being « DOWN »
if ($dncounter == 0) {
if ($recheckcounter == 1) {
# we now reload the trunks and force the check to occur again
`asterisk -rx « module reload »`;
print « Reloading trunks (will wait for reconnection before checking again\n »;
# adjust the length of time on the next line (seconds) if you find it takes longer to re-establish
# trunk connections after a reload
`sleep 20`;
$recheckcounter = 0;
$dncounter = 5;
}
else
{
mailalert();
$upcounter = 0;
#print $upcounter, »\n »; #Remove remark to see counter decrement once trunk is found to be down
print « houston we have a problem\n »;
print « $_ trunk is not registering\n »;
$recheckcounter = 1;
}
}
}
}
}
#end of while loop (read trunk file)
}
##################################################
##################################################
####
#### Email Subroutines
#### Change anywhere below where there is an email address an email address
#### must have \@ as perl needs to escape the @ symbol
####
##################################################
##################################################
sub mailalert {
my $sendmail = « /usr/sbin/sendmail -t »;
my $from= « FROM: <pbx\@$pbxdomain>\n »;
my $reply_to = « Reply-to: <pbx\@$pbxdomain\n »;
my $subject = « Subject: $_ is DOWN!!!!\n »;
my $content = « PBX TRUNK $_ is DOWN!!!!\n »;
my $send_to = « To:<$alertemail>\n »;
open(SENDMAIL, « |$sendmail ») or die « Cannot open $sendmail: $! »;
print SENDMAIL $from;
print SENDMAIL $reply_to;
print SENDMAIL $subject;
print SENDMAIL $send_to;
print SENDMAIL $content;
close(SENDMAIL);
print « An email has been sent!\n\n »;
}
Maintenant dans le fichier trunkalerts.pl que vous venez de créer, changez les paramètres suivants afin de répondre à vos besoins :
my $alertemail = « YOURNAME\@YOURDOMAIN.COM » ;
my $pbxdomain = « YOURDOMAIN.COM » ;
Vous pouvez aussi ajuster les paramètres des compteurs d’états suivants :
my $recheckcounter = 1;
my $upcounter = 5;
my $dncounter = 5;
« $dncounter » est un paramètre qui décrémentera de 5 à 0 lors de la vérification des Trunks pour un état qui est « DOWN ». Cela signifie que le script va essayer de valider chaque Trunk jusqu’à 5 fois permettant ainsi de contrebalancer les pertes sporadiques de communication avec certains Trunks. En fait les compteurs permettent d’éviter la génération de fausses alertes. Ainsi, c’est seulement lorsque la valeur du compteur sera à zéro que le système vous enverra un courriel pour vous aviser du problème.
De même « $upcounter » décrémentera de 5 à 0 lors de la vérification de l’état « UP » des Trunks.
De plus, si le paramètre « $checkcounter » est égal à 1, le script essaiera de recharger les modules Asterisk et de réinitialiser le Trunk et revalidera une deuxième fois si le Trunk est toujours en problème. En fait cela peut éviter une intervention manuelle permettant ainsi de remonter le Trunk en question.
Si vous ne désirez pas valider un type de Trunk en particulier, vous n’avez alors qu’à commenter la ligne correspondante au type de Trunk non désiré. Par exemple pour ne pas valider les Trunks de type IAX2, je n’aurais qu’à commenter comme ceci :
checktrunk(« sip »);
#checktrunk(« iax2″);
Après avoir effectué vos ajustements, vous devrez créer, selon le cas, jusqu’à deux fichiers de configuration selon le type de Trunk à valider. Ainsi, deux fichiers sont requis afin de spécifier les paramètres de chaque Trunk. Ces deux fichiers sont situés dans le répertoire /etc/asterisk.
Éditez le fichier /etc/asterisk/trunkalerts_iax2.conf et y ajouter l’adresse IP ou le nom du serveur et le port de communication pour lequel vous avez un Trunk IAX.
Exemple :
192.168.1.100:4569
ou
iax.trunk.com:4569
Ensuite, éditez le fichier /etc/asterisk/trunkalerts_sip.conf et y ajouter l’adresse IP ou le nom du serveur et le port de communication avec lequel vous avez un Trunk SIP.
Exemple :
192.168.1.100:5060
ou
sip.trunk.com:5060
Vous pouvez spécifier plus d’un Trunk par fichier de configuration, cependant chaque Trunk doit être identifié sur une ligne séparée.
Exemple pour trunkalerts_sip.conf :
192.168.1.100:5060
sip.trunk1.com:5060
sip.trunk2.com:5060
Pour valider que tout fonctionne, exécutez la ligne de commande suivante.
perl /usr/sbin/trunkalerts.pl
Si un des Trunks n’est pas enregistré, vous recevrez un courriel. Petit truc pour valider que le script fonctionne bien, changez le mot de passe d’un de vos Trunks dans la définition du Trunk afin de produire une condition d’erreur.
Vous pouvez aussi créer une tâche « cron » et exécuter le script à intervalle régulier par rapport à ce qui vous semble approprié pour votre situation.
Exécutez la ligne de commande suivante :
crontab -e
Mettre le contenu suivant afin d’exécuter le script toutes les heures.
* 1 * * * /usr/bin/perl /usr/sbin/trunkalerts.pl



































