#!/usr/bin/perl # trunk_mon.pl V2.0 # Check registration status of SIP Trunk(s) # External Depends: gnokii (sms) # This script is released under GPLv3, it might be of some use to others. # The latest version should be available at https://members.ausics.net/noelb # Copyright © Noel Butler 2017,2022. All Rights Reserved. ############################################################################ use Mail::Sendmail; use Sys::Syslog; use Sys::Hostname::FQDN qw(fqdn short); $myhost = fqdn(); $datetime = localtime(); $stime = time(); #--------------------------------------------------------------------------# $mnotify = "0400123"; $enotify = "noelb\@example.com"; $sipsrv = "100.100.100.254"; @cstrunks = ('sip1.ozvoip.net','sip2.ozvoip.net'); @pjtrunks = ('someothersip.example.net'); #--------------------------------------------------------------------------# foreach my $trunk (@cstrunks) { $siptrunks = `ssh -4 $sipsrv "/usr/sbin/asterisk -rx 'sip show registry' |grep $trunk" | awk '{print \$5}'`; if ($siptrunks =~ "Registered") { if (-e "/var/lock/UNregistered.$trunk") { print "\n$trunk Now Registered\n"; unlink ("/var/lock/UNregistered.$trunk"); if ( -e "/var/lock/Notified.$trunk") { unlink ("/var/lock/Notified.$trunk"); `echo \"TRUNK/UPALERT chan_sip Trunk $trunk ACTIVE\" |gnokii --sendsms $mnotify`; %mailfk = ( To => $enotify, From => 'pbx-noreply@'.$myhost, Subject => 'UPALERT - TRUNK ACTIVE', 'Auto-Submitted' => 'auto-generated', Message => 'TRUNK $trunk is back UP!' ); sendmail(%mailfk); syslog('info',"Trunk Notice - $trunk available"); } } print "\n$trunk is Registered\n\n" ; } else { print "\nERROR - " . $trunk . " is Down\n"; &trunkdown($trunk); } } foreach my $trunk (@pjtrunks) { $siptrunks = `ssh -4 $sipsrv "/usr/sbin/asterisk -rx 'pjsip show registrations' |grep $trunk" | awk '{print \$3}'`; if ($siptrunks =~ "Registered") { if (-e "/var/lock/UNregistered.$trunk") { print "\n$trunk Now Registered\n"; unlink ("/var/lock/UNregistered.$trunk"); if ( -e "/var/lock/Notified.$trunk") { unlink ("/var/lock/Notified.$trunk"); `echo \"TRUNK/UPALERT chan_sip Trunk $trunk ACTIVE\" |gnokii --sendsms $mnotify`; %mailfk = ( To => $enotify, From => 'pbx-noreply@'.$myhost, Subject => 'UPALERT - TRUNK ACTIVE', 'Auto-Submitted' => 'auto-generated', Message => 'TRUNK $trunk is back UP!' ); sendmail(%mailfk); syslog('info',"Trunk Notice - $trunk available"); } } print "\n$trunk is Registered\n\n" ; } else { print "\nERROR - " . $trunk . " is Down\n"; &trunkdown($trunk); } } sub trunkdown { my ($ntrunk) = @_; if (-e "/var/lock/UNregistered.$ntrunk") { $lckts = (stat("/var/lock/UNregistered.$ntrunk"))[9]; $tdif = ($stime - $lckts); if (($tdif > "600") && (!-e "/var/lock/Notified.$ntrunk")){ print "\nError $ntrunk still down, sending notifications\n\n"; syslog('info',"Trunk Error - $ntrunk sending notifications"); %mailfk = ( To => $enotify, From => 'pbx-noreply@'.$myhost, Subject => 'ALERT - TRUNK DOWN', 'Auto-Submitted' => 'auto-generated', Message => 'TRUNK ' .$ntrunk.' is down!' ); sendmail(%mailfk); `echo \"TRUNK/ALERT Trunk $ntrunk DOWN\" |gnokii --sendsms $mnotify`; open (GEM, ">/var/lock/Notified.$ntrunk"); print GEM $datetime; close GEM; } } else { open (GERR, ">/var/lock/UNregistered.$ntrunk"); print GERR $datetime; close GERR; syslog('info',"Initial Trunk Error - $ntrunk"); } }