Though I don't have any use for this script now, I know some of you have customers registering from multiple locations back to your Fusion box. The script requires that you add extensions you want to monitor. You'll only need one or two extensions per location added to the script. If you enter email address for an extension, an alert will be send to the phone operator/site admin as well in addition to the FusionPBX admin.
You may create extenfailalert.php in your home directory (eg. /root) and add an appropriate CRON entry to run the script at regular intervals. If you wish to manually run the script from a web browser, place the script in a location where your webserver can serve it from. For a Fusion box this could be something like /var/www/fusionpbx/scripts/extenfailalert.php Make sure to give www-data user read access to the file.
You may create extenfailalert.php in your home directory (eg. /root) and add an appropriate CRON entry to run the script at regular intervals. If you wish to manually run the script from a web browser, place the script in a location where your webserver can serve it from. For a Fusion box this could be something like /var/www/fusionpbx/scripts/extenfailalert.php Make sure to give www-data user read access to the file.
PHP:
<?php
/* Author: EasyBB on fusionpbxforums.
* Permission is hereby granted to anyone to use this code for any purpose
* they see fit. This code comes with no warranties of any kind,
* implied or expressive and this author is not responsible for any damage
* or loss resulting from using this code directly or indirectly.
* You may edit or modify the code as you like, again, at your own risk.
*/
//************************ extenfailalert.php *****************************
// Last edited: 2018 02 25.
// Tested on: Debian 8 Jessie.
// Dependencies: mail transfer agent (MTA), mailutils, php5+
//
// This PHP script is to be used with FreeSWITCH/ FusionPBX server to send
// email alert when extensions fail to register.
//
// You'll need a working mail transfer agent (ssmtp, sendmail etc) running
// on the FusionPBX server. https://help.ubuntu.com/community/EmailAlerts
// ssmtp is the simplest of all. Remember to disable other MTAs if you wish
// to go with ssmtp:
// http://linuxpitstop.com/install-ssmtp-to-send-emails-to-gmail-and-office3655/
//
// Please note that the script needs to be invoked from CRON.
// Recommended to set 30 min interval.
// e.g. */30 * * * * /usr/bin/php /root/extenfailalert.php > /dev/null 2>&1
//**********************************************************************
/*******************************************
******* EDIT BELOW VALUES TO SUIT *********
******************************************/
// Enter the admin email to receive alerts (mandatory)
$mailto = "admin@example.com";
// Enter the from email address or leave it as it is.
$mailfrom = "fusionpbx@gmail.com";
// Enter a friendly sender name or leave it as it is.
$fromname = "FusionPBX";
// Enter an email subject or leave it as it is.
$mailsub = "[ALERT] FusionPBX Extension Failure!";
// Location of fs_cli command.
$fscli = "/usr/bin/fs_cli";
//$fscli = "/usr/local/fs_cli";
// Enter extension numbers to check and optionally, email address.
// Make sure extension exists
$extensions = array(
'201' => '',
'234' => 'bob@example.com',
'257' => 'mary@test.com'
);
/*******************************************
****** DO NOT EDIT BELOW THIS LINE *******
******************************************/
// Function to check extension registration status
function checkExtn($ext){
global $fscli;
$res = shell_exec($fscli . ' -x "sofia status profile internal reg ' . $ext .'"');
$regex = '/returned:\s(\d+)/';
preg_match($regex, $res, $matches);
if($matches[1] > 0) {
return true;
}else {
return false;
}
}
// Function to send email
function sendMail($msg, $clientmail = ''){
global $fromname, $mailfrom, $mailsub, $mailto;
// Format from name
$from = $fromname . "\<" . $mailfrom . "\>";
// Email command line
$recipe = 'echo "%s" | mail -a "Content-Type: text/html" -s "%s" -aFrom:%s "%s %s"';
// Format email command
$cooked = sprintf($recipe,$msg,$mailsub,$from,$mailto,$clientmail);
// Send email
shell_exec($cooked);
}
// Function to check whether FreeSWITCH is running
function checkFS(){
$fs = trim(shell_exec('pidof "freeswitch"'));
if(! strlen($fs)){
return false;
}else{
return true;
}
}
// Set global variable to collect messages
$msg = "";
// Check if FS is running; restart if it isn't
if(! checkFS()){
$msg .= "FreeSWITCH isn't running, trying a service restart... <br />";
$msg .= shell_exec('systemctl restart freeswitch.service');
sleep(5);
if(! checkFS()){
$msg .= "<br /> FreeSWITCH wouldn't start; exiting... <br />";
sendMail($msg);
exit;
}else{
$msg .= "<br /> FreeSWITCH is running... <br />";
sleep(60);
}
}
// Iterate through nominated extensions for registration status
foreach($extensions as $ext => $clientmail){
if(! checkExtn($ext)) {
if(strlen(trim($clientmail))) {
$clientmail = ', ' . $clientmail;
}
sendMail($msg . 'Your phone ' . $ext . ' is offline; please check connectivity.', $clientmail);
}
}
?>
Last edited: