AppleScript C Perl Shell Xcode Other

FreePBX remote trunk monitoring

Post Reply
coding / perl     Views: 36Prev .. Next
FreePBX remote trunk monitoringPosted: Saturday, October 7, 2017 [14:58:45] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 134
To monitor FreePBX trunks from your Mac here is a small Perl script that could be run from a cron job:
View Code
#!/usr/bin/perl

my $res = `ssh user\@IP_ADDRES 'asterisk -rx "sip show registry"'`; ## change user and IP_ADDRESS accordingly
my @all = split(/\n/,$res); my $nol; my $mailsend;
foreach my $k (@all) {$nol++;
if($nol == 1) {next;} ## SKIP HEADER
if($k =~ m/\d SIP registrations/i) {next;} ## SKIP BOTTOM STATISTICS
$k =~ tr/ //s;
my ($trunk,$dnsmgr,$username,$refresh,$state,$wd,$da,$mo,$ye,$ti) = split(/ /,$k);
unless($state =~ m/Registered/i) {$mailsend .= "$trunk\t$username $state\n";}
print "$k\n";
} ## FOREACH END

if($mailsend) {
unless(-f "/lock/double/send/file") {`touch /lock/double/send/file`;
my $sendmail = '/usr/sbin/sendmail -t -oi';my $nowd=time;
my $finaltime = localtime($nowd);$finaltime =~ tr/ //s;
open(MAIL,"|$sendmail");
print MAIL <<EOM;
From: email\@host.com\nTo: email\@host.com\nSubject: Trunk down alert
\nThe following FreeNAS Trunk reported down on $finaltime
$mailsend
Trunks check result:
$res
EOM
close(MAIL);
} ## END UNLESS ALREADY REPORTED
} ## END MAIL SEND PRESENT
else {
if(-f "/lock/double/send/file") {unlink "/lock/double/send/file";}
}


Your Mac has to use login credentials to login without password.
Instead of email you can use email to SMS service or any other way to get notification.There's no place like ~
RE: FreePBX remote trunk monitoring updatePosted: Wednesday, October 11, 2017 [20:26:17] - 2
rootPosted by:rootMember Since:
June 16 2010
Posts: 134
In my case I have to reboot the FreePBX server in order to get one trunk online. Rarely, but it has to be done.
Also, before rebooting I have to make sure Internet connection is working. So server will only be rebooted if it is connected to the Internet. This is just an addition to the script:
View Code
...
if($mailsend) {
$dcheck = `ping -q -c 1 -W 1 8.8.8.8`;sleep(2);
if($dcheck =~ m/1 packets received/) {
unless(-f "/lock/double/send/file") {`touch /lock/double/send/file`;
my $sendmail = '/usr/sbin/sendmail -t -oi';my $nowd=time;
my $finaltime = localtime($nowd);$finaltime =~ tr/ //s;
open(MAIL,"|$sendmail");
print MAIL <<EOM;
From: email\@host.com\nTo: email\@host.com\nSubject: Trunk down alert
\nThe following FreeNAS Trunk reported down on $finaltime
$mailsend
Trunks check result:
$res
EOM
close(MAIL);

my $rdate = time;
my $prevttime = localtime($rdate);$prevttime =~ tr/ //s;
my ($wd,$mo,$da,$ti,$ye) = split(/\s/,$prevttime);
my($hr,$min,$sec) = split(/\:/,$ti);
## GRACEFULLY REBOOT SERVER STOPPING ASTERISK FIRST
unless(-f "/reporting/file/freepbx.reboot.$hr") {`touch /reporting/file/freepbx.reboot.$hr`;
`ssh root\@FREEPBX_IP '/usr/sbin/fwconsole stop'`;sleep(40); ## WAIT FOR COMMAND TO PROPAGATE
`ssh root\@FREEPBX_IP '/usr/sbin/reboot'`;sleep(5);
} ## END UNLESS ALREADY REBOOTED THIS HOUR
} ## END IF GOOD INTERNET

} ## END UNLESS ALREADY REPORTED
} ## END MAIL SEND PRESENT
...
There's no place like ~
coding / perlPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other